滚动逐步回归R

时间:2015-06-26 18:14:55

标签: r loops window time-series linear-regression

我有几个不同证券的月度时间序列回报流。我想使用多个时间序列因子对每个安全性进行逐步回归。理想情况下,回归将在36个月的时间内记录哪些因素出现,它们的系数是它们的p值,以及R2。我希望'R'在每个独立变量的单独表格上为每个回归系列产生结果。这就是输出理想情况......

start.date end.date   R2    const  X1   X2  X3 X4 X5
1     Jan-06   Dec-08 .034   .047  .423 NA  NA NA NA
2     Feb-06   Jan-09 .057   NA    NA  .059 NA NA NA
3     Mar-06   Feb-09 ...    NA    NA   NA  NA NA NA
4     Apr-06   Mar-09 ...    NA    NA   NA  NA NA NA

每个安全/自变量都有一个像这样的表^

我目前设置自变量的方式是从excel导入的一个数据表中,因变量分别在另一个名为“y”和“x”的变量中。他们看起来像

date     t   Y1 Y2 Y3 Y4 Y5
Jan-09   1   'return streams' 
Feb-06   2
...      ...

@jeremycg 所以我试过了,但这就是我得到的。此外,代码似乎没有在任何地方调用回归变量? cmdr是我标题数据集而不是SEAT

> cmnd<-read.csv("CM(nodate).csv",header=TRUE)

> library(zoo)

> cmnd <- as.zoo(cmnd)
> time(cmnd) <- as.yearmon(time(cmnd))
> cmnd <- merge(y = cmnd, y1 = lag(cmnd, k = -1),
+ y12 = lag(cmnd, k = -12), all = FALSE)
> rr <- rollapply(cmnd, width = 36,
+ FUN = function(z) coef(lm(y ~ y1 + y12, data = as.data.frame(z))),
+ by.column = FALSE, align = "right")
Error in eval(expr, envir, enclos) : object 'y1' not found
Called from: eval(expr, envir, enclos)
Browse[1]> y1
Error during wrapup: object 'y1' not found

这是X变量数据

