Hi all, Forecasting from an arima model is easy with predict. But I can't manage to backcast : invent data from the model before the begining of the sample. The theory is easy : take your parameters, reverse your data, forecast, and then reverse the forecast I've tried to adapt the predict function to do that (i'm not sure that the statistical procedure is fine (with the residuals), but that's not my point right now) : mav.backcast.arima<-function(model,n.backcast,...) { if (class(model)[1]!="Arima") stop("argument <model> must be an object of class 'Arima' (see ?arima)") model2<-model model$residuals<-rev(model$residuals) if (is.ts(model2$residuals)) model$residuals<-ts(model$residuals,start=start(model2$residuals), frequency=frequency(model2$residuals)) pred.before<-predict(model,n.ahead=n.backcast,...) freq<-frequency(model$residuals) startingdate<-per.sub(start(model2$residuals),n.backcast,freq=freq) pred<-ts(rev(pred.before$pred),start=startingdate,freq=freq) se<-ts(rev(pred.before$se),start=startingdate,freq=freq) return((list(pred = pred, se =se))) } This function does not work : it gives always the same result, it does not depend on the residuals (i've tried to insert a model$residuals<-rep(1,100) after the definition, to check that). Then i look at the code, with getS3method("predict","Arima"). And i get even more confused (!) : where does data play a role in the function ? residuals are loaded into rsd, but this variable is not used after... I looked at KalmanForecast and at the C code of KalmanFore, but it did not help me understand what was going on. thanks Franck A. btw, it has nothing to do with it, but i've done some stuff on time series (filtering with Hodrick prescott or Baxter King, for instance) that you can find on http://arnaud.ensae.net [[alternative HTML version deleted]]
Part of the fit is the Kalman filter state after running the model forwards. Try reversing your series, fitting and then forecasting. You might have more success in understanding arima0. On Sat, 25 Nov 2006, Franck Arnaud wrote:> Hi all, > > Forecasting from an arima model is easy with predict. > > But I can't manage to backcast : invent data from the model before the > begining of the sample. > The theory is easy : take your parameters, reverse your data, forecast, and > then reverse the forecast > I've tried to adapt the predict function to do that (i'm not sure that the > statistical procedure is fine (with the residuals), but that's not my point > right now) : > > mav.backcast.arima<-function(model,n.backcast,...) > { > if (class(model)[1]!="Arima") stop("argument <model> must be an object > of class 'Arima' (see ?arima)") > > model2<-model > model$residuals<-rev(model$residuals) > if (is.ts(model2$residuals)) > model$residuals<-ts(model$residuals,start=start(model2$residuals), > frequency=frequency(model2$residuals)) > pred.before<-predict(model,n.ahead=n.backcast,...) > > freq<-frequency(model$residuals) > startingdate<-per.sub(start(model2$residuals),n.backcast,freq=freq) > > pred<-ts(rev(pred.before$pred),start=startingdate,freq=freq) > se<-ts(rev(pred.before$se),start=startingdate,freq=freq) > > return((list(pred = pred, se =se))) > } > > This function does not work : it gives always the same result, it does not > depend on the residuals (i've tried to insert > a model$residuals<-rep(1,100) after the definition, to check that). > > Then i look at the code, with getS3method("predict","Arima"). And i get even > more confused (!) : > where does data play a role in the function ? residuals are loaded into rsd, > but this variable is not used after... > I looked at KalmanForecast and at the C code of KalmanFore, but it did not > help me understand what was going on. > > thanks > > Franck A. > > btw, it has nothing to do with it, but i've done some stuff on time series > (filtering with Hodrick prescott or Baxter King, for instance) that you can > find on http://arnaud.ensae.net > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Some code I have been playing with to do this follows ... get.best.arima <- function(x.ts, minord=c(0,0,0,0,0,0), maxord=c(2,1,1,2,1,1)) { # function based on 'Introductory Time Series with R' best.aic <- 1e8 # a big number n <- length(x.ts) for(p in minord[1]:maxord[1]) for(d in minord[2]:maxord[2]) for(q in minord[3]:maxord[3]) { for(P in minord[4]:maxord[4]) for(D in minord[5]:maxord[5]) for(Q in minord[6]:maxord[6]) { fit <- arima(x.ts, order=c(p,q,d), seas=list(order=c(P,D,Q), frequency(x.ts)), method='CSS') fit.aic <- -2 * fit$loglik + (log(n) + 1) * length(fit$coef) if(fit.aic < best.aic) # probably should do other tests here before accepting { best.aic <- fit.aic best.fit <- fit best.model <- c(p,d,q,P,D,Q) } } } #print(best.aic) #print(best.model) return(best.fit) } extend.series <- function(series.ts, extend.by=1) { log.series.ts <- log(series.ts) best.log <- get.best.arima(log.series.ts) prediction.log <- predict( best.log, n.ahead=(frequency(series.ts)*extend.by) ) reverse.log.series.ts <- ts( data=rev(as.vector(log.series.ts)), frequency=frequency(series.ts) ) best.log.reverse <- get.best.arima(reverse.log.series.ts) prediction.log.reverse <- predict( best.log.reverse, n.ahead=(frequency(series.ts)*extend.by) ) start.series.ts <- start(series.ts) end.series.ts <- end(series.ts) extended.vector <- append(rev(exp(as.vector(prediction.log.reverse$pred))), as.vector(series.ts) ) extended.vector <- append( extended.vector, exp(as.vector(prediction.log$pred)) ) extended.ts <- ts(data=extended.vector, frequency=frequency(series.ts), start=c(start.series.ts[1]-extend.by, 1)) } -- View this message in context: http://r.789695.n4.nabble.com/R-predict-and-arima-tp813419p3246820.html Sent from the R help mailing list archive at Nabble.com.