Skip to Content

drawing predicted values in r

I have monthly data that I would like to draw the predicted values:

z <- 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"), 
    Value = c(4.5e+07, 5.1e+07, 5.6e+07, 5.6e+07, 5.9e+07, 6e+07, 
    6e+07, 6e+07, 6.4e+07, 6.5e+07, 7.5e+07, 7.3e+07, 7.4e+07, 
    8e+07, 8.7e+07, 9.1e+07, 9.2e+07, 9.6e+07, 1.09e+08, 1.08e+08, 
    1.23e+08, 1.29e+08, 1.33e+08, 1.43e+08, 1.27e+08, 1.27e+08, 
    1.23e+08, 1.21e+08, 1.3e+08)), .Names = c("Date", "Value"
), row.names = c(NA, -29L), class = "data.frame")
 
a <- seq(as.Date(tail(z, 1)$Date), by="month", length=5) 
a <- data.frame(Date = a) 
z.lm <- lm(Value ~ Date, data=x) 
z.pre<-predict(z.lm, newdata=a)

I can plot the z data as this:
This works:

 plot(z$Date, z$Value, type="l", col="blue", 
      xlim=(c(as.Date(head(x,1)$Date)), c(as.Date(tail(a,1)$Date)), xaxt="n"))
 
 axis(1, z$Date, format(z$Date, "%b %Y"), cex.axis = .7) 

when I try to plot the predicted values as this:

lines(z.pre, col="red")

I don't get errors but I dont see the red lines graph added to original plot.

Any ideas?