我正在尝试制作一个滚动线性模型,该模型执行一些计算并在其末尾返回一个值。
library(zoo)
myfunction <- function(x){
lm_summary <- summary(lm(x ~ time(x)))
lm_coefs <- lm_summary$coefficients
lm_r2 <- lm_summary$r.squared
some_output_calc = lm_coef * lm_r2
return(some_output_calc)
}
myrollingdata <- rollapplyr(
data = d,
width = 60,
FUN = myfunction,
by.column = FALSE)
我正在尝试与此做“相似”的事情,但是我想做一些进一步的计算:
someworkingfunction <- function(x){
LinearReg <- coef(summary(lm(x ~ time(x))))
#sapply(res, "[" ,"(Intercept)" ,"Estimate")
sapply(LinearReg, "[" ,"time(x)" ,"Estimate")
}
someworkingrollingdata <- rollapplyr(
d,
width = 60,
someworkingfunction,
by.column = FALSE)
数据:
d <- structure(c(66.265457, 63.524181, 62.590988, 62.7243, 64.132416,
63.624161, 61.791103, 61.299488, 61.491142, 60.707909, 62.682636,
62.757614, 64.032448, 64.332413, 63.874138, 64.790665, 64.748978,
62.199383, 62.407677, 62.949257, 64.873993, 66.648743, 65.548882,
66.490417, 66.50708, 66.573761, 66.540413, 66.073814, 66.940369,
68.356827, 68.565125, 67.748581, 67.431969, 67.106987, 66.832047,
67.015358, 67.38195, 66.132141, 65.8405, 65.382248, 65.623878,
65.532219, 65.140617, 63.974102, 63.515858, 62.632648, 62.815956,
62.416016, 62.182701, 63.02425, 62.782616, 64.607368, 63.582512,
64.517868, 64.291351, 63.863499, 64.694023, 64.618507, 64.090034,
65.465775, 65.079887, 65.063133, 65.197342, 66.371765, 66.413712,
65.876831, 66.933807, 67.227417, 66.556313, 67.755905, 69.374939,
69.215546, 68.712234, 68.972275, 68.242462, 68.69545, 69.165222,
68.770943, 68.69545, 69.039383, 69.576256, 69.358154, 69.517555,
69.341377, 68.485718, 68.217308, 67.47908, 68.586395, 67.294533,
67.604904, 67.428749, 67.40358, 67.67202, 67.730736, 66.766045,
66.891869, 67.521027, 67.277756, 66.212387, 66.094933, 96.965538,
97.176277, 97.422142, 97.404579, 97.255272, 97.492409, 97.571419,
97.597763, 97.834816, 98.282669, 97.905075, 98.071945, 97.896294,
97.843636, 98.08947, 98.045547, 97.992867, 98.405571, 98.326515,
98.677757, 98.674232, 98.296112, 98.44561, 98.243332, 97.6717,
97.654114, 97.548584, 97.504608, 97.522141, 97.40786, 97.012108,
97.284737, 97.205582, 97.223175, 97.399078, 97.830009, 97.90033,
97.548584, 97.794807, 97.297989, 97.174652, 97.227493, 97.271553,
96.637276, 96.883949, 97.112976, 97.306763, 97.38607, 97.201042,
97.412491, 97.394859, 98.108444, 97.764877, 98.064369, 98.082001,
98.346237, 98.161278, 97.808914, 97.949875, 97.993927, 98.161278,
98.479935, 98.347511, 98.409294, 98.426956, 98.43576, 98.188637,
98.188637, 98.294571, 98.400467, 98.532852, 98.479935, 98.559319,
98.488716, 98.382828, 98.091576, 98.2416, 98.409294, 98.444611,
98.065086, 97.844437, 97.844437, 97.566788, 97.381104, 97.292664,
96.965508, 97.204224, 97.522568, 96.90358, 96.894775, 96.885902,
97.036255, 97.442963, 97.071609, 96.735573, 96.885902, 97.186539,
97.08046, 97.319191, 97.41642, 42.288589, 41.991215, 41.93874,
42.655945, 43.303169, 42.953316, 42.822124, 42.813377, 42.682178,
42.752148, 43.093266, 43.268188, 43.478092, 43.915421, 43.425625,
43.425625, 42.927082, 42.428528, 42.769642, 41.999966, 42.498505,
42.874599, 42.892097, 43.110752, 42.918327, 42.594719, 43.093266,
43.29443, 43.495594, 43.29443, 43.303169, 43.539322, 43.259438,
43.381886, 43.504341, 43.653019, 43.504341, 43.565559, 43.740486,
43.819206, 43.653019, 43.346905, 43.495594, 42.64719, 42.883347,
42.271095, 41.947487, 42.559731, 42.227364, 42.743404, 42.437283,
42.72591, 42.533497, 43.066769, 43.137165, 42.890759, 42.785172,
42.600372, 42.864365, 43.29557, 42.890759, 42.943562, 43.233959,
43.577168, 43.374763, 43.44516, 43.568359, 43.64756, 43.44516,
43.541958, 43.418766, 43.577168, 43.172371, 43.321957, 43.401165,
43.427563, 43.277962, 43.286766, 43.049156, 43.09317, 42.705975,
42.56517, 42.95237, 43.013966, 42.670769, 42.802769, 42.908363,
43.269154, 43.09317, 42.996361, 42.978775, 43.638767, 43.77956,
43.638767, 43.524361, 43.427563, 43.524361, 43.339569, 42.996361,
43.269154, 186.322159, 183.050537, 181.401199, 183.645386, 186.925995,
185.348816, 183.94281, 183.465118, 182.356567, 180.644119, 182.960388,
183.411057, 184.312317, 187.016144, 186.015732, 186.511429, 184.00589,
181.62648, 183.365967, 181.040665, 183.239792, 185.844513, 185.186554,
187.124329, 186.520462, 185.718307, 187.728134, 187.818298, 189.566818,
190.42305, 190.765533, 190.729431, 190.576279, 191.738922, 191.675781,
192.252655, 192.108414, 191.89209, 191.2612, 192.477966, 191.612732,
190.810577, 191.00882, 188.350006, 189.134201, 186.042801, 185.646225,
187.962494, 186.808868, 189.395569, 188.755615, 190.981781, 190.161652,
191.838074, 191.522598, 190.323868, 187.627045, 187.165222, 187.545547,
189.827637, 188.206619, 187.617981, 188.143219, 189.51973, 188.958221,
189.637466, 190.470612, 191.46669, 190.588318, 190.923355, 191.901413,
191.810822, 189.592178, 191.330917, 191.131699, 192.055359, 192.535324,
192.960953, 192.200226, 192.725479, 191.973862, 190.035919, 192.091522,
192.707397, 190.479645, 189.691772, 190.434357, 192.915649, 191.982895,
191.394241, 191.46669, 193.513351, 193.712601, 194.283096, 194.246841,
194.056671, 194.618149, 194.210632, 192.100601, 193.938934, 66.744164,
65.467628, 64.815361, 65.840324, 66.828033, 66.054634, 65.700562,
65.700562, 64.908546, 64.069939, 64.917862, 64.545143, 64.964439,
66.185074, 66.091911, 66.436661, 65.905548, 65.048294, 65.933502,
65.216019, 65.803062, 67.200752, 67.64801, 68.067314, 67.96479,
67.703903, 68.533203, 68.551834, 69.362495, 69.781815, 69.623413,
69.586136, 69.800446, 70.294296, 70.229065, 70.564522, 71.10495,
70.881302, 70.788124, 71.636055, 71.440392, 71.067673, 71.114273,
70.26635, 70.666992, 69.567497, 69.185455, 70.592468, 70.033394,
70.853363, 70.462021, 71.086296, 70.98381, 71.773827, 71.652283,
71.278297, 70.119011, 69.679596, 70.034851, 70.754753, 70.446213,
70.137718, 70.764099, 71.10067, 70.708015, 71.371803, 71.437248,
71.680321, 71.287643, 71.250252, 71.250252, 71.390511, 70.334038,
71.110039, 71.184837, 71.250252, 71.605537, 72.577858, 71.942093,
71.689674, 71.203522, 70.408844, 71.381157, 71.530739, 70.782806,
70.492981, 70.85759, 71.614876, 71.297012, 71.166115, 70.717377,
71.03524, 71.605537, 71.87664, 71.857964, 71.773827, 72.054298,
71.932777, 71.418533, 71.951447), class = c("xts", "zoo"), src = "yahoo", updated = structure(1582990683.39436, class = c("POSIXct",
"POSIXt")), index = structure(c(1420156800, 1420416000, 1420502400,
1420588800, 1420675200, 1420761600, 1421020800, 1421107200, 1421193600,
1421280000, 1421366400, 1421712000, 1421798400, 1421884800, 1421971200,
1422230400, 1422316800, 1422403200, 1422489600, 1422576000, 1422835200,
1422921600, 1423008000, 1423094400, 1423180800, 1423440000, 1423526400,
1423612800, 1423699200, 1423785600, 1424131200, 1424217600, 1424304000,
1424390400, 1424649600, 1424736000, 1424822400, 1424908800, 1424995200,
1425254400, 1425340800, 1425427200, 1425513600, 1425600000, 1425859200,
1425945600, 1426032000, 1426118400, 1426204800, 1426464000, 1426550400,
1426636800, 1426723200, 1426809600, 1427068800, 1427155200, 1427241600,
1427328000, 1427414400, 1427673600, 1427760000, 1427846400, 1427932800,
1428278400, 1428364800, 1428451200, 1428537600, 1428624000, 1428883200,
1428969600, 1429056000, 1429142400, 1429228800, 1429488000, 1429574400,
1429660800, 1429747200, 1429833600, 1430092800, 1430179200, 1430265600,
1430352000, 1430438400, 1430697600, 1430784000, 1430870400, 1430956800,
1431043200, 1431302400, 1431388800, 1431475200, 1431561600, 1431648000,
1431907200, 1431993600, 1432080000, 1432166400, 1432252800, 1432598400,
1432684800), tzone = "UTC", tclass = "Date"), .Dim = c(100L,
5L), .Dimnames = list(NULL, c("XLE.Adjusted", "AGG.Adjusted",
"XLP.Adjusted", "IVV.Adjusted", "XLY.Adjusted")))