【问题标题】:how to predict value for future date in r如何在r中预测未来日期的值
【发布时间】:2020-08-19 00:00:56
【问题描述】:

样本数据 ->

> tail(India_df)
          date confirmed deaths recovered
98  2020-04-28     31324   1008      7747
99  2020-04-29     33062   1079      8437
100 2020-04-30     34863   1154      9068
101 2020-05-01     37257   1223     10007
102 2020-05-02     39699   1323     10819
103 2020-05-03     42505   1391     11775

我想预测未来某个日期会有多少确诊病例,例如 ->“2020-05-05”

我正在使用多项式回归来拟合模型 ->

fit2 = lm(confirmed~poly(date,6))

我试过用这个 ->

new <- data.frame(pred_dates <- as.Date(c("2020-05-05","2020-05-06")))
predict.lm(fit2,new,interval = "confidence")

但它显示此错误 ->

'newdata' had 2 rows but variables found have 103 rows 

【问题讨论】:

  • lm(...) ignoring India_df,还是您向我们展示的代码与您使用的代码不同?
  • 我使用了attach(India_df),所以我直接使用列名,这就是为什么..
  • (在我看来,任何建议使用attach 的教程/课程/课程都应该停止。我想不出一种情况,除了拐杖不好,还有风险/使用它的后果超过了它可能带来的任何便利。)很高兴你得到了你需要的帮助:-)

标签: r date machine-learning prediction


【解决方案1】:

您必须在lm 中使用data 参数,并在新数据框中使用相同名称的预测变量:

fit2 <- lm(confirmed ~ poly(date,6), data = India_df)

new <- data.frame(date = as.Date(c("2020-05-05","2020-05-06")))

predict(fit2, new, interval = "confidence")

【讨论】:

    猜你喜欢
    • 2023-02-16
    • 1970-01-01
    • 2020-06-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多