在R插入符号中使用线性回归(lm),如何将截距强制设为0? [英] Using linear regression (lm) in R caret, how do I force the intercept through 0?

查看:1379
本文介绍了在R插入符号中使用线性回归(lm),如何将截距强制设为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R插入符号对我的线性回归模型进行交叉验证.在某些情况下,我想强制将截距设置为0.我使用标准lm语法尝试了以下操作:

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  `

此语法似乎可以与标准"lm"功能一起使用,但不能在插入符号包中使用.有什么建议吗?

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 

推荐答案

您可以利用caret::train中的tuneGrid参数.

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))

使用getModelInfo("lm", regex = TRUE)[[1]]$param来查看您可能在tuneGrid中进行了调整的所有内容(在lm情况下,唯一的调整参数是intercept).愚蠢的是,您不能仅仅依靠formula语法,而是.

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.

这篇关于在R插入符号中使用线性回归(lm),如何将截距强制设为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