Skip to content
Prev 316401 / 398503 Next

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

On 29/01/2013 9:14 AM, Michael Friendly wrote:
I think you need to do some of the low level calculations yourself, 
specifically the "terms" calculation.

For example, this forces no intercept, regardless of what the user 
specifies.  You might prefer just to change the default to no intercept 
and allow the user to use "+1" to add one, which looks harder...

# .... set formula to the user's formula ...
# Now modify it to suppress the intercept:

class(formula) <- c("nointercept", class(formula))
terms.nointercept <- function(x, ...) {
   result <- NextMethod(x, ...)
   attr(result, "intercept") <- 0
   result
}

Now lm(formula) does a fit with no intercept.

Duncan Murdoch