我有一个以汇率作为因变量和其他 7 个自变量(例如通货膨胀、利率等)的线性模型。我有 1993Q1-2011Q4 的季度数据。我想创建一个窗口大小为 60(从 1993 年第一季度到 2007 年第四季度)的滚动窗口回归(使用上面的模型),并使用估计的回归来预测其余样本。另外,我想将此模型与 Random Walk 模型进行比较(汇率遵循 RW)。最后,我想执行 dm.test 和 clarkwest 测试(不运行)。我的代码对吗?
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)