[R] Is the Intercept Term always in First Position?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jan 5 16:13:48 CET 2010


On Tue, 5 Jan 2010, Viechtbauer Wolfgang (STAT) wrote:

> Dear All,
>
> I have a question about formulas and model.matrix(). If one specifies a model via a formula, the corresponding design matrix can be obtained with the model.matrix() function. For example:
>
> x1 <- c(1,4,2,3,5)
> x2 <- c(1,1,2,2,2)
> myformula <- ~ x1 + factor(x2)
> model.matrix(myformula)
>
> My question is: If an intercept term is in the model (like in the 
> example above), is it always the first column in resulting design 
> matrix?
>
> For example, if I add the intercept explicitly as the last term in 
> the formula, it still ends up in the first column:
>
> myformula <- ~ x1 + factor(x2) + 1
> model.matrix(myformula)

>> So, is this always true or is it in principle possible that the 
>> intercept column ends up somewhere else?

Not in the current implementation, since

> terms(~ x1 + factor(x2) + 1)
~ x1 + factor(x2) + 1
attr(,"variables")
list(x1, factor(x2))
attr(,"factors")
            x1 factor(x2)
x1          1          0
factor(x2)  0          1
attr(,"term.labels")
[1] "x1"         "factor(x2)"
attr(,"order")
[1] 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 0

it is the "intercept" attribute which controls the generation of the 
intercept (see ?terms.object).

I can't imagine anyone wanting to change this implementation.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list