1

I'm trying to use R caret to perform cross-validation of my linear regression models. In some cases I want to force the intercept through 0. I have tried the following, using the standard lm syntax:

regressControl  <- trainControl(method="repeatedcv",
                        number = 4,
                        repeats = 5
                        )                      

regress         <- train(y ~ 0 + x,
               data = myData,
               method  = "lm",
               trControl = regressControl)

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

Coefficients:
(Intercept)     x 
-0.0009585    0.0033794  `

This syntax seems to work with the standard 'lm' function but not within the caret package. Any suggestions?

test <- lm(y ~ 0 + x,
       data = myData)


Call:
lm(formula = y ~ 0 + x, data = myData)

Coefficients:
x 
0.003079 
jmuhlenkamp
  • 2,102
  • 1
  • 14
  • 37
Sarah M
  • 23
  • 1
  • 1
  • 5

1 Answers1

2

You can take advantage of the tuneGrid parameter in caret::train.

regressControl  <- trainControl(method="repeatedcv",
                    number = 4,
                    repeats = 5
                    ) 

regress <- train(mpg ~ hp,
           data = mtcars,
           method  = "lm",
           trControl = regressControl, 
           tuneGrid  = expand.grid(intercept = FALSE))

Use getModelInfo("lm", regex = TRUE)[[1]]$param to see all the things you could have tweaked in tuneGrid (in the lm case, the only tuning parameter is the intercept). It's silly that you can't simply rely on formula syntax, but alas.

Chrisss
  • 3,211
  • 1
  • 16
  • 13
  • This seems to work. Thank you. (I had also tried using 'trace' to modify the formula but without any success - http://stackoverflow.com/questions/12394855/fit-a-no-intercept-model-in-caret) – Sarah M Jan 19 '17 at 15:43
  • @SarahM I believe the `caret` source code has changed since the trace answer was made, therefore it wouldn't be appropriate anymore. – Chrisss Jan 19 '17 at 16:15
  • @SarahM You should accept this as the answer. it's good in at least caret v6.0.76 –  May 29 '17 at 14:22