R中的N路方差分析 [英] N-way ANOVA in R
问题描述
我需要一些帮助来在 R 中执行 N 路方差分析以捕获不同因素之间的相互依赖关系.在我的数据中,大约有 100 个不同的因素,我使用以下代码来执行方差分析.
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)
据我所知(可能是我错了),这仅在每个因素上执行单向方差分析.由于某些原因,我需要在所有 100 个组之间执行 N 路方差分析,即从 x1 到 x100.我需要像下面这样指定每个因素还是有一个简写符号?
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)
推荐答案
您可以使用 update.formula
和 ~(.)^n
符号.
You can use update.formula
and the ~(.)^n
notation.
例如,对于包含来自 4 个变量 a
、b
、c
和 d
的 3 向交互的模型
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
因此,对于您想要拟合 100 向交互的示例,我建议您考虑一个更合适的模型(尤其是在您考虑此处的时候).
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).
如果您决定继续使用基本的方差分析方法,您可以执行类似的操作(并等待 R 因您的大数据/不适当的模型而出现内存问题而崩溃.)
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)
或者(感谢@Dason 选择了这个)
Or (thanks to @Dason for picking this up)
stillhorrible <- lm(y ~ .^100, data = data)
这篇关于R中的N路方差分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!