简体   繁体   中英

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. I have the following data (15 measures, 3 factors, 5 measures in each): measures:

##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. I get the same result using aov and AOVModel functions too.

However, both Excel and the manual calculations (and Minitab, actaully) give me F=1.728 and p=0.219, with 2 degrees of freedom. I cannot understand this - what am I doing wrong?

Thanks

This is because you have the data$Z as a numeric variable. See Dason's comment above. 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). 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. It's one of the most used R functions.

So...

str(dat) would have told you:

> 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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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