0

If I have a variable (condition) of 2 levels and want to create a model.matrix R automatically assigns conditionB as the term in the design matrix.

condition <- as.factor( c("A","A","A","B","B","B"))
df <- data.frame(condition)
design <- model.matrix( ~ condition)

> df
  condition
1         A
2         A
3         A
4         B
5         B
6         B


> design
  (Intercept) conditionB
1           1          0
2           1          0
3           1          0
4           1          1
5           1          1
6           1          1
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$condition
[1] "contr.treatment"

Question: I would like to have my results relative to conditionA. How can I specify this in model.matrix() ?

(A workaround would be to inverse the resulting FCs)

Obama
  • 3
  • 2
  • What do you mean by relative to condition A? – Onyambu Jul 03 '18 at 09:35
  • If you run the code, you will see that the resulting design matrix has two terms: (Intercept) conditionB. I would like to have conditionA instead of B. – Obama Jul 03 '18 at 09:38
  • Possible duplicate of [How to force R to use a specified factor level as reference in a regression?](https://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression) – LAP Jul 03 '18 at 09:43
  • My question is specific to the model.matrix() function, cant see this in the mentioned post. – Obama Jul 03 '18 at 09:54

2 Answers2

0

You can use the C function to determine the base that you want to be taken into consideration:

Taking A as the base:

 model.matrix(~C(condition,base=1))
  (Intercept) C(condition, base = 1)2
1           1                       0
2           1                       0
3           1                       0
4           1                       1
5           1                       1
6           1                       1
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$`C(condition, base = 1)`
  2
A 0
B 1

Taking B as the base:

model.matrix(~C(condition,base=2))
  (Intercept) C(condition, base = 2)1
1           1                       1
2           1                       1
3           1                       1
4           1                       0
5           1                       0
6           1                       0
attr(,"assign")
[1] 0 1
attr(,"contrasts")
attr(,"contrasts")$`C(condition, base = 2)`
  1
A 1
B 0
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • I think just re-leveling does not work. It indeed changes the term in the designmatrix to _conditionA_, but If you compare with the dataframe (see main post) the **"1"** is still **"B"**. Just relevelling would potentially compare the wrong cases. – Obama Jul 03 '18 at 10:13
0

Is it the result you want?

df <- data.frame(condition)
design <- model.matrix( ~ condition-1)
design
  conditionA conditionB
1          1          0
2          1          0
3          1          0
4          0          1
5          0          1
6          0          1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$`condition`
[1] "contr.treatment"