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 Veja quais s?o os assuntos do momento no Yahoo! +Buscados http://br.maisbuscados.yahoo.com
Here is one way: y <- rnorm(50) x <- cut(rnorm(50, mean=y, sd=0.25),c(-3,-1.5,0,1.5,3)) a <- rbind( int=c(1,0,0,0), ab=c(-1,1,0,0), ac=c(-1,0,1,0), cd=c(0,0,-1,1) ) ai <- zapsmall(solve(a)) contrasts(x) <- ai[,-1] reg <- lm(y ~ x, na.action=na.omit) # check tmp <- tapply( y, x, mean ) (tmp2 <- c(tmp[1], tmp[2]-tmp[1], tmp[3]-tmp[1], tmp[4]-tmp[3])) all.equal( as.vector(tmp2), as.vector(coef(reg)) ) hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Ricardo Arias Brito > Sent: Wednesday, June 03, 2009 11:51 AM > To: r-help at r-project.org > Subject: [R] contrasts > > > 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 > > > > Veja quais s?o os assuntos do momento no Yahoo! +Buscados > http://br.maisbuscados.yahoo.com > > ______________________________________________ > 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.
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, > RicardoI 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 ------------------------------