I am taking a statistics class and we are supposed to be using Stata but are free to use whatever software we like, so I am teaching myself R. So far, I've been able to replicate homework answers but I'm stumped on this one. As the title says, I've run a two-way ANOVA in R and compared it to the Stata output from a classmate. We get the same answer for the interaction term but not the individual factors. What am I doing wrong?
I have tried running a few different ANOVA tests as I saw some people talking about type II and type III tests using ANOVA from the car package, but those results did not match either (although the interaction term is the same in all of them).
Stata
. anova fiber smoking##vitamin
Number of obs = 314 R-squared = 0.0545
Root MSE = 2.69941 Adj R-squared = 0.0297
Source | Partial SS df MS F Prob>F
----------------+----------------------------------------------------
Model | 128.07633 8 16.009541 2.20 0.0276
|
smoking | 77.883865 2 38.941932 5.34 0.0052
vitamin | 15.546548 2 7.7732741 1.07 0.3454
smoking#vitamin | 23.770589 4 5.9426473 0.82 0.5160
|
Residual | 2222.478 305 7.2868131
----------------+----------------------------------------------------
Total | 2350.5543 313 7.5097582
R
> diet$smoking <- factor(diet$smoking)
> diet$vitamin <- factor(diet$vitamin)
> summary(aov(diet$fiber ~ diet$smoking*diet$vitamin))
Df Sum Sq Mean Sq F value Pr(>F)
diet$smoking 2 93.2 46.62 6.398 0.0019 **
diet$vitamin 2 11.1 5.53 0.760 0.4688
diet$smoking:diet$vitamin 4 23.8 5.94 0.816 0.5160
Residuals 305 2222.5 7.29
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> Anova(lm(diet$fiber ~ diet$smoking + diet$vitamin + diet$smoking:diet$vitamin), type = "II")
Anova Table (Type II tests)
Response: diet$fiber
Sum Sq Df F value Pr(>F)
diet$smoking 82.24 2 5.6431 0.003922 **
diet$vitamin 11.07 2 0.7595 0.468770
diet$smoking:diet$vitamin 23.77 4 0.8155 0.516025
Residuals 2222.48 305
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> Anova(lm(diet$fiber ~ diet$smoking + diet$vitamin + diet$smoking:diet$vitamin), type = "III")
Anova Table (Type III tests)
Response: diet$fiber
Sum Sq Df F value Pr(>F)
(Intercept) 4745.7 1 651.2703 <2e-16 ***
diet$smoking 23.6 2 1.6215 0.1993
diet$vitamin 11.5 2 0.7861 0.4565
diet$smoking:diet$vitamin 23.8 4 0.8155 0.5160
Residuals 2222.5 305
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1