> dput(Xvar)
structure(list(Date = structure(c(5L, 9L, 13L, 1L, 20L, 21L, 
25L, 29L, 32L, 35L, 38L, 41L, 6L, 10L, 14L, 2L, 17L, 22L, 26L, 
30L, 33L, 36L, 39L, 42L, 7L, 11L, 15L, 3L, 18L, 23L, 27L, 31L, 
34L, 37L, 40L, 43L, 8L, 12L, 16L, 4L, 19L, 24L, 28L), .Label = c("1/31/2012", 
"1/31/2013", "1/31/2014", "1/31/2015", "10/31/2011", "10/31/2012", 
"10/31/2013", "10/31/2014", "11/30/2011", "11/30/2012", "11/30/2013", 
"11/30/2014", "12/31/2011", "12/31/2012", "12/31/2013", "12/31/2014", 
"2/28/2013", "2/28/2014", "2/28/2015", "2/29/2012", "3/31/2012", 
"3/31/2013", "3/31/2014", "3/31/2015", "4/30/2012", "4/30/2013", 
"4/30/2014", "4/30/2015", "5/31/2012", "5/31/2013", "5/31/2014", 
"6/30/2012", "6/30/2013", "6/30/2014", "7/31/2012", "7/31/2013", 
"7/31/2014", "8/31/2012", "8/31/2013", "8/31/2014", "9/30/2012", 
"9/30/2013", "9/30/2014"), class = "factor"), ACWI = c(0.10714784, 
-0.02993811, -0.00202054, 0.05814695, 0.05031344, 0.0066489, 
-0.01144253, -0.08965802, 0.0493889, 0.01368985, 0.02174329, 
0.03149612, -0.00666522, 0.01278754, 0.02265065, 0.04606848, 
-0.00015564, 0.01828406, 0.02856742, -0.00274334, -0.02922894, 
0.04787248, -0.02083435, 0.05165115, 0.04019254, 0.01416213, 
0.01725175, -0.04000044, 0.04830765, 0.00444623, 0.0095198, 0.02126899, 
0.01882772, -0.01213777, 0.02208798, -0.03241597, 0.00704015, 
0.01672642, -0.01929628, -0.0156347, 0.05567139, -0.01549454, 
0.02901443), Momentum = c(0.0082, -0.0014, -0.0112, -0.0053, 
6e-04, 2e-04, 0.0193, -0.0123, -0.0156, -0.0087, 0.0122, 0.0071, 
0.0039, 0.0174, 0.0213, 0.0094, -0.0047, -0.0072, 0.029, -0.0165, 
0.0164, -0.0076, -0.004, 0.0339, -0.0272, 0.003, 0.0011, 0.0085, 
-0.0061, -0.0199, -0.0292, 0.0055, 0.0019, -0.0126, 0.0027, -0.0039, 
-0.0056, 5e-04, 0.0118, 0.0305, -0.0219, 0.0202, -0.0234), Value = c(0.0093, 
-0.0238, -2e-04, 0.008, 0.0205, -0.0121, -0.0182, -0.0274, 0.029, 
0.0044, -3e-04, 0.002, 0.0073, -0.0102, 0.0164, 0.0309, 0.005, 
-0.0023, 0.0225, 0.0138, -0.006, 0.0095, 0.0042, -0.0106, 0.0089, 
0.0022, -0.0054, -0.0033, 0.0126, 0.0217, 0.0084, -0.0146, -0.0016, 
0.0123, -0.019, -0.0096, -0.0303, -0.0138, -0.0089, 0.001, -0.0097, 
-0.0244, 0.029), Size = c(0.03983, 0.00035, -0.00192, 0.02249, 
-0.01858, -0.00586, -0.00922, -0.00355, 0.01154, -0.0251, 0.01026, 
0.00713, -0.00422, -0.00127, 0.02504, 0.00912, -0.0011, 0.00735, 
-0.02123, 0.01892, 0.00846, 0.01715, -0.00313, 0.02898, -0.01821, 
0.01302, -0.00711, 0.00474, 0.00107, -0.0133, -0.04298, -0.01393, 
0.03042, -0.04376, 0.00931, -0.04285, 0.0419, -0.02423, 0.03077, 
-0.00421, 0.00292, 0.0298, -0.03218), highyield = c(0.0599, -0.0216, 
0.0266, 0.0304, 0.0237, -0.0014, 0.0105, -0.0131, 0.0211, 0.019, 
0.0117, 0.0139, 0.0088, 0.008, 0.0158, 0.0134, 0.0051, 0.0102, 
0.0181, -0.0058, -0.0262, 0.019, -0.0061, 0.0099, 0.0251, 0.0051, 
0.0054, 0.007, 0.0202, 0.0024, 0.0063, 0.0092, 0.0084, -0.0133, 
0.0159, -0.0209, 0.0119, -0.0073, -0.0145, 0.0066, 0.0241, -0.0055, 
0.0121), levloan = c(0.0259, -0.0038, 0.0051, 0.02, 0.0074, 0.007, 
0.0078, -0.0051, 0.0074, 0.0097, 0.0105, 0.0108, 0.0037, 0.004, 
0.0073, 0.0112, 0.0039, 0.0085, 0.0075, 0.0023, -0.0055, 0.0107, 
4e-04, 0.0029, 0.008, 0.0048, 0.0053, 0.0071, 0.0022, 0.0037, 
0.0023, 0.0061, 0.006, -4e-04, 0.0023, -0.0052, 0.0029, 0.0046, 
-0.011, 0.0026, 0.0141, 0.0039, 0.009), credspread = c(0.05574, 
-0.00988, 0.01485, 0.02278, 0.02392, 0.00749, -0.00097, -0.02579, 
0.01948, -0.00018, 0.01334, 0.01276, 0.00615, 0.00807, 0.01993, 
0.02829, -0.00406, 0.01133, 0.0038, 0.02031, -0.00586, 0.01919, 
6e-05, 0.00602, 0.01513, 0.01091, 0.01042, -0.01772, 0.01303, 
0.00285, -0.00332, -0.00426, 0.00978, -0.01329, -0.00099, -0.01186, 
-0.00011, -0.01911, -0.02021, -0.03175, 0.04686, -0.01138, 0.02561
), currencycarry = c(0.021062475, -0.01510466, -0.002568471, 
0.029768377, 0.014335471, -0.014083719, -0.009933662, -0.010161815, 
0.001161926, 0.012676299, -0.007048623, 0.014129712, -0.009839174, 
0.000927652, 0.004964735, 0.010078797, 0.001763516, 0.00049031, 
0.000233107, -0.008327544, -0.009982759, -0.01182212, -0.029795975, 
0.00789667, 0.02197623, -0.01552312, 0.001641685, 0.007155053, 
0.010889696, 0.025011893, 0.006287957, 0.0120996, 0.004137323, 
-0.001635221, 0.000110375, -0.017334, 0.003541158, -0.021319839, 
-0.019467795, -0.020674554, 0.003802379, 0.000400475, 0.014433966
), imomentum = c(-0.004403353, -3.33e-06, 0.013376403, 0.008653358, 
-0.003250802, -0.005295369, 0.013898289, 0.016544334, -0.005828146, 
0.023201725, -0.002242994, 0.001525072, -0.005433106, 0.007562014, 
-0.00098009, -0.020339584, 0.004229495, 0.003519499, 0.005542791, 
-0.02215749, -0.006503849, -0.005613635, -0.001321511, -0.012246198, 
0.003410418, 0.005240792, -0.012126027, -0.007282515, 0.002130221, 
-0.005992288, 0.003595541, 0.012406047, -0.000480213, -0.00314075, 
0.013796189, -0.002540042, 0.012370828, 0.01288304, 0.003736943, 
0.02385686, -0.011689422, 0.010875238, -0.00586643), ivol = c(-0.008756546, 
0.022062491, 0.020627291, 0.009312844, 0.000633063, -0.003435856, 
0.000345385, 0.000666571, 0.009168612, -0.001728971, -0.000571634, 
0.00122868, 0.004500645, 0.014763756, 0.002747161, 0.006826445, 
0.011710971, 0.014738192, 0.002767517, -0.012497923, -0.007810444, 
0.005048078, -0.009423152, 0.005640964, 0.012481504, -0.001246134, 
0.01351537, -0.015125967, 0.008597012, -0.004397712, 0.005754998, 
0.005006953, 0.007820208, 0.00502678, 0.008944444, 0.010496687, 
-0.006165902, 0.005716496, 8.7e-05, -0.008800933, 0.000972154, 
-0.000637234, 0.015004946), eqvol = c(0.017778146, -0.011036976, 
0.04774293, 0.045361848, 0.023476636, 0.007858721, 0.000901623, 
-0.007516241, 0.013560922, 0.003495402, 0.014795421, 0.006635647, 
0.015026735, 0.012240781, 0.0158832, 0.012140144, -0.012779041, 
0.01011242, -0.01824379, -0.003782807, -0.018099914, 0.014596332, 
-0.000580989, 0.011161269, 0.007933959, 0.009525722, -0.000136551, 
-0.012901828, 0.015041506, 0.000910669, 0.006547493, 0.014910784, 
0.018309346, -0.023053361, 0.014402915, 0.001254913, -0.025422441, 
0.006742115, -0.029483984, -0.027812499, 0.027158237, -0.021171841, 
-0.003716239)), .Names = c("Date", "ACWI", "Momentum", "Value", 
"Size", "highyield", "levloan", "credspread", "currencycarry", 
"imomentum", "ivol", "eqvol"), class = "data.frame", row.names = c(NA, 
-43L))

