Предположим, у меня есть следующий фрейм данных, состоящий из 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")
Затем я хочу настроить скользящую регрессию во временном окне, которое охватывает, скажем, 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 наблюдений коэффициентов.
Может ли кто-нибудь помочь мне запустить скользящую регрессию в течение двухлетнего окна с этим набором данных?
Спасибо!