[R] Use of contrasts in ANOVA

David Winsemius dwinsemius at comcast.net
Wed Sep 2 21:53:32 CEST 2015


On Sep 2, 2015, at 12:02 PM, Fisher Dennis wrote:

> R 3.2.0
> OS X
> 
> Colleagues,
> 
> I am confused about the implementation of contrasts in ANOVA.  I have 4 groups (BATCH) in DATA.  My code reads:
> 	contrasts(DATA$BATCH)	<- c(-1, -1, 1, 1)/2
> 	LM						<- lm(VALUES ~ SIDN + BATCH, data=DATA)
> where both SIDN and BATCH are factors and VALUES is a real number
> 
> When I run this code, I get the exact results that a colleague obtains in SAS.  So far, so good.  Also, when I examine DATA$BATCH, i get:
>> DATA$BATCH
> [1] P R S Q R P S Q R P S Q P R Q S R P S Q R P Q S P R Q S P R Q S P R S Q P R
> [39] S Q P R Q S R P Q S R P S Q R P Q S P R S Q R P Q S R P S Q R P Q S P R S Q
> [77] P R S Q R P S Q P R Q S R P Q S P R Q S
> attr(,"contrasts")
>  [,1] [,2] [,3]
> P -0.5 -0.5 -0.5
> Q -0.5  0.5  0.5
> R  0.5  0.5 -0.5
> S  0.5 -0.5  0.5
> Levels: P Q R S
> 
> I noted that columns 2 and 3 did not seem relevant.

The help page for ?contrasts and ?contrasts<- says:

how.many	
How many contrasts should be made. Defaults to one less than the number of levels of x.

So it made 3 contrasts.


>  Out of curiosity, I modified the contrasts code:
> 	contrasts(DATA$BATCH, how.many=1)	<- c(-1, -1, 1, 1)/2
> Now when I examine DATA$BATCH, I get:
>> DATA$BATCH
> [1] P R S Q R P S Q R P S Q P R Q S R P S Q R P Q S P R Q S P R Q S P R S Q P R
> [39] S Q P R Q S R P Q S R P S Q R P Q S P R S Q R P Q S R P S Q R P Q S P R S Q
> [77] P R S Q R P S Q P R Q S R P Q S P R Q S
> attr(,"contrasts")
>  [,1]
> P -0.5
> Q -0.5
> R  0.5
> S  0.5
> Levels: P Q R S
> 
> This matches the previous output except for the disappearance of columns 2 and 3.  I thought that would be desirable.
> When I now run the code, the estimate for BATCH did not change — however, its SE (and the resulting t value) changed 10%.  

Wouldn't the number of degrees of freedom change when the contrasts changed? Specifics would depend on specifics. The implicit model would be different. When you run the first version with lm you see 3 non-Intercept parameters, while you only see 1 with the second model.

-- 
David.


> 
> I don’t understand how the 
> 	how.many 
> option in contrasts works.  Can anyone offer an explanation?
> 
> Thanks in advance.
> 
> Dennis	
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list