[R] contrasts

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Jun 3 20:35:02 CEST 2009


On 03-Jun-09 17:50:38, Ricardo Arias Brito wrote:
> Hi all,
> I want t use a contrasts in adjusted regression (lm()) for a factor
> with four levels, compared the coefficients first with second, first
> with third, ..., third with fourth.
> Someone can help with a contrast matrix to be used. I tried and not yet
> I obtained.
> 
> Say:
> 
> y <- rnorm(50)
> x <-  cut(rnorm(50, mean=y, sd=0.25),c(-3,-1.5,0,1.5,3))
> reg <-  lm(y ~ x, contrasts = list(x="contr.sum"))
> 
> Thank you,
> Ricardo

I am not sure if you have written what you meant!

If you mean to contrast first level with second, second with third,
and third with fourth (which is not what you wrote), then what you
want is contr.sdif in the MASS package.

However, what you wrote suggests that you want to obtain all possible
pairs of levels:
  1st with 2nd, 1st with 3rd, 1st with 4th,
  2nd with 3rd, 2nd with 4th, 3rd with 4th

This is not feasible at the level of model fitting (which is where
any matrix such as you ask for would be used), since you can
only have 3 linearly independent contrasts between 4 levels.
In this case you would be asking for 6 contrasts, so 3 of them
would be linear combinations of the 3 which were fitted in the
model.

If you use contr.sdif, you get 3 of them (1st with 2nd, 2nd with 3rd,
3rd with 4th) straight off. The other three can then be derived
subsequently from these 3 via differences. The model fit should
allow you to obtain the variance-covariance matrix of the three
which were fitted. Then you can derive the variance-covariance
matrix (and hence the SEs) of your derived contrasts.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 03-Jun-09                                       Time: 19:34:59
------------------------------ XFMail ------------------------------




More information about the R-help mailing list