Anita Narwani
2010-Jun-03 00:13 UTC
[R] Nested ANOVA with covariate using Type III sums of squares
Hello, I have been trying to get an ANOVA table for a linear model containing a single nested factor, two fixed factors and a covariate: carbonmean<-lm(C.Mean~ Mean.richness + Diversity + Zoop + Diversity/Phyto + Zoop*Diversity/Phyto) where, *Mean.richness* is a covariate*, Zoop* is a categorical variable (the species), *Diversity* is a categorical variable (Low or High), and *Phyto*(community composition) is also categorical but is nested within the level of *Diversity*. Quinn & Keough's statistics text recommends using Type III SS for a nested ANOVA with a covariate. I get the following output using the Type I SS ANOVA: Analysis of Variance Table Response: C.Mean Df Sum Sq Mean Sq F value Pr(>F) Mean.richness 1 56385326 56385326 23.5855 3.239e-05 *** Diversity 1 14476593 14476593 6.0554 0.019634 * Zoop 1 13002135 13002135 5.4387 0.026365 * Diversity:Phyto 6 126089387 21014898 8.7904 1.257e-05 *** Diversity:Zoop 1 263036 263036 0.1100 0.742347 Diversity:Zoop:Phyto 6 61710145 10285024 4.3021 0.002879 ** Residuals 31 74110911 2390675 I have tried using both the drop1() command and the Anova() command in the car package. When I use the Anova command I get the following error message:>Anova(carbonmean,type="III")“Error in linear.hypothesis.lm(mod, hyp.matrix, summary.model = sumry,: One or more terms aliased in model.” I am not sure why this is aliased. There are no missing cells, and the cells are balanced (aside from for the covariate). Each Phyto by Zoop cross is replicated 3 times, and there are four Phyto levels within each level of Diversity. When I remove the nested factor (Phyto), I am able to get the Type III SS output. Then when I use drop1(carbonmean,.~.,Test=”F”) I get the following output:> drop1(carbonmean,.~.,Test="F")Single term deletions Model: C.Mean ~ Mean.richness + Diversity + Zoop + Diversity/Phyto + Zoop * Diversity/Phyto Df Sum of Sq RSS AIC <none> 74110911 718 Mean.richness 1 49790403 123901314 741 Diversity 0 0 74110911 718 Zoop 0 0 74110911 718 Diversity:Phyto 6 118553466 192664376 752 Diversity:Zoop 0 -1.49e-08 74110911 718 Diversity:Zoop:Phyto 6 61710145 135821055 735 There are zero degrees of freedom for Diversity, Zoop and their interaction, and zero sums of sq for Diversity and Zoop. This cannot be correct, however when I do the model simplification by dropping terms from the models manually and comparing them using anova(), I get virtually the same results. I would appreciate any suggestions for things to try or pointers as to what I may be doing incorrectly. Thank you. Anita Narwani. [[alternative HTML version deleted]]
Joris Meys
2010-Jun-03 01:00 UTC
[R] Nested ANOVA with covariate using Type III sums of squares
That's what one would expect with type III sum of squares. You have Phyto twice in your model, but only as a nested factor. To compare the full model with a model without diversity of zoop, you have either the combination diversity/phyto, zoop/phyto or phyto twice in the formula. That's aliasing. Depending on how you stand on type III sum of squares, you could call that a "bug". Personally, I'd just not use them. https://stat.ethz.ch/pipermail/r-help/2001-October/015984.html Cheers Joris On Thu, Jun 3, 2010 at 2:13 AM, Anita Narwani <anitanarwani@gmail.com>wrote:> Hello, > > I have been trying to get an ANOVA table for a linear model containing a > single nested factor, two fixed factors and a covariate: > > carbonmean<-lm(C.Mean~ Mean.richness + Diversity + Zoop + Diversity/Phyto > + > Zoop*Diversity/Phyto) > > > > where, *Mean.richness* is a covariate*, Zoop* is a categorical variable > (the > species), *Diversity* is a categorical variable (Low or High), and > *Phyto*(community composition) is also categorical but is nested > within the level > of *Diversity*. Quinn & Keough's statistics text recommends using Type III > SS for a nested ANOVA with a covariate. > > I get the following output using the Type I SS ANOVA: > > > > Analysis of Variance Table > Response: C.Mean > Df Sum Sq > Mean > Sq F value Pr(>F) > Mean.richness 1 56385326 56385326 > 23.5855 3.239e-05 *** > Diversity 1 14476593 > 14476593 > 6.0554 0.019634 * > Zoop 1 13002135 > 13002135 > 5.4387 0.026365 * > Diversity:Phyto 6 126089387 21014898 > 8.7904 1.257e-05 *** > Diversity:Zoop 1 263036 > 263036 > 0.1100 0.742347 > Diversity:Zoop:Phyto 6 61710145 10285024 > 4.3021 > 0.002879 ** > Residuals 31 74110911 > 2390675 > > I have tried using both the drop1() command and the Anova() command in the > car package. > > When I use the Anova command I get the following error message: > > >Anova(carbonmean,type="III") > > “Error in linear.hypothesis.lm(mod, hyp.matrix, summary.model = sumry,: One > or more terms aliased in model.” > > > > I am not sure why this is aliased. There are no missing cells, and the > cells > are balanced (aside from for the covariate). Each Phyto by Zoop cross is > replicated 3 times, and there are four Phyto levels within each level of > Diversity. When I remove the nested factor (Phyto), I am able to get the > Type III SS output. > > > > Then when I use drop1(carbonmean,.~.,Test=”F”) I get the following output: > > > drop1(carbonmean,.~.,Test="F") > > Single term deletions > > > > Model: > > C.Mean ~ Mean.richness + Diversity + Zoop + Diversity/Phyto + Zoop * > Diversity/Phyto > > Df Sum of Sq > RSS AIC > > <none> 74110911 718 > > Mean.richness 1 49790403 123901314 > 741 > > Diversity 0 0 > 74110911 718 > > Zoop 0 0 > 74110911 718 > > Diversity:Phyto 6 118553466 192664376 > 752 > > Diversity:Zoop 0 -1.49e-08 74110911 > 718 > > Diversity:Zoop:Phyto 6 61710145 135821055 > 735 > > > > There are zero degrees of freedom for Diversity, Zoop and their > interaction, > and zero sums of sq for Diversity and Zoop. This cannot be correct, however > when I do the model simplification by dropping terms from the models > manually and comparing them using anova(), I get virtually the same > results. > > > > I would appreciate any suggestions for things to try or pointers as to what > I may be doing incorrectly. > > > > Thank you. > > Anita Narwani. > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Anita Narwani
2010-Jun-03 19:59 UTC
[R] Nested ANOVA with covariate using Type III sums of squares
Thanks for your response Joris. I was aware of the potential for aliasing, although I thought that this was only a problem when you have missing cell means. It was interesting to read the vehement argument regarding the Type III sums of squares, and although I knew that there were different positions on the topic, I had no idea how divisive it was. Nevertheless, Type III SS are generally recommended by statistical texts in ecology for my type of experimental design. Interestingly, despite the aliasing, SPSS has no problems calculating Type III SS for this data set. This is simply because I am entering a co-variate, which causes non-orthogonality. I would be happier using R and the default Type I SS, which are the same as the Type III SS anyway when I omit the co-variate of Mean.richness, except that these results are very sensitive to the order in which I add the variables into the model when I do enter the co-variate. I understand that the order is very important relates back to the scientific hypothesis, but I am equally interested in the main effects of Zoop, Diversity, and the nested effect of Phyto, so entering either of these variables before the other does not make sense from an ecological perspective, and because the results do change, the order cannot be ignored from a statistical perspective. Finally, I have tried using the Type II SS and received similar warnings. Do you have a recommendations? Anita. [[alternative HTML version deleted]]
Joris Meys
2010-Jun-03 23:25 UTC
[R] Nested ANOVA with covariate using Type III sums of squares
I see where my confusion comes from. I counted 4 levels of Phyto, but you have 8, being 4 in every level of Diversity. There's your aliasing.> table(Diversity,Phyto)???????? Phyto Diversity M1 M2 M3 M4 P1 P2 P3 P4 ??????? H? 0? 0? 0? 0? 6? 6? 6? 6 ??????? L? 6? 6? 6? 6? 0? 0? 0? 0 There's no need to code them differently for every level of Diversity. If you don't, all is fine :> Phyto <- gsub("M","P",as.character(Phyto)) > Phyto <- as.factor(Phyto) > > test <- lm(C.Mean~? Mean.richness + Diversity + Zoop + Diversity/Phyto ++ Zoop*Diversity/Phyto)> > Anova(test,type="III")Anova Table (Type III tests) Response: C.Mean ??????????????????????? Sum Sq Df F value??? Pr(>F) (Intercept)?????????? 23935609? 1 10.0121 0.0034729 ** Mean.richness???????? 49790385? 1 20.8269 7.471e-05 *** Diversity???????????? 35807205? 1 14.9779 0.0005234 *** Zoop????????????????? 10794614? 1? 4.5153 0.0416688 * Diversity:Phyto????? 118553464? 6? 8.2650 2.184e-05 *** Diversity:Zoop????????? 261789? 1? 0.1095 0.7429356 Diversity:Zoop:Phyto? 61710162? 6? 4.3021 0.0028790 ** Residuals???????????? 74110938 31 --- Signif. codes:? 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1>You can check with summary(test) that the model is fitted correctly. On Fri, Jun 4, 2010 at 12:48 AM, Anita Narwani <anitanarwani at gmail.com> wrote:> > You have everything right except that there are only 2 zooplankton species (C & D, which stand for Ceriodaphnia and Daphnia). >