Home Articles

Rollapply用于滚动回归,具有不同的窗口大小,保存矩阵中的系数

Asked
Viewed 156 times
0

我正在尝试执行滚动线性回归,重叠窗口以及不同的窗口大小 . 我想将结果保存在两个表中,一个是窗口大小和斜率,一个是窗口大小a和截取 .

我的数据集现在保存为xts对象 . 我正在尝试使用for循环生成不同的窗口大小,然后在两个变量之间进行滚动回归,使用rollapply重叠窗口 . 然后我希望每个回归的系数被保存为矩阵中的一行,可以构建,以便每个窗口大小对应一行 . 我看过很多以前的问题,但我无法做到 .

我有2个变量的每小时数据,这是x变量的数据:

> dput(Xq$x)
structure(c(339.76625, 176.7196875, 142.8063125, 118.5785625, 
102.0514375, 86.01156251, 86.99050001, 85.0089375, 380.0010626, 
2114.279375, 2157.76875, 2442.575, 2562.6375, 2537.7125, 2581.888125, 
1947.7575, 834.7918751, 765.1525001, 989.698125, 1433.2, 429.9081251, 
388.333125, 1500.995, 1523.816625, 1529.090126, 439.746875, 1029.03625, 
1470.07825, 1454.844813, 1448.8455, 413.7550001, 334.03125, 566.7362501, 
1609.184375, 1509.266876, 1403.1425, 1142.695625, 795.138125, 
1562.881875, 933.190625, 549.085625, 500.135, 535.83, 444.3493751, 
356.386875, 268.1875001, 141.1878125, 119.658875, 162.0425625, 
153.6944375, 161.0025, 151.37025, 155.9567501, 123.64825, 145.664125, 
141.4480625, 232.2009375, 913.6480001, 2227.08125, 1870.618751, 
2190.606251, 1357.81625, 824.555625, 699.99, 703.695, 620.74625, 
581.2375001, 393.391875, 349.96625, 188.5445625, 154.03125, 142.0825, 
339.7915625, 139.8941875, 154.6279376, 171.6016875, 151.1387501, 
159.7665, 168.62025, 180.7596875, 447.2746875, 1550.11625, 1887.275, 
1446.883125, 699.590625, 983.9925, 1319.62875, 769.505625, 812.713125, 
638.1437501, 585.8312501, 626.7875, 1924.293125, 629.855625, 
349.88375, 1038.54875, 1588.82225, 548.3475, 1053.368125, 964.825625, 
990.324375, 1327.160625, 395.5862501, 421.8154376, 1308.551875, 
1334.045, 1633.408125, 886.2662501, 1618.7975, 1325.59625, 847.0693751, 
667.501875, 594.0531251, 249.920625, 210.8869376, 224.3725001, 
209.6325, 213.64875, 210.3175001, 194.54, 90.1085625, 71.12575001, 
70.69412501, 69.6075, 68.404625, 67.88837501, 67.317125, 67.220375, 
67.696625, 273.2732501, 606.475625, 1277.3425, 913.24875, 612.5462501, 
571.42125, 503.27375, 316.68375, 208.7436251, 127.283, 126.12375, 
102.504625, 78.7938125, 70.0183125, 69.97900001, 77.7586875, 
76.60543751, 94.5235625, 103.0095625, 105.1938125, 103.8090625, 
104.005625, 104.544875, 105.2036875, 104.6611875, 375.7004375, 
793.7725001, 782.544375, 257.065625, 387.1487501, 571.1912501, 
356.6406251, 243.404375, 220.698125, 182.5680626, 161.060625, 
166.5178125, 137.9805, 129.6253125, 99.4291875, 75.88075001, 
65.769875, 62.62937501, 59.697875, 57.68675, 56.67275, 56.69425001, 
57.296, 638.95125, 1210.92875, 990.09, 907.895625, 1122.704375, 
569.139375, 295.6725, 263.21, 236.3885625, 153.935, 148.0899375, 
161.72675, 102.1535, 115.8895, 114.2579375, 95.0023125, 90.72425, 
84.2715, 71.06631251, 63.39812501, 75.2416875, 73.9420625, 72.26237501, 
402.8870625, 1148.9775, 1058.86875, 896.47875, 890.9687501, 833.645625, 
670.310625, 298.576875, 306.538125, 386.403125, 196.8343751, 
174.0815, 156.6158126, 117.9345625, 105.095, 90.24362501, 87.08800001, 
86.1628125, 105.3179375, 107.312625, 114.089, 104.85825, 95.9281875, 
126.2874375, 529.7325626, 1533.605625, 1089.700625, 760.47, 1905.04125, 
1744.460625, 1346.120625, 847.90625, 459.4625, 472.11, 474.5068751, 
481.83875, 937.3618751, 267.1339376, 137.7895625, 124.0425625, 
188.9461875, 108.8915, 111.67825, 84.319125, 127.1240625, 117.519125, 
104.16225, 105.2490625, 158.5540625, 1215.278063, 1275.396875, 
1301.14375, 1108.9225, 1280.390625, 1434.5225, 1038.926875, 599.3218751, 
665.22875, 646.935, 487.604375, 236.8148126, 300.6626876, 830.2973126, 
1286.612313, 403.0063751, 405.9081875, 156.5465625, 101.7661875, 
100.2301875, 144.424625, 126.781125, 143.940625, 365.5354376, 
849.313125, 1729.3175, 1762.6625, 1733.968751, 1275.4375, 1332.930625, 
620.5625, 504.115625, 538.4556251, 433.5725, 337.094375, 225.183125, 
184.7196251, 156.7545, 130.23325, 134.0796875, 145.196125, 153.243, 
141.096375, 414.8042501, 304.7252501, 204.1889375, 129.7909375, 
555.0276251, 1734.235625, 1780.209375, 1462.7825, 967.4743751, 
1248.791875, 1001.655625, 662.92875, 546.801875, 393.72375, 308.85125, 
259.985625, 280.439375, 274.1312501, 310.0375001, 270.3, 201.9288125, 
219.6061875, 208.372625, 156.2023125, 162.5355626, 167.823875, 
151.3928126, 151.42875, 293.7148125, 434.9101875, 943.9243751, 
995.955, 614.055, 1387.864375, 1752.330626, 1238.1725, 1260.93, 
565.383125, 253.179375, 275.66625, 280.4912501, 222.684375, 338.079875, 
201.9365625, 136.7436875, 146.5479375, 142.794625, 150.3969375, 
134.6478125, 88.22068751, 70.6445, 71.73737501, 243.176, 1559.0925, 
1527.626876, 1260.648125, 1251.564375, 714.1075001, 1124.58, 
901.6018751, 551.9075, 303.1025, 355.435625, 372.32375, 235.62125, 
185.39475, 161.7701876, 155.4823751), index = structure(c(1438380000, 
1438383600, 1438387200, 1438390800, 1438394400, 1438398000, 1438401600, 
1438405200, 1438408800, 1438412400, 1438416000, 1438419600, 1438423200, 
1438426800, 1438430400, 1438434000, 1438437600, 1438441200, 1438444800, 
1438448400, 1438452000, 1438455600, 1438459200, 1438462800, 1438466400, 
1438470000, 1438473600, 1438477200, 1438480800, 1438484400, 1438488000, 
1438491600, 1438495200, 1438498800, 1438502400, 1438506000, 1438509600, 
1438513200, 1438516800, 1438520400, 1438524000, 1438527600, 1438531200, 
1438534800, 1438538400, 1438542000, 1438545600, 1438549200, 1438552800, 
1438556400, 1438560000, 1438563600, 1438567200, 1438570800, 1438574400, 
1438578000, 1438581600, 1438585200, 1438588800, 1438592400, 1438596000, 
1438599600, 1438603200, 1438606800, 1438610400, 1438614000, 1438617600, 
1438621200, 1438624800, 1438628400, 1438632000, 1438635600, 1438639200, 
1438642800, 1438646400, 1438650000, 1438653600, 1438657200, 1438660800, 
1438664400, 1438668000, 1438671600, 1438675200, 1438678800, 1438682400, 
1438686000, 1438689600, 1438693200, 1438696800, 1438700400, 1438704000, 
1438707600, 1438711200, 1438714800, 1438718400, 1438722000, 1438725600, 
1438729200, 1438732800, 1438736400, 1438740000, 1438743600, 1438747200, 
1438750800, 1438754400, 1438758000, 1438761600, 1438765200, 1438768800, 
1438772400, 1438776000, 1438779600, 1438783200, 1438786800, 1438790400, 
1438794000, 1438797600, 1438801200, 1438804800, 1438808400, 1438812000, 
1438815600, 1438819200, 1438822800, 1438826400, 1438830000, 1438833600, 
1438837200, 1438840800, 1438844400, 1438848000, 1438851600, 1438855200, 
1438858800, 1438862400, 1438866000, 1438869600, 1438873200, 1438876800, 
1438880400, 1438884000, 1438887600, 1438891200, 1438894800, 1438898400, 
1438902000, 1438905600, 1438909200, 1438912800, 1438916400, 1438920000, 
1438923600, 1438927200, 1438930800, 1438934400, 1438938000, 1438941600, 
1438945200, 1438948800, 1438952400, 1438956000, 1438959600, 1438963200, 
1438966800, 1438970400, 1438974000, 1438977600, 1438981200, 1438984800, 
1438988400, 1438992000, 1438995600, 1438999200, 1439002800, 1439006400, 
1439010000, 1439013600, 1439017200, 1439020800, 1439024400, 1439028000, 
1439031600, 1439035200, 1439038800, 1439042400, 1439046000, 1439049600, 
1439053200, 1439056800, 1439060400, 1439064000, 1439067600, 1439071200, 
1439074800, 1439078400, 1439082000, 1439085600, 1439089200, 1439092800, 
1439096400, 1439100000, 1439103600, 1439107200, 1439110800, 1439114400, 
1439118000, 1439121600, 1439125200, 1439128800, 1439132400, 1439136000, 
1439139600, 1439143200, 1439146800, 1439150400, 1439154000, 1439157600, 
1439161200, 1439164800, 1439168400, 1439172000, 1439175600, 1439179200, 
1439182800, 1439186400, 1439190000, 1439193600, 1439197200, 1439200800, 
1439204400, 1439208000, 1439211600, 1439215200, 1439218800, 1439222400, 
1439226000, 1439229600, 1439233200, 1439236800, 1439240400, 1439244000, 
1439247600, 1439251200, 1439254800, 1439258400, 1439262000, 1439265600, 
1439269200, 1439272800, 1439276400, 1439280000, 1439283600, 1439287200, 
1439290800, 1439294400, 1439298000, 1439301600, 1439305200, 1439308800, 
1439312400, 1439316000, 1439319600, 1439323200, 1439326800, 1439330400, 
1439334000, 1439337600, 1439341200, 1439344800, 1439348400, 1439352000, 
1439355600, 1439359200, 1439362800, 1439366400, 1439370000, 1439373600, 
1439377200, 1439380800, 1439384400, 1439388000, 1439391600, 1439395200, 
1439398800, 1439402400, 1439406000, 1439409600, 1439413200, 1439416800, 
1439420400, 1439424000, 1439427600, 1439431200, 1439434800, 1439438400, 
1439442000, 1439445600, 1439449200, 1439452800, 1439456400, 1439460000, 
1439463600, 1439467200, 1439470800, 1439474400, 1439478000, 1439481600, 
1439485200, 1439488800, 1439492400, 1439496000, 1439499600, 1439503200, 
1439506800, 1439510400, 1439514000, 1439517600, 1439521200, 1439524800, 
1439528400, 1439532000, 1439535600, 1439539200, 1439542800, 1439546400, 
1439550000, 1439553600, 1439557200, 1439560800, 1439564400, 1439568000, 
1439571600, 1439575200, 1439578800, 1439582400, 1439586000, 1439589600, 
1439593200, 1439596800, 1439600400, 1439604000, 1439607600, 1439611200, 
1439614800, 1439618400, 1439622000, 1439625600, 1439629200, 1439632800, 
1439636400, 1439640000, 1439643600, 1439647200, 1439650800, 1439654400, 
1439658000, 1439661600, 1439665200, 1439668800, 1439672400), tzone = "UTC", tclass = c("chron", 
"dates", "times")), class = c("xts", "zoo"), .indexCLASS = c("chron", 
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L, 
1L), .Dimnames = list(NULL, "x"))

