rm(list=ls()) 味 <- c(6,4,5,3,2, 10,8,10,8,9, 11,12,12,10,10, 5,4,2,2,2, 7,6,5,4,3, 12,8,5,6,4) 温度 <- factor(c(rep("冷蔵庫", 15), rep("常温", 15))) 銘柄 <- factor(rep(c(rep("イカアン", 5), rep("ボスビッグ", 5), rep("ビビッテル", 5)), 2)) # tapply 関数を利用して,条件ごとの平均を求める # mean(data$味[温度=="冷蔵庫" & 銘柄=="ビビッテル"]) # のようにしてもよいが,類似の命令を繰り返すことになり,わずらわしい data <- data.frame(味=味, 温度=温度, 銘柄=銘柄) tapply(data$味, INDEX=list(data$温度, data$銘柄), FUN=mean) aggregate(data$味 ~ data$温度 + data$銘柄, data = data, FUN = mean) # 正しい分析 summary(aov(味 ~ 温度*銘柄)) interaction.plot(温度, 銘柄, 味) # 銘柄を横軸にしてもよい # interaction.plot(銘柄, 温度, 味) # 交互作用なしのモデル summary(aov(味 ~ 温度 + 銘柄)) # 温度のみの1要因と考えたモデル summary(aov(味 ~ 温度)) # 銘柄のみの1要因と考えたモデル summary(aov(味 ~ 銘柄)) # 平方和の分解を確認 pref <- c(6,4,5,3,2, 10,8,10,8,9, 11,12,12,10,10, 5,4,2,2,2, 7,6,5,4,3, 12,8,5,6,4) condition <- factor(c(rep("cold", 15), rep("room", 15) ) ) tmp <- rep(c(rep("Ik", 5), rep("Vo", 5), rep("Bi", 5) ), 2 ) brand <- factor(tmp, levels = c("Ik", "Vo", "Bi")) data <- data.frame(condition, brand, pref) grand <- mean(data$pref) tmp_c <- aggregate(data$pref ~ data$condition, data = data, FUN = mean) tmp_b <- aggregate(data$pref ~ data$brand, data = data, FUN = mean) tmp_int <- aggregate(data$pref ~ data$brand + data$condition, data = data, FUN = mean) data_mat <- matrix(data$pref, nrow = 5) grand_mat <- matrix(rep(grand,30), nrow = 5) cond_mean_mat <- matrix(c(rep(tmp_c[1,2],15), rep(tmp_c[2,2],15)), nrow = 5) brand_mean_mat <- matrix(rep(rep(tmp_b[,2],2), 5), nrow = 5, byrow=T) effect_mat <- matrix(rep(tmp_int[,3], 5), nrow = 5, byrow = T) c_effect_mat <- cond_mean_mat - grand_mat b_effect_mat <- brand_mean_mat - grand_mat int_effect_mat <- effect_mat - (c_effect_mat + b_effect_mat + grand_mat) error_mat <- data_mat - (grand_mat + c_effect_mat + b_effect_mat + int_effect_mat) sum(c_effect_mat^2) sum(b_effect_mat^2) sum(int_effect_mat^2) sum(error_mat^2)