【问题标题】:Multivariate Polynomial Regression in R (Prediction)R中的多元多项式回归(预测)
【发布时间】: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


    【解决方案1】:

    您过度指定了问题。由于您的模型公式使用training$x1,这就是它在进行预测时要寻找的确切变量。相反,使用列共享名称的事实,并将模型创建为

    model_poly = lm(y ~ poly(x1, degree=2, raw=T) +
      poly(x2, degree=2, raw=T), data=df.training)
    

    这将根据抽象变量 x1x2 等生成模型。

    然后您可以像这样使用 predict(您可以在此处省略 poly 调用,因为它已融入模型):

    predict(model_poly, df.test)
    

    以产生所需的结果。否则,您通常会收到一条警告,指出您的输出数据与提供的 newdata 不匹配,以预测它们是否具有不同的长度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2015-07-11
      • 2017-04-15
      相关资源
      最近更新 更多