Skip to content
Prev 316394 / 398503 Next

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

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:

Browse[1]> str(mt)
Classes 'terms', 'formula' length 3 cbind(SAT, PPVT, Raven) ~ n + s + ns 
+ na + ss
   ..- attr(*, "variables")= language list(cbind(SAT, PPVT, Raven), n, 
s, ns, na, ss)
   ..- attr(*, "factors")= int [1:6, 1:5] 0 1 0 0 0 0 0 0 1 0 ...
   .. ..- attr(*, "dimnames")=List of 2
   .. .. ..$ : chr [1:6] "cbind(SAT, PPVT, Raven)" "n" "s" "ns" ...
   .. .. ..$ : chr [1:5] "n" "s" "ns" "na" ...
   ..- attr(*, "term.labels")= chr [1:5] "n" "s" "ns" "na" ...
   ..- attr(*, "order")= int [1:5] 1 1 1 1 1
   ..- attr(*, "intercept")= int 1
   ..- attr(*, "response")= int 1
   ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
   ..- attr(*, "predvars")= language list(cbind(SAT, PPVT, Raven), n, s, 
ns, na, ss)
   ..- attr(*, "dataClasses")= Named chr [1:6] "nmatrix.3" "numeric" 
"numeric" "numeric" ...
   .. ..- attr(*, "names")= chr [1:6] "cbind(SAT, PPVT, Raven)" "n" "s" 
"ns" ...
Browse[1]>
On 1/29/2013 8:44 AM, Michael Friendly wrote: