0

I have the following R code:

library(lubridate)
library(vars)
library(tidyverse)
library(Metrics)
library(quantmod)

df = read.csv('oil-data.csv')
## Horizon is the number of months in advance we're trying to predict
horizon = 3
forecast_date <- ymd(df$date)
month(forecast_date) <- month(forecast_date) + horizon
forecast <- data.frame(
    forecastDate = forecast_date,
    actual = df$actual
)
varStart <- 72
VarData = df[, c("delta_production", "rea", "wti", "delta_inventories")]
## deflating the oil cost by the log of US CPI
VarData$wti = log(VarData$wti)
var_forecast <- rep(NA, nrow(df) - varStart + 1)
for (i in varStart:nrow(df)) {
    ## p value taken from Kilian and Murphy (2013)
    varModel <- VAR(VarData[1:i, ], p = 12, type="const")
    temp_var_forecast <- predict(varModel, n.ahead = horizon)
    oil_h <- exp(temp_var_forecast["fcst"]$fcst$wti[1])
    var_forecast[i - varStart + 1] <- oil_h
}
forecast = forecast[varStart:nrow(df),]
forecast$predicted = var_forecast
forecast$error <- mse(forecast$actual, forecast$predicted)
print(min(forecast$error))
print(mean(forecast$error))
print(max(forecast$error))

When I run this, the resulting forecast$error value is the same for every value (~40.23398). This seems weird to me, because at each iteration, I am fitting a new VAR model, on a (slightly) different data set.

Why is this? Am I doing something wrong here?

I am trying to replicate model 2.1 from this paper. I can provide more code/data if needed.

The oil-data.csv file can be downloaded here to reproduce the example.

Currently, the min, mean, and max are all equal to 40.22983.

Cettt
  • 11,460
  • 7
  • 35
  • 58
finbarr
  • 749
  • 4
  • 11
  • I cross posted this to [stats.stackexchange.com](http://stats.stackexchange.com/questions/259900/constant-error-in-var-model-despite-different-datasets) as I'm not sure if the error is statistical or code-based. – finbarr Feb 04 '17 at 00:31
  • When asking for help, please post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run and verify your code. It's makes it much easier to help you. – MrFlick Feb 04 '17 at 04:10
  • Sounds good. I'll add a reproducible example. – finbarr Feb 04 '17 at 17:32
  • Like I mentioned in the CV version of this post, the code you have shown is incomplete because it does not allow us to figure out where `forecast$actual` and `forecast$predicted` come from or what might be wrong with them. In particular, they are *not* modified inside the loop, which may be your problem. – Chris Haug Feb 04 '17 at 19:32
  • Sorry, I made a typo when I was editing this for SO. I set `forecast$predicted` at the end of the loop, and `forecast$actual` before the loop. – finbarr Feb 04 '17 at 19:45
  • I updated the question with a reproducible example. – finbarr Feb 07 '17 at 17:20

0 Answers0