Fit a no-intercept model in caret
Asked Answered
B

2

15

In R, I specify a model with no intercept as follows:

data(iris)
lmFit <- lm(Sepal.Length ~ 0 + Petal.Length + Petal.Width, data=iris)
> round(coef(lmFit),2)
Petal.Length  Petal.Width 
        2.86        -4.48 

However, if I fit the same model with caret, the resulting model includes an intercept:

library(caret)
caret_lmFit <- train(Sepal.Length~0+Petal.Length+Petal.Width, data=iris, "lm")
> round(coef(caret_lmFit$finalModel),2)
 (Intercept) Petal.Length  Petal.Width 
        4.19         0.54        -0.32 

How do I tell caret::train to exclude the intercept term?

Boudreau answered 12/9, 2012 at 19:20 Comment(1)
That's not possible without changes in the source code. See createModel.R line 25: modFormula <- as.formula(".outcome ~ ."); the intercept is always includedBanda
B
5

As discussed in a linked SO question https://mcmap.net/q/825538/-using-linear-regression-lm-in-r-caret-how-do-i-force-the-intercept-through-0-duplicate, this works in caret v6.0.76 (And the trace answer above no longer seems to work with code refactoring in caret):

caret_lmFit <- train(Sepal.Length~0+Petal.Length+Petal.Width, data=iris, "lm", 
           tuneGrid  = expand.grid(intercept = FALSE))

> caret_lmFit$finalModel

Call:
lm(formula = .outcome ~ 0 + ., data = dat)

Coefficients:
Petal.Length   Petal.Width  
       2.856        -4.479  
Balladmonger answered 29/5, 2017 at 14:27 Comment(0)
S
7

@rcs already told you which line in which function you need to change.

Just use trace to modify that function:

trace(caret::createModel, 
       quote(modFormula <- as.formula(".outcome ~ .-1")), at=5, print=FALSE)
caret_lmFit <- train(Sepal.Length~0+Petal.Length+Petal.Width, data=iris, "lm")
round(coef(caret_lmFit$finalModel),2)
#Petal.Length  Petal.Width 
#        2.86        -4.48 
untrace(caret::createModel)

However, I don't use caret. There might be unforeseen consequences. It's also often not a good idea to exclude the intercept from the model.

Shophar answered 29/11, 2013 at 23:29 Comment(1)
I did not know you could use trace to modify functions. That's great! Thank you.Boudreau
B
5

As discussed in a linked SO question https://mcmap.net/q/825538/-using-linear-regression-lm-in-r-caret-how-do-i-force-the-intercept-through-0-duplicate, this works in caret v6.0.76 (And the trace answer above no longer seems to work with code refactoring in caret):

caret_lmFit <- train(Sepal.Length~0+Petal.Length+Petal.Width, data=iris, "lm", 
           tuneGrid  = expand.grid(intercept = FALSE))

> caret_lmFit$finalModel

Call:
lm(formula = .outcome ~ 0 + ., data = dat)

Coefficients:
Petal.Length   Petal.Width  
       2.856        -4.479  
Balladmonger answered 29/5, 2017 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.