而我的y变量:

> dput(Xq$y)
structure(c(-0.274050833, -0.236638333, -0.1994325, -0.174091667, 
-0.153273333, -0.136978333, -0.124748333, -0.117348333, -0.147061667, 
-0.346170833, -0.517939167, -0.575585833, -0.595914167, -0.563639167, 
-0.53403, -0.5500525, -0.505650833, -0.471713333, -0.472485833, 
-0.547393333, -0.390174167, -0.321545, -0.29781, -0.26912, -0.345084167, 
-0.367618333, -0.279933333, -0.256805, -0.2514675, -0.314349167, 
-0.3594375, -0.33482, -0.369094167, -0.4801075, -0.554780833, 
-0.600498333, -0.604796667, -0.544491667, -0.636653333, -0.568401667, 
-0.485494167, -0.453199167, -0.417475, -0.38417, -0.341585833, 
-0.2821625, -0.248325, -0.2230575, -0.2449075, -0.2385375, -0.231885, 
-0.214125, -0.194190833, -0.178575833, -0.1677675, -0.1615725, 
-0.1739675, -0.2432125, -0.402414167, -0.448185833, -0.563599167, 
-0.5855025, -0.55586, -0.516350833, -0.47892, -0.4603325, -0.434146667, 
-0.37602, -0.320976667, -0.267863333, -0.250915, -0.241764167, 
-0.271475, -0.2170225, -0.206605, -0.2088, -0.214511667, -0.2086825, 
-0.203060833, -0.192895, -0.212629167, -0.319143333, -0.42647, 
-0.4623275, -0.467844167, -0.506395833, -0.507088333, -0.496953333, 
-0.511156667, -0.487846667, -0.4455525, -0.398383333, -0.565926667, 
-0.429720833, -0.314555, -0.418586667, -0.578256667, -0.414858333, 
-0.345411667, -0.3088925, -0.304373333, -0.334221667, -0.305029167, 
-0.273269167, -0.315901667, -0.409731667, -0.500245833, -0.505959167, 
-0.54742, -0.574725, -0.548458333, -0.5560675, -0.50246, -0.411618333, 
-0.35965, -0.331884167, -0.312573333, -0.298478333, -0.289144167, 
-0.274429167, -0.2218225, -0.198025833, -0.191955833, -0.1780825, 
-0.157910833, -0.135935, -0.116965833, -0.099886667, -0.0864975, 
-0.188904167, -0.325656667, -0.444044167, -0.5050425, -0.54236, 
-0.547500833, -0.5399775, -0.521490833, -0.444911667, -0.388053333, 
-0.327650833, -0.2478, -0.2026775, -0.1693425, -0.140848333, 
-0.130440833, -0.103501667, -0.088843333, -0.09344, -0.126231667, 
-0.158463333, -0.181145, -0.185095, -0.11657, -0.0349225, -0.223260833, 
-0.431800833, -0.516, -0.487566667, -0.49941, -0.541773333, -0.511953333, 
-0.4404425, -0.4021125, -0.380635, -0.3301275, -0.302468333, 
-0.311290833, -0.2774125, -0.223738333, -0.192925, -0.176034167, 
-0.158151667, -0.136234167, -0.117050833, -0.101201667, -0.08872, 
-0.0855675, -0.257761667, -0.424794167, -0.528980833, -0.56834, 
-0.591263333, -0.56574, -0.53178, -0.5410825, -0.552130833, -0.501294167, 
-0.44576, -0.402855833, -0.316163333, -0.2515275, -0.213383333, 
-0.186723333, -0.174315833, -0.170100833, -0.154446667, -0.147075833, 
-0.169245, -0.188474167, -0.187344167, -0.2586975, -0.440815, 
-0.534344167, -0.598391667, -0.613878333, -0.624658333, -0.583091667, 
-0.508740833, -0.518310833, -0.51102, -0.4203825, -0.364895833, 
-0.31302, -0.27689, -0.254130833, -0.232273333, -0.218198333, 
-0.22501, -0.235655833, -0.242728333, -0.260448333, -0.263113333, 
-0.243530833, -0.222301667, -0.274098333, -0.3974225, -0.484443333, 
-0.506165, -0.6136375, -0.631805, -0.596274167, -0.539795, -0.446769167, 
-0.398489167, -0.3986925, -0.4098625, -0.484515833, -0.375961667, 
-0.291685, -0.273963333, -0.2621025, -0.2054525, -0.177625833, 
-0.1564625, -0.141883333, -0.130685833, -0.121943333, -0.112834167, 
-0.136975, -0.3204725, -0.458004167, -0.5262175, -0.527530833, 
-0.520574167, -0.5800325, -0.561841667, -0.4909225, -0.4641625, 
-0.443126667, -0.405705833, -0.3098625, -0.286379167, -0.30477, 
-0.395341667, -0.38505, -0.356219167, -0.263050833, -0.219625833, 
-0.197383333, -0.2019775, -0.226838333, -0.2333075, -0.2890475, 
-0.3341175, -0.43458, -0.520441667, -0.577875, -0.568123333, 
-0.551936667, -0.463691667, -0.468790833, -0.4747725, -0.4125925, 
-0.394731667, -0.380213333, -0.308688333, -0.279549167, -0.249766667, 
-0.233964167, -0.229904167, -0.244835833, -0.232436667, -0.215466667, 
-0.198559167, -0.184533333, -0.185376667, -0.246823333, -0.395918333, 
-0.4956775, -0.540474167, -0.5402375, -0.577863333, -0.561466667, 
-0.503130833, -0.455221667, -0.4401875, -0.4187675, -0.389215, 
-0.345275, -0.3378175, -0.348759167, -0.325149167, -0.29995, 
-0.289409167, -0.291635, -0.301183333, -0.274799167, -0.2443375, 
-0.2254225, -0.21272, -0.245265, -0.295081667, -0.356848333, 
-0.4258325, -0.4329175, -0.487074167, -0.595525, -0.59333, -0.564645, 
-0.464548333, -0.4294775, -0.425609167, -0.403769167, -0.353311667, 
-0.282888333, -0.2464575, -0.231771667, -0.2275425, -0.224230833, 
-0.218419167, -0.1902275, -0.161319167, -0.143495833, -0.133691667, 
-0.181291667, -0.36056, -0.46681, -0.5194575, -0.532989167, -0.4899375, 
-0.533224167, -0.4976575, -0.428966667, -0.412929167, -0.416463333, 
-0.366666667, -0.316356667, -0.3023825, -0.282655, -0.267275833
), index = structure(c(1438380000, 1438383600, 1438387200, 1438390800, 
1438394400, 1438398000, 1438401600, 1438405200, 1438408800, 1438412400, 
1438416000, 1438419600, 1438423200, 1438426800, 1438430400, 1438434000, 
1438437600, 1438441200, 1438444800, 1438448400, 1438452000, 1438455600, 
1438459200, 1438462800, 1438466400, 1438470000, 1438473600, 1438477200, 
1438480800, 1438484400, 1438488000, 1438491600, 1438495200, 1438498800, 
1438502400, 1438506000, 1438509600, 1438513200, 1438516800, 1438520400, 
1438524000, 1438527600, 1438531200, 1438534800, 1438538400, 1438542000, 
1438545600, 1438549200, 1438552800, 1438556400, 1438560000, 1438563600, 
1438567200, 1438570800, 1438574400, 1438578000, 1438581600, 1438585200, 
1438588800, 1438592400, 1438596000, 1438599600, 1438603200, 1438606800, 
1438610400, 1438614000, 1438617600, 1438621200, 1438624800, 1438628400, 
1438632000, 1438635600, 1438639200, 1438642800, 1438646400, 1438650000, 
1438653600, 1438657200, 1438660800, 1438664400, 1438668000, 1438671600, 
1438675200, 1438678800, 1438682400, 1438686000, 1438689600, 1438693200, 
1438696800, 1438700400, 1438704000, 1438707600, 1438711200, 1438714800, 
1438718400, 1438722000, 1438725600, 1438729200, 1438732800, 1438736400, 
1438740000, 1438743600, 1438747200, 1438750800, 1438754400, 1438758000, 
1438761600, 1438765200, 1438768800, 1438772400, 1438776000, 1438779600, 
1438783200, 1438786800, 1438790400, 1438794000, 1438797600, 1438801200, 
1438804800, 1438808400, 1438812000, 1438815600, 1438819200, 1438822800, 
1438826400, 1438830000, 1438833600, 1438837200, 1438840800, 1438844400, 
1438848000, 1438851600, 1438855200, 1438858800, 1438862400, 1438866000, 
1438869600, 1438873200, 1438876800, 1438880400, 1438884000, 1438887600, 
1438891200, 1438894800, 1438898400, 1438902000, 1438905600, 1438909200, 
1438912800, 1438916400, 1438920000, 1438923600, 1438927200, 1438930800, 
1438934400, 1438938000, 1438941600, 1438945200, 1438948800, 1438952400, 
1438956000, 1438959600, 1438963200, 1438966800, 1438970400, 1438974000, 
1438977600, 1438981200, 1438984800, 1438988400, 1438992000, 1438995600, 
1438999200, 1439002800, 1439006400, 1439010000, 1439013600, 1439017200, 
1439020800, 1439024400, 1439028000, 1439031600, 1439035200, 1439038800, 
1439042400, 1439046000, 1439049600, 1439053200, 1439056800, 1439060400, 
1439064000, 1439067600, 1439071200, 1439074800, 1439078400, 1439082000, 
1439085600, 1439089200, 1439092800, 1439096400, 1439100000, 1439103600, 
1439107200, 1439110800, 1439114400, 1439118000, 1439121600, 1439125200, 
1439128800, 1439132400, 1439136000, 1439139600, 1439143200, 1439146800, 
1439150400, 1439154000, 1439157600, 1439161200, 1439164800, 1439168400, 
1439172000, 1439175600, 1439179200, 1439182800, 1439186400, 1439190000, 
1439193600, 1439197200, 1439200800, 1439204400, 1439208000, 1439211600, 
1439215200, 1439218800, 1439222400, 1439226000, 1439229600, 1439233200, 
1439236800, 1439240400, 1439244000, 1439247600, 1439251200, 1439254800, 
1439258400, 1439262000, 1439265600, 1439269200, 1439272800, 1439276400, 
1439280000, 1439283600, 1439287200, 1439290800, 1439294400, 1439298000, 
1439301600, 1439305200, 1439308800, 1439312400, 1439316000, 1439319600, 
1439323200, 1439326800, 1439330400, 1439334000, 1439337600, 1439341200, 
1439344800, 1439348400, 1439352000, 1439355600, 1439359200, 1439362800, 
1439366400, 1439370000, 1439373600, 1439377200, 1439380800, 1439384400, 
1439388000, 1439391600, 1439395200, 1439398800, 1439402400, 1439406000, 
1439409600, 1439413200, 1439416800, 1439420400, 1439424000, 1439427600, 
1439431200, 1439434800, 1439438400, 1439442000, 1439445600, 1439449200, 
1439452800, 1439456400, 1439460000, 1439463600, 1439467200, 1439470800, 
1439474400, 1439478000, 1439481600, 1439485200, 1439488800, 1439492400, 
1439496000, 1439499600, 1439503200, 1439506800, 1439510400, 1439514000, 
1439517600, 1439521200, 1439524800, 1439528400, 1439532000, 1439535600, 
1439539200, 1439542800, 1439546400, 1439550000, 1439553600, 1439557200, 
1439560800, 1439564400, 1439568000, 1439571600, 1439575200, 1439578800, 
1439582400, 1439586000, 1439589600, 1439593200, 1439596800, 1439600400, 
1439604000, 1439607600, 1439611200, 1439614800, 1439618400, 1439622000, 
1439625600, 1439629200, 1439632800, 1439636400, 1439640000, 1439643600, 
1439647200, 1439650800, 1439654400, 1439658000, 1439661600, 1439665200, 
1439668800, 1439672400), tzone = "UTC", tclass = c("chron", "dates", 
"times")), class = c("xts", "zoo"), .indexCLASS = c("chron", 
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L, 
1L), .Dimnames = list(NULL, "y"))

