Question: How to create two columns for model matrix used in limma?
0
7 days ago by
Ahdee40
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 ,
levels=design
)


thanks!

limma edger • 38 views
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?
0
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.