0
Sample data:- 
df:- 

      d     a     b     c
1  -4.41  Area1   NA    5.7
2  -5.22  Area1   25   5.9
3  -5.80  Area1   35    5.9
4  -5.20  Area2   58    9
5  -3.59  Area2   59    9
6  -3.53  Area2   129   9

require(forecast)

m <- lm(d ~ a + b + c, data=df)
f <- forecast.lm(m,df)
plot.forecast(f)

I am doing an in sample test for my model. I get the forecasted values when I display the object 'f'but when I try to plot it, it gives me an error as noted in the subject line along with the following line.

Error in plotlmforecast(x, plot.conf = plot.conf, shaded = shaded, shadecols = shadecols,  : 
 Forecast plot for regression models only available for a single predictor    

I thought it might be because of NA values in the data, but it gives the same error even after removing those values. Can anyone help on this?

Meesha
  • 801
  • 1
  • 9
  • 27
  • @nrussell Sorry I added the part in the subject line. Thanks. – Meesha Mar 18 '16 at 18:01
  • It helps if you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including sample input data. If you think it's because of NA value, be sure to include some in your sample. – MrFlick Mar 18 '16 at 18:02
  • It won't plot past 2 dimensions. It will work with `m <- lm(d ~ a, data=df)` – Pierre L Mar 18 '16 at 18:08
  • @PierreLafortune I want to plot the original values of d, along with the forecasted values of d, which I suppose will help me to see the fit of the model. – Meesha Mar 18 '16 at 18:13
  • Then you are looking to plot `df$d` and `f$mean`. – Pierre L Mar 18 '16 at 18:32

1 Answers1

1

You can plot the two values against each other with ggplot2 after melting the data frame with both vectors:

library(ggplot2)
library(reshape2)
set.seed(318)
df <- as.data.frame(replicate(4, rnorm(100)))
names(df) <- letters[1:4]
m <- lm(d ~ a + b + c, data=df)
f <- forecast.lm(m,df)

newdf <- melt(data.frame(d=df$d, f=f$mean))
newdf$ind <- rep(seq(length(df$d)), 2)

ggplot(newdf, aes(x=ind, y=value, color=variable)) + geom_line()

enter image description here

Pierre L
  • 28,203
  • 6
  • 47
  • 69