我的代码看起来像这样:

library(zoo)

mat_slope=matrix(nrow=i)
mat_intercept=matrix(nrow=i)
for(i in seq(from=24, to=240, by=24)){
  mov.reg<- rollapplyr(Xq,
                       width= i,
                       by=24,
                       FUN = function(y,x) 
                       { 
                         coefficients(lm(formula=y~x, data = Xq))
                       },
                       by.column=FALSE) 
  mat_slope[i] <- coefficients(mov.reg)[2]
  mat_intercept[i] <-coefficients(mov.reg)[1]
  mat_intercept=rbind(i,mat_intercept)
  mat_slope[n,i]=rbind(i, mat_slope)
}

我得到一个空矩阵作为结果,或以下错误消息:

Error in merge.xts(res, xts(, idx, attr(data, "frequency"))) : 
  (list) object cannot be coerced to type 'double'

如果有人有一些意见或想法,将非常感谢!非常感谢!

1 Answer

  • 2

    mov.reg 不是 lm objct,所以拿它的 coefficients 是没有意义的 . 在dput输出中还有chron的引用,因此我们需要加载chron包 .

    没有指定 Xq ,而是显示了两个单独的xts对象,因此我们假设这两个对象被称为 xy . 然后从它们创建一个动物园对象 z . 现在定义一个 Coef 函数,该函数采用带有 xy 列的矩阵,并执行返回系数的指示回归 . 同时定义 roll ,它将宽度作为输入并使用所需的参数运行 rollapplyr . 现在使用 lapply 运行 roll ,每个宽度生成一个动物园对象列表 . 此列表包含 widths 中每个元素的一个组件,该组件由具有拦截和斜率列的动物园对象组成 . 最后从列表中的每个组件中提取截距,并形成一个动物园对象,并对斜坡执行相同的操作 . 请注意,如果 L 是动物园对象的列表,那么 do.call("merge", L) 将从中生成一个动物园对象 .

    library(xts) # this also loads zoo
    library(chron)
    
    # inputs are xts objects x and y
    z <- cbind(x = as.zoo(x)[, 1], y = as.zoo(y)[, 1])
    
    Coef <- function(m) coef(lm(y ~ x, as.data.frame(m)))
    roll <- function(w) rollapplyr(z, w, Coef, by = 24, by.column = FALSE)
    widths <- seq(24, 240, 24)
    names(widths) <- widths # lapply will use these names for its output
    
    L <- lapply(widths, roll)
    intercepts <- do.call("merge", lapply(L, "[", TRUE, 1)) # extract 1st columns
    slopes <- do.call("merge", lapply(L, "[", TRUE, 2)) # 2nd columns
    

    或者用 rollapplyr 两次替换最后3行:

    intercepts <- do.call("merge", lapply(widths, function(w) roll(w)[, 1]))
    slopes <- do.call("merge", lapply(widths, function(w) roll(w)[, 2]))
    

Related