vendredi 3 novembre 2017

R: out of sample validation of Autoregressive Distributed Lag / LM

I've build a simple ADL/ auto regressive model for a time series PR(t) = PR(t-1)+MP(t)+c.

How can I validate the model for multiple steps on out-of-sample data using observed MP data but (rolling) predicted PR data?

I.e i want to pass a vector of real exogenous values for MP and to calculate PR(t+2) on the basis of predicted PR(t+1) rather then observed PR(t+1).

a) Is there a build in routine in R for this? b) Is my understanding correct, that lm's predict(newdata) will take observed values of PR(t+1) to predict the PR(t+2) values?

Many thanks for any inputs! As I did not find a documentation, I've included few lines of code I've done to test the b) question. Best Serge

# train and validate VARs / ADLs
data.2007=subset(data, Period_year>2006)
data.train <- subset(data.2007, Period_year<2014)
data.validate <- subset(data.2007,Period_year>2013)

model <- lm(PR_ln_RF ~ lagpad(PR_ln_RF,1) + MP, data=data.train)

PR.validate <- predict(model, newdata=data.validate)
PR.validate

# replace all PR_ln_RF to 0 starting from the second
# if predict takes predicted PR(t+1) to predict PR(t+2), PR.validate2 expected to be equal to PR.validate
data.validate$PR_ln_RF[-1] = 0

PR.validate2 <- predict(model, newdata=data.validate)
PR.validate2
# PR.validate2 differs from PR.validate


#function to lag data frames 
lagpad <- function(x, k) {
  if (!is.vector(x)) 
   stop('x must be a vector')
  if (!is.numeric(x)) 
   stop('x must be numeric')
  if (!is.numeric(k))
   stop('k must be numeric')
  if (1 != length(k))
   stop('k must be a single number')
  c(rep(NA, k), x)[1 : length(x)] 
 }

Aucun commentaire:

Enregistrer un commentaire