如何使用此数据集在 R 中执行滚动回归?

问题描述:

假设我有以下由 219 行组成的数据框.由于某些结构性原因,该数据集并非完全按月计算.

Let's suppose I have the following dataframe made of up 219 rows. The dataset is not perfectly monthly for some structural reasons.

df = structure(list(X1 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X2 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X3 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X4 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X5 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X6 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X7 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86), X8 = c(0.67, -1.45, 0.01, -1.07, -0.8, 0.21, -0.27, 
0.44, 1.09, 0.99, 0.62, -0.43, -0.29, -0.57, -1.1, 0.18, 0.26, 
0.38, -2.38, 0.79, 0.11, 0.03, 1.02, 0.02, 0.33, 1.03, -0.41, 
-1.46, -0.06, 1.95, -1.04, -0.95, 1.61, 0.46, -0.6, -1.42, -0.8, 
0.92, 0.84, -1, 1.55, -0.86, 0.58, -0.35, 1.13, 0.39, -0.71, 
-0.67, 1.47, -0.01, 0.09, -1.19, 0.22, -1.8, -0.59, 1.06, -1.05, 
1.42, -1.91, 0.73, 0.75, 0.82, -0.69, -0.52, 1.1, -0.56, -0.52, 
1.16, -0.35, -0.71, 0.92, -0.01, 0.89, -0.06, 0.87, 0.96, 0.97, 
0.38, 0.95, -0.23, -0.43, -1.17, 0.65, -0.76, 2.12, -0.16, 2.21, 
1.06, -0.35, 0.44, -0.46, 1.56, 1.66, -0.51, 1.08, -0.81, 0.71, 
1.08, 0.79, -0.44, 0.92, -0.03, -0.15, -0.25, -0.48, 0.28, -0.86, 
-1.07, -2.52, 0.15, -0.5, 1.13, 1.94, -0.35, -0.3, -0.12, -0.04, 
2.48, -0.3, -0.28, -3.04, 0.68, 1.02, -1.07, 1.59, -0.11, -0.44, 
1.27, 0.1, -0.1, 1.32, 0.08, 1.24, 1.46, 0.33, 1.55, -0.87, 1.26, 
-0.56, 0.76, -0.51, -0.24, -0.94, 0.88, -0.08, -2.27, 1.09, 1.15, 
-1.59, -0.65, 1.22, 0, 1.49, -2.03, 0.16, 0.21, 0.25, -2.21, 
1.43, 0.67, -1.33, 0.06, -0.34, 0.15, 1.93, -0.94, 0.21, -0.97, 
-0.95, -0.43, 1.86, 0.96, -0.32, 0.69, -0.54, 0.16, -0.04, -0.78, 
1.39, -0.39, -0.52, -0.82, -0.51, -0.18, -0.38, -0.68, 0.44, 
1.38, -0.27, 0.63, -0.56, 0.12, -1.02, 1.59, -1.03, -0.77, -0.17, 
-0.89, 0.56, -0.22, 1.43, -0.55, 0.69, 0.82, -0.32, 0.55, -0.94, 
0.31, 0.55, 1.11, -0.54, 0.58, -1.49, 2.33, -1.45, 1.05, 0.28, 
1.68, 0.86)), row.names = c(NA, -219L), class = "data.frame")

然后,我想做的是在包含 2 年(24 个月)的时间窗口中设置滚动回归.为此,我运行以下代码:

Then, what I want to do is setting up a rolling regression in a time window that encompasses, say, 2 years (24 months). To do so, I run the following codes:

library(rollRegres)
library(zoo)

roll_model1 = roll_regres(X1 ~ ., df, 24L, do_compute = c("sigmas", "r.squareds"), do_downdates = TRUE)

roll_model2 = rollapply(df, width = 24, FUN = function(x) coef(lm(X1 ~ ., data = as.data.frame(x))), by.column = FALSE, align = "right")

在第一种情况下,模型不起作用.在第二种情况下,我只得到拦截的结果(并且只有 coefficinets).另外,我不明白为什么有 196 个系数观测值.

In the first case, the model doesn't work. In the second case, I only get results for the intercept (and only coefficinets). Besides, I don't understand why there are 196 coefficient observations.

谁能帮我用这个数据集运行超过 2 年的滚动回归?

Can anyone help me run a rolling regression over 2 years window with this dataset?

谢谢!

df的所有列都一样

all(df == df[, 1])
## [1] TRUE

因此它可以使用 X2 完美地预测 X1,并且不需要其他的所以它给出了 NA.

so it can perfectly predict X1 using X2 and the others are not needed so it gives NA.

关于 rollapply 代码,它只给出系数,因为这是您要求的 coef(lm(...)) .你的函数应该返回一个你想得到的向量.

Regarding the rollapply code it only gave coefficients because that is what you asked for coef(lm(...)) . Your function should return a vector of whatever it is you want to get out.

它对第 1:24 行、第 2:25 行、...第 196:219 行进行回归,所以很明显有 196 个这样的集合,所以结果有 196 行.如果您指定 fill=NA,那么它将用 NA 填充它以提供与 df 相同的行数.

It does a regression for rows 1:24, rows 2:25, ... rows 196:219 so clearly there are 196 such sets so the result has 196 rows. If you specify fill=NA then it will pad it with NAs to give the same number of rows as df.

请注意,rollapplyr 可用,默认为 align = "right".

Note that rollapplyr is available which defaults to align = "right".

这是一个可能返回各种信息的函数:

Here is a possible function that returns a variety of information:

library(broom)

stats <- function(x) {
  fm <- lm(X1 ~., as.data.frame(x))
  c(coef(fm), unlist(glance(fm)))
}
rollapplyr(df, width = 24, FUN = stats, by.column = FALSE)