在插入符号中拟合无截距模型 [英] Fit a no-intercept model in caret

查看:40
本文介绍了在插入符号中拟合无截距模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中,我指定了一个没有截距的模型,如下所示:

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 

如何告诉 caret::train 排除拦截项?

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

推荐答案

如链接的 SO 问题 https://stackoverflow 中所述.com/a/41731117/7613376,这适用于 caret v6.0.76(并且上面的跟踪答案似乎不再适用于 caret 中的代码重构):

As discussed in a linked SO question https://stackoverflow.com/a/41731117/7613376, 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  

这篇关于在插入符号中拟合无截距模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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