Viechtbauer Wolfgang (STAT)
2010-Jan-05 13:30 UTC
[R] Is the Intercept Term always in First Position?
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? Best, -- Wolfgang Viechtbauer http://www.wvbauer.com/ Department of Methodology and Statistics Tel: +31 (0)43 388-2277 School for Public Health and Primary Care Office Location: Maastricht University, P.O. Box 616 Room B2.01 (second floor) 6200 MD Maastricht, The Netherlands Debyeplein 1 (Randwyck)
Prof Brian Ripley
2010-Jan-05 15:13 UTC
[R] Is the Intercept Term always in First Position?
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