Question: How to create two columns for model matrix used in limma?
gravatar for Ahdee
7 days 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 • 38 views
ADD COMMENTlink modified 7 days ago by Gordon Smyth38k • written 7 days ago by Ahdee40
Answer: How to create two columns for model matrix used in limma?
gravatar for Gordon Smyth
7 days 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 6 days ago • written 7 days ago by Gordon Smyth38k
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: 338 users visited in the last hour