[R] Model matrix with redundant columns included

Hong Ooi Hong.Ooi at iag.com.au
Wed Mar 14 04:57:45 CET 2007


_______________________________________________________________________________________


Hello,

Normally when you call model.matrix, you get a matrix that has
aliased/redundant columns deleted. For example:

> m <- expand.grid(a=factor(1:3), b=factor(1:3))
> model.matrix(~a + b, m)
  (Intercept) a2 a3 b2 b3
1           1  0  0  0  0
2           1  1  0  0  0
3           1  0  1  0  0
4           1  0  0  1  0
5           1  1  0  1  0
6           1  0  1  1  0
7           1  0  0  0  1
8           1  1  0  0  1
9           1  0  1  0  1
attr(,"assign")
[1] 0 1 1 2 2
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"

attr(,"contrasts")$b
[1] "contr.treatment"

The result is a matrix with 5 columns including the intercept.

However, for my purposes I need a matrix that includes all columns,
including those that would normally be redundant. Is there any way to do
this? For the example, this would be something like

  a1 a2 a3 b1 b2 b3
1  1  0  0  1  0  0
2  0  1  0  1  0  0
3  0  0  1  1  0  0
4  1  0  0  0  1  0
5  0  1  0  0  1  0
6  0  0  1  0  1  0
7  1  0  0  0  0  1
8  0  1  0  0  0  1
9  0  0  1  0  0  1

Including -1 as part of the model formula removes the intercept and adds
the column for the base level of the first variable, but not the rest.

Thanks,


-- 
Hong Ooi
Senior Research Analyst, IAG Limited
388 George St, Sydney NSW 2000
+61 (2) 9292 1566

_______________________________________________________________________________________

The information transmitted in this message and its attachme...{{dropped}}



More information about the R-help mailing list