Using linear regression (lm) in R caret, how do I force the intercept through 0? [duplicate]

空扰寡人 提交于 2019-12-24 08:57:20

问题


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 

回答1:


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.



来源:https://stackoverflow.com/questions/41730532/using-linear-regression-lm-in-r-caret-how-do-i-force-the-intercept-through-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!