在R中具有3个固定因子的ANOVA

时间:2010-03-15 02:08:45

标签: r anova

我试图运行一个带有响应变量p和3个固定因子的模型来获得ANOVA。这就是我的代码的样子:

#run it as 3 fixed factor model 
p1=c(37,38,37,41,41,40,41,42,41)
p2=c(42,41,43,42,42,42,43,42,43)
p3=c(30,31,31,31,31,31,29,30,28)
p4=c(42,43,42,43,43,43,42,42,42)
p5=c(28,30,29,29,30,29,31,29,29)
p6=c(42,42,43,45,45,45,44,46,45)
p7=c(25,26,27,28,28,30,29,27,27)
p8=c(40,40,40,43,42,42,43,43,41)
p9=c(37,38,37,41,41,40,41,42,41)
p10=c(35,34,34,35,35,34,35,34,35)
p = cbind(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
partnumber=c(rep(1,9),rep(2,9),rep(3,9),rep(4,9),rep(5,9),rep(6,9),rep(7,9),rep(8,9),rep(9,9),rep(10,9))
test=c(rep(c(rep(1:3,3)),10))
inspector = rep(c(rep(1,3),rep(2,3),rep(3,3)),10)
fpartnumber = factor(partnumber)
ftest = factor(test)
finspector = factor(inspector)
model=lm(p~fpartnumber*ftest*finspector)
summary(model)
anova(model)

但是当我运行它时我得到了这个错误:它说fpartnumber的变量长度不同,但是当我检查每个变量的长度并且是90时。发生了什么事?

  

模型= LM(Y〜fpartnumber FTEST finspector)       model.frame.default中的错误(公式= yang~fpartnumber * ftest * finspector,:       变量长度不同(找到'fpartnumber')

1 个答案:

答案 0 :(得分:2)

如果您不想cbind p1 ... p10。

然后尝试:

data.frame(p, fpartnumber, ftest, finspector)

和:

str(p)

使用:

p = c(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10) 

代替