[R] ARIMA prediction

2007-07-02 Thread Chris Bowring
Hi

This is my first post to this group, so apologies in advance if I get it wrong.

I would like to know how the prediction for arima models works in R. I
have a time series to which I fit an arima model, of varying AR and MA
orders. I then use the predict function to project it forward. I have
also written my own function to perform the prediction, but it gives
different answers to Arima.predict when the MA order is non-zero.


I use the residuals from the arima function in my custom prediction
function. I think this may be my problem. In the arima model:


x{t} = a(1)x{t-1} + a(2)x{t-2} + ... + a(p)x{t-p} + e{t} + b(1)e{t-1}
+ b(2)e{t-2} + ... + b(q)e{t-q}


I am treating the residuals (i.e. arima()$res)  as the e{t} terms.
This gives different answers both in the region of the simulation and
in the region of the prediction, so I'm guessing they're not what I
think they are. Indeed, after q intervals in the prediction, the
prediction proceeds as I would expect, presumably because all the
residuals that have an effect are zero by this stage.


Any help greatly appreciated - my code is below.


Thanks


Chris


--

The code to produce the two predictions is as follows:


AR - 5
MA - 3


sim - arima.sim(list(order=c(AR,0,MA), ar=c(.1, .1, .1, .1, .1),
ma=c(.1, .1, .1)), n=100) + 50


fit - arima(sim, order = c(AR, 0, MA))


coefs - fit$coef
series - sim
innov - fit$res
pred - 100


fit.predict - predict(fit, n.ahead = pred)


fit.r - c(sim, fit.predict$pred)


fit.custom - ProjectCentralArima(AR = AR, MA = MA, d = 0, coefs =
coefs, series = sim, innov = innov, pred = pred)$ser


ProjectCentralArima function:


ProjectCentralArima - function(AR, MA, d, coefs, series, innov, pred)
{


  if(d==0){
series.diff - series
  }
  else {
series.diff - diff(series, lag=1, differences=d)
  }


  intercept - coefs[length(coefs)]


  for(i in 1:pred){
temp - intercept
l.s - length(series.diff)
if(AR  0){
  for(j in 1:AR){
temp - temp + coefs[j] * (series.diff[l.s - j + 1] -
intercept)
  }
}
if(MA  0){
  for(j in (1:MA)){
temp - temp + coefs[j + AR] * innov[l.s - j + 1]
  }
}


innov - c(innov, 0)
series.diff - c(series.diff, temp)
  }


  if(d==0){
 series.undiff - series.diff
  }
  else {
series.undiff - diffinv(series.diff, lag=1, differences=d, xi =
series[1:d])
  }


  return(list(series = series.undiff, innov = innov))
}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] arima prediction

2005-11-21 Thread Xiaodong Jin
x-c(-1.873,-0.121)  # 23 numerics;
  x.arma12 - armaFit(x ~ arma(1,2))
  #estimates y[t]= -0.11465 - 0.23767 y[t-1] - 0.14230 e[t-1] -0.85770 e[t-2] + 
e[t];
   
  # ? how to predict 46 steps ahead based on 23 data points? 
  # the following doesn't work since n is in armaSim rather than armaFit;
  predict(x.arima12, n.ahead=46) 
   
  # Thanks


-

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] arima prediction

2005-11-21 Thread Wensui Liu
Jin, I think you are using ARIMA in a wrong situation because ARIMA is a
short-momery model. Plus, 23 data points is also problematic.

On 11/21/05, Xiaodong Jin [EMAIL PROTECTED] wrote:

 x-c(-1.873,-0.121) # 23 numerics;
 x.arma12 - armaFit(x ~ arma(1,2))
 #estimates y[t]= -0.11465 - 0.23767 y[t-1] - 0.14230 e[t-1] -0.85770e[t-2] + 
 e[t];

 # ? how to predict 46 steps ahead based on 23 data points?
 # the following doesn't work since n is in armaSim rather than armaFit;
 predict(x.arima12, n.ahead=46)

 # Thanks


 -

 [[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html




--
WenSui Liu
(http://statcompute.blogspot.com)
Senior Decision Support Analyst
Cincinnati Children Hospital Medical Center

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html