# Simple Question. Prediction intervals in R.

#### Time Serious

##### New Member
Hi, I'm using R and I've fitted a seasonal Arima model to a time series. I wish to compute a prediction for the next 12 values of the time series with 95% confidence interval. Can someone advise me what command to use? Also could someone point me to where I might find out the answers to similar questions to this one rather than posting all the questions here?
I'm thinking of using something like

Clearly this is missing the prediction interval part, but I have no idea how to put that in.

Last edited:

#### trinker

##### ggplot2orBust
This is done with the predict function as in the following example (note: I think the new values have to be in a data.frame to work):

Code:
model <- lm(mpg~hp+disp+hp:disp, data=mtcars)                  #your model
new.values <-data.frame(hp=c(260, 280), disp=c(330, 350))      #values predict from arranged as a data frame
predict(model, new.values, level = 0.9, interval = "confidence")
Where:
1. model is your original model
2. new.values is a data frame of predictors to predict from
3. level is your confidence level
4. interval is the type of (just use confidence I did)

#### Time Serious

##### New Member
I'm sorry but I have almost no training with R (we were just told "for this course we'll be using R" and then basically let loose on R). I'm sure what I have to do is not as difficult as what you suggest. Is there anything I can add to my own code "> predict(arima(whatever,order=c(0,1,2),seasonal=lis t(order=c(0,1,2))),n.ahead=12)" in order to obtain the prediction with confidence interval?

#### trinker

##### ggplot2orBust
I shortened it a bit, gave more than one predictor and added annotations. I really can't help you with your code because yours is not reproducible. If you try my code in R you'll be able to reproduce my results. This really should get you to where you need to go.

#### jpkelley

##### TS Contributor
You mentioned that you want to predict values? If you want to predict the mean (or other central) value, then confidence intervals will do the trick. I just get confused when I hear that someone describe predictions with confidence intervals. Wouldn't prediction intervals be more appropriate, since they add in the model variance not accounted for by predictors? This would be achieved by:

Code:
model <- lm(mpg~hp+disp+hp:disp, data=mtcars)                  #your model
new.values <-data.frame(hp=c(260, 280), disp=c(330, 350))      #values predict from arranged as a data frame
predict(model, new.values, level = 0.9, interval = "prediction")
These, of course, should be much wider than confidence intervals and should provide a better guess as to what future values would be (by adding in noise again)!

#### jpkelley

##### TS Contributor
Riiiiiiight, Dason. Likely story. If there's one thing I've learned from you all is that you have to be a fast first responder! I'm usually as slow at a sea slug.

#### Time Serious

##### New Member
You mentioned that you want to predict values? If you want to predict the mean (or other central) value, then confidence intervals will do the trick. I just get confused when I hear that someone describe predictions with confidence intervals. Wouldn't prediction intervals be more appropriate, since they add in the model variance not accounted for by predictors? This would be achieved by:

Code:
model <- lm(mpg~hp+disp+hp:disp, data=mtcars)                  #your model
new.values <-data.frame(hp=c(260, 280), disp=c(330, 350))      #values predict from arranged as a data frame
predict(model, new.values, level = 0.9, interval = "prediction")
These, of course, should be much wider than confidence intervals and should provide a better guess as to what future values would be (by adding in noise again)!
I really appreciate your help but I think your solution is more complicated than what we've been taught to do. Are you sure there's no way to get the result by adding something like level=0.95, interval="prediction" onto my code, without the stuff like
Code:
model <- lm(mpg~hp+disp+hp:disp, data=mtcars)
new.values <-data.frame(hp=c(260, 280), disp=c(330, 350))
This could be a very stupid question, but I'm a total noob at R.

#### jpkelley

##### TS Contributor
Those lines of code were just an example data set. Often, when we're communicating without access to the original poster's dataset, we use datasets that come loaded with R, so that there is a reproducible example that everyone can access. In place of the linear model (designated by "lm"), you'll presumably have your own model for your time series. You first model the data and then you can predict (extrapolate, etc.).

Does that help?

Don't worry about being a noob at R. We all start at zero. Forums like like help people climb the learning curve. And, every one of us is at different points along that curve. (It shouldn't be called a curve, anyway...has no asymptote).

This is a good thread. It will help lots of future visitors.

#### jpkelley

##### TS Contributor
Grrr, what a technicality...I always thought they followed a logistic curve until I began to learn R. Methinks the "learning curve" hasn't been formally described by statisticians.