N-way ANOVA in R

一世执手 提交于 2020-01-25 11:10:52

问题


I need some help in performing N-way ANOVA in R to capture inter dependencies among different factors. In my data, there are around 100 different factors and I am using the following code to perform ANOVA.

model.lm<-lm(y~., data=data)
anova(model.lm)

As far as I know (may be I am wrong) that this performs 1-way ANOVA at each factor alone. For some reasons, I need to perform N-way ANOVA between all the 100 groups i.e from x1 to x100. Do I need to specify each factor like the following or there is a shorthand notation for this?

model.lm<-lm(y~x1*x2*x3....,x100, data=data)
anova(model.lm)

回答1:


You can use update.formula and the ~(.)^n notation.

Eg for a model including 3-way interactions from 4 variables a, b, c and d

update(~a+b+c+d, ~(.)^3)


## ~a + b + c + d + a:b + a:c + a:d + b:c + b:d + c:d + a:b:c + a:b:d + a:c:d + b:c:d

So for your example where you want to fit 100-way interactions, I would suggest thinking of a more appropriate model (especially if it is time you are accounting for here).

If you decide to continue with the basic ANOVA approach you could do something like this (and wait for R to crash due having memory issues due to your large data / inappropriate model.)

xvars <- paste0('x',1:100)
oneway <- reformulate(termlabels=  xvars, response = 'y')


horribleformula <- update(oneway, . ~ (.)^100)

horriblemodel <- lm(horribleformula, data=data)

Or (thanks to @Dason for picking this up)

 stillhorrible <- lm(y ~ .^100, data = data)


来源:https://stackoverflow.com/questions/13557286/n-way-anova-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!