计算预测值时发出警告 [英] warning when calculating predicted values

查看:85
本文介绍了计算预测值时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数据框

x
    Date      Val
    1/1/2012   7
    2/1/2012   9
    3/1/2012   20
    4/1/2012   24
    5/1/2012   50
a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5)
a <- data.frame(a)
x.lm <- lm(x$Val ~ x$Date)

x.pre<-predict(x.lm, newdata=a)

我得到这个错误:

Warning message:
'newdata' had 5 rows but variable(s) found have 29 rows 

我在做什么错了?

这是dput输出:

dput(x)
structure(list(Date = structure(c(14610, 14641, 14669, 14700, 
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14975, 
15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 
15279, 15309, 15340, 15371, 15400, 15431, 15461), class = "Date"), 
    Val = c(45, 51, 56, 56, 59, 60, 60, 60, 64, 65, 75, 73, 74, 
    80, 87, 91, 92, 96, 109, 108, 123, 129, 133, 143, 127, 127, 
    123, 121, 130)), .Names = c("Date", "Val"), row.names = c(NA, 
29L), class = "data.frame")

推荐答案

存储在x.lm模型中的变量名称是指x数据框. a中没有同名的变量,因此它将再次使用x中的29个变量,这可能不是您想要的,因此发出警告.您可以执行以下操作以始终在模型中使用名为Date的非限定变量:

Your variable names, as stored in the x.lm model, refer to the x dataframe. There are no variables of the same names in a, so it will use those 29 from x again, which is probably not what you wanted, thus the warning. You can do the following to always use an unqualified variable named Date in the model:

a <- seq(as.Date(tail(x, 1)$Date), by="month", length=5)
a <- data.frame(Date = a)
x.lm <- lm(Val ~ Date, data=x)
x.pre<-predict(x.lm, newdata=a)

这篇关于计算预测值时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