对R中的所有变量重复测量Anova

时间:2016-01-22 11:51:20

标签: r anova

我执行重复测量Anova,我有任何变量(4个或更多),当我这样做时,我必须分别对每个变量进行Anova。 这对我来说很不方便,例如这里有2个变量的代码rep.anova - tember_body和SAD(我有8个度量)

library("car")
tabl20=read.csv("path to dataset",sep=";",dec=",")
ageLevels <- c(1, 2, 3,4,5,6,7,8)
ageFactor <- as.factor(ageLevels)
ageFrame <- data.frame(ageFactor)
ageBind <- cbind(tabl20$temper_body_v0,
                 tabl20$temper_body_v1,
                 tabl20$temper_body_v2,
                 tabl20$temper_body_v3,
                 tabl20$temper_body_v4,
                 tabl20$temper_body_v5,
                 tabl20$temper_body_v6,
                 tabl20$temper_body_v7)
ageModel <- lm(ageBind ~ 1)
analysis <- Anova(ageModel, idata = ageFrame, idesign = ~ageFactor)
summary(analysis)
######
tabl20=read.csv("path to data",sep=";",dec=",")
ageLevels <- c(1, 2, 3,4,5,6,7,8)
ageFactor <- as.factor(ageLevels)
ageFrame <- data.frame(ageFactor)
ageBind <- cbind(tabl20$SAD_v0,
                 tabl20$SAD_v1,
                 tabl20$SAD_v2,
                 tabl20$SAD_v3,
                 tabl20$SAD_v4,
                 tabl20$SAD_v5,
                 tabl20$SAD_v6,
                 tabl20$SAD_v7)

ageModel <- lm(ageBind ~ 1)
analysis <- Anova(ageModel, idata = ageFrame, idesign = ~ageFactor)
summary(analysis)

如何同时为所有变量执行rep.measure anova,而不是单独为每个变量分析执行。

1 个答案:

答案 0 :(得分:0)

我找到了答案,也许对某人有用

library(car) 
tabl20=read.csv("c:/path/,sep=";",dec=",") 
ageLevels <- c(1, 2, 3,4,5,6,7,8) 
ageFactor <- as.factor(ageLevels) 
ageFrame <- data.frame(ageFactor) 

tabl20 <- tabl20[, order(names(tabl20))] 

measures <- function(data = tabl20, n = 4) { 
  list <- list(n) 
  for(i in 0:3) {list[[i+1]] <- as.matrix(cbind(data[, 8*i + 1:8])) 
  } 
  list 
} 

measures_list <- measures() 

models <- lapply( 
  measures_list, function(x) { 
    ageModel <- lm(x ~ 1) 
    Anova(ageModel, idata = ageFrame, idesign = ~ageFactor) 
  } )

但我不知道如何为所有变量执行绘图

如果我这样做的话 图(模型)

我收到此错误

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'
也许有人会告诉我,如何创建所有变量的情节