警告计算预测值
当一个数据帧警告计算预测值
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)
我得到这个工作ERRO:
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
不合格的变量:
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)
或'x.lm 2012-08-03 21:53:57
@BenBolker,你说的对,'with(x,...)'可能是过度杀伤的。相应地更改了我的答案。谢谢。 – MvG 2012-08-04 05:54:30
顺便提一句,有没有什么方法可以看R参数传递如何工作,不会伤害到你的大脑? – 2014-08-06 21:33:13
你data.frame a
有一个名为a
列。您使用名称为Val
和Date
的列创建了您的模型,以便查找它。
当你让你data.frame a
名称列Date
,你是好去:
a <- data.frame(Date=a)
然后它在运行时的警告。
根据注释:
编辑您的通话lm
是:
lm(Val ~ Date, data=x)
如果你不能让predict.lm()
的工作,那么你应该尝试使用function()
写自己的功能:
yourown_function<- function(predictor1, predictor2,...){intercept+b1*predictor1+b2*predictor2+...}
使用yourown_function
从任何新的数据帧预测:使用新的值
newvalues<- yourown_function(predictor1=data.frame$predictor1, predictor2=data.frame$predictor2,....)
,你可以COMPUT e残差,MSE等...
这种字体和格式真的很难读,Dude。 – skyline75489 2015-04-22 09:47:54
而不是x.lm < -lm(x $ Val〜x $ Date,data = x)使用x.lm(Val_ Date,data = x) 。在lm函数中删除变量名前的数据集名称应该有所帮助。
你的第一行中的'z'是否应该是'x'?否则我们不知道'z'是什么。你没有收到错误,你会收到警告。另外,你向我们展示的任何东西都有29行,所以我们不能重复这一点。使用'dput'向我们显示您的实际数据。 – Justin 2012-08-03 20:47:27
对不起,是的,它是x。我只是更新它 – user1471980 2012-08-03 20:50:32