如何使用 R 对数据集的起点执行线性回归
How to perform linear regression on the starting points of a dataset using R
如何使用日期约束执行回归?我只想对数据集的 "non-zero" 部分进行回归。
主要问题是第 2 列和第 3 列的起始位置不同 Dates
并且我编写了一个循环,它将执行从起点 (2012-01-01) 到 2013-12- 的回归31,在每次迭代时增加 6 个月的间隔。
DATES <- c("201201/201206", "201201/201212", "201201/201306", "201201/201312")
我如何执行回归,以便在给定迭代时使用第 2 列和第 3 列的起点?
这是 RETS
数据集:
RETS <- structure(c(0.0159, 0.0016, 0.0027, -0.0026, 0.0024, 0.0086,
6e-04, 0.0024, -0.0052, 0.0039, 0.0111, 0.0053, 0.0037, -0.0026,
-0.0011, 0.0083, -0.0051, -4e-04, -0.0035, -4e-04, 0.0088, 0.0015,
0.014, -6e-04, 0.0025, 0.0029, 0.0013, -0.0074, 0.0074, -0.0013,
-0.0046, 0.0111, 0.0027, 5e-04, -0.0033, 0.0045, 0.0022, 0.0017,
0.0029, -0.0039, 0.0051, -0.003, -0.0041, -0.0146, 0.007, 0.0099,
0.0039, 1e-04, 0.0181, -0.0011, 0.0059, 0.0014, 0.0039, -0.0029,
-0.0017, -0.0072, 0.0033, 0.014, -0.0031, -0.0049, -0.0017, 0.0042,
0.0073, -0.004, -0.01, -5e-04, -0.0113, -0.0168, 0.0081, 0.0131,
-0.0119, -6e-04, 0.0148, -0.0034, -0.0064, 0.0017, -0.0084, 0.0038,
0.0137, 0.007, 0.0016, -0.0037, 0.0063, -0.003, -0.0076, -0.0162,
8e-04, -0.004, -0.006, 0.0021, -0.003, -0.0111, -0.0058, -0.0038,
-0.0148, -0.0086, 0.0172, 0.0018, 5e-04, 0.002, -0.0033, 0.0121,
-0.0145, -0.0022, -0.0252, -5e-04, 0.0076, 0.0225, 6e-04, 0.0079,
-0.0127, 0.0115, -0.0065, 0.0107, 0.0102, 0.0019, 0.0097, -0.0016,
-0.0225, 0.0077, -0.016, 0.0051, 0.009, -0.0029, 0.025, 0.003,
0.0066, -0.0045, -0.0095, -0.0012, -0.0087, 2e-04, -0.0049, 0.0168,
-0.0024, 0.0068, 0.0074, 0.0027, -0.0092, -0.0101, -0.0086, 2e-04,
0.0164, 0.0185, 0, -0.007, -9e-04, -0.0069, 0.0198, 0.002, 0.0051,
0.0012, 8e-04, 0.0016, -5e-04, 1e-04, 0.0011, 0.0074, 0.0013,
1e-04, -0.003, 4e-04, -0.0082, 0.0061, 1e-04, -0.001, 8e-04,
-0.0072, 0.0047, -9e-04, -8e-04, 0.0203, 0.0039, -0.0057, 0.0028,
0.0034, 0.0152, 0.0045, -0.0034, -8e-04, 5e-04, 1e-04, -4e-04,
-0.0015, -0.0106, -0.0056, 0.0094, -0.0046, 0.0026, 0.0011, 0.0041,
0.0071, 1e-04, -0.0034, -0.0099, -0.0064, 6e-04, -0.0033, 0.0084,
0.0101, 0.0045, -0.0026, -0.0167, 1e-04, -0.0139, -0.0029, 0.003,
-6e-04, 0, 0.0104, -0.0089, 0.0021, 0.0078, -0.0227, -0.012,
8e-04, 8e-04, -0.0035, -0.0135, -0.0017, 0.0049, 0.0202, 5e-04,
0.0019, 0.0136, -0.0021, -0.0051, 0.008, 0.0047, 2e-04, -0.0049,
-0.0015, 0.0018, 0.0034, 0.003, 4e-04, 0.0068, 5e-04, -0.0061,
-0.0037, 0.0117, 0.0111, -0.0074, 0.0057, -0.0091, -0.0031, -0.0042,
-0.0013, -0.0108, 0.017, 0.0256, -0.0023, 0.0044, -0.0027, -0.0029,
0.0026, 0.008, -1e-04, -7e-04, 7e-04, -1e-04, 0.0064, 0.0022,
0.0054, 0.0016, 3e-04, 0.0056, -0.0012, 0.004, -0.0039, -0.0025,
0.0104, -0.0113, 0.0101, 8e-04, -0.0013, 0.0055, -2e-04, 0.0017,
8e-04, 9e-04, -0.0012, 0.0075, -0.0125, -0.006, 0.0097, -0.019,
0.0068, 0.0126, -0.002, 0.0033, 0.0053, 0.009, 0.0013, 0.0018,
0.0042, 0.0038, -0.0023, 0.0015, 0.0053, -0.0013, -0.0055, -0.0023,
0.007, -0.0086, 0.0081, -0.0042, 0.008, 0, 0.0031, -0.004, 0.0049,
-0.0101, 0.004, -0.0045, 0.0067, 0.0034, 0.0123, 0.0033, -0.0025,
-0.0231, 0.0148, -0.0146, -0.0063, 0.0087, 0.0044, 0.0103, 6e-04,
0.0041, -0.0018, 0.0067, 0.0023, -0.0088, 0.0093, 0.0101, 0.0026,
0.0051, 0.0046, -0.0029, 0.0032, 8e-04, 0.0103, 0.0054, -0.0047,
0.0096, -1e-04, 0.0015, -0.0074, -0.0029, -8e-04, 0.006, -0.0065,
0.0037, -0.0144, 0.0055, -0.0048, -0.014, 0.0091, 0.0127, 0,
-0.0103, -0.0083, 0.0152, -0.0063, 0.0077, 0.0079, -0.0138, -0.0248,
0.0032, -0.0127, 0.0096, 0.0099, 0.0059, -0.0041, 0.0058, -0.001,
4e-04, 0.0108, 0.0057, 0.0072, 4e-04, 0.0136, 4e-04, 0.0038,
-0.0037, 0.0026, 0.0055, 0.0018, 0.0019, -0.0021, -0.0037, 0.0024,
0.001, -0.003, 0, 7e-04, 0.0116, 0.0017, -0.0014, -0.0057, -0.0032,
0.0037, -0.0029, -0.0012, 0.003, -0.0052, -0.014, -0.0033, -0.0064,
0.0049, -0.0062, 0.0091, 0.0034, -0.0038, -0.0161, 0.0035, 0.0016,
-0.0032, 0.0045, 0.0083, 0.0012, 5e-04, 0.0096, 0.0074, 0.0031,
-0.0026, 0.0023, 0.0058, 0.0045, 0.0116, -0.0017, -0.007, -0.0046,
-0.0024, -0.0029, 0.0039, -0.0046, -0.0053, 0.0079, -0.001, -0.0092,
0.0076, -0.0087, -0.0116, 7e-04, 0.0215, 0.0065, 0.004, -0.0073,
0.014, 0.0067, 0.0067, 1e-04, 0.0058, -0.0048, 0.0033, 0.0046,
0.0016, 0.0054, -0.005, -0.0028, 0.0024, 0.0035, -0.0031, 0.0051,
-0.0127, 0.0135, 2e-04, -0.002, 0.0081, 0.0049, 0.0044, -0.0035,
-0.0022, -0.0031, 0.0081, 0.005, -0.001, 3e-04, 0.0024, -7e-04,
-0.0026, -0.0043, -1e-04, -0.0044, 0.0112, 0.0025, -0.0036, -0.0112,
-0.0033, -1e-04, 0.0063, -0.0032, 0.0171, -0.0012, 0.0058, 0.0053,
0.0022, 0.0051, -1e-04, -2e-04, 0.0047, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, -0.1099, -0.089, 0.0323, 0.0322, -0.0339, -0.0962,
-0.0225, 0.05, -0.0635, -0.0296, -0.0383, 0.0363, -0.0186, 0.03,
-0.0033, 0.0144, -0.0047, 0.0374, 0.0608, 0.0467, 0.0159, -0.0097,
0.0076, 0.038, -0.03, 0.0324, -0.0263, -0.027, -0.0083, -0.0106,
0.014, 0.0087, 0.0083, 0.0139, -0.0218, -0.0159, -0.0052, -0.0029,
-0.0804, -0.0057, 0.0363, -0.0038, -0.0083, -3e-04, -0.0104,
0.0313, -0.0849, -0.1169, -0.0236, -0.0622, -0.0382, -0.0402,
0.0524, 0.0394, -0.0547, 0, 0.014, 0.0381, -0.0096, -0.0565,
0.0402, -0.0627, -0.0413, 0.0504, -0.0425, 0.0146, 0, -0.0015,
-0.0134, 0.0099, -0.0124, -5e-04, -0.054, -0.0183, 0.0479, 0.0205,
0.0011, -0.009, 0.033, 0.0772, -0.0105, 0.0623, -0.0218, 0.0163,
0.0649, -0.0301, 0.012, -0.0906, -0.0245, 0.0168, -0.0145, 0.0659,
0.0152, 0.0127, -0.0198, 0.0055, -0.0474, -0.0244, -0.0083, -0.0292,
0.0056, -0.0116, 0, -0.002, 0.0205, -0.0453, 0.0011, 0.0168,
0.0093, 0.1913, -0.0288, -0.0275, -0.0378, 0.0047, -0.0014, 0.0033,
-0.0038, -0.0331, -0.0234, -0.039, 0.0448, -0.0105, 0.1259, -0.0085,
0.0627, -0.0272, 0.0079, 0.0528, -0.0132, 0.0808, 0.0081, 0.008,
0.0364, 0.0249, -0.0343, 0.0155, 0.0091, -0.0267, 0.0193, 0.0127,
0.005, -0.0143, 0.0239, -0.0506, -0.0022, 0.0359, -0.0108, -0.0018,
-0.0402, 0.0255, -0.0156, -0.0174, -0.0054, 0.0274, 0.0518, -0.0082,
0.0356, 0.0229, -0.0122, 0.0526, 0.0232, 0.0134, -0.0243, -0.0275,
-0.0083, 0.0097, -0.0159, 0.0361, 0.0029, 0.0084, 0.0148, 0.0295,
-0.0517, 0.0146, -0.0083, -0.0403, -0.0545, 0.0189, 0.0143, -0.0138,
-0.0035, -0.0102, -0.0315, 0.0197, 0.0211, -0.0063, 0.0215, -0.0162,
-0.0415, -0.0055, 0.0052, 0.0044, -0.019, 0.0141, 0.0194, -0.0022,
-0.0072, -0.0025, 0.0412, -0.0217, 0.0064, -0.011, -0.0269, -0.0015,
-0.0144, -0.006, 0.0023, -0.026, -0.0046, -4e-04, -0.0233, 0.0032,
0.0349, -0.0195, -0.002, -0.0043, 0.0327, 0.0312, 0.0118, -0.0197,
-0.0097, 0.0369, 0.0163, -0.0221, -0.0321, 0.0151, -0.0108, -0.0353,
0.0016, 0.0093, 4e-04, 0.005, 0.0011, 0.0272, 0.0048, 0.0293,
-0.0122, 0.0561, -0.0228, -0.0261, -0.0247, 0.0086, -0.0029,
-0.0133, 0.0052, 0.0093, -0.0174, -0.0177, 0.0046, -0.0187, -0.0039,
-0.0195, -0.004, -0.0299, -0.0086, -0.0324, 0.0527, -0.0081,
-0.0205, -0.0138, -0.0264, 0.0031, 0.0139, 0.0447, -0.0123, -0.0108,
-0.0017, -0.0042, 0.0165, 0.0079, 0.0041, -0.0169, 0.0264, -0.0241,
0.0129, -0.0037, 0.0207, 0.0089, -0.0028, -0.0161, 0.0045, -0.0061,
0.014, 0.0312, 0.0126, 4e-04, 0.0039, 0.0143, 0.0015, 0.0125,
-0.0176, -0.0115, 0.0066, 0.0031, 0.0145, 0.2961, -0.0102, 0.0418,
0.0621, -0.0221, 0.0188, 0.0149, 0.03, -0.0163, 0.0083, -0.0085,
-0.001, -0.0073, -0.0314, -0.01, -0.0025, 0.0142, 0.0197, 0.0159,
-0.0023, 0.006, 0.0519, 0.0195, -0.0411, 0.023, 0.018, 2e-04,
0.014, -0.0021, 0.0211, 0.0302, 0.002, -0.01, 0.033, -0.0064,
-0.0098, -0.0406, 0.0602, 0.0036, 0.0166, 0.0328, -0.0063, 0.0267,
0.0208, 0.0188, 0.0169, -0.0197, 0.0038, -0.0028, -0.0219, 0.0378,
-0.0102, -0.0669, -0.0078, 0.0487, 0.0012, 0.0081, -2e-04, 0.0331,
0.0209, 0.0385, -0.0068, -0.0217, -0.0148, 0.0106, -0.0095, -0.0331,
-0.0165, -0.0079, 0.0245, -0.0092, -0.0308, 0.0392, -0.0198,
-0.0318, -6e-04, -0.028, 0.0089, 0.0451, 0.0057, 4e-04, -0.0649,
0.0116, 0.0015, 0.0058, -0.0101, -0.0305, 0.0239, 0.0131, 0.0112,
0.0011, -0.007, 0.0404, -0.0058, -0.0083, 0.0188, 0.0289, -0.0173,
0.0496, 0.0287, 0.0092, 0.0195, 0.0129, -0.0094, 0.0013, 0.0481,
0.0033, -0.004, -0.0397, -0.0312, 0.0175, 0.0154, 0.0054, 0.0111,
0.0105, -0.0016, 0.0035, -0.0016, -0.0028, -0.0037, 0.0117, 0.0103,
-0.0031, -0.0173, 0.0168, -0.0163, 0.0625, -0.0047, 0.006, 0.0128,
0.0077, -7e-04, -0.0023, 0.0099, 0.0094, 0.0106, 0.0166, 0.0347,
5e-04, 0.0186, 0.0136, -0.0232, 0.0091, -1e-04, 0.0254, -0.0036,
0.0065, 0.0118, 0.0064, 0.0183, 0.013, 0.0038, 0.0012, -0.0221,
-0.0054, 8e-04, 0.0213, 0.0059, 0.0125, 0.0292, 0.0379, -0.0069,
1e-04, 0.0265, 0.008, -0.0056, -0.0053, -0.0054, 0.0183, 0.0123,
0.0051, -0.0125, -0.0169, 0.0318, 0.0173, -0.0079, 0.015, 0.004,
-0.0122, -0.0035, -0.0055, -0.0281, -0.0416, 0.051, -0.0023,
-0.0343, -0.0246, -0.0022, -0.02, 0.0887, -0.0038, -0.0078, -0.0315,
-0.0032, 0.0066, -0.0071, -0.0284, 0.0075, -0.0023, 0.0018, 0.0023,
-0.0068, -0.0149, -0.009, -0.0129, -0.0292, 6e-04, 0.0582, -0.0077,
0.0244, -0.0091, -0.0054, 0.0177, 0.0121, -0.0024, -0.029, 0.006,
-0.0026, 0.0153, 5e-04, 0.0151, -0.0157, 0.0087, -0.007, -0.0012,
0.0045, 0.0203, 0.0028, -0.0028, -0.0138, 0.0077, -0.0195, 0.0022,
0.0043, -0.0094, 0.0262, 0.0146, 0.0116, 0.0176, -0.0067, 0.0132,
-0.0092, -0.0062, -0.0092, 0.0102, 0.0032, 1e-04, -0.0012, 0.0133,
-0.0163, -7e-04, -0.0049, -0.0431, -1e-04, 0.0178, 0.0169, 0.0264,
-0.0064, 0.0016, 0.0131, 0.0111, -0.0026, -0.0017, 0.0056, 0.0015,
0.0134, 0.0027, -0.0014, 0.0088, 0.0185, 0.0262, -0.0137, 0.0196,
-0.0094, 9e-04, 0.0188, -0.0013, -0.002, -0.0143, 0.0021, 0.0146,
-0.007, 0.009, 0.0061, -0.026, -0.0032, 0.0139, 0.0197, 0.0121,
0.0123, 0.0031, 2e-04, -0.0048, 0.002, -0.0133, -0.0249, -0.0124,
0.0242, -0.0208, -0.0116, 0.0029, 0.0154, -0.0069, -0.0214, -0.0221,
-0.0036, 0.0079, -0.0199, 0.0026, 0.008, 0.0237, -0.0081, -0.0186,
-0.036, 0.0396, -0.0325, 0.0057, -0.0119, -0.0091, -0.0144, 0.0021,
-0.0331, 0.0136, -0.003, -0.0383, -0.0363, 0.0172, -0.0077, 1e-04,
-0.0111, -0.021, 0.0039, 0.0721, -0.0086, 0.0014, 0.0174, 0.0317,
-0.0081, -0.0031, 0.011, -0.0069, 0.0015, -0.0176, -0.0644, 0.0157,
-0.0257, -0.0063, 0.0218, -0.0043, -0.0173, -0.0376, 0.0177,
0.0291, -0.0142, -0.0087, -0.0047, 0.0017, -0.0139, 0.004, -0.0106,
0.0443, 0.0317, -0.0126, -0.028, -0.0059, 0.0027, -0.0155, 0.0124,
-0.0062, -0.0356, -0.0315, 0.0415, -0.0067, -0.0054, 0.0096,
0.0184, -0.1236, -0.0235, 0.0226, 0.0188, -0.0032, -0.0029, -0.004,
-0.025, 0.0351, -0.001, 0.0296, 0.0145, 0.0105, -0.0251, -0.0019,
-9e-04, -0.0138, -3e-04, -0.0242, -0.0064, 0.0107, -0.0177, 0.0139,
-0.0098, -0.0071, -0.0249, -0.0241, 0.0263, -0.0127, 0.0117,
0.0025, 0.0142, -0.0215, -2e-04, 0.0097, 0.0259, 0.0272, -0.0027,
-0.0053, 0.0015, 0.0202, 0.0036, -0.0052, -0.0197, -0.0209, -0.0311,
0.002, 0.0051, -0.0098, -0.0106, 0.0071, 0.0019, 0.0204, -0.0032,
-0.0104, -0.0231, 0.0152, -0.0549, -0.0267, -0.0039, 0.0208,
0.0187, -0.0016, 0.0072, 0.0217, 0.0309, 0.0296, -0.0079, 0.0142,
0.01, 0.0239, -0.0044, 0.0113, -0.0088, -0.0083, 0.0038, -0.0238,
-0.0339, 0.0134, -0.003, 0.0223, -0.0074, 0.0038, 0.0018, 0.0067,
-0.0083, 0.0081, 0.0149, -0.0042, 0.0023, -0.0031, -0.0094, -0.015,
0.0076, -0.0066, -0.003, -0.0123, 0.0087, -0.0137, 0.0046, -5e-04,
-0.0204, -0.0144, -0.008, -0.0265, 2e-04, -0.0114, -0.0108, 0.007,
0.0321, 0.0225, 0.0056, -0.0079, -0.0057, 0.0175, -0.0038, 0.0155,
-0.0017, 0.002, 0.0065, 3e-04, 0.0034, -0.0158, 0.0032, -0.0172,
0.0513, -0.0045, 0.0056, 0.0155, 0.0123, -0.0018, 0.0092, 0.0129,
0.0149, -0.009, -6e-04, -0.002, -0.0141, 0.0283, 0.0476, 0.0183,
-0.0012, 0.0088, 0.0108, -0.0131, 0.0024, 0.0013, -0.0039, 0.0039,
-0.0286, 0.0047, 0.0016, -0.0091, 0.0027, 0.0208, -0.0069, 0.006,
0.0159, -0.0228, -0.0544, 0.0106, -0.0165, -0.0317, 0.0115, 0.0204,
0.0165, -0.0104, 0.0497, -0.0031, -0.0155, 0.0097, -0.0071, -0.0124,
0.0235, 0.0033, -0.0125, -7e-04, 0.0097, -0.0139, 0.0117, 0.0062,
0.0065, 0.0066, 0.0052, 0.0049, 0.0068, 0.0087, 0.0245, -0.0029,
0.0099, 0.0132, -0.0113, 0.0075, -0.0249, 0.0159, -0.0043, -0.0051,
0.0129, -0.0025, -0.0027, -0.0163, 0.0158, -0.0029, 0.0018, 0.0012,
0.0145, -0.0061, -0.012, 0.0018, -0.0088, 0.012, -0.0026, 0.0076,
0.0185, 0.0235, 0.0185, -0.0088, 0.0273, -0.0023, 0.0051, -0.0138,
0.0114, -0.0015, -0.0075, -0.0014, -0.0109, 0.0054, -0.0045,
-0.0075, -0.0115, 0.0083, 0.0384, -0.0043, -0.0066, -0.0067,
-0.01, 0.0118), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1424069985.73567, class = c("POSIXct",
"POSIXt")), ret_type = "discrete", index = structure(c(1325548800,
1325635200, 1325721600, 1325808000, 1326067200, 1326153600, 1326240000,
1326326400, 1326412800, 1326758400, 1326844800, 1326931200, 1327017600,
1327276800, 1327363200, 1327449600, 1327536000, 1327622400, 1327881600,
1327968000, 1328054400, 1328140800, 1328227200, 1328486400, 1328572800,
1328659200, 1328745600, 1328832000, 1329091200, 1329177600, 1329264000,
1329350400, 1329436800, 1329782400, 1329868800, 1329955200, 1330041600,
1330300800, 1330387200, 1330473600, 1330560000, 1330646400, 1330905600,
1330992000, 1331078400, 1331164800, 1331251200, 1331510400, 1331596800,
1331683200, 1331769600, 1331856000, 1332115200, 1332201600, 1332288000,
1332374400, 1332460800, 1332720000, 1332806400, 1332892800, 1332979200,
1333065600, 1333324800, 1333411200, 1333497600, 1333584000, 1333929600,
1334016000, 1334102400, 1334188800, 1334275200, 1334534400, 1334620800,
1334707200, 1334793600, 1334880000, 1335139200, 1335225600, 1335312000,
1335398400, 1335484800, 1335744000, 1335830400, 1335916800, 1336003200,
1336089600, 1336348800, 1336435200, 1336521600, 1336608000, 1336694400,
1336953600, 1337040000, 1337126400, 1337212800, 1337299200, 1337558400,
1337644800, 1337731200, 1337817600, 1337904000, 1338249600, 1338336000,
1338422400, 1338508800, 1338768000, 1338854400, 1338940800, 1339027200,
1339113600, 1339372800, 1339459200, 1339545600, 1339632000, 1339718400,
1339977600, 1340064000, 1340150400, 1340236800, 1340323200, 1340582400,
1340668800, 1340755200, 1340841600, 1340928000, 1341187200, 1341273600,
1341446400, 1341532800, 1341792000, 1341878400, 1341964800, 1342051200,
1342137600, 1342396800, 1342483200, 1342569600, 1342656000, 1342742400,
1343001600, 1343088000, 1343174400, 1343260800, 1343347200, 1343606400,
1343692800, 1343779200, 1343865600, 1343952000, 1344211200, 1344297600,
1344384000, 1344470400, 1344556800, 1344816000, 1344902400, 1344988800,
1345075200, 1345161600, 1345420800, 1345507200, 1345593600, 1345680000,
1345766400, 1346025600, 1346112000, 1346198400, 1346284800, 1346371200,
1346716800, 1346803200, 1346889600, 1346976000, 1347235200, 1347321600,
1347408000, 1347494400, 1347580800, 1347840000, 1347926400, 1348012800,
1348099200, 1348185600, 1348444800, 1348531200, 1348617600, 1348704000,
1348790400, 1349049600, 1349136000, 1349222400, 1349308800, 1349395200,
1349654400, 1349740800, 1349827200, 1349913600, 1.35e+09, 1350259200,
1350345600, 1350432000, 1350518400, 1350604800, 1350864000, 1350950400,
1351036800, 1351123200, 1351209600, 1351641600, 1351728000, 1351814400,
1352073600, 1352160000, 1352246400, 1352332800, 1352419200, 1352678400,
1352764800, 1352851200, 1352937600, 1353024000, 1353283200, 1353369600,
1353456000, 1353628800, 1353888000, 1353974400, 1354060800, 1354147200,
1354233600, 1354492800, 1354579200, 1354665600, 1354752000, 1354838400,
1355097600, 1355184000, 1355270400, 1355356800, 1355443200, 1355702400,
1355788800, 1355875200, 1355961600, 1356048000, 1356307200, 1356480000,
1356566400, 1356652800, 1356912000, 1357084800, 1357171200, 1357257600,
1357516800, 1357603200, 1357689600, 1357776000, 1357862400, 1358121600,
1358208000, 1358294400, 1358380800, 1358467200, 1358812800, 1358899200,
1358985600, 1359072000, 1359331200, 1359417600, 1359504000, 1359590400,
1359676800, 1359936000, 1360022400, 1360108800, 1360195200, 1360281600,
1360540800, 1360627200, 1360713600, 1360800000, 1360886400, 1361232000,
1361318400, 1361404800, 1361491200, 1361750400, 1361836800, 1361923200,
1362009600, 1362096000, 1362355200, 1362441600, 1362528000, 1362614400,
1362700800, 1362960000, 1363046400, 1363132800, 1363219200, 1363305600,
1363564800, 1363651200, 1363737600, 1363824000, 1363910400, 1364169600,
1364256000, 1364342400, 1364428800, 1364774400, 1364860800, 1364947200,
1365033600, 1365120000, 1365379200, 1365465600, 1365552000, 1365638400,
1365724800, 1365984000, 1366070400, 1366156800, 1366243200, 1366329600,
1366588800, 1366675200, 1366761600, 1366848000, 1366934400, 1367193600,
1367280000, 1367366400, 1367452800, 1367539200, 1367798400, 1367884800,
1367971200, 1368057600, 1368144000, 1368403200, 1368489600, 1368576000,
1368662400, 1368748800, 1369008000, 1369094400, 1369180800, 1369267200,
1369353600, 1369699200, 1369785600, 1369872000, 1369958400, 1370217600,
1370304000, 1370390400, 1370476800, 1370563200, 1370822400, 1370908800,
1370995200, 1371081600, 1371168000, 1371427200, 1371513600, 1371600000,
1371686400, 1371772800, 1372032000, 1372118400, 1372204800, 1372291200,
1372377600, 1372636800, 1372723200, 1372809600, 1372982400, 1373241600,
1373328000, 1373414400, 1373500800, 1373587200, 1373846400, 1373932800,
1374019200, 1374105600, 1374192000, 1374451200, 1374537600, 1374624000,
1374710400, 1374796800, 1375056000, 1375142400, 1375228800, 1375315200,
1375401600, 1375660800, 1375747200, 1375833600, 1375920000, 1376006400,
1376265600, 1376352000, 1376438400, 1376524800, 1376611200, 1376870400,
1376956800, 1377043200, 1377129600, 1377216000, 1377475200, 1377561600,
1377648000, 1377734400, 1377820800, 1378166400, 1378252800, 1378339200,
1378425600, 1378684800, 1378771200, 1378857600, 1378944000, 1379030400,
1379289600, 1379376000, 1379462400, 1379548800, 1379635200, 1379894400,
1379980800, 1380067200, 1380153600, 1380240000, 1380499200, 1380585600,
1380672000, 1380758400, 1380844800, 1381104000, 1381190400, 1381276800,
1381363200, 1381449600, 1381708800, 1381795200, 1381881600, 1381968000,
1382054400, 1382313600, 1382400000, 1382486400, 1382572800, 1382659200,
1382918400, 1383004800, 1383091200, 1383177600, 1383264000, 1383523200,
1383609600, 1383696000, 1383782400, 1383868800, 1384128000, 1384214400,
1384300800, 1384387200, 1384473600, 1384732800, 1384819200, 1384905600,
1384992000, 1385078400, 1385337600, 1385424000, 1385510400, 1385683200,
1385942400, 1386028800, 1386115200, 1386201600, 1386288000, 1386547200,
1386633600, 1386720000, 1386806400, 1386892800, 1387152000, 1387238400,
1387324800, 1387411200, 1387497600, 1387756800, 1387843200, 1388016000,
1388102400, 1388361600, 1388448000), tzone = "UTC", tclass = "Date"), .Dim = c(502L,
3L), .Dimnames = list(NULL, c("SPY", "FB", "AAPL")))
这是我用来执行回归的循环。由于 RETS
中有 3 个资产,DATES
中有 4 个不同的日期,它将 return 12
回归存储在 BETA
中
BETA <- data.frame()
ALL <- data.frame()
for (k in 1:length(DATES)){
for (i in 1:ncol(RETS)){
rets <- RETS[DATES[k]]
MOD <- lm(rets$SPY ~ rets[,i])
COEF <- cbind(coef(MOD)[1],coef(MOD)[2])
BETA <- rbind(BETA,COEF)
}
ALL <- merge(ALL, BETA)
}
用 NA 替换零。在这种情况下,lm
的默认 na.action = na.omit
参数将自动删除它们。
RETS.na <- replace(RETS, RETS == 0, NA)
或者,如果您想删除任何列中有 NA
的所有行,则:
RETS.na <- na.omit(RETS.na)
要获得 DATES[1]
的分数子集,请说:
RETS.na[ DATES[1] ]
如何使用日期约束执行回归?我只想对数据集的 "non-zero" 部分进行回归。
主要问题是第 2 列和第 3 列的起始位置不同 Dates
并且我编写了一个循环,它将执行从起点 (2012-01-01) 到 2013-12- 的回归31,在每次迭代时增加 6 个月的间隔。
DATES <- c("201201/201206", "201201/201212", "201201/201306", "201201/201312")
我如何执行回归,以便在给定迭代时使用第 2 列和第 3 列的起点?
这是 RETS
数据集:
RETS <- structure(c(0.0159, 0.0016, 0.0027, -0.0026, 0.0024, 0.0086,
6e-04, 0.0024, -0.0052, 0.0039, 0.0111, 0.0053, 0.0037, -0.0026,
-0.0011, 0.0083, -0.0051, -4e-04, -0.0035, -4e-04, 0.0088, 0.0015,
0.014, -6e-04, 0.0025, 0.0029, 0.0013, -0.0074, 0.0074, -0.0013,
-0.0046, 0.0111, 0.0027, 5e-04, -0.0033, 0.0045, 0.0022, 0.0017,
0.0029, -0.0039, 0.0051, -0.003, -0.0041, -0.0146, 0.007, 0.0099,
0.0039, 1e-04, 0.0181, -0.0011, 0.0059, 0.0014, 0.0039, -0.0029,
-0.0017, -0.0072, 0.0033, 0.014, -0.0031, -0.0049, -0.0017, 0.0042,
0.0073, -0.004, -0.01, -5e-04, -0.0113, -0.0168, 0.0081, 0.0131,
-0.0119, -6e-04, 0.0148, -0.0034, -0.0064, 0.0017, -0.0084, 0.0038,
0.0137, 0.007, 0.0016, -0.0037, 0.0063, -0.003, -0.0076, -0.0162,
8e-04, -0.004, -0.006, 0.0021, -0.003, -0.0111, -0.0058, -0.0038,
-0.0148, -0.0086, 0.0172, 0.0018, 5e-04, 0.002, -0.0033, 0.0121,
-0.0145, -0.0022, -0.0252, -5e-04, 0.0076, 0.0225, 6e-04, 0.0079,
-0.0127, 0.0115, -0.0065, 0.0107, 0.0102, 0.0019, 0.0097, -0.0016,
-0.0225, 0.0077, -0.016, 0.0051, 0.009, -0.0029, 0.025, 0.003,
0.0066, -0.0045, -0.0095, -0.0012, -0.0087, 2e-04, -0.0049, 0.0168,
-0.0024, 0.0068, 0.0074, 0.0027, -0.0092, -0.0101, -0.0086, 2e-04,
0.0164, 0.0185, 0, -0.007, -9e-04, -0.0069, 0.0198, 0.002, 0.0051,
0.0012, 8e-04, 0.0016, -5e-04, 1e-04, 0.0011, 0.0074, 0.0013,
1e-04, -0.003, 4e-04, -0.0082, 0.0061, 1e-04, -0.001, 8e-04,
-0.0072, 0.0047, -9e-04, -8e-04, 0.0203, 0.0039, -0.0057, 0.0028,
0.0034, 0.0152, 0.0045, -0.0034, -8e-04, 5e-04, 1e-04, -4e-04,
-0.0015, -0.0106, -0.0056, 0.0094, -0.0046, 0.0026, 0.0011, 0.0041,
0.0071, 1e-04, -0.0034, -0.0099, -0.0064, 6e-04, -0.0033, 0.0084,
0.0101, 0.0045, -0.0026, -0.0167, 1e-04, -0.0139, -0.0029, 0.003,
-6e-04, 0, 0.0104, -0.0089, 0.0021, 0.0078, -0.0227, -0.012,
8e-04, 8e-04, -0.0035, -0.0135, -0.0017, 0.0049, 0.0202, 5e-04,
0.0019, 0.0136, -0.0021, -0.0051, 0.008, 0.0047, 2e-04, -0.0049,
-0.0015, 0.0018, 0.0034, 0.003, 4e-04, 0.0068, 5e-04, -0.0061,
-0.0037, 0.0117, 0.0111, -0.0074, 0.0057, -0.0091, -0.0031, -0.0042,
-0.0013, -0.0108, 0.017, 0.0256, -0.0023, 0.0044, -0.0027, -0.0029,
0.0026, 0.008, -1e-04, -7e-04, 7e-04, -1e-04, 0.0064, 0.0022,
0.0054, 0.0016, 3e-04, 0.0056, -0.0012, 0.004, -0.0039, -0.0025,
0.0104, -0.0113, 0.0101, 8e-04, -0.0013, 0.0055, -2e-04, 0.0017,
8e-04, 9e-04, -0.0012, 0.0075, -0.0125, -0.006, 0.0097, -0.019,
0.0068, 0.0126, -0.002, 0.0033, 0.0053, 0.009, 0.0013, 0.0018,
0.0042, 0.0038, -0.0023, 0.0015, 0.0053, -0.0013, -0.0055, -0.0023,
0.007, -0.0086, 0.0081, -0.0042, 0.008, 0, 0.0031, -0.004, 0.0049,
-0.0101, 0.004, -0.0045, 0.0067, 0.0034, 0.0123, 0.0033, -0.0025,
-0.0231, 0.0148, -0.0146, -0.0063, 0.0087, 0.0044, 0.0103, 6e-04,
0.0041, -0.0018, 0.0067, 0.0023, -0.0088, 0.0093, 0.0101, 0.0026,
0.0051, 0.0046, -0.0029, 0.0032, 8e-04, 0.0103, 0.0054, -0.0047,
0.0096, -1e-04, 0.0015, -0.0074, -0.0029, -8e-04, 0.006, -0.0065,
0.0037, -0.0144, 0.0055, -0.0048, -0.014, 0.0091, 0.0127, 0,
-0.0103, -0.0083, 0.0152, -0.0063, 0.0077, 0.0079, -0.0138, -0.0248,
0.0032, -0.0127, 0.0096, 0.0099, 0.0059, -0.0041, 0.0058, -0.001,
4e-04, 0.0108, 0.0057, 0.0072, 4e-04, 0.0136, 4e-04, 0.0038,
-0.0037, 0.0026, 0.0055, 0.0018, 0.0019, -0.0021, -0.0037, 0.0024,
0.001, -0.003, 0, 7e-04, 0.0116, 0.0017, -0.0014, -0.0057, -0.0032,
0.0037, -0.0029, -0.0012, 0.003, -0.0052, -0.014, -0.0033, -0.0064,
0.0049, -0.0062, 0.0091, 0.0034, -0.0038, -0.0161, 0.0035, 0.0016,
-0.0032, 0.0045, 0.0083, 0.0012, 5e-04, 0.0096, 0.0074, 0.0031,
-0.0026, 0.0023, 0.0058, 0.0045, 0.0116, -0.0017, -0.007, -0.0046,
-0.0024, -0.0029, 0.0039, -0.0046, -0.0053, 0.0079, -0.001, -0.0092,
0.0076, -0.0087, -0.0116, 7e-04, 0.0215, 0.0065, 0.004, -0.0073,
0.014, 0.0067, 0.0067, 1e-04, 0.0058, -0.0048, 0.0033, 0.0046,
0.0016, 0.0054, -0.005, -0.0028, 0.0024, 0.0035, -0.0031, 0.0051,
-0.0127, 0.0135, 2e-04, -0.002, 0.0081, 0.0049, 0.0044, -0.0035,
-0.0022, -0.0031, 0.0081, 0.005, -0.001, 3e-04, 0.0024, -7e-04,
-0.0026, -0.0043, -1e-04, -0.0044, 0.0112, 0.0025, -0.0036, -0.0112,
-0.0033, -1e-04, 0.0063, -0.0032, 0.0171, -0.0012, 0.0058, 0.0053,
0.0022, 0.0051, -1e-04, -2e-04, 0.0047, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, -0.1099, -0.089, 0.0323, 0.0322, -0.0339, -0.0962,
-0.0225, 0.05, -0.0635, -0.0296, -0.0383, 0.0363, -0.0186, 0.03,
-0.0033, 0.0144, -0.0047, 0.0374, 0.0608, 0.0467, 0.0159, -0.0097,
0.0076, 0.038, -0.03, 0.0324, -0.0263, -0.027, -0.0083, -0.0106,
0.014, 0.0087, 0.0083, 0.0139, -0.0218, -0.0159, -0.0052, -0.0029,
-0.0804, -0.0057, 0.0363, -0.0038, -0.0083, -3e-04, -0.0104,
0.0313, -0.0849, -0.1169, -0.0236, -0.0622, -0.0382, -0.0402,
0.0524, 0.0394, -0.0547, 0, 0.014, 0.0381, -0.0096, -0.0565,
0.0402, -0.0627, -0.0413, 0.0504, -0.0425, 0.0146, 0, -0.0015,
-0.0134, 0.0099, -0.0124, -5e-04, -0.054, -0.0183, 0.0479, 0.0205,
0.0011, -0.009, 0.033, 0.0772, -0.0105, 0.0623, -0.0218, 0.0163,
0.0649, -0.0301, 0.012, -0.0906, -0.0245, 0.0168, -0.0145, 0.0659,
0.0152, 0.0127, -0.0198, 0.0055, -0.0474, -0.0244, -0.0083, -0.0292,
0.0056, -0.0116, 0, -0.002, 0.0205, -0.0453, 0.0011, 0.0168,
0.0093, 0.1913, -0.0288, -0.0275, -0.0378, 0.0047, -0.0014, 0.0033,
-0.0038, -0.0331, -0.0234, -0.039, 0.0448, -0.0105, 0.1259, -0.0085,
0.0627, -0.0272, 0.0079, 0.0528, -0.0132, 0.0808, 0.0081, 0.008,
0.0364, 0.0249, -0.0343, 0.0155, 0.0091, -0.0267, 0.0193, 0.0127,
0.005, -0.0143, 0.0239, -0.0506, -0.0022, 0.0359, -0.0108, -0.0018,
-0.0402, 0.0255, -0.0156, -0.0174, -0.0054, 0.0274, 0.0518, -0.0082,
0.0356, 0.0229, -0.0122, 0.0526, 0.0232, 0.0134, -0.0243, -0.0275,
-0.0083, 0.0097, -0.0159, 0.0361, 0.0029, 0.0084, 0.0148, 0.0295,
-0.0517, 0.0146, -0.0083, -0.0403, -0.0545, 0.0189, 0.0143, -0.0138,
-0.0035, -0.0102, -0.0315, 0.0197, 0.0211, -0.0063, 0.0215, -0.0162,
-0.0415, -0.0055, 0.0052, 0.0044, -0.019, 0.0141, 0.0194, -0.0022,
-0.0072, -0.0025, 0.0412, -0.0217, 0.0064, -0.011, -0.0269, -0.0015,
-0.0144, -0.006, 0.0023, -0.026, -0.0046, -4e-04, -0.0233, 0.0032,
0.0349, -0.0195, -0.002, -0.0043, 0.0327, 0.0312, 0.0118, -0.0197,
-0.0097, 0.0369, 0.0163, -0.0221, -0.0321, 0.0151, -0.0108, -0.0353,
0.0016, 0.0093, 4e-04, 0.005, 0.0011, 0.0272, 0.0048, 0.0293,
-0.0122, 0.0561, -0.0228, -0.0261, -0.0247, 0.0086, -0.0029,
-0.0133, 0.0052, 0.0093, -0.0174, -0.0177, 0.0046, -0.0187, -0.0039,
-0.0195, -0.004, -0.0299, -0.0086, -0.0324, 0.0527, -0.0081,
-0.0205, -0.0138, -0.0264, 0.0031, 0.0139, 0.0447, -0.0123, -0.0108,
-0.0017, -0.0042, 0.0165, 0.0079, 0.0041, -0.0169, 0.0264, -0.0241,
0.0129, -0.0037, 0.0207, 0.0089, -0.0028, -0.0161, 0.0045, -0.0061,
0.014, 0.0312, 0.0126, 4e-04, 0.0039, 0.0143, 0.0015, 0.0125,
-0.0176, -0.0115, 0.0066, 0.0031, 0.0145, 0.2961, -0.0102, 0.0418,
0.0621, -0.0221, 0.0188, 0.0149, 0.03, -0.0163, 0.0083, -0.0085,
-0.001, -0.0073, -0.0314, -0.01, -0.0025, 0.0142, 0.0197, 0.0159,
-0.0023, 0.006, 0.0519, 0.0195, -0.0411, 0.023, 0.018, 2e-04,
0.014, -0.0021, 0.0211, 0.0302, 0.002, -0.01, 0.033, -0.0064,
-0.0098, -0.0406, 0.0602, 0.0036, 0.0166, 0.0328, -0.0063, 0.0267,
0.0208, 0.0188, 0.0169, -0.0197, 0.0038, -0.0028, -0.0219, 0.0378,
-0.0102, -0.0669, -0.0078, 0.0487, 0.0012, 0.0081, -2e-04, 0.0331,
0.0209, 0.0385, -0.0068, -0.0217, -0.0148, 0.0106, -0.0095, -0.0331,
-0.0165, -0.0079, 0.0245, -0.0092, -0.0308, 0.0392, -0.0198,
-0.0318, -6e-04, -0.028, 0.0089, 0.0451, 0.0057, 4e-04, -0.0649,
0.0116, 0.0015, 0.0058, -0.0101, -0.0305, 0.0239, 0.0131, 0.0112,
0.0011, -0.007, 0.0404, -0.0058, -0.0083, 0.0188, 0.0289, -0.0173,
0.0496, 0.0287, 0.0092, 0.0195, 0.0129, -0.0094, 0.0013, 0.0481,
0.0033, -0.004, -0.0397, -0.0312, 0.0175, 0.0154, 0.0054, 0.0111,
0.0105, -0.0016, 0.0035, -0.0016, -0.0028, -0.0037, 0.0117, 0.0103,
-0.0031, -0.0173, 0.0168, -0.0163, 0.0625, -0.0047, 0.006, 0.0128,
0.0077, -7e-04, -0.0023, 0.0099, 0.0094, 0.0106, 0.0166, 0.0347,
5e-04, 0.0186, 0.0136, -0.0232, 0.0091, -1e-04, 0.0254, -0.0036,
0.0065, 0.0118, 0.0064, 0.0183, 0.013, 0.0038, 0.0012, -0.0221,
-0.0054, 8e-04, 0.0213, 0.0059, 0.0125, 0.0292, 0.0379, -0.0069,
1e-04, 0.0265, 0.008, -0.0056, -0.0053, -0.0054, 0.0183, 0.0123,
0.0051, -0.0125, -0.0169, 0.0318, 0.0173, -0.0079, 0.015, 0.004,
-0.0122, -0.0035, -0.0055, -0.0281, -0.0416, 0.051, -0.0023,
-0.0343, -0.0246, -0.0022, -0.02, 0.0887, -0.0038, -0.0078, -0.0315,
-0.0032, 0.0066, -0.0071, -0.0284, 0.0075, -0.0023, 0.0018, 0.0023,
-0.0068, -0.0149, -0.009, -0.0129, -0.0292, 6e-04, 0.0582, -0.0077,
0.0244, -0.0091, -0.0054, 0.0177, 0.0121, -0.0024, -0.029, 0.006,
-0.0026, 0.0153, 5e-04, 0.0151, -0.0157, 0.0087, -0.007, -0.0012,
0.0045, 0.0203, 0.0028, -0.0028, -0.0138, 0.0077, -0.0195, 0.0022,
0.0043, -0.0094, 0.0262, 0.0146, 0.0116, 0.0176, -0.0067, 0.0132,
-0.0092, -0.0062, -0.0092, 0.0102, 0.0032, 1e-04, -0.0012, 0.0133,
-0.0163, -7e-04, -0.0049, -0.0431, -1e-04, 0.0178, 0.0169, 0.0264,
-0.0064, 0.0016, 0.0131, 0.0111, -0.0026, -0.0017, 0.0056, 0.0015,
0.0134, 0.0027, -0.0014, 0.0088, 0.0185, 0.0262, -0.0137, 0.0196,
-0.0094, 9e-04, 0.0188, -0.0013, -0.002, -0.0143, 0.0021, 0.0146,
-0.007, 0.009, 0.0061, -0.026, -0.0032, 0.0139, 0.0197, 0.0121,
0.0123, 0.0031, 2e-04, -0.0048, 0.002, -0.0133, -0.0249, -0.0124,
0.0242, -0.0208, -0.0116, 0.0029, 0.0154, -0.0069, -0.0214, -0.0221,
-0.0036, 0.0079, -0.0199, 0.0026, 0.008, 0.0237, -0.0081, -0.0186,
-0.036, 0.0396, -0.0325, 0.0057, -0.0119, -0.0091, -0.0144, 0.0021,
-0.0331, 0.0136, -0.003, -0.0383, -0.0363, 0.0172, -0.0077, 1e-04,
-0.0111, -0.021, 0.0039, 0.0721, -0.0086, 0.0014, 0.0174, 0.0317,
-0.0081, -0.0031, 0.011, -0.0069, 0.0015, -0.0176, -0.0644, 0.0157,
-0.0257, -0.0063, 0.0218, -0.0043, -0.0173, -0.0376, 0.0177,
0.0291, -0.0142, -0.0087, -0.0047, 0.0017, -0.0139, 0.004, -0.0106,
0.0443, 0.0317, -0.0126, -0.028, -0.0059, 0.0027, -0.0155, 0.0124,
-0.0062, -0.0356, -0.0315, 0.0415, -0.0067, -0.0054, 0.0096,
0.0184, -0.1236, -0.0235, 0.0226, 0.0188, -0.0032, -0.0029, -0.004,
-0.025, 0.0351, -0.001, 0.0296, 0.0145, 0.0105, -0.0251, -0.0019,
-9e-04, -0.0138, -3e-04, -0.0242, -0.0064, 0.0107, -0.0177, 0.0139,
-0.0098, -0.0071, -0.0249, -0.0241, 0.0263, -0.0127, 0.0117,
0.0025, 0.0142, -0.0215, -2e-04, 0.0097, 0.0259, 0.0272, -0.0027,
-0.0053, 0.0015, 0.0202, 0.0036, -0.0052, -0.0197, -0.0209, -0.0311,
0.002, 0.0051, -0.0098, -0.0106, 0.0071, 0.0019, 0.0204, -0.0032,
-0.0104, -0.0231, 0.0152, -0.0549, -0.0267, -0.0039, 0.0208,
0.0187, -0.0016, 0.0072, 0.0217, 0.0309, 0.0296, -0.0079, 0.0142,
0.01, 0.0239, -0.0044, 0.0113, -0.0088, -0.0083, 0.0038, -0.0238,
-0.0339, 0.0134, -0.003, 0.0223, -0.0074, 0.0038, 0.0018, 0.0067,
-0.0083, 0.0081, 0.0149, -0.0042, 0.0023, -0.0031, -0.0094, -0.015,
0.0076, -0.0066, -0.003, -0.0123, 0.0087, -0.0137, 0.0046, -5e-04,
-0.0204, -0.0144, -0.008, -0.0265, 2e-04, -0.0114, -0.0108, 0.007,
0.0321, 0.0225, 0.0056, -0.0079, -0.0057, 0.0175, -0.0038, 0.0155,
-0.0017, 0.002, 0.0065, 3e-04, 0.0034, -0.0158, 0.0032, -0.0172,
0.0513, -0.0045, 0.0056, 0.0155, 0.0123, -0.0018, 0.0092, 0.0129,
0.0149, -0.009, -6e-04, -0.002, -0.0141, 0.0283, 0.0476, 0.0183,
-0.0012, 0.0088, 0.0108, -0.0131, 0.0024, 0.0013, -0.0039, 0.0039,
-0.0286, 0.0047, 0.0016, -0.0091, 0.0027, 0.0208, -0.0069, 0.006,
0.0159, -0.0228, -0.0544, 0.0106, -0.0165, -0.0317, 0.0115, 0.0204,
0.0165, -0.0104, 0.0497, -0.0031, -0.0155, 0.0097, -0.0071, -0.0124,
0.0235, 0.0033, -0.0125, -7e-04, 0.0097, -0.0139, 0.0117, 0.0062,
0.0065, 0.0066, 0.0052, 0.0049, 0.0068, 0.0087, 0.0245, -0.0029,
0.0099, 0.0132, -0.0113, 0.0075, -0.0249, 0.0159, -0.0043, -0.0051,
0.0129, -0.0025, -0.0027, -0.0163, 0.0158, -0.0029, 0.0018, 0.0012,
0.0145, -0.0061, -0.012, 0.0018, -0.0088, 0.012, -0.0026, 0.0076,
0.0185, 0.0235, 0.0185, -0.0088, 0.0273, -0.0023, 0.0051, -0.0138,
0.0114, -0.0015, -0.0075, -0.0014, -0.0109, 0.0054, -0.0045,
-0.0075, -0.0115, 0.0083, 0.0384, -0.0043, -0.0066, -0.0067,
-0.01, 0.0118), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1424069985.73567, class = c("POSIXct",
"POSIXt")), ret_type = "discrete", index = structure(c(1325548800,
1325635200, 1325721600, 1325808000, 1326067200, 1326153600, 1326240000,
1326326400, 1326412800, 1326758400, 1326844800, 1326931200, 1327017600,
1327276800, 1327363200, 1327449600, 1327536000, 1327622400, 1327881600,
1327968000, 1328054400, 1328140800, 1328227200, 1328486400, 1328572800,
1328659200, 1328745600, 1328832000, 1329091200, 1329177600, 1329264000,
1329350400, 1329436800, 1329782400, 1329868800, 1329955200, 1330041600,
1330300800, 1330387200, 1330473600, 1330560000, 1330646400, 1330905600,
1330992000, 1331078400, 1331164800, 1331251200, 1331510400, 1331596800,
1331683200, 1331769600, 1331856000, 1332115200, 1332201600, 1332288000,
1332374400, 1332460800, 1332720000, 1332806400, 1332892800, 1332979200,
1333065600, 1333324800, 1333411200, 1333497600, 1333584000, 1333929600,
1334016000, 1334102400, 1334188800, 1334275200, 1334534400, 1334620800,
1334707200, 1334793600, 1334880000, 1335139200, 1335225600, 1335312000,
1335398400, 1335484800, 1335744000, 1335830400, 1335916800, 1336003200,
1336089600, 1336348800, 1336435200, 1336521600, 1336608000, 1336694400,
1336953600, 1337040000, 1337126400, 1337212800, 1337299200, 1337558400,
1337644800, 1337731200, 1337817600, 1337904000, 1338249600, 1338336000,
1338422400, 1338508800, 1338768000, 1338854400, 1338940800, 1339027200,
1339113600, 1339372800, 1339459200, 1339545600, 1339632000, 1339718400,
1339977600, 1340064000, 1340150400, 1340236800, 1340323200, 1340582400,
1340668800, 1340755200, 1340841600, 1340928000, 1341187200, 1341273600,
1341446400, 1341532800, 1341792000, 1341878400, 1341964800, 1342051200,
1342137600, 1342396800, 1342483200, 1342569600, 1342656000, 1342742400,
1343001600, 1343088000, 1343174400, 1343260800, 1343347200, 1343606400,
1343692800, 1343779200, 1343865600, 1343952000, 1344211200, 1344297600,
1344384000, 1344470400, 1344556800, 1344816000, 1344902400, 1344988800,
1345075200, 1345161600, 1345420800, 1345507200, 1345593600, 1345680000,
1345766400, 1346025600, 1346112000, 1346198400, 1346284800, 1346371200,
1346716800, 1346803200, 1346889600, 1346976000, 1347235200, 1347321600,
1347408000, 1347494400, 1347580800, 1347840000, 1347926400, 1348012800,
1348099200, 1348185600, 1348444800, 1348531200, 1348617600, 1348704000,
1348790400, 1349049600, 1349136000, 1349222400, 1349308800, 1349395200,
1349654400, 1349740800, 1349827200, 1349913600, 1.35e+09, 1350259200,
1350345600, 1350432000, 1350518400, 1350604800, 1350864000, 1350950400,
1351036800, 1351123200, 1351209600, 1351641600, 1351728000, 1351814400,
1352073600, 1352160000, 1352246400, 1352332800, 1352419200, 1352678400,
1352764800, 1352851200, 1352937600, 1353024000, 1353283200, 1353369600,
1353456000, 1353628800, 1353888000, 1353974400, 1354060800, 1354147200,
1354233600, 1354492800, 1354579200, 1354665600, 1354752000, 1354838400,
1355097600, 1355184000, 1355270400, 1355356800, 1355443200, 1355702400,
1355788800, 1355875200, 1355961600, 1356048000, 1356307200, 1356480000,
1356566400, 1356652800, 1356912000, 1357084800, 1357171200, 1357257600,
1357516800, 1357603200, 1357689600, 1357776000, 1357862400, 1358121600,
1358208000, 1358294400, 1358380800, 1358467200, 1358812800, 1358899200,
1358985600, 1359072000, 1359331200, 1359417600, 1359504000, 1359590400,
1359676800, 1359936000, 1360022400, 1360108800, 1360195200, 1360281600,
1360540800, 1360627200, 1360713600, 1360800000, 1360886400, 1361232000,
1361318400, 1361404800, 1361491200, 1361750400, 1361836800, 1361923200,
1362009600, 1362096000, 1362355200, 1362441600, 1362528000, 1362614400,
1362700800, 1362960000, 1363046400, 1363132800, 1363219200, 1363305600,
1363564800, 1363651200, 1363737600, 1363824000, 1363910400, 1364169600,
1364256000, 1364342400, 1364428800, 1364774400, 1364860800, 1364947200,
1365033600, 1365120000, 1365379200, 1365465600, 1365552000, 1365638400,
1365724800, 1365984000, 1366070400, 1366156800, 1366243200, 1366329600,
1366588800, 1366675200, 1366761600, 1366848000, 1366934400, 1367193600,
1367280000, 1367366400, 1367452800, 1367539200, 1367798400, 1367884800,
1367971200, 1368057600, 1368144000, 1368403200, 1368489600, 1368576000,
1368662400, 1368748800, 1369008000, 1369094400, 1369180800, 1369267200,
1369353600, 1369699200, 1369785600, 1369872000, 1369958400, 1370217600,
1370304000, 1370390400, 1370476800, 1370563200, 1370822400, 1370908800,
1370995200, 1371081600, 1371168000, 1371427200, 1371513600, 1371600000,
1371686400, 1371772800, 1372032000, 1372118400, 1372204800, 1372291200,
1372377600, 1372636800, 1372723200, 1372809600, 1372982400, 1373241600,
1373328000, 1373414400, 1373500800, 1373587200, 1373846400, 1373932800,
1374019200, 1374105600, 1374192000, 1374451200, 1374537600, 1374624000,
1374710400, 1374796800, 1375056000, 1375142400, 1375228800, 1375315200,
1375401600, 1375660800, 1375747200, 1375833600, 1375920000, 1376006400,
1376265600, 1376352000, 1376438400, 1376524800, 1376611200, 1376870400,
1376956800, 1377043200, 1377129600, 1377216000, 1377475200, 1377561600,
1377648000, 1377734400, 1377820800, 1378166400, 1378252800, 1378339200,
1378425600, 1378684800, 1378771200, 1378857600, 1378944000, 1379030400,
1379289600, 1379376000, 1379462400, 1379548800, 1379635200, 1379894400,
1379980800, 1380067200, 1380153600, 1380240000, 1380499200, 1380585600,
1380672000, 1380758400, 1380844800, 1381104000, 1381190400, 1381276800,
1381363200, 1381449600, 1381708800, 1381795200, 1381881600, 1381968000,
1382054400, 1382313600, 1382400000, 1382486400, 1382572800, 1382659200,
1382918400, 1383004800, 1383091200, 1383177600, 1383264000, 1383523200,
1383609600, 1383696000, 1383782400, 1383868800, 1384128000, 1384214400,
1384300800, 1384387200, 1384473600, 1384732800, 1384819200, 1384905600,
1384992000, 1385078400, 1385337600, 1385424000, 1385510400, 1385683200,
1385942400, 1386028800, 1386115200, 1386201600, 1386288000, 1386547200,
1386633600, 1386720000, 1386806400, 1386892800, 1387152000, 1387238400,
1387324800, 1387411200, 1387497600, 1387756800, 1387843200, 1388016000,
1388102400, 1388361600, 1388448000), tzone = "UTC", tclass = "Date"), .Dim = c(502L,
3L), .Dimnames = list(NULL, c("SPY", "FB", "AAPL")))
这是我用来执行回归的循环。由于 RETS
中有 3 个资产,DATES
中有 4 个不同的日期,它将 return 12
回归存储在 BETA
BETA <- data.frame()
ALL <- data.frame()
for (k in 1:length(DATES)){
for (i in 1:ncol(RETS)){
rets <- RETS[DATES[k]]
MOD <- lm(rets$SPY ~ rets[,i])
COEF <- cbind(coef(MOD)[1],coef(MOD)[2])
BETA <- rbind(BETA,COEF)
}
ALL <- merge(ALL, BETA)
}
用 NA 替换零。在这种情况下,lm
的默认 na.action = na.omit
参数将自动删除它们。
RETS.na <- replace(RETS, RETS == 0, NA)
或者,如果您想删除任何列中有 NA
的所有行,则:
RETS.na <- na.omit(RETS.na)
要获得 DATES[1]
的分数子集,请说:
RETS.na[ DATES[1] ]