[R] Design matrix question

Steven McKinney smckinney at bccrc.ca
Thu May 17 04:34:19 CEST 2007


Hi useRs,

Perhaps I am having a senior moment?

I have a nested variable situation to model,

toy example:


> df <- data.frame(A = factor(c("a", "a", "x", "x"), levels = c("x", "a")),
+                  B = factor(c("b", "x", "x", "x"), levels = c("x", "b")))
> 
> df
  A B
1 a b
2 a x
3 x x
4 x x

So of course the full design matrix is singular

> model.matrix(~ A * B, df)
  (Intercept) Aa Bb Aa:Bb
1           1  1  1     1
2           1  1  0     0
3           1  0  0     0
4           1  0  0     0
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$A
[1] "contr.treatment"

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


I'd like not to have the B term main effect in the model

> model.matrix(~ A * B - B, df)
  (Intercept) Aa Ax:Bb Aa:Bb
1           1  1     0     1
2           1  1     0     0
3           1  0     0     0
4           1  0     0     0
attr(,"assign")
[1] 0 1 2 2
attr(,"contrasts")
attr(,"contrasts")$A
[1] "contr.treatment"

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

> 

How do I get model.matrix to not add that
column of zeroes?

Why does model.matrix add that column of zeroes?

Is this a bug, or a senior moment?





Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney at bccrc.ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C. 
V5Z 1L3
Canada



More information about the R-help mailing list