和Y var

> dput(Yvar)
structure(list(Date = structure(c(6L, 10L, 14L, 2L, 21L, 22L, 
26L, 30L, 33L, 36L, 39L, 42L, 7L, 11L, 15L, 3L, 18L, 23L, 27L, 
31L, 34L, 37L, 40L, 43L, 8L, 12L, 16L, 4L, 19L, 24L, 28L, 32L, 
35L, 38L, 41L, 44L, 9L, 13L, 17L, 5L, 20L, 25L, 29L, 1L), .Label = c("", 
"1/31/2012", "1/31/2013", "1/31/2014", "1/31/2015", "10/31/2011", 
"10/31/2012", "10/31/2013", "10/31/2014", "11/30/2011", "11/30/2012", 
"11/30/2013", "11/30/2014", "12/31/2011", "12/31/2012", "12/31/2013", 
"12/31/2014", "2/28/2013", "2/28/2014", "2/28/2015", "2/29/2012", 
"3/31/2012", "3/31/2013", "3/31/2014", "3/31/2015", "4/30/2012", 
"4/30/2013", "4/30/2014", "4/30/2015", "5/31/2012", "5/31/2013", 
"5/31/2014", "6/30/2012", "6/30/2013", "6/30/2014", "7/31/2012", 
"7/31/2013", "7/31/2014", "8/31/2012", "8/31/2013", "8/31/2014", 
"9/30/2012", "9/30/2013", "9/30/2014"), class = "factor"), Y1 = c(0.0041, 
0.0083, 0.0044, 0.0194, 0.014, 0.011, 0.0146, 0.0098, 0.0127, 
0.019, 0.0234, 0.0231, 0.0141, 0.0093, 0.0209, 0.0172, 0.0126, 
0.027, 0.0172, 0.0165, -0.0158, 0.0058, 0.0084, -0.0025, 0.0113, 
0.0208, 0.0067, 0.011, 0.0108, 0.0103, 0.0078, 0.0106, 0.0159, 
0.011, 0.004, 0.008, -0.0025, 0.001, 0.0115, 0.0049, 0.008, 0.009, 
0.0075, NA), Y2 = c(0.0099, 0.0012, 0.005, 0.0014, -0.0074, 0.0101, 
0.001, 0.0127, -0.0115, 0.0199, -0.0016, 0.0101, 0.0021, 0.0039, 
0.0127, 0.0183, -0.0097, 0.0093, 0.0425, 0.0218, -0.0205, -0.017, 
-7e-04, -0.0103, 0.0066, 0.0052, 0.0359, 0.0133, -0.0138, -0.0156, 
-0.0037, 0.0092, 0.0249, 0.0116, 0.002, 0.0365, 0.0365, 0.0258, 
-0.0051, 0.0114, 0.0193, 0.0295, -0.0107, NA), Y3 = c(0.134, 
-0.034, -0.022, 0.174, -0.035, 0.03, -0.018, -0.002, -0.019, 
-0.075, 0.018, 0.022, 0.043, 0.074, 0.03, 0.052, 0.007, 0.024, 
0.033, 0.045, -0.007, 0.058, -0.013, 0.026, 0.06, 0.043, 0.051, 
0.03, 0.056, -0.016, -0.035, 0.001, -0.009, 0.009, 0.011, -0.016, 
0.045, 0.057, 0.028, -0.007, -0.005, 0.027, -0.02525, NA), Y4 = c(-0.0017, 
-0.0177, -0.0141, 0.0022, 0.0073, -0.0078, 0.0169, -0.0309, 0.0035, 
-0.0146, 0.0201, -0.0093, -0.0179, 0.0155, 0.024, 0.023, -0.0018, 
0.0031, 0.0451, -0.0014, 0.0045, 0.0119, -0.0103, 0.0301, 0.0262, 
0.0102, 0.0132, -0.0234, 0.0153, -0.0471, -0.0036, 0.0227, 0.003, 
-0.0024, 0.0016, -0.0069, 0.021, 0.0195, 0.011, 0.0044, -0.007, 
2e-04, 0.0372, NA), Y5 = c(0.0367, -0.0226, 0.0224, 0.0278, 0, 
-0.003, 0.004, -0.028, -0.01, 0.036, 0.001, 0.028, -0.022, 0.008, 
0.028, 0.003, -0.027, 0.001, 0.019, 0.011, -0.047, 0.043, -0.004, 
0.029, 0.031, -0.0068, -0.0175, 0.0189, 0.0283, -0.005, 0.0278, 
0.0299, 0.0227, -0.0435, 0.021, -0.0133, -0.0135, 0.0205, -0.007849, 
0.121815, 0.043226, 0.043, -0.0457, NA), Y6 = c(-0.0016, 0.0135, 
-0.0019, -0.008, -0.0044, 7e-04, 0.0018, 0.008, -0.0057, 0.0044, 
0.0043, 0, -0.0024, 0.0051, 4e-04, 0.0047, 0.0121, 0.0092, -6e-04, 
0.0128, -0.007, 0.0117, 0.0122, 0.0113, 0.0018, 0.0024, 0.0031, 
0.0109, 0.0022, 0.0032, 0.0061, 0.0058, 0.006, 0.0041, 0.0043, 
0.0087, 0.0028, 0.0128, 0.0109, 0.0191, 0.0065, 0.0091, -0.0014, 
NA), Y7 = c(0.0098, -0.0034, 8e-04, 0.0147, 0.0142, 0.0032, 0.0017, 
-0.0015, 0.0084, 0.0032, 0.0103, 0.0179, 0.0074, 0.0038, 0.0164, 
0.0216, 0.0046, 0.0079, 0.0052, 0.0104, -0.0053, 0.004, 0.002, 
0.0106, 0.0116, 0.0063, 0.0115, 0.0126, 0.016, 0.0034, 0.005, 
0.0014, 0.0078, -0.0037, 0.0039, -0.0059, -0.007, 0.0054, -0.0109, 
-0.0177, 0.0104, 0.0051, 0.0035, NA)), .Names = c("Date", "Y1", 
"Y2", "Y3", "Y4", "Y5", "Y6", "Y7"), class = "data.frame", row.names = c(NA, 
-44L))

