使用 R 中的交互项进行线性回归预测
我正在尝试编码使用交互项并使用模型生成样本外预测的模型。
我的培训样本有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要
“ x.1.test:x.2.test”
在新数据中,互动是在stats ::: precadion.lm
中自动创建的。通过model.matrix
。You won't need
"X.1.test:X.2.test"
in your new data, the interaction is created automatically instats:::predict.lm
via themodel.matrix
.