[R] Use of contrasts in ANOVA

Fisher Dennis fisher at plessthan.com
Wed Sep 2 21:02:14 CEST 2015


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

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



More information about the R-help mailing list