输出有望像这样格式化,每个36个月回归的第一组X变量标签下的回归系数和下一组的每个系数的标准误差,每个Y将有一个单独的表格,如下所示一个

> dput(Outputtemp)
structure(list(X = structure(c(45L, 9L, 13L, 2L, 17L, 21L, 25L, 
29L, 33L, 36L, 39L, 42L, 6L, 10L, 14L, 3L, 18L, 22L, 26L, 30L, 
34L, 37L, 40L, 43L, 7L, 11L, 15L, 4L, 19L, 23L, 27L, 31L, 35L, 
38L, 41L, 44L, 8L, 12L, 16L, 5L, 20L, 24L, 28L, 32L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("", "1/31/2009", "1/31/2010", "1/31/2011", 
"1/31/2012", "10/31/2009", "10/31/2010", "10/31/2011", "11/30/2008", 
"11/30/2009", "11/30/2010", "11/30/2011", "12/31/2008", "12/31/2009", 
"12/31/2010", "12/31/2011", "2/28/2009", "2/28/2010", "2/28/2011", 
"2/29/2012", "3/31/2009", "3/31/2010", "3/31/2011", "3/31/2012", 
"4/30/2009", "4/30/2010", "4/30/2011", "4/30/2012", "5/31/2009", 
"5/31/2010", "5/31/2011", "5/31/2012", "6/30/2009", "6/30/2010", 
"6/30/2011", "7/31/2009", "7/31/2010", "7/31/2011", "8/31/2009", 
"8/31/2010", "8/31/2011", "9/30/2009", "9/30/2010", "9/30/2011", 
"Start Date"), class = "factor"), X.1 = structure(c(45L, 6L, 
10L, 14L, 2L, 21L, 22L, 26L, 30L, 33L, 36L, 39L, 42L, 7L, 11L, 
15L, 3L, 18L, 23L, 27L, 31L, 34L, 37L, 40L, 43L, 8L, 12L, 16L, 
4L, 19L, 24L, 28L, 32L, 35L, 38L, 41L, 44L, 9L, 13L, 17L, 5L, 
20L, 25L, 29L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1/31/2012", 
"1/31/2013", "1/31/2014", "1/31/2015", "10/31/2011", "10/31/2012", 
"10/31/2013", "10/31/2014", "11/30/2011", "11/30/2012", "11/30/2013", 
"11/30/2014", "12/31/2011", "12/31/2012", "12/31/2013", "12/31/2014", 
"2/28/2013", "2/28/2014", "2/28/2015", "2/29/2012", "3/31/2012", 
"3/31/2013", "3/31/2014", "3/31/2015", "4/30/2012", "4/30/2013", 
"4/30/2014", "4/30/2015", "5/31/2012", "5/31/2013", "5/31/2014", 
"6/30/2012", "6/30/2013", "6/30/2014", "7/31/2012", "7/31/2013", 
"7/31/2014", "8/31/2012", "8/31/2013", "8/31/2014", "9/30/2012", 
"9/30/2013", "9/30/2014", "End Date"), class = "factor"), coeffecients = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"constant"), class = "factor"), X.2 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L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, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"Size"), class = "factor"), X.6 = structure(c(2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"highyield"), class = "factor"), X.7 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"levloan"), class = "factor"), X.8 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"credspread"), class = "factor"), X.9 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"currencycarry"), class = "factor"), X.10 = structure(c(2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"imomentum"), class = "factor"), X.11 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1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 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"Value"), class = "factor"), X.16 = structure(c(2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"Size"), class = "factor"), X.17 = structure(c(2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"highyield"), class = "factor"), X.18 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"levloan"), class = "factor"), X.19 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"credspread"), class = "factor"), X.20 = structure(c(2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"currencycarry"), class = "factor"), X.21 = structure(c(2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"imomentum"), class = "factor"), X.22 = structure(c(2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"ivol"), class = "factor"), X.23 = structure(c(2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"eqvol"), class = "factor"), X.24 = structure(c(2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"R2"), class = "factor")), .Names = c("X", "X.1", "coeffecients", 
"X.2", "X.3", "X.4", "X.5", "X.6", "X.7", "X.8", "X.9", "X.10", 
"X.11", "X.12", "standard.errors", "X.13", "X.14", "X.15", "X.16", 
"X.17", "X.18", "X.19", "X.20", "X.21", "X.22", "X.23", "X.24"
), class = "data.frame", row.names = c(NA, -80L))

