使用 R 中的交互项进行线性回归预测

发布于 2025-01-17 22:35:51 字数 957 浏览 1 评论 0原文

我正在尝试编码使用交互项并使用模型生成样本外预测的模型。

我的培训样本有3个变量和11行。 我的测试样本有3个变量和1行。

我的代码如下。

inter.model <- lm(Y.train ~ Y.lag.train +  X.1.train + X.1.train:X.2.train)

但是,我不太确定R如何处理交互项。 我使用模型和测试数据的系数编码了预测。

inter.prediction <- inter.model$coef[1] + inter.model$coef[2]*Y.lag.test + 
        inter.model$coef[3]*X.1.test + (inter.model$coef[4]*X.1.test*X.2.test)

我想确保正确编码这些预测。因此,我试图用R的预测功能生产它们。

inter.pred.function <- predict(inter.model, newdata=test_data)

但是,我收到一条错误消息:

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'X.2.train')
In addition: Warning message:
'newdata' had 1 row but variables found have 11 rows 
names(test_data)
[1] "Y.lag.test" "X.1.test" "X.1.test:X.2.test"

因此,我的问题是,您如何用R中的交互项进行编码并进行线性回归预测?

I am trying to code a model which uses interaction term and generate out-of-sample predictions using the model.

My training sample has 3 variables and 11 rows.
My test sample has 3 variables and 1 row.

My code is the following.

inter.model <- lm(Y.train ~ Y.lag.train +  X.1.train + X.1.train:X.2.train)

However, I am not quite sure how R handles the interaction terms.
I have coded the predictions using the coefficients from the model and the test data.

inter.prediction <- inter.model$coef[1] + inter.model$coef[2]*Y.lag.test + 
        inter.model$coef[3]*X.1.test + (inter.model$coef[4]*X.1.test*X.2.test)

I wanted to make sure that these predictions were correctly coded. Thus, I tried to produce them with the R´s predict-function.

inter.pred.function <- predict(inter.model, newdata=test_data)

However, I am getting a error message:

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'X.2.train')
In addition: Warning message:
'newdata' had 1 row but variables found have 11 rows 
names(test_data)
[1] "Y.lag.test" "X.1.test" "X.1.test:X.2.test"

So, my question is, how do you code and make linear regression predictions with interaction terms in R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-01-24 22:35:51

您不需要“ x.1.test:x.2.test”在新数据中,互动是在stats ::: precadion.lm中自动创建的。通过model.matrix

fit <- lm(mpg ~ hp*am, mtcars[1:10, ])

test <- mtcars[-(1:10), c('mpg', 'hp', 'am')]

as.numeric(predict(fit, newdata=test))
# [1] 20.220513 17.430053 17.430053 17.430053 16.206167 15.716612 14.982281 25.658824 27.141176 25.764706
# [11] 21.493355 18.898716 18.898716 14.247949 17.674830 25.658824 23.011765 20.682353  4.694118 14.117647
# [21] -2.823529 21.105882

You won't need "X.1.test:X.2.test" in your new data, the interaction is created automatically in stats:::predict.lm via the model.matrix.

fit <- lm(mpg ~ hp*am, mtcars[1:10, ])

test <- mtcars[-(1:10), c('mpg', 'hp', 'am')]

as.numeric(predict(fit, newdata=test))
# [1] 20.220513 17.430053 17.430053 17.430053 16.206167 15.716612 14.982281 25.658824 27.141176 25.764706
# [11] 21.493355 18.898716 18.898716 14.247949 17.674830 25.658824 23.011765 20.682353  4.694118 14.117647
# [21] -2.823529 21.105882
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文