[R] Specify contrasts in R

peter dalgaard pdalgd at gmail.com
Mon Aug 2 16:22:31 CEST 2010


On Aug 2, 2010, at 3:53 PM, Rebecca Ross wrote:

> Could anyone advise if this is the 'correct' contrast matrix to use to make contrasts between one treatment and the mean of 2 other treatments (in this case, mid-parent values to the F1 and F2 from a line cross experiment)?
> Desired contrasts are
> 1) P1 versus P2
> 2) average of P1 and P2 versus F1
> 3) average of P1 and P2 versus F2
> levels(MyData$Type)
> "P1", "F1", "F2","P2"
> (I have renamed these A, B, C, D in the actual data so they appear in the order above)
> Contrast matrix:
> contr_mat<-cbind(
> c(1,0,0,-1),
> c(-1,2,0,-1),
> c(-1,0,2,-1))
> 
> contrasts(MyData$Type)<-contr_mat

If you have played with this for some time, you develop a strong aversion to non-orthogonal contrast matrices...

You have to be very careful to notice that contrast matrices map FROM parameters TO mean values, whereas what we normally call contrasts map in the other direction.

In general, to get a set of parameters that has a particular contrast parametrization, you have to do a (generalized) matrix inverse. In the present case,

> library(MASS)
> zapsmall(ginv(rbind(c(1,0,0,-1),c(-.5,1,0,-.5),c(-.5,0,1,-.5))))
     [,1]  [,2]  [,3]
[1,]  0.5 -0.25 -0.25
[2,]  0.0  0.75 -0.25
[3,]  0.0 -0.25  0.75
[4,] -0.5 -0.25 -0.25

which looks pretty whacked when you first look at it, but to wit:

> CM <- zapsmall(ginv(rbind(c(1,0,0,-1),c(-.5,1,0,-.5),c(-.5,0,1,-.5))))
> solve(cbind(1,CM))
      [,1] [,2] [,3]  [,4]
[1,]  0.25 0.25 0.25  0.25
[2,]  1.00 0.00 0.00 -1.00
[3,] -0.50 1.00 0.00 -0.50
[4,] -0.50 0.00 1.00 -0.50

So, no, you guessed incorrectly.


-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list