design matrix construction question

4 messages Options
Embed this post
Permalink
Ben Bolker

design matrix construction question

Reply Threaded More More options
Print post
Permalink

  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
[hidden email] / www.zoology.ufl.edu/bolker
GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc



______________________________________________
[hidden email] 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.

signature.asc (269 bytes) Download Attachment
David Winsemius

Re: design matrix construction question

Reply Threaded More More options
Print post
Permalink

On Nov 2, 2009, at 10:40 PM, Ben Bolker wrote:

>
>  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?

Elegance? You decide. But at least more economical from a keystroke  
perspective:

model.matrix(~z-1,dd)*(dd$x>0)
#------
   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
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$z
[1] "contr.treatment


> 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
>
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[hidden email] 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.
Ken Knoblauch

Re: design matrix construction question

Reply Threaded More More options
Print post
Permalink
> On Nov 2, 2009, at 10:40 PM, Ben Bolker wrote:
> >  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

How about something like this?

dd$xf <- (dd$x > 0) + 0
model.matrix(~ z:xf - 1, dd)
  za:xf zb:xf
1     1     0
2     1     0
3     1     0
4     0     0
5     0     0
6     0     0
7     0     1
8     0     1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$z
[1] "contr.treatment"

> > thanks
> >  Ben Bolker
> >
> >

--
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html

______________________________________________
[hidden email] 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.
Greg Snow-2

Re: design matrix construction question

Reply Threaded More More options
Print post
Permalink
In reply to this post by Ben Bolker
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
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Ben Bolker
> Sent: Monday, November 02, 2009 8:41 PM
> To: [hidden email]
> 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
> [hidden email] / www.zoology.ufl.edu/bolker
> GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc

______________________________________________
[hidden email] 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.