如何使用此数据集在 R 中执行滚动回归?
How to perform rolling regression in R with this dataset?
假设我有以下由 219 行组成的数据框。由于某些结构原因,数据集不是完全每月一次。
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")
然后,我想做的是在 window 时间段内设置滚动回归,例如 2 年(24 个月)。为此,我 运行 以下代码:
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")
在第一种情况下,模型不起作用。在第二种情况下,我只得到截距的结果(而且只有系数)。另外,我不明白为什么会有196个系数观测值。
任何人都可以帮助我 运行 使用此数据集进行 2 年 window 的滚动回归吗?
谢谢!
df
的所有列都相同
all(df == df[, 1])
## [1] TRUE
因此它可以使用 X2 完美地预测 X1 并且不需要其他的所以它给出 NA。
关于 rollapply
代码,它只给出了系数,因为那是你要求的 coef(lm(...))
。你的函数应该 return 一个你想要输出的向量。
它对第 1:24 行、2:25 行、... 196:219 行进行回归,显然有 196 个这样的集合,因此结果有 196 行。如果您指定 fill=NA
那么它将用 NA 填充它以提供与 df
.
相同的行数
请注意 rollapplyr
可用,默认为 align = "right"
。
这里是return各种信息的可能函数:
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)
假设我有以下由 219 行组成的数据框。由于某些结构原因,数据集不是完全每月一次。
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")
然后,我想做的是在 window 时间段内设置滚动回归,例如 2 年(24 个月)。为此,我 运行 以下代码:
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")
在第一种情况下,模型不起作用。在第二种情况下,我只得到截距的结果(而且只有系数)。另外,我不明白为什么会有196个系数观测值。
任何人都可以帮助我 运行 使用此数据集进行 2 年 window 的滚动回归吗?
谢谢!
df
的所有列都相同
all(df == df[, 1])
## [1] TRUE
因此它可以使用 X2 完美地预测 X1 并且不需要其他的所以它给出 NA。
关于 rollapply
代码,它只给出了系数,因为那是你要求的 coef(lm(...))
。你的函数应该 return 一个你想要输出的向量。
它对第 1:24 行、2:25 行、... 196:219 行进行回归,显然有 196 个这样的集合,因此结果有 196 行。如果您指定 fill=NA
那么它将用 NA 填充它以提供与 df
.
请注意 rollapplyr
可用,默认为 align = "right"
。
这里是return各种信息的可能函数:
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)