2
$\begingroup$

Can anyone kindly point out if I made any mistakes in making predictions using quadratic regression model in time series? I called the predict() function with the appropriate data vector and model, but the predictions do not sit well with what I observe on the time plot. I will like to know if there's anything wrong with my commands or is there something more to it?

Here are my commands and data vectors:

sales <- c(99,99,96,101,99,105,101,106,107,106,105,112,112,118,121,126,127,128,133)
year <- 1985:1986
year.sq <- year^2
sales.df <- data.frame(Year=year,Year.Squared=year.sq,Sales=sales)
quad.reg.model <- lm(sales.df$Sales ~ sales.df$Year + I(sales.df$Year.Squared), 
                     data=sales.df)

year <- sales.df$Year
year.sq <- sales.df$Year.Squared

# Creating a data.frame to be an argument for predict.lm() function
newdata <- data.frame(year=2004,year.sq=2004^2) 

# Prediction for Sales (1-year ahead)
sales.pred <- predict.lm(quad.reg.model,newdata,interval='predict')

and the output from the last line of command gave:

         fit       lwr      upr
1   98.53383  93.00996 104.0577

However, when I plotted the sales series and overlaid with the quadratic graph, it clearly shows that the trend is increasing and it seems like the 1-year ahead prediction does not sit well empirically. Why is this so?

Plot of Sales & Best Fit Curve (Quadratic)

$\endgroup$
4
  • 2
    $\begingroup$ Should it be $1985:2003$ instead of $1985:1986$? And I can't really read the formula in lm. $\endgroup$
    – Richi W
    Jan 30, 2013 at 20:59
  • $\begingroup$ And I can't really read the formula in lm. Shouldn't it just be lm(sales~year+I(year^2)) equivalent to lm(sales~year+year.sq) ? $\endgroup$
    – Richi W
    Jan 30, 2013 at 21:06
  • $\begingroup$ agree w/richard. The code should not even run without errors as it is displayed. $\endgroup$
    – pat
    Jan 30, 2013 at 21:12
  • 1
    $\begingroup$ This should fix it but Dylan Koh should review the changes $\endgroup$
    – Bob Jansen
    Jan 30, 2013 at 21:33

1 Answer 1

1
$\begingroup$

I can' read your lm lines but I think this works:


mymodel = lm(sales~year+I(year^2))
plot(sales)
lines(fitted.values(mymodel))
Or you try just

mymodel = lm(sales~I(year^2)) 
Finally

new.data = list(year=2004)
predict.lm(mymodel,new.data)
gives a useful value.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.