【发布时间】:2018-10-19 23:22:47
【问题描述】:
我正在使用 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))
测试数据大约有 20 万行,训练数据大约有 30 万行。
问题是,poly_predictions 具有训练数据的维度,而不是测试数据的维度。因此,有些事情是错误的。
我在这里缺少什么?使用简单的线性模型进行预测时,例如
model_lm = lm(training$y ~ ., training)
lm_predictions = predict(model_lm, test)
我没问题。
【问题讨论】:
标签: r regression predict polynomials