[R] contrasts
Greg Snow
Greg.Snow at imail.org
Wed Jun 3 20:17:38 CEST 2009
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.
More information about the R-help
mailing list