[R] how to suppress the intercept in an lm()-like formula method?

Ken Knoblauch ken.knoblauch at inserm.fr
Tue Jan 29 15:20:45 CET 2013


Michael Friendly <friendly <at> yorku.ca> writes:

> 
> To partly answer my own question:  It wasn't that hard to hack the 
> result of model.matrix() to remove the intercept,
> 
> remove.intercept <- function(x) {
> 	if (colnames(x)[1] == "(Intercept)") {
> 		x <- x[,-1]
> 		attr(x, "assign") <- attr(x, "assign")[-1]
> 	}
> 	x
> }
> 
> However, the model frame and therefore the model terms stored in the 
> object are wrong, still including the intercept:
> 
>>>  clipped  <<<<<<
> 
> On 1/29/2013 8:44 AM, Michael Friendly wrote:
> > I'm trying to write a formula method for canonical correlation analysis,
> > that could be called similarly to lm() for
> > a multivariate response:
> >
> > cancor(cbind(y1,y2,y3) ~ x1+x2+x3+x4, data=, ...)
> > or perhaps more naturally,
> > cancor(cbind(y1,y2,y3) ~ cbind(x1,x2,x3,x4), data=, ...)
> >
> > I've adapted the code from lm() to my case, but in this situation, it
> > doesn't make sense to
> > include an intercept, since X & Y are mean centered by default in the
> > computation.
> >
> > In the code below, I can't see where the intercept gets included in the
> > model matrix and therefore
> > how to suppress it.  There is a test case at the end, showing that the
> > method fails when called
> > normally, but works if I explicitly use -1 in the formula.  I could hack
> > the result of model.matrix(),
> > but maybe there's an easier way?

Have a look at the polr function in MASS where this same problem
is handled, I think, around lines 10 - 15, so right near the beginning,
starting where the variable xint is defined.

best,

Ken

-- 
Kenneth Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html



More information about the R-help mailing list