R(预测)中的多元多项式回归

R(预测)中的多元多项式回归,r,regression,predict,polynomials,R,Regression,Predict,Polynomials,我正在使用60/40测试分割建立一个预测模型。 我想建立一个包含10个解释变量的多项式回归模型 首先,我建立了一个基于培训的模型,并对training$y进行了回归 model_poly = lm(training$y ~ poly(training$x1, degree=2, raw=TRUE) + poly(training$x2, degree=2, raw=TRUE) + poly(training$x3, degree=2, raw=TRUE) + pol

我正在使用60/40测试分割建立一个预测模型。 我想建立一个包含10个解释变量的多项式回归模型

首先,我建立了一个基于培训的模型,并对
training$y
进行了回归

model_poly = lm(training$y ~ poly(training$x1, degree=2, raw=TRUE) +
     poly(training$x2, degree=2, raw=TRUE) +
     poly(training$x3, degree=2, raw=TRUE) +
     poly(training$x4, degree=2, raw=TRUE) +
     poly(training$x5, degree=2, raw=TRUE) +
     poly(training$x6, degree=2, raw=TRUE) +
     poly(training$x7, degree=2, raw=TRUE) +
     poly(training$x8, degree=2, raw=TRUE) +
     poly(training$x9, degree=2, raw=TRUE) +
     poly(training$x10, degree=2, raw=TRUE))
之后,我想使用此模型预测新数据(
test

poly_predictions = predict(model_poly, poly(test$x1, degree=2, raw=TRUE)+
     poly(test$x2, degree=2, raw=TRUE) +
     poly(test$x3, degree=2, raw=TRUE) +
     poly(test$x4, degree=2, raw=TRUE) +
     poly(test$x5, degree=2, raw=TRUE) +
     poly(test$x6, degree=2, raw=TRUE) +
     poly(test$x7, degree=2, raw=TRUE) +
     poly(test$x8, degree=2, raw=TRUE) +
     poly(test$x9, degree=2, raw=TRUE) +
     poly(test$x10, degree=2, raw=TRUE))
测试数据约有200k行,训练数据约有300k行

问题是,
poly_预测
具有训练数据的维度,而不是测试数据的维度。因此,有点不对劲

我错过了什么?使用简单线性模型进行预测时,例如

model_lm = lm(training$y ~ ., training)
lm_predictions = predict(model_lm, test)

我没问题。

你把问题说得太过分了。由于您的模型公式使用的是
training$x1
,因此它在进行预测时将查找的变量正是这个变量。相反,请使用列共享名称的事实,并将模型创建为

model_poly = lm(y ~ poly(x1, degree=2, raw=T) +
  poly(x2, degree=2, raw=T), data=df.training)
这将根据抽象变量
x1
x2
等生成一个模型

然后,您可以像这样使用predict(您可以省略这里的
poly
调用,因为它被烘焙到模型中):

产生期望的结果。否则,您通常会收到一条警告,指出您的输出数据与用于预测其长度是否不同的
newdata
不匹配

predict(model_poly, df.test)