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

peter dalgaard pdalgd at gmail.com
Mon Jan 18 00:39:21 CET 2016

```> On 17 Jan 2016, at 19:34 , Charles C. Berry <ccberry at ucsd.edu> wrote:
>
>
> 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.
>

And IIRC: yup, and one of the issues is that
(a) some rules work left-to-right
(b) the logic is oblivious to the factor/vector distinction

For factors a,b,c, what happens for ~a:b + b:c is that a:b gets the full term expansion since the marginals a and b are not in the model but since b is part of the fully expanded a:b,  b:c gets the reduced form expansion as it would in ~b + b:c (the c-within-b thing). Swapping the terms gives you a different result, but at least it is the same model in the sense that the columns span the same subspace.

If a and b are vectors, and c is a factor, you get the same logic: expand a:b fully, then treat b:c as in b + b:c. Unfortunately, a:b is just the product of a and b, whether or not it is fully expanded, so it doesn't really make sense to proceed as if b is contained in a preceding term. So the net result is that you end up with one column less than you probably wanted.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

```