I have a time series data:
output of x data frame:
Date vol
1990 12
1991 13
1992 15
1994 18
1995 20
1996 35
I am trying to plot this data and predict 4 year ahead as below:
plot(x$Date, x$vol, col="blue")
x.lm<-lm(x$Vol ~ x$Date)
x.pre<-predict(x.lm, n.ahead=4)
abline(x.pre, col="red")
I get this error:
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid a=, b= specification
can anybody tell me what I am doing wrong?
You capitalized “Vol” in
x.lm<-lm(x$Vol ~ x$Date).Try
Also, you’re not going to be able to pass the value of
predict()intoablinelike that without some extra modification. Since you’re not doing any sophisticated prediction, but really just wanting to plot the linear fit, you could plot the line usingIf you separate your variables and then use the newdata parameter in
predict, you should be able to get the actual predictions. That would probably be the cleanest way to do it. For instance:This will return the predictions for 1997 – 2000. I believe the “x” in
data.frame(x=1997:2000)must match the variable you put intolm()as your x variable. In your case, that has a dollar sign accessor, which makes the whole thing a bit more complex. I’d just take the approach above and rename the x component fed into yourlm()function as a valid variable name which can be referenced later.