paul.boutros@utoronto.ca
2004-Jan-24 07:31 UTC
[R] Re-Post: Combining Factors in model.matrix
Hello, I didn't get any response on this before, leading me to believe I've missed something fundamental. Can anybody guide me in the correct direction for more help on this? Paul ================================================I want to be able to create a design matrix with two factors. For instance, if I have:> t1 <- factor(c(1,1,2,2)); > t2 <- factor(c(1,2,1,2)); > design <- model.matrix(~ -1 + (t1+t2)); > design;t11 t12 t22 1 1 0 0 2 1 0 1 3 0 1 0 4 0 1 1 But the design matrix I want is: t1 t2 1 1 0 2 1 1 3 0 0 4 0 1 Actually, in general I'm struggling with the syntax for formulating a design matrix I can write down on paper. Is there a reference for this beyond the R documentation? Thanks, Paul
On Sat, 24 Jan 2004 02:31:19 -0500, you wrote:>Hello, > >I didn't get any response on this before, leading me to believe I've missed >something fundamental. Can anybody guide me in the correct direction for more >help on this?I didn't see the earlier posting, but nothing seems to be wrong with this one.>I want to be able to create a design matrix with two factors. For instance, if >I have: > >> t1 <- factor(c(1,1,2,2)); >> t2 <- factor(c(1,2,1,2)); >> design <- model.matrix(~ -1 + (t1+t2)); >> design; > t11 t12 t22 >1 1 0 0 >2 1 0 1 >3 0 1 0 >4 0 1 1 > >But the design matrix I want is: > t1 t2 >1 1 0 >2 1 1 >3 0 0 >4 0 1You seem to want something like> model.matrix(~ t1+t2 )[,-1]t12 t22 1 0 0 2 0 1 3 1 0 4 1 1 (i.e. leave the intercept in the model, but delete it from the result). This doesn't give the exact encoding you asked for; the "contrasts" options might be able to fix this (see ?contr.poly, or maybe ?contrasts, and experiment a bit.)> >Actually, in general I'm struggling with the syntax for formulating a design >matrix I can write down on paper. Is there a reference for this beyond the R >documentation?I don't know. Duncan Murdoch
On Sat, 24 Jan 2004 paul.boutros at utoronto.ca wrote:> I didn't get any response on this before, leading me to believe I've missed > something fundamental. Can anybody guide me in the correct direction for more > help on this?You will need to explain to us why the object you list is `the design matrix': have *you* a reference for that? R is doing the conventional thing, and I at least have no idea where your example comes from. You seem to have coded variables t1 and t2 the opposite ways (the reference level is 2 for t1 and 1 for t2), and your model has the fit at levels t1=2,t1=1 constrained to pass through the origin. I don't think R has a simple syntax for that (although you can fake anything), and I find it hard to believe that is actually what you want.> > Paul > > ================================================> I want to be able to create a design matrix with two factors. For instance, if > I have: > > > t1 <- factor(c(1,1,2,2)); > > t2 <- factor(c(1,2,1,2)); > > design <- model.matrix(~ -1 + (t1+t2)); > > design; > t11 t12 t22 > 1 1 0 0 > 2 1 0 1 > 3 0 1 0 > 4 0 1 1 > > But the design matrix I want is: > t1 t2 > 1 1 0 > 2 1 1 > 3 0 0 > 4 0 1 > > Actually, in general I'm struggling with the syntax for formulating a design > matrix I can write down on paper. Is there a reference for this beyond the R > documentation?Chapter 6 of MASS has the most complete exposition (by Bill Venables) that I know of, and the White Book (Chambers & Hastie, 1992) goes well beyind the R documentation (which uses it as the reference). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595