方差分析:自由度几乎都等于 1 [英] ANOVA: Degrees of freedom almost all equal 1
问题描述
我有一个这样开头的数据集:
I have a data set that begins like this:
> d.weight
R N P C D.weight
1 1 0 0 GO 45.3
2 2 0 0 GO 34.0
3 3 0 0 GO 19.1
4 4 0 0 GO 26.6
5 5 0 0 GO 23.5
6 1 45 0 GO 22.1
7 2 45 0 GO 15.5
8 3 45 0 GO 23.4
9 4 45 0 GO 15.8
10 5 45 0 GO 42.9
...
等等.
R
是复制的,有 5 个(1-5).N
是氮含量,也有 5 个(0、45、90、180、360).P
是磷含量,也有 5 个(0, 35, 70, 140, 280).C
是植物组合,有 4 个(GO、GB、LO、LB).D.weight
是以克为单位的干重.
R
is replicate and there are 5 of them (1-5).N
is nitrogen level, and there are 5 as well (0, 45, 90, 180, 360).P
is phosphorus level, and there are 5 as well (0, 35, 70, 140, 280).C
is plant combination, and there are 4 (GO, GB, LO, LB).D.weight
is dry weight in grams.
然而,当我做方差分析时,我得到了错误的自由度.我通常在完整数据集的子集上运行我的方差分析,但让我们做一个分析,否则我实际上不会做,这样你就可以看到几乎所有的 Df(自由度)都是错误的.
However, when I do an ANOVA I get the wrong degrees of freedom. I usually run my ANOVAs on subsets of that full set of data, but let's just do an analysis I wouldn't actually do otherwise, just so you can see that almost all of the Df (degrees of freedom) are wrong.
> example.aov=aov(D.weight ~ R+N+P+C, data=d.weight)
> summary(example.aov)
Df Sum Sq Mean Sq F value Pr(>F)
R 1 1158 1158 9.484 0.00226 **
N 1 202 202 1.657 0.19900
P 1 11040 11040 90.408 < 2e-16 ***
C 3 41032 13677 112.010 < 2e-16 ***
Residuals 313 38220 122
所以,基本上,唯一正确的是 C 因子.是不是因为它有字母而不是数字?
So, basically, the only one that's right is the C factor. Is it because it has letters instead of numbers?
我发现如果我为每个术语编写 interaction()
,我会得到正确的 Df,但我不知道这是否是整体上正确的做法.例如:
I found somewhere that if I write interaction()
with each term, I get the right Df, but I don't know if that's the right thing to do overall. For example:
> example.aov2=aov(D.weight ~ interaction(R)+interaction(N)+interaction(P)+interaction(C), data=d.weight)
> summary(example.aov2)
Df Sum Sq Mean Sq F value Pr(>F)
interaction(R) 4 7423 1856 19.544 2.51e-14 ***
interaction(N) 4 543 136 1.429 0.224
interaction(P) 4 13788 3447 36.301 < 2e-16 ***
interaction(C) 3 41032 13677 144.042 < 2e-16 ***
Residuals 304 28866 95
我用 C
因子试了一下,只是想看看它是否搞砸了什么:
I tried it with the C
factor only to see if it messed up anything:
> example.aov3=aov(D.weight ~ C, data=d.weight)
> summary(example.aov3)
Df Sum Sq Mean Sq F value Pr(>F)
C 3 41032 13677 85.38 <2e-16 ***
Residuals 316 50620 160
>
> example.aov4=aov(D.weight ~ interaction(C), data=d.weight)
> summary(example.aov4)
Df Sum Sq Mean Sq F value Pr(>F)
interaction(C) 3 41032 13677 85.38 <2e-16 ***
Residuals 316 50620 160
它看起来一样.我应该在任何地方添加 interaction()
吗?
And it looks the same. Should I be adding interaction()
everywhere?
推荐答案
R 通过检查变量是否为 numeric
或 factor
变量.最简单的是,您可以通过
R determines whether it should treat variables as categorical (ANOVA-type analysis) or continuous (regression-type analysis) by checking whether they are numeric
or factor
variables. Most simply, you can convert your predictor (independent) variables to factors via
facs <- c("R","N","P")
d.weight[facs] <- lapply(d.weight[facs],factor)
如果你想创建辅助变量而不是覆盖你可以做类似的事情
If you want to create auxiliary variables instead of overwriting you could do something like
for (varname in facs) {
d.weight[[paste0("f",varname)]] <- factor(d.weight[[varname]])
}
可能有一种更紧凑的方法来做到这一点,但应该可以...
There might be a more compact way to do it but that should serve ...
这篇关于方差分析:自由度几乎都等于 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!