我改编的代码看起来像这样

library(zoo)

Yvar <- as.zoo(Yvar)
time(Yvar) <- as.yearmon(time(Yvar))
Yvar <- merge(y = Yvar, y1 = lag(Yvar, k = -1),
y12 = lag(Yvar, k = -12), all = FALSE)

Xvar <- as.zoo(Xvar)
time(Xvar) <- as.yearmon(time(Xvar))
Xvar <- merge(x = Xvar, x1 = lag(Xvar, k = -1),
x12 = lag(Xvar, k = -12), all = FALSE)

rr <- rollapply(Yvar, width = 36,
FUN = function(z) coef(lm(Yvar ~ Xvar, data = as.data.frame(z))),
by.column = FALSE, align = "right")

我一直收到错误

2 个答案:

答案 0 :(得分:2)

如果没有更多数据,很难给出一个超级具体的例子。

您可能希望使用包rollapply

中的zoo函数

以下是rollapply功能帮助页?rollapply中的示例:

library(zoo)

获取数据:

seat <- as.zoo(log(UKDriverDeaths))
time(seat) <- as.yearmon(time(seat))
seat <- merge(y = seat, y1 = lag(seat, k = -1),
  y12 = lag(seat, k = -12), all = FALSE)

运行回归:

rr <- rollapply(seat, width = 36,
  FUN = function(z) coef(lm(y ~ y1 + y12, data = as.data.frame(z))),
  by.column = FALSE, align = "right")

答案 1 :(得分:0)

所以我想出如何进行回归的方式是这样的。我将数据格式化为动物园矩阵。

library(zoo)

Yvar.nd <- Yvar[,-1]

Yvar.mt <- as.matrix(Yvar.nd)

Yvar.mt.zoo <- zoo(Yvar.mt)


#Steprollbig big model rolling no coef coercsion
steprollbig4 <- rollapply(Yvar.mt.zoo, width = 36, function(x) step(lm(Yvar ~ ACWI + Momentum + Value + Size + highyield + levloan + credspread + Carry.Trade + IR.Momentum + IR.Vol + EQ.Vol, direction = "both", data = as.data.frame(x))), by.column = FALSE, align = "right")

#Into data frame
steproll <- as.data.frame(steprollbig4)

#call coefficients
steproll$coefficients

这会产生每个窗口的所有系数和标签