How to create two columns for model matrix used in limma?
1
0
Entering edit mode
Ahdee ▴ 50
@ahdee-8938
Last seen 18 months ago
United States

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 ,
    levels=design
)

thanks!

limma edger • 907 views
ADD COMMENT
2
Entering edit mode
@gordon-smyth
Last seen 4 hours ago
WEHI, Melbourne, Australia

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 COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 867 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6