[R] car::linearHypothesis Sum of Sqaures Error?
John Jay Wiley Jr.
jwileyjr at syr.edu
Tue Oct 9 22:38:43 CEST 2012
John,
Thank you.
There are different means of the covariate among the levels in my contrasts, which is producing the non-orthogonality and non-additivity of the sum of squares.
Should be all set.
Cheers,
John
________________________________________
From: John Fox [jfox at mcmaster.ca]
Sent: Tuesday, October 09, 2012 4:00 PM
To: John Jay Wiley Jr.
Cc: r-help at r-project.org
Subject: RE: [R] car::linearHypothesis Sum of Sqaures Error?
Dear John,
> -----Original Message-----
> From: John Jay Wiley Jr. [mailto:jwileyjr at syr.edu]
> Sent: Tuesday, October 09, 2012 3:37 PM
> To: John Fox
> Cc: r-help at r-project.org
> Subject: RE: [R] car::linearHypothesis Sum of Sqaures Error?
>
> John,
>
> Again, thank you for your reply.
>
> Unfortunately, I didn't specify that I had previously changed the
> contrast options to contr.sum. I was aware of that requirement, and I
> still found that the sum of squares didn't add up.
>
> It seems that you made the SS add up by removing the covariate.
> Ultimately, the reason I moved to Anova and linearHypothesis was to deal
> with the covariate since Type I sum of squares produce non-orthogonality
> in factorial ANCOVAs.
>
> I am interested to know how you assessed the covariate as unbalanced? It
> is equally replicated across the groups and the variances are similar.
> Is there another metric I am missing for a continuous covariate?
Unless the covariate mean is identical in the groups (combinations of factor
levels), it is unbalanced in the sense of being correlated with the
contrasts for groups. You could easily have checked that by looking the
covariance matrix of the coefficients.
John
>
> Cheers,
> John
>
> ________________________________________
> From: John Fox [jfox at mcmaster.ca]
> Sent: Tuesday, October 09, 2012 2:59 PM
> To: John Jay Wiley Jr.
> Cc: r-help at r-project.org
> Subject: RE: [R] car::linearHypothesis Sum of Sqaures Error?
>
> Dear John,
>
> > -----Original Message-----
> > From: John Jay Wiley Jr. [mailto:jwileyjr at syr.edu]
> > Sent: Tuesday, October 09, 2012 9:17 AM
> > To: John Fox
> > Cc: r-help at r-project.org
> > Subject: RE: [R] car::linearHypothesis Sum of Sqaures Error?
> >
> > John,
> >
> > Thank you for the reply.
> >
> > The data are balanced; I double-checked. I believe the contrasts are
> > orthogonal. Sum of squares in summary(aov) with the contrasts split
> out
> > add to the main effect. I am still unsure of where the error is for
> the
> > sum of squares calculation.
> >
> > I have written some code with a parallel model structure that may help
> > (see below).
> >
> > Can linearHypothesis return type II tests? It seems to only return
> type
> > III with no option to set 'type'.
> >
> > Cheers,
> > John
> >
> > y<-runif(36,0,100)
> > block<-factor(rep(c("A","B","C"),each=12))
> > a<-factor(rep(c("A","B"),times=3,each=6))
> > b<-factor(rep(c("A","B"),times=6,each=3))
> > c<-factor(rep(c("A","B","C"),times=12,each=1))
> > covar<-0.6*y+rnorm(36,10,25)
> > data<-data.frame(y,block,a,b,c,covar)
> >
> > c_contrasts<-matrix(c(-1,2,-1,1,0,-1),3,2)
> > dimnames(c_contrasts)<-list(levels(data$c),c("B vs. A&C","A vs. C"))
> > contrasts(data$c)<-c_contrasts
> >
> > model<-lm(y~block+a*b*c+covar,data=data)
> > summary.aov(model,split=list(c=list("B vs. A&C"=1,"A vs. C"=2)))
> > #Sum of squares add here, but factorial ANCOVA non-orthogonal in type
> I
> > SS
> >
> > Anova(model,type=2)
> > Anova(model,type=3)
> > linearHypothesis(model,c("cB vs. A&C","cA vs. C"))
>
> There are two problems here: (1) the covariate isn't balanced; (2) you
> didn't pay attention to the contrasts for factors other than c. The
> default
> is contr.treatment, which produces terms whose contrasts are not
> orthogonal
> in the row basis of the design.
>
> So, the following two ANOVAs (not ANCOVAs) give the same result, but the
> third doesn't. For balanced data, types I (sequential), II, and III
> tests
> should all be the same.
>
> ----------- snip --------------
>
> model.2 <- lm(y~block+a*b*c, data=data)
> anova(model.2)
> Anova(model.2)
> Anova(model.2, type=3) # incorrect
>
> ----------- snip --------------
>
> Here's one way to get correct type III tests:
>
> ----------- snip --------------
>
> options(contrasts=c("contr.sum", "contr.poly"))
> model.3 <- update(model.2)
> anova(model.3)
> Anova(model.3)
> Anova(model.3, type=3) # now all the same
>
> ----------- snip --------------
>
> > #Anova and linear hypothesis produce equal sum of squares for c
> > main effect in type III
> > linearHypothesis(model,"cB vs. A&C")
> > linearHypothesis(model,"cA vs. C")
> > #Sum of squares of the individual contrasts do not add to the
> main
> > effect of c
>
> This is a manifestation of the same problem:
>
> ----------- snip --------------
>
> linearHypothesis(model.3, c("cB vs. A&C","cA vs. C")) # SS equal to the
> sum
> of the next two
>
> linearHypothesis(model.3, "cB vs. A&C")
> linearHypothesis(model.3, "cA vs. C")
>
> ----------- snip --------------
>
> It doesn't make sense to ask whether linearHypothesis() does "type II"
> or
> "type III" tests -- it just tests directly specified linear hypotheses
> concerning the parameters of the model as parametrized. Anova() can
> formulate sets of type II and III linear hypotheses (but for the latter,
> you
> do have to pay attention to the parametrization).
>
> Best,
> John
>
> >
> >
> > John J. Wiley, Jr.
> > PhD Candidate
> > State University of New York
> > College of Environmental Science and Forestry
> > Department of Environmental and Forest Biology
> > 460 Illick Hall
> > Syracuse, NY 13210
> > 315.470.4825 (office)
> > 740.590.6121 (cell)
> >
> > ________________________________________
> > From: John Fox [jfox at mcmaster.ca]
> > Sent: Tuesday, October 09, 2012 7:15 AM
> > To: John Jay Wiley Jr.
> > Cc: r-help at r-project.org
> > Subject: Re: [R] car::linearHypothesis Sum of Sqaures Error?
> >
> > Dear John
> >
> > On Tue, 9 Oct 2012 02:07:07 +0000
> > "John Jay Wiley Jr." <jwileyjr at syr.edu> wrote:
> > > I am working with a RCB 2x2x3 ANCOVA, and I have noticed a
> difference
> > in the calculation of sum of squares in a Type III calculation.
> >
> > For type III tests, you should use contrasts that are orthogonal in
> the
> > row basis of the design. Perhaps you've done that (by setting the
> > contrasts for the factors directly), but I suspect not. Why not just
> use
> > type II tests? They're hard to screw up.
> >
> > As well, I assume that the variables that enter additively are the
> > covariates. If not, and a covariate is involved in the interaction,
> the
> > type III tests aren't sensible unless the 0 point of the covariate is
> > where you want to test a "main effect" or lower-order interaction.
> >
> > >
> > > Anova output is a follows:
> > >
> > > > Anova(aov(MSOIL~Forest+Burn*Thin*Moisture+ROCK,data=env3l),type=3)
> > > Anova Table (Type III tests)
> > >
> > > Response: MSOIL
> > > Sum Sq Df F value Pr(>F)
> > > (Intercept) 22.3682 1 53.2141 3.499e-07 ***
> > > Forest 1.0954 2 1.3029 0.29282
> > > Burn 2.6926 1 6.4058 0.01943 *
> > > Thin 0.0494 1 0.1176 0.73503
> > > Moisture 1.2597 2 1.4984 0.24644
> > > ROCK 2.1908 1 5.2119 0.03296 *
> > > Burn:Thin 0.2002 1 0.4764 0.49763
> > > Burn:Moisture 1.0612 2 1.2623 0.30360
> > > Thin:Moisture 1.6590 2 1.9734 0.16392
> > > Burn:Thin:Moisture 1.1175 2 1.3292 0.28605
> > > Residuals 8.8272 21
> > >
> > >
> > > However, I would like to calculate some a priori contrasts within
> the
> > Moisture factor as follows:
> > >
> > > Transect_moisture_contrasts<-matrix(c(-1,2,-1,1,0,-1),3,2)
> > > dimnames(Transect_moisture_contrasts)<-
> list(levels(env$Moisture),c("I
> > vs. X&M","X vs. M"))
> > > contrasts(env$Moisture)<-Transect_moisture_contrasts
> > > > contrasts(env3l$Moisture)
> > > I vs. X&M X vs. M
> > > X -1 1
> > > I 2 0
> > > M -1 -1
> > >
> > >
> > > soilmodel<-lm(MSOIL~Forest+Burn*Thin*Moisture+ROCK,data=env3l)
> > > > linearHypothesis(soilmodel,"MoistureI vs. X&M")
> > > Linear hypothesis test
> > >
> > > Hypothesis:
> > > MoistureI vs. X&M = 0
> > >
> > > Model 1: restricted model
> > > Model 2: MSOIL ~ Forest + Burn * Thin * Moisture + ROCK
> > >
> > > Res.Df RSS Df Sum of Sq F Pr(>F)
> > > 1 22 9.4106
> > > 2 21 8.8272 1 0.58333 1.3877 0.252
> > > > linearHypothesis(soilmodel,"MoistureX vs. M")
> > > Linear hypothesis test
> > >
> > > Hypothesis:
> > > MoistureX vs. M = 0
> > >
> > > Model 1: restricted model
> > > Model 2: MSOIL ~ Forest + Burn * Thin * Moisture + ROCK
> > >
> > > Res.Df RSS Df Sum of Sq F Pr(>F)
> > > 1 22 9.6359
> > > 2 21 8.8272 1 0.80871 1.9239 0.18
> > >
> > > The sum of squares for these two contrasts do not add up to the sum
> of
> > squares of the main effect Moisture
> > > > .80871+.58333
> > > [1] 1.39204
> > > > 1.39204-1.2596
> > > [1] 0.13244
> > >
> > > Checking them together produces the correct sum of squares for the
> > main effect
> > > > linearHypothesis(soilmodel,c("MoistureI vs. X&M","MoistureX vs.
> M"))
> > > Linear hypothesis test
> > >
> > > Hypothesis:
> > > MoistureI vs. X&M = 0
> > > MoistureX vs. M = 0
> > >
> > > Model 1: restricted model
> > > Model 2: MSOIL ~ Forest + Burn * Thin * Moisture + ROCK
> > >
> > > Res.Df RSS Df Sum of Sq F Pr(>F)
> > > 1 23 10.0869
> > > 2 21 8.8272 2 1.2596 1.4984 0.2464
> > >
> > >
> > > So my question is:
> > > Should the sum of squares for the two contrasts add to the main
> effect
> > here?
> >
> > Only if the data are balanced.
> >
> > I hope this helps,
> > John
> >
> > ------------------------------------------------
> > John Fox
> > Sen. William McMaster Prof. of Social Statistics
> > Department of Sociology
> > McMaster University
> > Hamilton, Ontario, Canada
> > http://socserv.mcmaster.ca/jfox/
> >
> > > If they should, maybe we can figure out why mine do not.
> > >
> > > Thanks in advance for any assistance.
> > >
> > > Cheers,
> > > John
> > >
> > >
> > > John J. Wiley, Jr.
> > > PhD Candidate
> > > State University of New York
> > > College of Environmental Science and Forestry
> > > Department of Environmental and Forest Biology
> > > 460 Illick Hall
> > > Syracuse, NY 13210
> > > 315.470.4825 (office)
> > > 740.590.6121 (cell)
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at 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.
> >
> >
>
>
More information about the R-help
mailing list