0

I have a linear model with the exchange rate as a dependent variable and 7 others independent variables(e.g. inflation, interest rate etc.). I have quarterly data from 1993Q1-2011Q4. I would like to create a rolling window regression (with the model above) with window size 60(from 1993Q1-2007Q4) and use the estimated regression to forecast the rest sample. Also, I would like to compare this model with the Random Walk model(exchange rate follows a R.W.). In the end, I would like to perform the dm.test and clarkwest test(does not run). Is my code right?

X = embed(data)
X = as.data.frame(X)

install.packages("foreach")
library(foreach)
w_size=60
n_windows = nrow(X) - 60 #until 2007Q4
forecasts = foreach(i=1:n_windows, .combine = rbind) %do%{
    # = Select data for the window (in and out-of-sample) = #
  X_in = X[i:(w_size + i - 1), ] # = change to X[1:(w_size + i - 1), ] for expanding window
  X_out = X[w_size + i, ]

  # = Regression Model = #
  m1 = lm(V1 ~ V2+V3+V4+V5+V6+V7+V8, data = X_in)
  f1 = predict(m1, X_out)

  # = Random Walk = #
  f2 = tail(X_in$V1, 1)

  return(c(f1, f2))
}
 e1 = tail(X[ ,"V1"], nrow(forecasts)) - forecasts[ ,1]
 e2 = tail(X[ ,"V1"], nrow(forecasts)) - forecasts[ ,2]
library(tseries)     
library(forecast)
dm.test(e1,e2, "l") #p-value is more than 5% for all the cases( two.sided, greater, less)
clarkwest(e1,e2)
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Teh Hyped
  • 1
  • 1
  • 1
    I'm having trouble understanding what you're asking - can you clarify what part of your code isn't working? – Evan M Dec 03 '18 at 15:15
  • Hello Evan, My code works fine until the clarkwest(e1,e2) command(which probably R does not support). However, it is more essential for me to know if my code before this command is right. I am not keen on R, so what I am asking is if this is how we create a rolling regression based on which we forecast. My code follows this article : https://www.r-bloggers.com/formal-ways-to-compare-forecasting-models-rolling-windows/ – Teh Hyped Feb 06 '19 at 16:09

1 Answers1

0

It seems like the clarkwest() function is not supported anymore. I recently wrote my own function: CW Note that I used normal standard errors and not Newey-West corrected.

To investigate your loop you could try:

i=1
X_in = X[i:(w_size + i - 1), ] # = change to X[1:(w_size + i - 1), ] for expanding window
X_out = X[w_size + i, ]

# = Regression Model = #
m1 = lm(V1 ~ V2+V3+V4+V5+V6+V7+V8, data = X_in)
f1 = predict(m1, X_out)

# = Random Walk = #
f2 = tail(X_in$V1, 1)

Here you can see the composition the loop creates when i=1

Martin
  • 312
  • 2
  • 15
  • Hello, Do you know if my code for out-of-sample prediction is right(before clarkwest() command)? Could you suggest me anything else? I follow this article: https://www.r-bloggers.com/formal-ways-to-compare-forecasting-models-rolling-windows/ – Teh Hyped Feb 06 '19 at 16:11
  • The code looks okay to me. But it is always a good idea to test it. You could check the outcomes in detail for some values i. (e.g. i=1, 10, 30) – Martin Feb 07 '19 at 08:26
  • Thank you for the response. Whole sample has 75 observations after some alterations. Following this code, when I type to show "X_in" it shows that it takes values from observation 15 to 74. Shouldn't it include the first 1 to 60 observations, since I want e.g. to estimate the model with the first 60 observations to forecast the obsevation 61? Also when I type to show "X_out" it shows the values of obsevation 75(last observation). – Teh Hyped Feb 07 '19 at 16:36
  • It seems okay to me. It seems that you just exucuted the hole loop in one piece. You are looping from i to n.windows. n. windows should be 75-60=15. So you first you set i=1, then i=2 etc.. in the last step you will have i=15. See my edit in my comment. – Martin Feb 08 '19 at 07:43
  • I see. Instead of forming the random walk model with this code above: f2 = tail(X_in$V1, 1) I created the random walk model with this code: m2 = lm(V1 ~ 1, data= X_in) #random walk with drift f2 = predict(m2, X_out) Which one do you think is more suitable? Consider that I want to compare my model with the random walk model with drift. – Teh Hyped Mar 17 '19 at 21:19
  • If you want to include a drift you could compare your result with the `forecast` package. The command `rwf(X_in,h=1,drift=TRUE)$mean` to crosscheck it. – Martin Mar 18 '19 at 07:53