# [R] Order of formula terms in model.matrix

Lars Bishop lars52r at gmail.com
Sun Jan 17 21:10:19 CET 2016

```Question: I(a*b) would work as long as both “a" and “b” are numeric. Is there a way I can force the behaviour of model.matrix when one of these variables is a factor (as in "f1:trt" from my example below)?

Specifically, based on my example below, I would like to always return the first matrix (where the first level of “f1” is omitted in the resulting matrix). This would’t happen if the user specifies the terms in reverse order (as per the second matrix).

Thanks again,
Lars.

> On Jan 17, 2016, at 2:53 PM, Lars Bishop <lars52r at gmail.com> wrote:
>
> This is very helpful, thanks!
>
> Lars.
>
>
>> On Jan 17, 2016, at 1:34 PM, Charles C. Berry <ccberry at ucsd.edu> wrote:
>>
>> On Sun, 17 Jan 2016, Lars Bishop wrote:
>>
>>> I’d appreciate your help on understanding the following.
>>
>>> It is not very clear to me from the model.matrix documentation, why simply changing the order of terms in the formula may change the number of resulting columns. Please note I’m purposely not including main effects in the model formula in this case.
>>
>>
>> IIRC, there are some heuristics involved harking back to the White Book. I recall there have been discussions of whether and how this could be fixed before on this list and or R-devel, but I cannot seem to lay my browser on them right now.
>>
>>
>>>
>>> set.seed(1)
>>> x1 <- rnorm(100)
>>> f1 <- factor(sample(letters[1:3], 100, replace = TRUE))
>>> trt <- sample(c(-1,1), 100, replace = TRUE)
>>> df <- data.frame(x1=x1, f1=f1, trt=trt)
>>>
>>> dim(model.matrix( ~ x1:trt + f1:trt, data = df))
>>> [1] 100 4
>>>
>>> dim(model.matrix(~ f1:trt + x1:trt, data = df))
>>> [1] 100 5
>>>
>>
>> By `x1:trt' I guess you mean the same thing as `I(x1*trt)'.
>>
>> If you use the latter form, the issue you raise goes away.
>>
>> Note that `I(some.expr)' gives you the ability to force the behavior of model.matrix to be exactly what you want by suitably crafting `some.expr', heuristics notwithstanding.
>>
>> HTH,
>>
>> Chuck
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help