首页 > 代码库 > 数学建模2012A题部分解答基于R语言

数学建模2012A题部分解答基于R语言

  1 library(xlsx)
  2 library(nortest)
  3 library(Kendall)
  4 library(stats)
  5 
  6 chart1 <- read.xlsx("E:/迅雷下载/cumcm2012problems/A/附件1-葡萄酒品尝评分表.xls",stringsAsFactors = FALSE,sheetIndex = 1,header = F,encoding = "UTF-8")
  7 chart2 <- read.xlsx("E:/迅雷下载/cumcm2012problems/A/附件1-葡萄酒品尝评分表.xls",stringsAsFactors = FALSE,sheetIndex = 2,header = F,encoding = "UTF-8")
  8 chart3 <- read.xlsx("E:/迅雷下载/cumcm2012problems/A/附件1-葡萄酒品尝评分表.xls",stringsAsFactors = FALSE,sheetIndex = 3,header = F,encoding = "UTF-8")
  9 chart4 <- read.xlsx("E:/迅雷下载/cumcm2012problems/A/附件1-葡萄酒品尝评分表.xls",stringsAsFactors = FALSE,sheetIndex = 4,header = F,encoding = "UTF-8")
 10 classfydata <- read.xlsx("E:/迅雷下载/cumcm2012problems/A/附件2-指标总表.xls",stringsAsFactors = FALSE,sheetIndex = 2,header = F,encoding = "UTF-8")
 11 
 12 chart1 <- chart1[-1,-c(1,2,13,14)]
 13 chart1 <- na.omit(chart1)
 14 chart3 <- chart3[-1,-c(1,2)]
 15 chart3 <- na.omit(chart3)
 16 chart2 <- chart2[-c(1,2),-c(1,2,3,14)]
 17 chart2 <- na.omit(chart2)
 18 chart4 <- chart4[-c(1,2),-c(1:4)]
 19 chart4 <- na.omit(chart4)
 20 chart4 <- chart4[-281,]
 21 
 22 chart1Mean <- array(,27)
 23 chart2Mean <- array(,28)
 24 chart3Mean <- array(,27)
 25 chart4Mean <- array(,28)
 26 temp <- array(,10)
 27 
 28 for(i in 1:27){
 29   for(h in 1:10){
 30     temp[h] <- as.numeric(chart1[12*i,h])
 31   }
 32   chart1Mean[i] <- mean(temp)
 33 }
 34 qqnorm(chart1Mean)
 35 
 36 for(i in 1:27){
 37   for(h in 1:10){
 38     temp[h] <- as.numeric(chart3[12*i,h])
 39   }
 40   chart3Mean[i] <- mean(temp)
 41 }
 42 qqnorm(chart3Mean)
 43 
 44 for(i in 1:28){
 45   for(h in 1:10){
 46     temp[h] <- as.numeric(chart2[10*i,h])
 47   }
 48   chart2Mean[i] <- mean(temp)
 49 }
 50 qqnorm(chart2Mean)
 51 
 52 for(i in 1:28){
 53   for(h in 1:10){
 54     temp[h] <- as.numeric(chart4[10*i,h])
 55   }
 56   chart4Mean[i] <- mean(temp)
 57 }
 58 qqnorm(chart4Mean)
 59 
 60 shapiro.test(chart1Mean)
 61 shapiro.test(chart2Mean)
 62 shapiro.test(chart3Mean)
 63 shapiro.test(chart4Mean)
 64 
 65 lillie.test(chart1Mean)
 66 lillie.test(chart2Mean)
 67 lillie.test(chart3Mean)
 68 lillie.test(chart4Mean)
 69 
 70 wilcox.test(chart1Mean,chart3Mean,exact = FALSE,correct = FALSE)
 71 wilcox.test(chart1Mean,chart4Mean,exact = FALSE,correct = FALSE)
 72 
 73 MannKendall(chart1Mean)
 74 MannKendall(chart2Mean)
 75 MannKendall(chart3Mean)
 76 MannKendall(chart4Mean)
 77 
 78 classfydata <- classfydata[-c(1,2)]
 79 classfydata <- classfydata[-c(1,2),]
 80 classfydata1 <- classfydata[c(1:27),c(1:31)]
 81 classfydata1 <- classfydata1[-c(4,5,9,13,17,23,27)]
 82 
 83 for(i in 1 : 27){
 84   for(h in 1:24){
 85     classfydata1[i,h] <- as.numeric(classfydata1[i,h])
 86   }
 87 }
 88 z = 0;
 89 y = 0;
 90 for(j in 1:4){
 91   z = j * 3 - 2
 92   y = z + 2
 93   for(i in 1:27){
 94     temp = 0
 95     for(h in z:y){
 96       temp = temp + as.numeric(classfydata1[i,h])
 97     }
 98     classfydata1[i,z] <- temp / 3
 99   }
100 }
101 
102 for(i in 1:27){
103   temp = 0
104   for(h in 18:20){
105     temp = temp + as.numeric(classfydata1[i,h])
106   }
107   classfydata1[i,18] <- temp / 3
108 }
109 
110 classfydata1 <- classfydata1[-c(2,3,5,6,8,9,19,20)]
111 
112 temp <- classfydata[1:27,32]
113 classfydata1 <- cbind(classfydata1,temp)
114 
115 b = chart1Mean
116 c = array(,27)
117 b1 = c(25,27,7,10,11,20,16,24,19,18,6,4,13,23,17,1,2,3,8,12,5,23,15,26,9,21,14)
118 for(i in 1:27)
119 {
120   c[b1[i]] = b[i]
121 }
122 classfydata1 <- cbind(classfydata1,c)
123 classfydata1
124 
125 classfydata1 <- as.matrix(classfydata1)
126 classfydata1 <- as.data.frame(classfydata1)
127 
128 mc <- kmeans(classfydata1[-18],4)
129 mc$cluster

 

数学建模2012A题部分解答基于R语言