Question: How to create two columns for model matrix used in limma?
gravatar for Ahdee
9 weeks ago by
United States
Ahdee40 wrote:

Sorry this is a bit of silly question but its been bothering me a bit. So suppose I have a dataframe as such and want to compare gender or drug.

df = data.frame ( 
    gender = c("m","m","f","m","m","f"),
    drug = c("D1","D2","D1","D2","D1","D2")

gender = factor ( df$gender)
drug = factor ( df$drug)

When I create this model however for some reason the last factor always just converges into one column instead of two.

    model.matrix(~0 + gender + drug )
> model.matrix(~0 + gender + drug )
  genderf genderm drugD2
1       0       1      0
2       0       1      1
3       1       0      0
4       0       1      1
5       0       1      0
6       1       0      1

Why does it do this and how would my contrast look like if I wanted to compare drug D1 and D2 regardless of gender? for example for gender its straighforward and I can just do something like

contrast.matrix <- makeContrasts( 
    Group = genderf-genderm ,


limma edger • 84 views
ADD COMMENTlink modified 9 weeks ago by Gordon Smyth38k • written 9 weeks ago by Ahdee40
Answer: How to create two columns for model matrix used in limma?
gravatar for Gordon Smyth
9 weeks ago by
Gordon Smyth38k
Walter and Eliza Hall Institute of Medical Research, Melbourne, Australia
Gordon Smyth38k wrote:

The 3rd coefficient is the contrast of D2 vs D1. Just test the 3rd coefficient.

In your case, you don't actually need to use makeContrasts at all. You could just use

design <- model.matrix(~ gender + drug)

and then later test for

topTable(fit, coef=2)
topTable(fit, coef=3)

to get the m-f and D2-D1 comparisons respectively.

You model assumes that the D2-D1 comparison is the same for male and females. I assume you are making this assumption deliberately. If you didn't intend to make that assumption, then you should combine gender and drug into one combined factor with four levels.

ADD COMMENTlink modified 9 weeks ago • written 9 weeks ago by Gordon Smyth38k

thanks Gordon, out of curiosity do you know why the model.matrix would deliberatly combine the 3rd coef to one column?

ADD REPLYlink written 6 weeks ago by Ahdee40
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 121 users visited in the last hour