[R] design matrix construction question
Greg Snow
Greg.Snow at imail.org
Tue Nov 3 19:26:52 CET 2009
Try this:
dd$i <- with(dd,interaction(z,x>0))
contrasts(dd$i,2) <- rbind( c(0,0),c(0,0),c(1,0),c(0,1) )
model.matrix( ~i, dd )[,-1]
--
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 Ben Bolker
> Sent: Monday, November 02, 2009 8:41 PM
> To: r-help at r-project.org
> Subject: [R] design matrix construction question
>
>
> with the following simple data frame
> dd = structure(list(z = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
> ), .Label = c("a", "b"), class = "factor"), x = c(0.3, 0.2, 0.1,
> 0, 0, 0, 0.2, 0.3)), .Names = c("z", "x"), row.names = c(NA,
> -8L), class = "data.frame")
>
> I would like know if it's possible to use model.matrix()
> to construct the following design matrix in some sensible way:
>
> za zb
> 1 1 0
> 2 1 0
> 3 1 0
> 4 0 0
> 5 0 0
> 6 0 0
> 7 0 1
> 8 0 1
>
> In other words, I want column 1 to be (z=="a" & x>0) and
> column 2 to be (z=="b" & x>0). I can construct this matrix
> using
>
> sweep(model.matrix(~z-1,dd),1,dd$x>0,"*")
>
> and then stick it into lm.fit -- but is there a more
> elegant way to do this in general? I haven't found a formula combining
> (z-1) and I(x>0) that works, although I can imagine there is one.
>
> thanks
> Ben Bolker
>
>
> --
> Ben Bolker
> Associate professor, Biology Dep't, Univ. of Florida
> bolker at ufl.edu / www.zoology.ufl.edu/bolker
> GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc
More information about the R-help
mailing list