简体   繁体   English

为什么与excel和手动计算相比,使用R获得单向方差分析输出的方式有所不同?

[英]Why do I get different one way ANOVA output using R compared to excel and manual calculation?

I'm new to R, so might be doing something wrong, but I've searched many different ways of doing the same thing, and still get the same results. 我是R的新手,所以可能做错了什么,但是我搜索了许多不同的方法来做相同的事情,但仍然得到相同的结果。 I have the following data (15 measures, 3 factors, 5 measures in each): measures: 我有以下数据(15个度量,3个因子,每个度量5个度量):度量:

##factors: 1, 2, 3

         Y    Z
1     43.0    1
2     40.5    1
3     39.6    1
4     44.9    1
5     37.2    1
6     44.4    2
7     40.5    2
8     40.1    2
9     43.3    2
10    36.1    2
11    41.1    3
12    39.2    3
13    36.4    3
14    37.2    3
15    36.7    3

When I perform a one-way anova in R, using > anova(lm(Y~Z, data=data)) , I get F=2.7934, p=0.1185 and Df=1. 当我在R中使用> anova(lm(Y~Z, data=data)) Y〜Z, anova(lm(Y~Z, data=data))执行单向方差分析时,得到F = 2.7934,p = 0.1185和Df = 1。 I get the same result using aov and AOVModel functions too. 我也使用aov和AOVModel函数得到相同的结果。

However, both Excel and the manual calculations (and Minitab, actaully) give me F=1.728 and p=0.219, with 2 degrees of freedom. 但是,Excel和手动计算(以及Minitab,实际上)都给我F = 1.728和p = 0.219,并具有2个自由度。 I cannot understand this - what am I doing wrong? 我无法理解-我在做什么错?

Thanks 谢谢

This is because you have the data$Z as a numeric variable. 这是因为您将data$Z作为数字变量。 See Dason's comment above. 请参阅上方的Dason评论。 So you'd want to convert Z to a factor (I renamed data to dat as data is the name of an R base object). 因此,您需要将Z转换为因子(我将数据重命名为dat,因为data是R基础对象的名称)。 Here's how: 这是如何做:

dat$Z <- as.factor(dat$Z)

Yielding: 屈服:

> anova(lm(Y~Z, data=dat))
Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value Pr(>F)
Z          2 26.949 13.4747  1.7281  0.219
Residuals 12 93.568  7.7973  

Side note use str to see how your variables are stored. 旁注使用str查看变量的存储方式。 It's one of the most used R functions. 它是最常用的R函数之一。

So... 所以...

str(dat) would have told you: str(dat)会告诉您:

> str(dat)
'data.frame':   15 obs. of  2 variables:
 $ Y: num  43 40.5 39.6 44.9 37.2 44.4 40.5 40.1 43.3 36.1 ...
 $ Z: int  1 1 1 1 1 2 2 2 2 2 ...

And after the factor conversion: 在因子转换之后:

> str(dat)
'data.frame':   15 obs. of  2 variables:
 $ Y: num  43 40.5 39.6 44.9 37.2 44.4 40.5 40.1 43.3 36.1 ...
 $ Z: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM