I'm trying to plot this base plot
df <-
structure(list(X = c(1e-04, 2e-04, 3e-04, 4e-04, 5e-04, 6e-04,
7e-04, 8e-04, 9e-04, 0.001, 0.0011, 0.0012, 0.0013, 0.0014, 0.0015,
0.0016, 0.0017, 0.0018, 0.0019, 0.002, 0.0021, 0.0022, 0.0023,
0.0024, 0.0025, 0.0026, 0.0027, 0.0028, 0.0029, 0.003, 0.0031,
0.0032, 0.0033, 0.0034, 0.0035, 0.0036, 0.0037, 0.0038, 0.0039,
0.004, 0.0041, 0.0042, 0.0043, 0.0044, 0.0045, 0.0046, 0.0047,
0.0048, 0.0049, 0.005, 0.0051, 0.0052, 0.0053, 0.0054, 0.0055,
0.0056, 0.0057, 0.0058, 0.0059, 0.006, 0.0061, 0.0062, 0.0063,
0.0064, 0.0065, 0.0066, 0.0067, 0.0068, 0.0069, 0.007, 0.0071,
0.0072, 0.0073, 0.0074, 0.0075, 0.0076, 0.0077, 0.0078, 0.0079,
0.008, 0.0081, 0.0082, 0.0083, 0.0084, 0.0085, 0.0086, 0.0087,
0.0088, 0.0089, 0.009, 0.0091, 0.0092, 0.0093, 0.0094, 0.0095,
0.0096, 0.0097, 0.0098, 0.0099, 0.01, 0.0101, 0.0102, 0.0103,
0.0104, 0.0105, 0.0106, 0.0107, 0.0108, 0.0109, 0.011, 0.0111,
0.0112, 0.0113, 0.0114, 0.0115, 0.0116, 0.0117, 0.0118, 0.0119,
0.012, 0.0121, 0.0122, 0.0123, 0.0124, 0.0125, 0.0126, 0.0127,
0.0128, 0.0129, 0.013, 0.0131, 0.0132, 0.0133, 0.0134, 0.0135,
0.0136, 0.0137, 0.0138, 0.0139, 0.014, 0.0141, 0.0142, 0.0143,
0.0144, 0.0145, 0.0146, 0.0147, 0.0148, 0.0149, 0.015, 0.0151,
0.0152, 0.0153, 0.0154, 0.0155, 0.0156, 0.0157, 0.0158, 0.0159,
0.016, 0.0161, 0.0162, 0.0163, 0.0164, 0.0165, 0.0166, 0.0167,
0.0168, 0.0169, 0.017, 0.0171, 0.0172, 0.0173, 0.0174, 0.0175,
0.0176, 0.0177, 0.0178, 0.0179, 0.018, 0.0181, 0.0182, 0.0183,
0.0184, 0.0185, 0.0186, 0.0187, 0.0188, 0.0189, 0.019, 0.0191,
0.0192, 0.0193, 0.0194, 0.0195, 0.0196, 0.0197, 0.0198, 0.0199,
0.02, 0.0201, 0.0202, 0.0203, 0.0204, 0.0205, 0.0206, 0.0207,
0.0208, 0.0209, 0.021, 0.0211, 0.0212, 0.0213, 0.0214, 0.0215,
0.0216, 0.0217, 0.0218, 0.0219, 0.022, 0.0221, 0.0222, 0.0223,
0.0224, 0.0225, 0.0226, 0.0227, 0.0228, 0.0229, 0.023, 0.0231,
0.0232, 0.0233, 0.0234, 0.0235, 0.0236, 0.0237, 0.0238, 0.0239,
0.024, 0.0241, 0.0242, 0.0243, 0.0244, 0.0245, 0.0246, 0.0247,
0.0248, 0.0249, 0.025, 0.0251, 0.0252, 0.0253, 0.0254, 0.0255,
0.0256, 0.0257, 0.0258, 0.0259, 0.026, 0.0261, 0.0262, 0.0263,
0.0264, 0.0265, 0.0266, 0.0267, 0.0268, 0.0269, 0.027, 0.0271,
0.0272, 0.0273, 0.0274, 0.0275, 0.0276, 0.0277, 0.0278, 0.0279,
0.028, 0.0281, 0.0282, 0.0283, 0.0284, 0.0285, 0.0286, 0.0287,
0.0288, 0.0289, 0.029, 0.0291, 0.0292, 0.0293, 0.0294, 0.0295,
0.0296, 0.0297, 0.0298, 0.0299, 0.03, 0.0301, 0.0302, 0.0303,
0.0304, 0.0305, 0.0306, 0.0307, 0.0308, 0.0309, 0.031, 0.0311,
0.0312, 0.0313, 0.0314, 0.0315, 0.0316, 0.0317, 0.0318, 0.0319,
0.032, 0.0321, 0.0322, 0.0323, 0.0324, 0.0325, 0.0326, 0.0327,
0.0328, 0.0329, 0.033, 0.0331, 0.0332, 0.0333, 0.0334, 0.0335,
0.0336, 0.0337, 0.0338, 0.0339, 0.034, 0.0341, 0.0342, 0.0343,
0.0344, 0.0345, 0.0346, 0.0347, 0.0348, 0.0349, 0.035, 0.0351,
0.0352, 0.0353, 0.0354, 0.0355, 0.0356, 0.0357, 0.0358, 0.0359,
0.036, 0.0361, 0.0362, 0.0363, 0.0364, 0.0365, 0.0366, 0.0367,
0.0368, 0.0369, 0.037, 0.0371, 0.0372, 0.0373, 0.0374, 0.0375,
0.0376, 0.0377, 0.0378, 0.0379, 0.038, 0.0381, 0.0382, 0.0383,
0.0384, 0.0385, 0.0386, 0.0387, 0.0388, 0.0389, 0.039, 0.0391,
0.0392, 0.0393, 0.0394, 0.0395, 0.0396, 0.0397, 0.0398, 0.0399,
0.04, 0.0401, 0.0402, 0.0403, 0.0404, 0.0405, 0.0406, 0.0407,
0.0408, 0.0409, 0.041, 0.0411, 0.0412, 0.0413, 0.0414, 0.0415,
0.0416, 0.0417, 0.0418, 0.0419, 0.042, 0.0421, 0.0422, 0.0423,
0.0424, 0.0425, 0.0426, 0.0427, 0.0428, 0.0429, 0.043, 0.0431,
0.0432, 0.0433, 0.0434, 0.0435, 0.0436, 0.0437, 0.0438, 0.0439,
0.044, 0.0441, 0.0442, 0.0443, 0.0444, 0.0445, 0.0446, 0.0447,
0.0448, 0.0449, 0.045, 0.0451, 0.0452, 0.0453, 0.0454, 0.0455,
0.0456, 0.0457, 0.0458, 0.0459, 0.046, 0.0461, 0.0462, 0.0463,
0.0464, 0.0465, 0.0466, 0.0467, 0.0468, 0.0469, 0.047, 0.0471,
0.0472, 0.0473, 0.0474, 0.0475, 0.0476, 0.0477, 0.0478, 0.0479,
0.048, 0.0481, 0.0482, 0.0483, 0.0484, 0.0485, 0.0486, 0.0487,
0.0488, 0.0489, 0.049, 0.0491, 0.0492, 0.0493, 0.0494, 0.0495,
0.0496, 0.0497, 0.0498, 0.0499, 0.05, 0.0501, 0.0502, 0.0503,
0.0504, 0.0505, 0.0506, 0.0507, 0.0508, 0.0509, 0.051, 0.0511,
0.0512, 0.0513, 0.0514, 0.0515, 0.0516, 0.0517, 0.0518, 0.0519,
0.052, 0.0521, 0.0522, 0.0523, 0.0524, 0.0525, 0.0526, 0.0527,
0.0528, 0.0529, 0.053, 0.0531, 0.0532, 0.0533, 0.0534, 0.0535,
0.0536, 0.0537, 0.0538, 0.0539, 0.054, 0.0541, 0.0542, 0.0543,
0.0544, 0.0545, 0.0546, 0.0547, 0.0548, 0.0549, 0.055, 0.0551,
0.0552, 0.0553, 0.0554, 0.0555, 0.0556, 0.0557, 0.0558, 0.0559,
0.056, 0.0561, 0.0562, 0.0563, 0.0564, 0.0565, 0.0566, 0.0567,
0.0568, 0.0569, 0.057, 0.0571, 0.0572, 0.0573, 0.0574, 0.0575,
0.0576, 0.0577, 0.0578, 0.0579, 0.058, 0.0581, 0.0582, 0.0583,
0.0584, 0.0585, 0.0586, 0.0587, 0.0588, 0.0589, 0.059, 0.0591,
0.0592, 0.0593, 0.0594, 0.0595, 0.0596, 0.0597, 0.0598, 0.0599,
0.06, 0.0601, 0.0602, 0.0603, 0.0604, 0.0605, 0.0606, 0.0607,
0.0608, 0.0609, 0.061, 0.0611, 0.0612, 0.0613, 0.0614, 0.0615,
0.0616, 0.0617, 0.0618, 0.0619, 0.062, 0.0621, 0.0622, 0.0623,
0.0624, 0.0625, 0.0626, 0.0627, 0.0628, 0.0629, 0.063, 0.0631,
0.0632, 0.0633, 0.0634, 0.0635, 0.0636, 0.0637, 0.0638, 0.0639,
0.064, 0.0641, 0.0642, 0.0643, 0.0644, 0.0645, 0.0646, 0.0647,
0.0648, 0.0649, 0.065, 0.0651, 0.0652, 0.0653, 0.0654, 0.0655,
0.0656, 0.0657, 0.0658, 0.0659, 0.066, 0.0661, 0.0662, 0.0663,
0.0664, 0.0665, 0.0666, 0.0667, 0.0668, 0.0669, 0.067, 0.0671,
0.0672, 0.0673, 0.0674, 0.0675, 0.0676, 0.0677, 0.0678, 0.0679,
0.068, 0.0681, 0.0682, 0.0683, 0.0684, 0.0685, 0.0686, 0.0687,
0.0688, 0.0689, 0.069, 0.0691, 0.0692, 0.0693, 0.0694, 0.0695,
0.0696, 0.0697, 0.0698, 0.0699, 0.07, 0.0701, 0.0702, 0.0703,
0.0704, 0.0705, 0.0706, 0.0707, 0.0708, 0.0709, 0.071, 0.0711,
0.0712, 0.0713, 0.0714, 0.0715, 0.0716, 0.0717, 0.0718, 0.0719,
0.072, 0.0721, 0.0722, 0.0723, 0.0724, 0.0725, 0.0726, 0.0727,
0.0728, 0.0729, 0.073, 0.0731, 0.0732, 0.0733, 0.0734, 0.0735,
0.0736, 0.0737, 0.0738, 0.0739, 0.074, 0.0741, 0.0742, 0.0743,
0.0744, 0.0745, 0.0746, 0.0747, 0.0748, 0.0749, 0.075, 0.0751,
0.0752, 0.0753, 0.0754, 0.0755, 0.0756, 0.0757, 0.0758, 0.0759,
0.076, 0.0761, 0.0762, 0.0763, 0.0764, 0.0765, 0.0766, 0.0767,
0.0768, 0.0769, 0.077, 0.0771, 0.0772, 0.0773, 0.0774, 0.0775,
0.0776, 0.0777, 0.0778, 0.0779, 0.078, 0.0781, 0.0782, 0.0783,
0.0784, 0.0785, 0.0786, 0.0787, 0.0788, 0.0789, 0.079, 0.0791,
0.0792, 0.0793, 0.0794, 0.0795, 0.0796, 0.0797, 0.0798, 0.0799,
0.08, 0.0801, 0.0802, 0.0803, 0.0804, 0.0805, 0.0806, 0.0807,
0.0808, 0.0809, 0.081, 0.0811, 0.0812, 0.0813, 0.0814, 0.0815,
0.0816, 0.0817, 0.0818, 0.0819, 0.082, 0.0821, 0.0822, 0.0823,
0.0824, 0.0825, 0.0826, 0.0827, 0.0828, 0.0829, 0.083, 0.0831,
0.0832, 0.0833, 0.0834, 0.0835, 0.0836, 0.0837, 0.0838, 0.0839,
0.084, 0.0841, 0.0842, 0.0843, 0.0844, 0.0845, 0.0846, 0.0847,
0.0848, 0.0849, 0.085, 0.0851, 0.0852, 0.0853, 0.0854, 0.0855,
0.0856, 0.0857, 0.0858, 0.0859, 0.086, 0.0861, 0.0862, 0.0863,
0.0864, 0.0865, 0.0866, 0.0867, 0.0868, 0.0869, 0.087, 0.0871,
0.0872, 0.0873, 0.0874, 0.0875, 0.0876, 0.0877, 0.0878, 0.0879,
0.088, 0.0881, 0.0882, 0.0883, 0.0884, 0.0885, 0.0886, 0.0887,
0.0888, 0.0889, 0.089, 0.0891, 0.0892, 0.0893, 0.0894, 0.0895,
0.0896, 0.0897, 0.0898, 0.0899, 0.09, 0.0901, 0.0902, 0.0903,
0.0904, 0.0905, 0.0906, 0.0907, 0.0908, 0.0909, 0.091, 0.0911,
0.0912, 0.0913, 0.0914, 0.0915, 0.0916, 0.0917, 0.0918, 0.0919,
0.092, 0.0921, 0.0922, 0.0923, 0.0924, 0.0925, 0.0926, 0.0927,
0.0928, 0.0929, 0.093, 0.0931, 0.0932, 0.0933, 0.0934, 0.0935,
0.0936, 0.0937, 0.0938, 0.0939, 0.094, 0.0941, 0.0942, 0.0943,
0.0944, 0.0945, 0.0946, 0.0947, 0.0948, 0.0949, 0.095, 0.0951,
0.0952, 0.0953, 0.0954, 0.0955, 0.0956, 0.0957, 0.0958, 0.0959,
0.096, 0.0961, 0.0962, 0.0963, 0.0964, 0.0965, 0.0966, 0.0967,
0.0968, 0.0969, 0.097, 0.0971, 0.0972, 0.0973, 0.0974, 0.0975,
0.0976, 0.0977, 0.0978, 0.0979, 0.098, 0.0981, 0.0982, 0.0983,
0.0984, 0.0985, 0.0986, 0.0987, 0.0988, 0.0989, 0.099, 0.0991,
0.0992, 0.0993, 0.0994, 0.0995, 0.0996, 0.0997, 0.0998, 0.0999,
0.1), Y = c(3.9e-125, 1.47e-124, 3.1e-124, 5.19e-124, 7.62e-124,
1.03e-123, 1.32e-123, 1.62e-123, 1.93e-123, 2.24e-123, 2.55e-123,
2.86e-123, 3.15e-123, 3.44e-123, 3.71e-123, 3.97e-123, 4.21e-123,
4.44e-123, 4.66e-123, 4.85e-123, 5.03e-123, 5.19e-123, 5.33e-123,
5.46e-123, 5.57e-123, 5.67e-123, 5.75e-123, 5.81e-123, 5.86e-123,
5.9e-123, 5.92e-123, 5.93e-123, 5.93e-123, 5.92e-123, 5.9e-123,
5.87e-123, 5.83e-123, 5.78e-123, 5.73e-123, 5.67e-123, 5.6e-123,
5.52e-123, 5.44e-123, 5.36e-123, 5.27e-123, 5.18e-123, 5.08e-123,
4.98e-123, 4.88e-123, 4.78e-123, 4.68e-123, 4.57e-123, 4.46e-123,
4.36e-123, 4.25e-123, 4.14e-123, 4.03e-123, 3.93e-123, 3.82e-123,
3.72e-123, 3.61e-123, 3.51e-123, 3.4e-123, 3.3e-123, 3.2e-123,
3.1e-123, 3.01e-123, 2.91e-123, 2.82e-123, 2.73e-123, 2.64e-123,
2.55e-123, 2.46e-123, 2.38e-123, 2.3e-123, 2.22e-123, 2.14e-123,
2.07e-123, 1.99e-123, 1.92e-123, 1.85e-123, 1.78e-123, 1.72e-123,
1.65e-123, 1.59e-123, 1.53e-123, 1.47e-123, 1.42e-123, 1.36e-123,
1.31e-123, 1.26e-123, 1.21e-123, 1.16e-123, 1.12e-123, 1.07e-123,
1.03e-123, 9.87e-124, 9.47e-124, 9.08e-124, 8.71e-124, 8.35e-124,
8.01e-124, 7.67e-124, 7.35e-124, 7.04e-124, 6.75e-124, 6.46e-124,
6.19e-124, 5.93e-124, 5.67e-124, 5.43e-124, 5.19e-124, 4.97e-124,
4.75e-124, 4.55e-124, 4.35e-124, 4.16e-124, 3.98e-124, 3.8e-124,
3.63e-124, 3.47e-124, 3.32e-124, 3.17e-124, 3.03e-124, 2.89e-124,
2.76e-124, 2.63e-124, 2.52e-124, 2.4e-124, 2.29e-124, 2.19e-124,
2.09e-124, 1.99e-124, 1.9e-124, 1.81e-124, 1.73e-124, 1.65e-124,
1.57e-124, 1.5e-124, 1.43e-124, 1.36e-124, 1.3e-124, 1.24e-124,
1.18e-124, 1.12e-124, 1.07e-124, 1.02e-124, 9.71e-125, 9.25e-125,
8.81e-125, 8.39e-125, 7.98e-125, 7.6e-125, 7.24e-125, 6.89e-125,
6.56e-125, 6.24e-125, 5.94e-125, 5.65e-125, 5.38e-125, 5.12e-125,
4.87e-125, 4.63e-125, 4.41e-125, 4.19e-125, 3.98e-125, 3.79e-125,
3.6e-125, 3.43e-125, 3.26e-125, 3.1e-125, 2.94e-125, 2.8e-125,
2.66e-125, 2.53e-125, 2.4e-125, 2.28e-125, 2.17e-125, 2.06e-125,
1.96e-125, 1.86e-125, 1.77e-125, 1.68e-125, 1.59e-125, 1.51e-125,
1.44e-125, 1.37e-125, 1.3e-125, 1.23e-125, 1.17e-125, 1.11e-125,
1.05e-125, 1e-125, 9.5e-126, 9.02e-126, 8.56e-126, 8.12e-126,
7.71e-126, 7.32e-126, 6.94e-126, 6.59e-126, 6.25e-126, 5.93e-126,
5.63e-126, 5.34e-126, 5.06e-126, 4.8e-126, 4.55e-126, 4.32e-126,
4.1e-126, 3.89e-126, 3.68e-126, 3.49e-126, 3.31e-126, 3.14e-126,
2.98e-126, 2.82e-126, 2.68e-126, 2.54e-126, 2.41e-126, 2.28e-126,
2.16e-126, 2.05e-126, 1.94e-126, 1.84e-126, 1.74e-126, 1.65e-126,
1.57e-126, 1.48e-126, 1.41e-126, 1.33e-126, 1.26e-126, 1.2e-126,
1.13e-126, 1.07e-126, 1.02e-126, 9.63e-127, 9.12e-127, 8.64e-127,
8.18e-127, 7.75e-127, 7.34e-127, 6.95e-127, 6.58e-127, 6.23e-127,
5.9e-127, 5.59e-127, 5.29e-127, 5.01e-127, 4.74e-127, 4.49e-127,
4.25e-127, 4.03e-127, 3.81e-127, 3.61e-127, 3.41e-127, 3.23e-127,
3.06e-127, 2.89e-127, 2.74e-127, 2.59e-127, 2.45e-127, 2.32e-127,
2.2e-127, 2.08e-127, 1.97e-127, 1.86e-127, 1.76e-127, 1.67e-127,
1.58e-127, 1.49e-127, 1.41e-127, 1.33e-127, 1.26e-127, 1.19e-127,
1.13e-127, 1.07e-127, 1.01e-127, 9.56e-128, 9.04e-128, 8.55e-128,
8.09e-128, 7.65e-128, 7.23e-128, 6.84e-128, 6.47e-128, 6.12e-128,
5.78e-128, 5.47e-128, 5.17e-128, 4.89e-128, 4.62e-128, 4.37e-128,
4.13e-128, 3.91e-128, 3.69e-128, 3.49e-128, 3.3e-128, 3.12e-128,
2.95e-128, 2.79e-128, 2.63e-128, 2.49e-128, 2.35e-128, 2.22e-128,
2.1e-128, 1.99e-128, 1.88e-128, 1.77e-128, 1.68e-128, 1.58e-128,
1.5e-128, 1.41e-128, 1.34e-128, 1.26e-128, 1.19e-128, 1.13e-128,
1.06e-128, 1.01e-128, 9.5e-129, 8.98e-129, 8.48e-129, 8.01e-129,
7.57e-129, 7.15e-129, 6.75e-129, 6.38e-129, 6.02e-129, 5.69e-129,
5.37e-129, 5.08e-129, 4.79e-129, 4.53e-129, 4.28e-129, 4.04e-129,
3.81e-129, 3.6e-129, 3.4e-129, 3.21e-129, 3.03e-129, 2.86e-129,
2.7e-129, 2.55e-129, 2.41e-129, 2.28e-129, 2.15e-129, 2.03e-129,
1.92e-129, 1.81e-129, 1.71e-129, 1.61e-129, 1.52e-129, 1.44e-129,
1.36e-129, 1.28e-129, 1.21e-129, 1.14e-129, 1.08e-129, 1.02e-129,
9.59e-130, 9.05e-130, 8.54e-130, 8.06e-130, 7.61e-130, 7.18e-130,
6.78e-130, 6.4e-130, 6.04e-130, 5.7e-130, 5.38e-130, 5.07e-130,
4.79e-130, 4.52e-130, 4.26e-130, 4.02e-130, 3.8e-130, 3.58e-130,
3.38e-130, 3.19e-130, 3.01e-130, 2.84e-130, 2.68e-130, 2.53e-130,
2.38e-130, 2.25e-130, 2.12e-130, 2e-130, 1.89e-130, 1.78e-130,
1.68e-130, 1.58e-130, 1.49e-130, 1.41e-130, 1.33e-130, 1.25e-130,
1.18e-130, 1.12e-130, 1.05e-130, 9.92e-131, 9.36e-131, 8.83e-131,
8.32e-131, 7.85e-131, 7.4e-131, 6.98e-131, 6.58e-131, 6.21e-131,
5.85e-131, 5.52e-131, 5.21e-131, 4.91e-131, 4.63e-131, 4.36e-131,
4.12e-131, 3.88e-131, 3.66e-131, 3.45e-131, 3.25e-131, 3.07e-131,
2.89e-131, 2.73e-131, 2.57e-131, 2.42e-131, 2.28e-131, 2.15e-131,
2.03e-131, 1.91e-131, 1.8e-131, 1.7e-131, 1.6e-131, 1.51e-131,
1.42e-131, 1.34e-131, 1.27e-131, 1.19e-131, 1.12e-131, 1.06e-131,
9.99e-132, 9.41e-132, 8.87e-132, 8.36e-132, 7.88e-132, 7.43e-132,
7e-132, 6.59e-132, 6.22e-132, 5.86e-132, 5.52e-132, 5.2e-132,
4.9e-132, 4.62e-132, 4.35e-132, 4.1e-132, 3.86e-132, 3.64e-132,
3.43e-132, 3.23e-132, 3.05e-132, 2.87e-132, 2.7e-132, 2.55e-132,
2.4e-132, 2.26e-132, 2.13e-132, 2.01e-132, 1.89e-132, 1.78e-132,
1.68e-132, 1.58e-132, 1.49e-132, 1.4e-132, 1.32e-132, 1.25e-132,
1.17e-132, 1.11e-132, 1.04e-132, 9.81e-133, 9.24e-133, 8.7e-133,
8.19e-133, 7.72e-133, 7.27e-133, 6.85e-133, 6.45e-133, 6.07e-133,
5.72e-133, 5.39e-133, 5.07e-133, 4.78e-133, 4.5e-133, 4.24e-133,
3.99e-133, 3.76e-133, 3.54e-133, 3.33e-133, 3.14e-133, 2.96e-133,
2.78e-133, 2.62e-133, 2.47e-133, 2.32e-133, 2.19e-133, 2.06e-133,
1.94e-133, 1.83e-133, 1.72e-133, 1.62e-133, 1.53e-133, 1.44e-133,
1.35e-133, 1.27e-133, 1.2e-133, 1.13e-133, 1.06e-133, 1e-133,
9.42e-134, 8.87e-134, 8.35e-134, 7.86e-134, 7.4e-134, 6.96e-134,
6.56e-134, 6.17e-134, 5.81e-134, 5.47e-134, 5.15e-134, 4.85e-134,
4.56e-134, 4.29e-134, 4.04e-134, 3.8e-134, 3.58e-134, 3.37e-134,
3.17e-134, 2.99e-134, 2.81e-134, 2.64e-134, 2.49e-134, 2.34e-134,
2.2e-134, 2.08e-134, 1.95e-134, 1.84e-134, 1.73e-134, 1.63e-134,
1.53e-134, 1.44e-134, 1.36e-134, 1.28e-134, 1.2e-134, 1.13e-134,
1.06e-134, 1e-134, 9.42e-135, 8.86e-135, 8.34e-135, 7.85e-135,
7.39e-135, 6.95e-135, 6.54e-135, 6.15e-135, 5.79e-135, 5.45e-135,
5.12e-135, 4.82e-135, 4.54e-135, 4.27e-135, 4.02e-135, 3.78e-135,
3.55e-135, 3.34e-135, 3.15e-135, 2.96e-135, 2.78e-135, 2.62e-135,
2.46e-135, 2.32e-135, 2.18e-135, 2.05e-135, 1.93e-135, 1.82e-135,
1.71e-135, 1.61e-135, 1.51e-135, 1.42e-135, 1.34e-135, 1.26e-135,
1.18e-135, 1.11e-135, 1.05e-135, 9.84e-136, 9.26e-136, 8.71e-136,
8.19e-136, 7.7e-136, 7.25e-136, 6.81e-136, 6.41e-136, 6.03e-136,
5.67e-136, 5.33e-136, 5.01e-136, 4.72e-136, 4.43e-136, 4.17e-136,
3.92e-136, 3.69e-136, 3.47e-136, 3.26e-136, 3.07e-136, 2.88e-136,
2.71e-136, 2.55e-136, 2.4e-136, 2.26e-136, 2.12e-136, 1.99e-136,
1.87e-136, 1.76e-136, 1.66e-136, 1.56e-136, 1.47e-136, 1.38e-136,
1.3e-136, 1.22e-136, 1.15e-136, 1.08e-136, 1.01e-136, 9.52e-137,
8.95e-137, 8.41e-137, 7.91e-137, 7.44e-137, 6.99e-137, 6.57e-137,
6.18e-137, 5.81e-137, 5.46e-137, 5.13e-137, 4.83e-137, 4.54e-137,
4.26e-137, 4.01e-137, 3.77e-137, 3.54e-137, 3.33e-137, 3.13e-137,
2.94e-137, 2.77e-137, 2.6e-137, 2.44e-137, 2.3e-137, 2.16e-137,
2.03e-137, 1.91e-137, 1.79e-137, 1.68e-137, 1.58e-137, 1.49e-137,
1.4e-137, 1.31e-137, 1.24e-137, 1.16e-137, 1.09e-137, 1.03e-137,
9.64e-138, 9.06e-138, 8.51e-138, 8e-138, 7.52e-138, 7.07e-138,
6.64e-138, 6.24e-138, 5.86e-138, 5.51e-138, 5.18e-138, 4.87e-138,
4.57e-138, 4.3e-138, 4.04e-138, 3.79e-138, 3.56e-138, 3.35e-138,
3.15e-138, 2.96e-138, 2.78e-138, 2.61e-138, 2.45e-138, 2.31e-138,
2.17e-138, 2.04e-138, 1.91e-138, 1.8e-138, 1.69e-138, 1.59e-138,
1.49e-138, 1.4e-138, 1.32e-138, 1.24e-138, 1.16e-138, 1.09e-138,
1.02e-138, 9.62e-139, 9.04e-139, 8.49e-139, 7.98e-139, 7.5e-139,
7.04e-139, 6.61e-139, 6.21e-139, 5.84e-139, 5.48e-139, 5.15e-139,
4.84e-139, 4.55e-139, 4.27e-139, 4.01e-139, 3.77e-139, 3.54e-139,
3.32e-139, 3.12e-139, 2.93e-139, 2.75e-139, 2.59e-139, 2.43e-139,
2.28e-139, 2.14e-139, 2.01e-139, 1.89e-139, 1.78e-139, 1.67e-139,
1.57e-139, 1.47e-139, 1.38e-139, 1.3e-139, 1.22e-139, 1.14e-139,
1.07e-139, 1.01e-139, 9.48e-140, 8.9e-140, 8.36e-140, 7.85e-140,
7.37e-140, 6.92e-140, 6.5e-140, 6.1e-140, 5.73e-140, 5.38e-140,
5.05e-140, 4.75e-140, 4.46e-140, 4.19e-140, 3.93e-140, 3.69e-140,
3.46e-140, 3.25e-140, 3.05e-140, 2.87e-140, 2.69e-140, 2.53e-140,
2.37e-140, 2.23e-140, 2.09e-140, 1.97e-140, 1.84e-140, 1.73e-140,
1.63e-140, 1.53e-140, 1.43e-140, 1.35e-140, 1.26e-140, 1.19e-140,
1.11e-140, 1.05e-140, 9.81e-141, 9.21e-141, 8.65e-141, 8.12e-141,
7.62e-141, 7.16e-141, 6.72e-141, 6.31e-141, 5.92e-141, 5.56e-141,
5.22e-141, 4.9e-141, 4.6e-141, 4.31e-141, 4.05e-141, 3.8e-141,
3.57e-141, 3.35e-141, 3.14e-141, 2.95e-141, 2.77e-141, 2.6e-141,
2.44e-141, 2.29e-141, 2.15e-141, 2.02e-141, 1.89e-141, 1.78e-141,
1.67e-141, 1.57e-141, 1.47e-141, 1.38e-141, 1.29e-141, 1.21e-141,
1.14e-141, 1.07e-141, 1e-141, 9.42e-142, 8.84e-142, 8.3e-142,
7.79e-142, 7.31e-142, 6.86e-142, 6.44e-142, 6.04e-142, 5.67e-142,
5.32e-142, 4.99e-142, 4.68e-142, 4.39e-142, 4.12e-142, 3.87e-142,
3.63e-142, 3.41e-142, 3.2e-142, 3e-142, 2.81e-142, 2.64e-142,
2.48e-142, 2.33e-142, 2.18e-142, 2.05e-142, 1.92e-142, 1.8e-142,
1.69e-142, 1.59e-142, 1.49e-142, 1.4e-142, 1.31e-142, 1.23e-142,
1.15e-142, 1.08e-142, 1.01e-142, 9.52e-143, 8.93e-143, 8.38e-143,
7.86e-143, 7.38e-143, 6.92e-143, 6.49e-143, 6.09e-143, 5.71e-143,
5.36e-143, 5.03e-143, 4.72e-143, 4.42e-143, 4.15e-143, 3.89e-143,
3.65e-143, 3.43e-143, 3.21e-143, 3.01e-143, 2.83e-143, 2.65e-143,
2.49e-143, 2.33e-143, 2.19e-143, 2.05e-143, 1.93e-143, 1.81e-143,
1.69e-143, 1.59e-143, 1.49e-143, 1.4e-143, 1.31e-143, 1.23e-143,
1.15e-143, 1.08e-143, 1.01e-143, 9.51e-144, 8.92e-144, 8.37e-144,
7.85e-144, 7.36e-144, 6.9e-144, 6.47e-144, 6.07e-144, 5.69e-144,
5.34e-144, 5.01e-144, 4.7e-144, 4.4e-144, 4.13e-144, 3.87e-144,
3.63e-144, 3.41e-144, 3.19e-144, 2.99e-144, 2.81e-144, 2.63e-144,
2.47e-144, 2.32e-144, 2.17e-144, 2.04e-144, 1.91e-144, 1.79e-144,
1.68e-144, 1.57e-144, 1.48e-144, 1.38e-144, 1.3e-144, 1.22e-144,
1.14e-144, 1.07e-144, 1e-144, 9.4e-145, 8.81e-145, 8.26e-145,
7.75e-145, 7.26e-145, 6.81e-145, 6.38e-145, 5.99e-145, 5.61e-145,
5.26e-145, 4.93e-145, 4.62e-145, 4.33e-145, 4.06e-145, 3.81e-145,
3.57e-145, 3.35e-145, 3.14e-145, 2.94e-145, 2.76e-145, 2.59e-145,
2.42e-145, 2.27e-145, 2.13e-145, 2e-145, 1.87e-145, 1.75e-145,
1.64e-145, 1.54e-145, 1.44e-145, 1.35e-145, 1.27e-145, 1.19e-145,
1.12e-145, 1.05e-145, 9.8e-146, 9.18e-146, 8.61e-146, 8.07e-146,
7.56e-146, 7.09e-146, 6.64e-146, 6.23e-146, 5.84e-146, 5.47e-146,
5.13e-146, 4.8e-146, 4.5e-146, 4.22e-146, 3.95e-146, 3.71e-146,
3.47e-146, 3.26e-146, 3.05e-146, 2.86e-146, 2.68e-146, 2.51e-146,
2.35e-146, 2.21e-146, 2.07e-146, 1.94e-146, 1.81e-146, 1.7e-146,
1.59e-146, 1.49e-146, 1.4e-146, 1.31e-146, 1.23e-146, 1.15e-146,
1.08e-146, 1.01e-146, 9.47e-147, 8.88e-147, 8.32e-147, 7.79e-147,
7.3e-147, 6.84e-147, 6.41e-147, 6.01e-147, 5.63e-147)), .Names = c("X",
"Y"), class = "data.frame", row.names = c(NA, -1000L))
Code
plot(x=df$X, y=df$Y)
with ggplot2
library(ggplot2)
library(scales)
p <- ggplot(data=df, aes(x=X, y=Y))+geom_point()
p
I could not figure out how to set the ylim in ggplot2 to get the right graph.
If you absolutely want to use ggplot2, then you can use scaling and labeling to work around the bug until it is fixed:
# a scaling constant
s <- 1E120
# scale Y to get around bug, but then unscale Y's labels
ggplot(df, aes(x=X, y=Y*s)) +
geom_point() +
scale_y_continuous(labels=function(x) as.character(x/s)) +
ylab("Y")
Related
I am looking for a way to calculate the formulas in the code below based on the last four observations. The results should then be saved in the last used observations. Meaning if the beta is calculated using the first four observations then the beta value should be saved in the fourth row. This should be repeated in a loop for the entire dataset. I have no idea how to implement this into my current code (attached below) and I am therefore looking for help in calculating this rolling window value. Another property I would like is for the beta to be missing whenever an observation is missing but this should be, to my understanding, be happening automatically.
Thanks in advance
My current code looks like this:
risk <- matrix(data = NA, 14660, ncol = 3)
for (i in 1:14657) {
j <- i+3
risk[j,1] <- cov(CombData[i:j,c(72)],CombData[i:j,c(79)])/var(CombData[i:j,c(79)])
reg_risk <- lm(CombData[i:j,c(72)] ~ CombData[i:j,c(79)])
risk[j,2] <- sigma(reg_risk)
risk[j,3] <- sd(CombData[i:j,c(72)])
}
My data looks like this:
structure(list(ExReturn = c(NA, 0.0138248847926268, 0.165809090909091,
-0.00204931773879148, -0.091896875, 0.0343086021505376, 0.0331640332640333,
-0.118812273641851, 0.0158817351598173, 0.0920348314606742, 0.160393827160494,
0.0637297872340425, 0.0315666666666667, 0.0935995153473343, 0.0102397341211227,
0.0043859649122806, -0.00291120815138279, -0.0262773722627737,
0.0494752623688155, -0.135814285714286, 0.00165289256198342,
-0.0264026402640264, -0.0405405405405406, 0.0746478873239436,
0.0484927916120577, 0.01875, 0.0662576687116565, -0.0334716915995397,
-0.025, 0.16971916971917, 0.0364344467640918, -0.0192339375629406,
0.00606016427104715, 0.00500204081632648, -0.0132979695431472,
0.0288065843621399, 0, -0.017, 0.00203458799593093, -0.016243654822335,
0.0309597523219813, -0.0230230230230231, 0.00307377049180335,
0.0469867211440245, 0.0253658536585366, 0.0466222645099905, 0.0181818181818181,
-0.0178571428571429, -0.0227272727272727, 0.0186046511627906,
-0.0182648401826484, 0.0437209302325581, 0.0204991087344029,
0.0122270742358079, -0.0163934426229508, 0, -0.0719298245614035,
-0.0737240075614367, -0.0214285714285715, 0.032325338894682,
0.0434343434343434, 0.0658276863504357, 0.064486830154405, 0.0238907849829351,
-0.0808333333333333, 0.029918404351768, -0.0536971830985915,
-0.0148837209302326, -0.00188857412653443, 0.0236518448438978,
-0.0259780036968577, -0.0589235294117647, -0.00826451612903225,
0.00081626016260159, -0.0173588832487309, 0.146594214876033,
0.0493495495495495, 0.0882120171673819, -0.0238593059936908,
-0.0090852988691438, 0.0226198859005706, -0.00966175298804784,
-0.00190901045856798, 0.00282320709105561, 0.00200963855421694,
0.00851410256410264, 0.0352426528991263, 0.040044171779141, 0.00161075902726597,
-0.0264352941176471, -0.00920188679245281, 0.00366621004566204,
0.0028878787878789, -0.0158943396226415, -0.00549770114942526,
-0.00417929176289457, 0.00276100386100385, 0.0295692307692307,
0.0321820895522387, 0.0238707581227436, 0.0373323943661972, -0.0185491525423728,
0.00529655172413794, -0.0151986301369864, 0.0258777777777777,
-0.116664864864865, 0.0133671755725191, -0.0021, 0.00947819548872178,
-0.0242048327137546, 0.0511319391634981, -0.0454212996389891,
-0.0168943396226415, -0.0287199233716475, -0.017348031496063,
0.0182, -0.0015, 0.0340941176470589, 0.0213272727272727, 0.00240370370370376,
NA, -0.0796460176991151, 0.102464102564103, -0.0989372093023255,
-0.0936483870967742, -0.0677156583629893, 0.114403816793893,
-0.0446205479452054, 0.0285738351254481, 0.0138372822299653,
0.109865635738832, 0.114451083591331, -0.0723222222222222, -0.0390221556886228,
-0.00321526479750779, 0.046875, -0.0686567164179105, -0.092948717948718,
0.137809187279152, -0.109803921568628, 0.118942731277533, -0.0866141732283464,
0.021551724137931, 0.0970464135021096, 0.0448717948717949, -0.0521472392638037,
-0.0971873786407767, 0.0250896057347669, 0, -0.15044965034965,
0.0204761316872428, -0.104938709677419, -0.0496495495495496,
-0.0996260663507109, 0.142105263157895, -0.119815668202765, 0.0523560209424083,
-0.0149253731343284, -0.0252525252525253, -0.16580310880829,
0.118012422360248, 0.0166666666666666, 0.0163934426229508, 0.010752688172043,
-0.0904255319148937, -0.0701754385964912, -0.0251572327044025,
0.135483870967742, 0, -0.0209790209790209, -0.0714285714285714,
-0.115384615384615, -0.0260869565217391, 0.0982142857142858,
-0.130081300813008, 0.02803738317757, -0.0545454545454546, 0,
-0.109677419354839, -0.130434782608696, -0.0666666666666667,
0, -0.157142857142857, -0.0689655172413793, -0.0462962962962963,
-0.00773358778625955, -0.0463538461538461, 0.00786451612903225,
-1e-04, -0.0161, -0.105891056910569, 0.0998000000000001, 0.0493867768595042,
0.0470440944881889, -0.00771879699248126, -0.136463636363636,
-0.0178438596491229, 0.0353142857142858, -0.0176413793103449,
0.0172438596491229, 0.0339827586206897, -0.0339333333333333,
-0.0437034482758621, 0.026327027027027, -0.0710754385964912,
-0.0103339622641509, -0.0485190476190477, -0.0587999999999999,
-0.0561016985138004, 0.0191247191011236, -0.0649766519823788,
-0.144729411764706, -0.0783230769230769, -0.0728285714285715,
-0.0815282051282052, -0.127035540069686, -0.133074103585657,
-0.0960176470588235, -0.0560568345323741, -0.127275285171103,
-0.149081627296588, -0.0892524714828897, -0.156066666666667,
0.160879510703364, -0.122452631578947, 0.0915143712574852, NA,
-0.0886956521739131, -0.0268175572519084, -0.114854098360656,
-1e-04, 0.0276777777777777, -0.0631630630630631, -0.0481769230769231,
-0.0102010101010101, -0.0531612244897959, 0.0343827586206897,
-0.00426666666666665, -0.0461251046025104, 0.0415666666666667,
-0.116842105263158, 0.0715137067938021, -0.110122358175751, 0.0249999999999999,
0.158436585365854, -0.00421052631578944, -0.101479915433404,
0.0352941176470589, 0, -0.0113636363636364, -0.0114942528735632,
0.0232558139534884, 0.00356972477064209, 0.0329067641681902,
0.00884955752212391, 0.0700754385964912, 0.0162934426229508,
0.096674193548387, -1e-04, -0.0295117647058824, -0.0303030303030303,
0, 0, 0, 0.015625, 0, 0, 0.0384615384615385, -0.00740740740740742,
-0.0328358208955224, -0.00462962962962965, 0.130232558139535,
-0.0123456790123457, -0.0277777777777778, 0.1, 0.0714285714285714,
0.175757575757576, -0.00515463917525771, 0.0611398963730569,
0.03515625, -0.108490566037736, -0.0338624338624338, -0.0131434830230011,
0.0288568257491675, -0.0258899676375405, 0.00775193798449614,
-0.0109890109890109, 0, -0.0555555555555556, -0.0352941176470588,
0.0317073170731708, 0.0401891252955082, 0.00681818181818183,
-0.0519187358916479, -0.107142857142857, 0.133233333333333, -0.0706882352941176,
-0.0141240506329114, -0.00661848523748401, 0.00635994832041352,
0.0217228498074455, -0.00773768844221103, -0.0267822784810127,
0.0440132639791937, 0.00851731008717314, 0.00350370370370376,
0.00851008610086111, -0.0149341463414634, -0.00411287128712872,
0.00208447204968949, -0.00897410161090454, -0.0255, 0.0109384615384615,
0.0450273764258555, -0.0334272727272727, -0.025962656641604,
-0.026606940874036, -0.0431163588390502, -0.0366126721763086,
-0.0151857142857142, 0.0133927536231885, -0.0011, 0.00594285714285712,
-0.136151773049645, 0.0559770491803278, -0.0014, -0.0016, 0.0139038759689923,
-0.0473015267175572, 0.0301, -0.0483116279069767, -0.0100300813008131,
0.0142934426229508, 0.0223935483870968, -0.0019, -0.0257220472440944,
-0.0262935483870968, -0.0018, -0.0266933884297521, -0.060922033898305,
-0.0738720720720721, -0.0209174757281553, -0.0150613861386139,
-0.178106827309237, 0.130407317073171, NA, -0.0618336886993603,
0.0794454545454546, 0.0314789473684211, -0.122548979591837, -0.00475116279069766,
-0.00594112149532712, 0.0175263219741482, -0.0532177829099307,
0.024290243902439, -0.056052380952381, 0.0503413619167718, -0.0685273709483794,
-0.0490690721649485, 0.070360704607046, 0, -0.029113924050633,
-0.0365058670143416, -0.00541271989174563, 0.0284714285714285,
-0.0555555555555556, -0.0336134453781513, -0.063768115942029,
-0.086687306501548, 0.123728813559322, -0.0253164556962026, -0.0909090909090909,
0.0513274336283185, -0.0420875420875421, 0.0216983651226159,
0.0265666666666666, -0.103996103896104, 0.0752623188405796, 0.0242587601078168,
0.102631578947368, 0.121718377088305, -0.0853658536585366, -0.0580952380952381,
0.0556117290192113, -0.0229885057471264, -0.0392156862745098,
0.0265306122448981, 0.0735586481113319, -0.05, -0.00584795321637432,
0.0686274509803921, 0.00550458715596336, 0.0109489051094891,
0.0415162454873645, 0.050259965337955, 0.0297029702970297, 0.0657051282051282,
0.018045112781955, -0.051698670605613, 0.0202492211838006, -0.0229007633587787,
-0.00156250000000002, 0.0453834115805947, -0.0494011976047904,
0.0692913385826772, -0.00589101620029453, -0.0237037037037037,
0.0318664643399089, 0, -0.0176470588235295, -0.00748502994011979,
-0.0271493212669683, 0.0200550387596898, -0.074568085106383,
-0.0231885057471264, -0.0438974789915966, 0.0192321616871704,
-0.0173413793103449, 0.00506315789473686, 0.036449214659686,
0.0940760942760943, 0.0582615384615386, 0.118986046511628, -0.00919090909090904,
0.0246017038007864, 0.153052685421995, -0.0746793791574279, 0.0535922155688624,
0.1245, -0.0268626262626262, 0.0159975103734439, 0.0197081632653061,
0.0831000000000001, -0.0654756457564576, -0.020623865877712,
-8e-04, -0.0501957746478874, -0.0116820105820106, 0.0256379679144386,
0.0404666666666667, -0.0214, -0.0371142857142857, -0.0014, -0.00689100529100535,
-0.0707489361702128, 0.00992857142857134, -0.0527474576271186,
-0.0672761904761905, -0.0273777070063694, -0.0936032679738562,
-0.138490647482014, 0.0314333333333334, -0.0182290322580645,
-0.0184934426229508, 0.0065333333333333, 0.0146289256198347,
-0.074770731707317, 0.0508315789473684, -0.0015, 0.0071333333333333,
0.0151289256198347, 0.0149601626016261, NA, -0.0815850815850816,
-0.0863944162436549, 0.0415666666666667, -0.106766666666667,
-0.0239805970149254, -0.0215067278287462, 0.059275, -0.0266486725663717,
-0.0152515151515151, 0.0768230769230769, 0.0427571428571429,
-0.106949315068493, 0.0520472392638036, -0.0234236151603499,
-0.0149253731343284, 0.00909090909090904, 0.0210210210210211,
-0.0588235294117647, -1e-04, -0.078125, -0.0305084745762711,
-0.0909090909090909, -0.1, -0.0907407407407408, 0.0386965376782078,
-0.125590196078431, -0.0582959641255605, 0.0857142857142856,
0.0525315789473684, -0.0292666666666667, -0.0473103004291845,
-0.121721621621622, -0.128305128205128, -0.0647058823529412,
-0.0628930817610063, 0, 0.00671140939597326, -0.0133333333333333,
0.0304054054054055, -0.0163934426229508, 0.133333333333333, 0,
0.088235294117647, 0.164864864864865, -0.0672853828306265, 0.0223880597014925,
0.0900243309002433, 0.142857142857143, -0.04296875, 0.0775510204081633,
-0.0549242424242424, 0.0160320641282565, -0.00591715976331364,
-0.0595238095238095, -0.0126582278481012, 0.0277777777777777,
-0.027027027027027, -0.0427350427350427, 0.0334821428571428,
-0.041036717062635, 0.072072072072072, -0.023109243697479, -0.0150537634408602,
0, 0.00655021834061142, -0.0520607375271149, 0.0274599542334095,
0.0289532293986636, 0.0691640692640694, -0.0102214574898786,
0.0222948875255624, -0.0142, -0.0244407707910751, -0.0520750519750519,
-0.0659894736842105, -0.00254741784037562, 0.122152941176471,
0.050114465408805, 0.0177640718562875, 0.031272549019608, -0.0170666666666667,
-0.107210039113429, 0.0856313868613138, 0.0410666666666667, -0.056083870967742,
0.0293546448087431, -0.014162599469496, 0.0419893905191874, -0.0375965367965368,
-0.0391022471910113, 0.0316102803738317, 0.00794977375565608,
0.0391587443946187, 0.0115310344827587, -0.0311872340425532,
-0.0145578947368421, 0.0161777777777777, 0.00276681222707414,
0.0202391304347827, -0.0657297872340425, -0.010890909090909,
-0.020248623853211, 0.00724579439252326, -0.0295777777777778,
0.00286190476190475, -0.035275355450237, 0.0175078431372548,
-0.0546846153846154, -0.0323568527918782, -0.0330136125654451,
0.00360540540540535, 0.020005376344086, -0.0801473684210527,
0.0157428571428571, 0.0155539325842696, NA, 0.0446428571428572,
-0.0513820512820513, -0.0631630630630631, -0.0654846153846154,
0.00813045267489708, -0.00418163265306127, -0.180427868852459,
0.1049, -0.0589235294117647, -0.122696153846154, 0.021817808219178,
0.13931018766756, 0.0516647058823529, -0.125379642058166, -0.0153452685421995,
0.025974025974026, -0.0151898734177216, -0.017994858611825, -0.036749214659686,
-0.0624999999999999, -0.0434782608695652, 0, 0.0606060606060606,
0, 0.114285714285714, -0.0769230769230769, -0.0416666666666666,
0.0288855072463767, -0.0422535211267606, -0.0294117647058824,
0.0756575757575757, -0.0423535211267606, 0.0293117647058822,
-0.1001, 0.0792650793650793, 0.102941176470588, -0.0613333333333332,
0.0235294117647058, -0.0114942528735632, 0.13953488372093, -0.0346938775510205,
-0.0380549682875264, -0.0527472527472527, 0, -0.0255220417633411,
0.0428571428571427, -0.0593607305936071, -0.0412621359223302,
-0.0303797468354431, -0.00783289817232369, 0.0105263157894737,
-0.0651041666666666, -0.00278551532033433, 0.0307262569832403,
-0.0948509485094851, 0.00299401197604787, 0.0149253731343284,
-0.0382352941176469, -0.0336391437308869, 0.0379746835443036,
-0.0853658536585366, 0, 0.03, 0, -0.00323624595469252, -0.0194805194805195,
0, 0.0132450331125828, 0.0196078431372548, -0.0385615384615384,
-1e-04, 0.0264666666666666, 0.0127870129870129, -0.0225358974358975,
0.0818672131147542, -0.0395939393939394, -0.0191274447949526,
0.044816077170418, 0.0551846153846154, -0.0118618075801749, 0.00284985250737478,
0.00264117647058822, 0.0993067448680351, 0.0822666666666667,
-0.111137438423645, -0.00327008310249316, -6e-04, -0.0144888888888888,
0.0133845070422535, 0.0129888888888888, 0.0127986301369864, 0.00180270270270268,
0.0369358490566038, -9e-04, -0.040061038961039, 0.025927027027027,
0.0251157894736843, -0.0629384615384615, -0.0287224043715847,
-0.0182539325842697, -0.0587428571428572, 0.101430303030303,
-0.00699450549450536, -0.106872375690608, 0.0475827160493827,
-0.0548411764705882, 0.159390683229814, -0.0873614973262031,
0.00394795321637421, 0.102551162790698, -0.0231526315789473,
-0.0824451612903226, 0.12090701754386, -0.0120166666666666, -0.0281157894736842,
-0.0015, 0.0096108108108107, 0.0627711229946524, -0.066626633165829,
NA, -0.05, 0.0512157894736842, -0.048911013767209, -0.0685210526315789,
0.172216384180791, -0.0241963855421686, -0.0618283950617285,
0.0525315789473684, -0.00259999999999995, 0.0525315789473684,
0.0118047619047619, -1e-04, 0.0304882352941176, 0.0387127853881279,
-0.0384615384615384, 0.04, -0.021978021978022, -0.0561797752808989,
-1e-04, -0.0238095238095238, 0.024390243902439, 0.0476190476190477,
0.0772727272727274, -0.0295358649789029, 0.0326086956521738,
0.0210526315789474, -0.0123711340206185, -0.0606427974947808,
0.108888888888889, -0.0581162324649298, 0.0105382978723405, -1e-04,
-0.0737842105263158, 0.0271727272727273, -0.00452477876106195,
0.0222222222222221, -0.0152173913043478, -0.00662251655629142,
0, 0.00555555555555554, -0.00331491712707177, 0.0565410199556542,
-0.0451206715634838, 0.0879120879120878, -0.0909090909090909,
0.0222222222222221, 0.0326086956521738, 0.11578947368421, -0.0415094339622641,
-0.015748031496063, -0.023, 0.105424769703173, -0.0611111111111111,
0.0157790927021697, -0.0485436893203883, 0.0265306122448981,
-0.104373757455268, 0.1165371809101, 0.0318091451292246, 0.0385356454720616,
-0.0575139146567718, 0.0925196850393701, 0, -0.0720720720720721,
0.029126213592233, 0.00943396226415105, -0.0186915887850467,
-0.123809523809524, 0.0652173913043479, -0.122448979591837, -0.0117279069767442,
-0.028335294117647, -0.0679966101694916, 0.0387610389610389,
-1e-04, -0.0251, 0.00877435897435894, -0.00909453621346892, -0.0143025641025641,
0.0141042912873863, -0.0386615384615384, 0.0132333333333334,
0.112857894736842, 0.0279687943262412, -0.0532735632183908, 0.0361077669902914,
0.043996487119438, -0.0499273542600897, 0.0135509433962264, 0.0225558139534884,
-0.0463545454545454, 0.0229095238095237, -9e-04, -0.0217302325581395,
0.0608577197149644, -0.0167599552572707, -0.0283727272727272,
-0.0105457943925234, -0.0344188679245283, 0.0717707317073171,
0.0258727272727273, 0.0470725663716814, -0.0522329113924051,
-0.0015, -0.0641222222222222, 0.0740293838862558, -0.054763436123348,
0.0118534883720931, 0.0394844036697249, -0.0459528634361234,
-0.00670829493087556, 0.0210481481481481, -0.0198995475113123,
0.030358064516129, -0.0685642857142857, 0.0364775119617225, 0.030758064516129,
0.00326428571428581, 0.0119333333333334, -0.00568596491228072,
NA, -0.0833333333333334, 0.0908090909090908, -0.1501, -0.0687274509803921,
-0.0527315789473685, -1e-04, 0.0546945205479452, -0.116983116883117,
-0.0736294117647058, 0.0395825396825398, 0.00753358778625944,
-0.121312121212121, 0.0343827586206897, 0.075, -0.00775193798449614,
0.015625, -0.123076923076923, 0.0963912280701753, 0, -0.0640000000000001,
0.0256410256410258, 0.15, -0.0942028985507247, -0.00800000000000001,
0.120967741935484, -0.064748201438849, 0.0537461538461539, 0,
0.0729927007299269, -0.142957142857143, 0.0713285714285714, 0.036937037037037,
0.0713285714285714, -0.0750000000000001, -0.00900900900900903,
-0.0454545454545454, 0.142857142857143, -0.166666666666667, 0,
0.12, -0.142857142857143, 0.104166666666667, 0.0566037735849056,
-0.0267857142857142, -0.0733944954128442, 0.0495049504950495,
-0.0377358490566038, 0.0490196078431373, 0.121495327102804, 0,
-0.0158730158730158, 0.0161290322580645, -0.158730158730159,
-0.150943396226415, 0.111111111111111, 0.01, 0.0693069306930694,
0.0833333333333333, 0.111111111111111, -0.0461538461538461, 0,
-0.0403225806451613, 0.00840336134453779, -0.0750000000000001,
0.0180180180180181, -0.0353982300884956, 0.100817431192661, -0.0751000000000001,
-0.150575939849624, -0.0266486725663717, 0.127172727272727, 0.032058064516129,
0.015425, 0.122876923076923, 0.0134986301369864, 0.114664864864865,
0.0180818181818183, 0.160414285714286, 0.148317948717949, 0.0531714285714286
), `Mkt-RF` = c(-0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223, -0.1231, 0.0713,
0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249,
-0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704, 0.0449,
0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077, -0.0012,
-0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621,
-0.0228, 0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427,
0.0619, -0.0149, -0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038,
-0.0501, 0.0444, 0.0077, 0.0095, -0.0308, -0.025, 0.0472, 0.0284,
0.0068, 0.0427, 0.0463, 0.0503, -0.0067, 0.0349, 0.0011, 0.0272,
0.0059, -1e-04, 0.0147, 0.0552, -0.0535, -0.0071, 0.022, -0.0273,
-0.0092, 0.0269, -0.0265, 0.0012, -0.0854, -0.0131, -0.0459,
0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587, -0.0256, -0.0239,
0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528, -0.021, 0.0641,
-0.0166, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156, 0.0498, -0.0803,
0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306, -0.0205, -0.0333,
-0.1161, 0.1097, -0.0485, -0.0221, 0.0569, 2e-04, -0.0223, -0.1231,
0.0713, 0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0077, -0.0012,
-0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0479,
-0.0015, -0.0284, 0.0239, -0.0602, 0.0619, -0.0149, -0.0643,
-0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077, 0.0095,
-0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463, 0.0503,
-0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147, 0.0552,
-0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265, -0.0854,
0.0619, 0.0293, 0.037, 0.0587, -0.0256, 0.0178, 0.0407, -0.0255,
-0.0528, -0.021, 0.0641, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156,
0.0498, -0.0803, 0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306,
-0.0205, -0.0333, -0.1011, -0.1161, 0.1097, -0.0485, -0.0221,
0.0569, 0.0641, 2e-04, -0.1231, 0.0713, 0.0056, 0.045, 0.0349,
0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054, 0.0416, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, -0.0221, 0.0569, 2e-04, -0.0223, 0.0713, 0.0056,
0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, 0.0569, 2e-04, -0.0223, -0.1231, 0.0713, 0.0056,
0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054,
0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133,
0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041,
-2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228,
0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149,
-0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444,
0.0077, 0.0095, -0.0308, -0.025, 0.0068, 0.0427, 0.0463, 0.0503,
-0.0067, 0.0349, 0.0011, 0.0059, -1e-04, 0.0147, 0.0552, -0.0535,
-0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265, 0.0012, -0.0854,
-0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587,
-0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528,
-0.021, 0.0641, -0.0166, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156,
0.0498, -0.0803, 0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306,
-0.0205, -0.0333, -0.1011, -0.1161, 0.1097, -0.0485, -0.0221,
0.0569, 0.0641, 2e-04, -0.0223, -0.1231, 0.0713, 0.0056, 0.045,
0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, 0.0416, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223, -0.1231,
0.0713, 0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077,
-0.0012, -0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003,
0.0621, -0.0228, 0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427,
0.0619, -0.0149, -0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038,
-0.0501, 0.0444, 0.0077, 0.0095, -0.0308, -0.025, 0.0472, 0.0284,
0.0068, 0.0427, 0.0463, 0.0503, -0.0067, 0.0349, 0.0011, 0.0272,
0.0059, -1e-04, 0.0147, 0.0552, -0.0535, -0.0071, 0.022, -0.0273,
-0.0092, 0.0269, -0.0265, 0.0012, -0.0854, -0.0131, -0.0459,
0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587, -0.0256, -0.0239,
0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528, -0.021, 0.0641,
-0.0166, -0.1206, -0.0097, 0.1188, 0.1156, 0.0498, -0.0803, 0.0904,
0.0368, 0.0306, -0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011,
-0.1161, 0.1097, -0.0485, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223,
-0.1231, 0.0713, 0.0056, 0.045, 0.0349, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, -0.041,
-2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228,
0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149,
-0.0186, -0.0643, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -900L))
This code works:
CombData <- CombData[order(CombData$ISIN),]
CombData2 <- unlist(CombData$ExReturn)
CombData3 <- unlist(CombData$`Mkt-RF`)
risk <- matrix(data = NA, 14660, ncol = 3)
for (i in 1:14657) {
j <- i+3
risk[j,1] <- cov(CombData[i:j,c(72)],CombData[i:j,c(79)])/var(CombData[i:j,c(79)])
reg_risk <- lm(CombData2[i:j, 1] ~ CombData3[i:j, 1], na.action = na.fail)
risk[j,2] <- sigma(reg_risk)
risk[j,3] <- sd(CombData2[i:j, ])
}
Define a function stats to compute the statistics given a 4x2 input and then use rollapplyr to apply it to the 900x2 data in the question in a rolling fashion.
library(zoo)
stats <- function(x) {
reg_risk <- lm(x[, 1] ~ x[, 2])
c(cov = cov(x[, 1], x[, 2]), sigma = sigma(reg_risk), sd = sd(x[, 1]))
}
cols <- c(1, 2) # use columns 1 and 2
risk <- rollapplyr(CombData[cols], 4, stats, by.column = FALSE, fill = NA)
risk
giving:
cov sigma sd
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
[4,] NA 0.01720850 NA
[5,] 0.007823542 0.03307555 0.10692274
[6,] 0.007870034 0.03236960 0.10692813
[7,] 0.004845966 0.04012509 0.05930418
[8,] 0.005937995 0.06735241 0.08105379
[9,] 0.004039549 0.06844512 0.07378167
[10,] 0.004691849 0.07836488 0.08909710
...snip...
I am looking for a way to calculate the formulas in the code below based on the last four observations. The results should then be saved in the same row as the last used observations. Meaning if the beta is calculated using the first four observations then the beta value should be saved in the fourth row. This should be repeated in a loop for the entire dataset. However, when running the code attached below I get the error:
Error in CombData2[i:j, 1] : incorrect number of dimensions
Another property I would like is for the beta, sd or sigma to be missing whenever an observation is missing. The error occurs when estimating the linear model. My current code is attached below:
CombData2 <- unlist(CombData$ExReturn)
CombData3 <- unlist(CombData$`Mkt-RF`)
risk <- matrix(data = NA, 14660, ncol = 3)
for (i in 1:14657) {
j <- i+3
risk[j,1] <- cov(CombData[i:j,c(72)],CombData[i:j,c(79)])/var(CombData[i:j,c(79)])
reg_risk <- lm(CombData2[i:j, 1] ~ CombData3[i:j, 1], na.action = na.fail)
risk[j,2] <- sigma(reg_risk)
risk[j,3] <- sd(CombData2[i:j, ])
}
Thanks in advance
The first 900 observations of my current data looks like this:
structure(list(ExReturn = c(NA, 0.0138248847926268, 0.165809090909091,
-0.00204931773879148, -0.091896875, 0.0343086021505376, 0.0331640332640333,
-0.118812273641851, 0.0158817351598173, 0.0920348314606742, 0.160393827160494,
0.0637297872340425, 0.0315666666666667, 0.0935995153473343, 0.0102397341211227,
0.0043859649122806, -0.00291120815138279, -0.0262773722627737,
0.0494752623688155, -0.135814285714286, 0.00165289256198342,
-0.0264026402640264, -0.0405405405405406, 0.0746478873239436,
0.0484927916120577, 0.01875, 0.0662576687116565, -0.0334716915995397,
-0.025, 0.16971916971917, 0.0364344467640918, -0.0192339375629406,
0.00606016427104715, 0.00500204081632648, -0.0132979695431472,
0.0288065843621399, 0, -0.017, 0.00203458799593093, -0.016243654822335,
0.0309597523219813, -0.0230230230230231, 0.00307377049180335,
0.0469867211440245, 0.0253658536585366, 0.0466222645099905, 0.0181818181818181,
-0.0178571428571429, -0.0227272727272727, 0.0186046511627906,
-0.0182648401826484, 0.0437209302325581, 0.0204991087344029,
0.0122270742358079, -0.0163934426229508, 0, -0.0719298245614035,
-0.0737240075614367, -0.0214285714285715, 0.032325338894682,
0.0434343434343434, 0.0658276863504357, 0.064486830154405, 0.0238907849829351,
-0.0808333333333333, 0.029918404351768, -0.0536971830985915,
-0.0148837209302326, -0.00188857412653443, 0.0236518448438978,
-0.0259780036968577, -0.0589235294117647, -0.00826451612903225,
0.00081626016260159, -0.0173588832487309, 0.146594214876033,
0.0493495495495495, 0.0882120171673819, -0.0238593059936908,
-0.0090852988691438, 0.0226198859005706, -0.00966175298804784,
-0.00190901045856798, 0.00282320709105561, 0.00200963855421694,
0.00851410256410264, 0.0352426528991263, 0.040044171779141, 0.00161075902726597,
-0.0264352941176471, -0.00920188679245281, 0.00366621004566204,
0.0028878787878789, -0.0158943396226415, -0.00549770114942526,
-0.00417929176289457, 0.00276100386100385, 0.0295692307692307,
0.0321820895522387, 0.0238707581227436, 0.0373323943661972, -0.0185491525423728,
0.00529655172413794, -0.0151986301369864, 0.0258777777777777,
-0.116664864864865, 0.0133671755725191, -0.0021, 0.00947819548872178,
-0.0242048327137546, 0.0511319391634981, -0.0454212996389891,
-0.0168943396226415, -0.0287199233716475, -0.017348031496063,
0.0182, -0.0015, 0.0340941176470589, 0.0213272727272727, 0.00240370370370376,
NA, -0.0796460176991151, 0.102464102564103, -0.0989372093023255,
-0.0936483870967742, -0.0677156583629893, 0.114403816793893,
-0.0446205479452054, 0.0285738351254481, 0.0138372822299653,
0.109865635738832, 0.114451083591331, -0.0723222222222222, -0.0390221556886228,
-0.00321526479750779, 0.046875, -0.0686567164179105, -0.092948717948718,
0.137809187279152, -0.109803921568628, 0.118942731277533, -0.0866141732283464,
0.021551724137931, 0.0970464135021096, 0.0448717948717949, -0.0521472392638037,
-0.0971873786407767, 0.0250896057347669, 0, -0.15044965034965,
0.0204761316872428, -0.104938709677419, -0.0496495495495496,
-0.0996260663507109, 0.142105263157895, -0.119815668202765, 0.0523560209424083,
-0.0149253731343284, -0.0252525252525253, -0.16580310880829,
0.118012422360248, 0.0166666666666666, 0.0163934426229508, 0.010752688172043,
-0.0904255319148937, -0.0701754385964912, -0.0251572327044025,
0.135483870967742, 0, -0.0209790209790209, -0.0714285714285714,
-0.115384615384615, -0.0260869565217391, 0.0982142857142858,
-0.130081300813008, 0.02803738317757, -0.0545454545454546, 0,
-0.109677419354839, -0.130434782608696, -0.0666666666666667,
0, -0.157142857142857, -0.0689655172413793, -0.0462962962962963,
-0.00773358778625955, -0.0463538461538461, 0.00786451612903225,
-1e-04, -0.0161, -0.105891056910569, 0.0998000000000001, 0.0493867768595042,
0.0470440944881889, -0.00771879699248126, -0.136463636363636,
-0.0178438596491229, 0.0353142857142858, -0.0176413793103449,
0.0172438596491229, 0.0339827586206897, -0.0339333333333333,
-0.0437034482758621, 0.026327027027027, -0.0710754385964912,
-0.0103339622641509, -0.0485190476190477, -0.0587999999999999,
-0.0561016985138004, 0.0191247191011236, -0.0649766519823788,
-0.144729411764706, -0.0783230769230769, -0.0728285714285715,
-0.0815282051282052, -0.127035540069686, -0.133074103585657,
-0.0960176470588235, -0.0560568345323741, -0.127275285171103,
-0.149081627296588, -0.0892524714828897, -0.156066666666667,
0.160879510703364, -0.122452631578947, 0.0915143712574852, NA,
-0.0886956521739131, -0.0268175572519084, -0.114854098360656,
-1e-04, 0.0276777777777777, -0.0631630630630631, -0.0481769230769231,
-0.0102010101010101, -0.0531612244897959, 0.0343827586206897,
-0.00426666666666665, -0.0461251046025104, 0.0415666666666667,
-0.116842105263158, 0.0715137067938021, -0.110122358175751, 0.0249999999999999,
0.158436585365854, -0.00421052631578944, -0.101479915433404,
0.0352941176470589, 0, -0.0113636363636364, -0.0114942528735632,
0.0232558139534884, 0.00356972477064209, 0.0329067641681902,
0.00884955752212391, 0.0700754385964912, 0.0162934426229508,
0.096674193548387, -1e-04, -0.0295117647058824, -0.0303030303030303,
0, 0, 0, 0.015625, 0, 0, 0.0384615384615385, -0.00740740740740742,
-0.0328358208955224, -0.00462962962962965, 0.130232558139535,
-0.0123456790123457, -0.0277777777777778, 0.1, 0.0714285714285714,
0.175757575757576, -0.00515463917525771, 0.0611398963730569,
0.03515625, -0.108490566037736, -0.0338624338624338, -0.0131434830230011,
0.0288568257491675, -0.0258899676375405, 0.00775193798449614,
-0.0109890109890109, 0, -0.0555555555555556, -0.0352941176470588,
0.0317073170731708, 0.0401891252955082, 0.00681818181818183,
-0.0519187358916479, -0.107142857142857, 0.133233333333333, -0.0706882352941176,
-0.0141240506329114, -0.00661848523748401, 0.00635994832041352,
0.0217228498074455, -0.00773768844221103, -0.0267822784810127,
0.0440132639791937, 0.00851731008717314, 0.00350370370370376,
0.00851008610086111, -0.0149341463414634, -0.00411287128712872,
0.00208447204968949, -0.00897410161090454, -0.0255, 0.0109384615384615,
0.0450273764258555, -0.0334272727272727, -0.025962656641604,
-0.026606940874036, -0.0431163588390502, -0.0366126721763086,
-0.0151857142857142, 0.0133927536231885, -0.0011, 0.00594285714285712,
-0.136151773049645, 0.0559770491803278, -0.0014, -0.0016, 0.0139038759689923,
-0.0473015267175572, 0.0301, -0.0483116279069767, -0.0100300813008131,
0.0142934426229508, 0.0223935483870968, -0.0019, -0.0257220472440944,
-0.0262935483870968, -0.0018, -0.0266933884297521, -0.060922033898305,
-0.0738720720720721, -0.0209174757281553, -0.0150613861386139,
-0.178106827309237, 0.130407317073171, NA, -0.0618336886993603,
0.0794454545454546, 0.0314789473684211, -0.122548979591837, -0.00475116279069766,
-0.00594112149532712, 0.0175263219741482, -0.0532177829099307,
0.024290243902439, -0.056052380952381, 0.0503413619167718, -0.0685273709483794,
-0.0490690721649485, 0.070360704607046, 0, -0.029113924050633,
-0.0365058670143416, -0.00541271989174563, 0.0284714285714285,
-0.0555555555555556, -0.0336134453781513, -0.063768115942029,
-0.086687306501548, 0.123728813559322, -0.0253164556962026, -0.0909090909090909,
0.0513274336283185, -0.0420875420875421, 0.0216983651226159,
0.0265666666666666, -0.103996103896104, 0.0752623188405796, 0.0242587601078168,
0.102631578947368, 0.121718377088305, -0.0853658536585366, -0.0580952380952381,
0.0556117290192113, -0.0229885057471264, -0.0392156862745098,
0.0265306122448981, 0.0735586481113319, -0.05, -0.00584795321637432,
0.0686274509803921, 0.00550458715596336, 0.0109489051094891,
0.0415162454873645, 0.050259965337955, 0.0297029702970297, 0.0657051282051282,
0.018045112781955, -0.051698670605613, 0.0202492211838006, -0.0229007633587787,
-0.00156250000000002, 0.0453834115805947, -0.0494011976047904,
0.0692913385826772, -0.00589101620029453, -0.0237037037037037,
0.0318664643399089, 0, -0.0176470588235295, -0.00748502994011979,
-0.0271493212669683, 0.0200550387596898, -0.074568085106383,
-0.0231885057471264, -0.0438974789915966, 0.0192321616871704,
-0.0173413793103449, 0.00506315789473686, 0.036449214659686,
0.0940760942760943, 0.0582615384615386, 0.118986046511628, -0.00919090909090904,
0.0246017038007864, 0.153052685421995, -0.0746793791574279, 0.0535922155688624,
0.1245, -0.0268626262626262, 0.0159975103734439, 0.0197081632653061,
0.0831000000000001, -0.0654756457564576, -0.020623865877712,
-8e-04, -0.0501957746478874, -0.0116820105820106, 0.0256379679144386,
0.0404666666666667, -0.0214, -0.0371142857142857, -0.0014, -0.00689100529100535,
-0.0707489361702128, 0.00992857142857134, -0.0527474576271186,
-0.0672761904761905, -0.0273777070063694, -0.0936032679738562,
-0.138490647482014, 0.0314333333333334, -0.0182290322580645,
-0.0184934426229508, 0.0065333333333333, 0.0146289256198347,
-0.074770731707317, 0.0508315789473684, -0.0015, 0.0071333333333333,
0.0151289256198347, 0.0149601626016261, NA, -0.0815850815850816,
-0.0863944162436549, 0.0415666666666667, -0.106766666666667,
-0.0239805970149254, -0.0215067278287462, 0.059275, -0.0266486725663717,
-0.0152515151515151, 0.0768230769230769, 0.0427571428571429,
-0.106949315068493, 0.0520472392638036, -0.0234236151603499,
-0.0149253731343284, 0.00909090909090904, 0.0210210210210211,
-0.0588235294117647, -1e-04, -0.078125, -0.0305084745762711,
-0.0909090909090909, -0.1, -0.0907407407407408, 0.0386965376782078,
-0.125590196078431, -0.0582959641255605, 0.0857142857142856,
0.0525315789473684, -0.0292666666666667, -0.0473103004291845,
-0.121721621621622, -0.128305128205128, -0.0647058823529412,
-0.0628930817610063, 0, 0.00671140939597326, -0.0133333333333333,
0.0304054054054055, -0.0163934426229508, 0.133333333333333, 0,
0.088235294117647, 0.164864864864865, -0.0672853828306265, 0.0223880597014925,
0.0900243309002433, 0.142857142857143, -0.04296875, 0.0775510204081633,
-0.0549242424242424, 0.0160320641282565, -0.00591715976331364,
-0.0595238095238095, -0.0126582278481012, 0.0277777777777777,
-0.027027027027027, -0.0427350427350427, 0.0334821428571428,
-0.041036717062635, 0.072072072072072, -0.023109243697479, -0.0150537634408602,
0, 0.00655021834061142, -0.0520607375271149, 0.0274599542334095,
0.0289532293986636, 0.0691640692640694, -0.0102214574898786,
0.0222948875255624, -0.0142, -0.0244407707910751, -0.0520750519750519,
-0.0659894736842105, -0.00254741784037562, 0.122152941176471,
0.050114465408805, 0.0177640718562875, 0.031272549019608, -0.0170666666666667,
-0.107210039113429, 0.0856313868613138, 0.0410666666666667, -0.056083870967742,
0.0293546448087431, -0.014162599469496, 0.0419893905191874, -0.0375965367965368,
-0.0391022471910113, 0.0316102803738317, 0.00794977375565608,
0.0391587443946187, 0.0115310344827587, -0.0311872340425532,
-0.0145578947368421, 0.0161777777777777, 0.00276681222707414,
0.0202391304347827, -0.0657297872340425, -0.010890909090909,
-0.020248623853211, 0.00724579439252326, -0.0295777777777778,
0.00286190476190475, -0.035275355450237, 0.0175078431372548,
-0.0546846153846154, -0.0323568527918782, -0.0330136125654451,
0.00360540540540535, 0.020005376344086, -0.0801473684210527,
0.0157428571428571, 0.0155539325842696, NA, 0.0446428571428572,
-0.0513820512820513, -0.0631630630630631, -0.0654846153846154,
0.00813045267489708, -0.00418163265306127, -0.180427868852459,
0.1049, -0.0589235294117647, -0.122696153846154, 0.021817808219178,
0.13931018766756, 0.0516647058823529, -0.125379642058166, -0.0153452685421995,
0.025974025974026, -0.0151898734177216, -0.017994858611825, -0.036749214659686,
-0.0624999999999999, -0.0434782608695652, 0, 0.0606060606060606,
0, 0.114285714285714, -0.0769230769230769, -0.0416666666666666,
0.0288855072463767, -0.0422535211267606, -0.0294117647058824,
0.0756575757575757, -0.0423535211267606, 0.0293117647058822,
-0.1001, 0.0792650793650793, 0.102941176470588, -0.0613333333333332,
0.0235294117647058, -0.0114942528735632, 0.13953488372093, -0.0346938775510205,
-0.0380549682875264, -0.0527472527472527, 0, -0.0255220417633411,
0.0428571428571427, -0.0593607305936071, -0.0412621359223302,
-0.0303797468354431, -0.00783289817232369, 0.0105263157894737,
-0.0651041666666666, -0.00278551532033433, 0.0307262569832403,
-0.0948509485094851, 0.00299401197604787, 0.0149253731343284,
-0.0382352941176469, -0.0336391437308869, 0.0379746835443036,
-0.0853658536585366, 0, 0.03, 0, -0.00323624595469252, -0.0194805194805195,
0, 0.0132450331125828, 0.0196078431372548, -0.0385615384615384,
-1e-04, 0.0264666666666666, 0.0127870129870129, -0.0225358974358975,
0.0818672131147542, -0.0395939393939394, -0.0191274447949526,
0.044816077170418, 0.0551846153846154, -0.0118618075801749, 0.00284985250737478,
0.00264117647058822, 0.0993067448680351, 0.0822666666666667,
-0.111137438423645, -0.00327008310249316, -6e-04, -0.0144888888888888,
0.0133845070422535, 0.0129888888888888, 0.0127986301369864, 0.00180270270270268,
0.0369358490566038, -9e-04, -0.040061038961039, 0.025927027027027,
0.0251157894736843, -0.0629384615384615, -0.0287224043715847,
-0.0182539325842697, -0.0587428571428572, 0.101430303030303,
-0.00699450549450536, -0.106872375690608, 0.0475827160493827,
-0.0548411764705882, 0.159390683229814, -0.0873614973262031,
0.00394795321637421, 0.102551162790698, -0.0231526315789473,
-0.0824451612903226, 0.12090701754386, -0.0120166666666666, -0.0281157894736842,
-0.0015, 0.0096108108108107, 0.0627711229946524, -0.066626633165829,
NA, -0.05, 0.0512157894736842, -0.048911013767209, -0.0685210526315789,
0.172216384180791, -0.0241963855421686, -0.0618283950617285,
0.0525315789473684, -0.00259999999999995, 0.0525315789473684,
0.0118047619047619, -1e-04, 0.0304882352941176, 0.0387127853881279,
-0.0384615384615384, 0.04, -0.021978021978022, -0.0561797752808989,
-1e-04, -0.0238095238095238, 0.024390243902439, 0.0476190476190477,
0.0772727272727274, -0.0295358649789029, 0.0326086956521738,
0.0210526315789474, -0.0123711340206185, -0.0606427974947808,
0.108888888888889, -0.0581162324649298, 0.0105382978723405, -1e-04,
-0.0737842105263158, 0.0271727272727273, -0.00452477876106195,
0.0222222222222221, -0.0152173913043478, -0.00662251655629142,
0, 0.00555555555555554, -0.00331491712707177, 0.0565410199556542,
-0.0451206715634838, 0.0879120879120878, -0.0909090909090909,
0.0222222222222221, 0.0326086956521738, 0.11578947368421, -0.0415094339622641,
-0.015748031496063, -0.023, 0.105424769703173, -0.0611111111111111,
0.0157790927021697, -0.0485436893203883, 0.0265306122448981,
-0.104373757455268, 0.1165371809101, 0.0318091451292246, 0.0385356454720616,
-0.0575139146567718, 0.0925196850393701, 0, -0.0720720720720721,
0.029126213592233, 0.00943396226415105, -0.0186915887850467,
-0.123809523809524, 0.0652173913043479, -0.122448979591837, -0.0117279069767442,
-0.028335294117647, -0.0679966101694916, 0.0387610389610389,
-1e-04, -0.0251, 0.00877435897435894, -0.00909453621346892, -0.0143025641025641,
0.0141042912873863, -0.0386615384615384, 0.0132333333333334,
0.112857894736842, 0.0279687943262412, -0.0532735632183908, 0.0361077669902914,
0.043996487119438, -0.0499273542600897, 0.0135509433962264, 0.0225558139534884,
-0.0463545454545454, 0.0229095238095237, -9e-04, -0.0217302325581395,
0.0608577197149644, -0.0167599552572707, -0.0283727272727272,
-0.0105457943925234, -0.0344188679245283, 0.0717707317073171,
0.0258727272727273, 0.0470725663716814, -0.0522329113924051,
-0.0015, -0.0641222222222222, 0.0740293838862558, -0.054763436123348,
0.0118534883720931, 0.0394844036697249, -0.0459528634361234,
-0.00670829493087556, 0.0210481481481481, -0.0198995475113123,
0.030358064516129, -0.0685642857142857, 0.0364775119617225, 0.030758064516129,
0.00326428571428581, 0.0119333333333334, -0.00568596491228072,
NA, -0.0833333333333334, 0.0908090909090908, -0.1501, -0.0687274509803921,
-0.0527315789473685, -1e-04, 0.0546945205479452, -0.116983116883117,
-0.0736294117647058, 0.0395825396825398, 0.00753358778625944,
-0.121312121212121, 0.0343827586206897, 0.075, -0.00775193798449614,
0.015625, -0.123076923076923, 0.0963912280701753, 0, -0.0640000000000001,
0.0256410256410258, 0.15, -0.0942028985507247, -0.00800000000000001,
0.120967741935484, -0.064748201438849, 0.0537461538461539, 0,
0.0729927007299269, -0.142957142857143, 0.0713285714285714, 0.036937037037037,
0.0713285714285714, -0.0750000000000001, -0.00900900900900903,
-0.0454545454545454, 0.142857142857143, -0.166666666666667, 0,
0.12, -0.142857142857143, 0.104166666666667, 0.0566037735849056,
-0.0267857142857142, -0.0733944954128442, 0.0495049504950495,
-0.0377358490566038, 0.0490196078431373, 0.121495327102804, 0,
-0.0158730158730158, 0.0161290322580645, -0.158730158730159,
-0.150943396226415, 0.111111111111111, 0.01, 0.0693069306930694,
0.0833333333333333, 0.111111111111111, -0.0461538461538461, 0,
-0.0403225806451613, 0.00840336134453779, -0.0750000000000001,
0.0180180180180181, -0.0353982300884956, 0.100817431192661, -0.0751000000000001,
-0.150575939849624, -0.0266486725663717, 0.127172727272727, 0.032058064516129,
0.015425, 0.122876923076923, 0.0134986301369864, 0.114664864864865,
0.0180818181818183, 0.160414285714286, 0.148317948717949, 0.0531714285714286
), `Mkt-RF` = c(-0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223, -0.1231, 0.0713,
0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249,
-0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704, 0.0449,
0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077, -0.0012,
-0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621,
-0.0228, 0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427,
0.0619, -0.0149, -0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038,
-0.0501, 0.0444, 0.0077, 0.0095, -0.0308, -0.025, 0.0472, 0.0284,
0.0068, 0.0427, 0.0463, 0.0503, -0.0067, 0.0349, 0.0011, 0.0272,
0.0059, -1e-04, 0.0147, 0.0552, -0.0535, -0.0071, 0.022, -0.0273,
-0.0092, 0.0269, -0.0265, 0.0012, -0.0854, -0.0131, -0.0459,
0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587, -0.0256, -0.0239,
0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528, -0.021, 0.0641,
-0.0166, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156, 0.0498, -0.0803,
0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306, -0.0205, -0.0333,
-0.1161, 0.1097, -0.0485, -0.0221, 0.0569, 2e-04, -0.0223, -0.1231,
0.0713, 0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0077, -0.0012,
-0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0479,
-0.0015, -0.0284, 0.0239, -0.0602, 0.0619, -0.0149, -0.0643,
-0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077, 0.0095,
-0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463, 0.0503,
-0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147, 0.0552,
-0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265, -0.0854,
0.0619, 0.0293, 0.037, 0.0587, -0.0256, 0.0178, 0.0407, -0.0255,
-0.0528, -0.021, 0.0641, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156,
0.0498, -0.0803, 0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306,
-0.0205, -0.0333, -0.1011, -0.1161, 0.1097, -0.0485, -0.0221,
0.0569, 0.0641, 2e-04, -0.1231, 0.0713, 0.0056, 0.045, 0.0349,
0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054, 0.0416, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, -0.0221, 0.0569, 2e-04, -0.0223, 0.0713, 0.0056,
0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, 0.0569, 2e-04, -0.0223, -0.1231, 0.0713, 0.0056,
0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, -0.0054,
0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133,
0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041,
-2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228,
0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149,
-0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444,
0.0077, 0.0095, -0.0308, -0.025, 0.0068, 0.0427, 0.0463, 0.0503,
-0.0067, 0.0349, 0.0011, 0.0059, -1e-04, 0.0147, 0.0552, -0.0535,
-0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265, 0.0012, -0.0854,
-0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587,
-0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528,
-0.021, 0.0641, -0.0166, -0.1206, -0.0097, 0.1188, -0.0426, 0.1156,
0.0498, -0.0803, 0.0904, 0.0368, 0.0306, -0.0017, 0.0814, -0.0306,
-0.0205, -0.0333, -0.1011, -0.1161, 0.1097, -0.0485, -0.0221,
0.0569, 0.0641, 2e-04, -0.0223, -0.1231, 0.0713, 0.0056, 0.045,
0.0349, 0.0166, 0.0227, 0.0338, 0.062, -0.0249, 0.0416, 0.0106,
-0.0464, 0.0745, -0.007, 0.0704, 0.0449, 0.0133, 0.0237, -0.0303,
0.0742, -0.0055, 0.0177, 0.0077, -0.0012, -0.041, -2e-04, -0.0379,
-0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228, 0.0479, -0.0015,
-0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149, -0.0186,
-0.0643, -0.0171, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068, 0.0427, 0.0463,
0.0503, -0.0067, 0.0349, 0.0011, 0.0272, 0.0059, -1e-04, 0.0147,
0.0552, -0.0535, -0.0071, 0.022, -0.0273, -0.0092, 0.0269, -0.0265,
0.0012, -0.0854, -0.0131, -0.0459, 0.0619, 0.0293, 0.0058, 0.037,
-0.0534, 0.0587, -0.0256, -0.0239, 0.0215, 0.0326, 0.0178, 0.0407,
-0.0255, -0.0528, -0.021, 0.0641, -0.0166, -0.1206, -0.0097,
0.1188, -0.0426, 0.1156, 0.0498, -0.0803, 0.0904, 0.0368, 0.0306,
-0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011, -0.1161,
0.1097, -0.0485, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223, -0.1231,
0.0713, 0.0056, 0.045, 0.0349, 0.0166, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, 0.0077,
-0.0012, -0.041, -2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003,
0.0621, -0.0228, 0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427,
0.0619, -0.0149, -0.0186, -0.0643, -0.0171, 0.067, 0.0238, -0.0038,
-0.0501, 0.0444, 0.0077, 0.0095, -0.0308, -0.025, 0.0472, 0.0284,
0.0068, 0.0427, 0.0463, 0.0503, -0.0067, 0.0349, 0.0011, 0.0272,
0.0059, -1e-04, 0.0147, 0.0552, -0.0535, -0.0071, 0.022, -0.0273,
-0.0092, 0.0269, -0.0265, 0.0012, -0.0854, -0.0131, -0.0459,
0.0619, 0.0293, 0.0058, 0.037, -0.0534, 0.0587, -0.0256, -0.0239,
0.0215, 0.0326, 0.0178, 0.0407, -0.0255, -0.0528, -0.021, 0.0641,
-0.0166, -0.1206, -0.0097, 0.1188, 0.1156, 0.0498, -0.0803, 0.0904,
0.0368, 0.0306, -0.0017, 0.0814, -0.0306, -0.0205, -0.0333, -0.1011,
-0.1161, 0.1097, -0.0485, -0.0221, 0.0569, 0.0641, 2e-04, -0.0223,
-0.1231, 0.0713, 0.0056, 0.045, 0.0349, 0.0227, 0.0338, 0.062,
-0.0249, -0.0054, 0.0416, 0.0106, -0.0464, 0.0745, -0.007, 0.0704,
0.0449, 0.0133, 0.0237, -0.0303, 0.0742, -0.0055, 0.0177, -0.041,
-2e-04, -0.0379, -0.027, 0.0212, -0.037, -0.003, 0.0621, -0.0228,
0.0479, -0.0015, -0.0284, 0.0239, -0.0602, -0.0427, 0.0619, -0.0149,
-0.0186, -0.0643, 0.067, 0.0238, -0.0038, -0.0501, 0.0444, 0.0077,
0.0095, -0.0308, -0.025, 0.0472, 0.0284, 0.0068)), row.names = c(NA,
-900L), class = c("tbl_df", "tbl", "data.frame"))
The error message is right.You are trying to access 2nd dimension from 1-d vector.
update the loop as below:
reg_risk <- lm(CombData2[i:j] ~ CombData3[i:j], na.action = na.fail)
risk[j,2] <- sigma(reg_risk)
risk[j,3] <- sd(CombData2[i:j])
Also, There is no point in using unlist here.
I computed historic VaR on a simple equity portfolio (for simplicity's sake, let's say there are only two stocks with one year of daily history). Then I did it again using the PerformanceAnalytics R package. My two results don't match.
Am I missing something on how to do basic VaR, or is PerformanceAnalytics doing something strange?
My sample code is below:
library(xts)
# (I'll define "sr" at the end of the question. It's a big xts structure)
weights = c(0.7929511, 0.2070489)
p = 0.95
# How I do it:
m = data.matrix(as.data.frame(sr))
daily_whatif_scenario = tcrossprod(weights, m)[1,]
my_historic_VaR = as.numeric(-quantile(daily_whatif_scenario, 1 - p)[1])
# How PerformanceAnalytics does it:
their_historic_VaR = PerformanceAnalytics::VaR(R = sr, p = p,
method = "historical", portfolio_method = "component", weights = weights)
My calculation comes up with 1.325%, but the package gives me 1.395%. Why such a big difference? These differences only get bigger as I add more stocks and history to my example. Am I doing something wrong?
Here is the result of dput(sr):
sr = structure(c(-0.0162, 0.0049, 0.0029, -1e-04, -0.0079, -0.0101,
0.0053, 0.007, -0.0107, 0.001, -0.003, -0.0033, 0.0013, 0.0163,
0.0102, 2e-04, -0.0027, 0.0303, -0.0149, 0.0034, 0.0073, 0.0111,
-0.0057, 0.0022, 0.0266, 0.0038, 0.0062, -0.0063, 0.0078, 0.0022,
0.0091, -0.0074, 0.008, 0.0068, 0.0048, 0.0031, 0.0142, -0.0034,
-0.0089, 0.001, 0.001, -0.0141, 0.0042, 0.0026, -0.017, 0.001,
-0.006, 0.0048, 0.0064, -0.0063, -0.0122, 0.0122, -0.0126, 0.0239,
-0.0236, -9e-04, 0.004, -0.0049, 0.0028, -0.0109, 0.0106, 0.0066,
0.0096, 0.0075, 0.0082, -0.008, 0.047, -6e-04, 5e-04, -0.005,
-0.0084, -0.0074, 0.0078, -0.0029, 0.0071, 5e-04, 0.0118, 0.0068,
-0.0032, 0.0068, -0.0011, -0.0095, 0.0029, -0.0019, -0.0084,
0.0065, -0.014, 0.0049, -0.0042, 0.0071, -0.0097, -0.0038, -0.0049,
0.0045, 0.001, -7e-04, -0.0056, 0.0098, -0.0112, 0.0093, 5e-04,
-6e-04, 0, -0.0013, 0.0026, 0.0199, -8e-04, 0.0023, -0.0038,
-0.0035, -0.0045, -0.0323, -0.0021, -0.0054, -0.0102, 0.0222,
-0.0139, -0.0027, 6e-04, -5e-04, -0.0069, -8e-04, 0.0074, -0.0071,
0.0017, -0.0142, 0.0111, 0.0022, -0.0085, 0.0183, -0.0061, -0.0031,
0.0083, 0.0111, -0.0028, -0.0051, -0.0149, 0.0026, -0.0046, -0.0105,
-0.003, 0.0019, 0.0055, 0.0471, 0.0042, 9e-04, -0.0226, -0.0175,
0.0169, 3e-04, 8e-04, -0.0258, -0.0047, -0.0044, 0.0071, 0.0129,
0.0063, 0.0248, 0.0107, 0.0133, -0.0214, 0.0045, 0.0175, -0.0034,
0.0048, -1e-04, -0.0118, 0.0082, -0.0024, 0.0019, -0.0139, 0.0037,
-0.0018, -0.0166, -0.002, -0.0058, 0.0053, -0.0086, 0.0159, 0.0027,
-0.0097, 0.0186, -0.0099, 0.0362, 0.0034, -0.0044, 0.005, -0.0074,
0.0031, 0.0151, 0, -0.0036, -0.0054, -0.004, -0.0088, 0, 0.015,
-0.0019, 3e-04, 0.0087, -0.0017, 0.0015, 0.0046, -0.0177, 0.0018,
0.0021, -0.0142, 4e-04, -0.005, -0.001, -0.0108, -5e-04, 0.0167,
0, -7e-04, -0.0079, 0.0397, -6e-04, -0.0132, -0.0013, -0.0022,
0.001, -0.0021, 0.0119, -0.0031, 0.0065, 0.001, -0.0014, -0.0054,
8e-04, -0.0021, 0.0103, -0.0016, -0.0014, 0.0169, -4e-04, -0.0067,
0.0249, 0.0018, 0.0047, -0.0117, -0.0015, 0.0133, 0.0144, -3e-04,
-0.0084, -0.0067, 0.0048, -0.0226, 0.001, -0.0079, -1e-04, 0.0062,
0.0037, 0, 7e-04, -0.0046, -0.0045, -7e-04, 0.0097, -0.0066,
-0.0054, -0.0029, -0.0041, -0.0113, 0.0059, -0.0134, -0.0177,
0.0057, -0.0043, -0.0106, -0.0115, -0.0198, 0.0131, 0.0086, -0.008,
0.0286, -0.0042, 6e-04, 0.0151, 0.0071, 0.0058, 0.0056, 0.0165,
0.0143, 0.0092, 0.0023, 0.0168, 0.0068, -0.0033, -0.0296, -5e-04,
0.0191, -0.0106, 0.0062, -0.002, -0.001, 0.0038, 0.0039, 0.001,
-0.01, 0.0059, 0.0036, -0.0063, -0.0082, -0.0034, 0.0039, 0.0184,
0.0055, -0.0084, 0.0093, -0.0104, 0.0289, -0.01, -0.0098, 0.0108,
-0.0068, -0.0056, 0.002, 0.0206, -0.008, 0.0212, 0.0496, 0.0038,
-0.008, 0.0122, 0.0029, -0.0014, -9e-04, -0.0077, -0.016, 0.0064,
0.0012, 0.0056, 0.0039, 0.0176, -0.0125, -0.0088, 0.0075, 0.0049,
-0.0131, -0.004, 0.0052, -0.004, 0.0021, -0.0162, -0.0126, 0.0042,
0.0032, -0.0059, 0.0014, -0.0034, 7e-04, 0.0043, -6e-04, -0.0067,
0.0067, 0.0045, 0.0096, -3e-04, -0.0082, 0.0038, 0.0053, -0.013,
0.0197, -0.0057, 0.0041, -0.0142, 0.0114, -0.0045, -0.0123, -0.0433,
-0.0066, -0.0212, 0.0257, -0.0068, 8e-04, -0.0031, -0.0029, -0.0097,
-0.0074, 0.0065, -0.013, -0.0036, -0.013, -0.002, 0.0032, -0.0087,
0.0054, -0.001, -0.0118, 0.0034, 0.0051, -0.0065, 2e-04, 0.0015,
-3e-04, 0.0043, -0.0168, 8e-04, -0.0033, -0.0094, -0.0017, -4e-04,
0.0024, -3e-04, -0.0087, 0.0073, 0.0176, 0.0014, -0.0121, -0.005,
-0.006, -0.0059, 0.0159, 0.0273, -0.0071, -0.0163, 0.0041, 0.004,
-6e-04, 0.0095, 0.0012, 0.0156, 0.003, 0.0153, -0.0203, -0.0024,
0.0101, -0.0108, 0.0118, -0.0172, -0.0077, -2e-04, 0, 2e-04,
0.0287, -0.0023, 0.0096, 0.0043, -0.0016, -0.0169, 0.0221, 0.0208,
-0.0036, -0.0236, 0.014, 0.0047, 0.0023, 0, -0.0013, -0.0033,
0.0024, -0.0134, 0, 0.0081, -0.0085, -0.006, -0.0014, -0.0184,
-0.0066, 0.0103, -0.0123, -0.0022, 0.0021, 0.0059, -0.0014, -0.0046,
0.0124, -0.009, 0.0079, 0.0096, -0.0019, -0.0025, -0.0024, -0.0063,
-0.0139, -0.0094, 0.0152, -0.0145, 0.0061, 0.0055, 0.016, -0.0105,
0.0061, 0.0053, -0.0058, -0.0272, -0.0345, -0.0021, 0.0322, -0.0092,
-0.0022, 0.0154, -0.0243, 0.0072, 0.0078, 0.0152, -7e-04, -0.0081,
-0.0042, 0.0038, 0.0157, -0.0064, -0.0063, -3e-04, 0.0094, -0.0162,
-0.0045, -0.0122, -0.0062, -0.0081, 0.0112, -1e-04, -0.0059,
9e-04, 0.0043, -0.0081), .indexCLASS = "Date", tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1459728000,
1459814400, 1459900800, 1459987200, 1460073600, 1460332800, 1460419200,
1460505600, 1460592000, 1460678400, 1460937600, 1461024000, 1461110400,
1461196800, 1461283200, 1461542400, 1461628800, 1461715200, 1461801600,
1461888000, 1462147200, 1462233600, 1462320000, 1462406400, 1462492800,
1462752000, 1462838400, 1462924800, 1463011200, 1463097600, 1463356800,
1463443200, 1463529600, 1463616000, 1463702400, 1463961600, 1464048000,
1464134400, 1464220800, 1464307200, 1464566400, 1464652800, 1464739200,
1464825600, 1464912000, 1465171200, 1465257600, 1465344000, 1465430400,
1465516800, 1465776000, 1465862400, 1465948800, 1466035200, 1466121600,
1466380800, 1466467200, 1466553600, 1466640000, 1466726400, 1466985600,
1467072000, 1467158400, 1467244800, 1467331200, 1467590400, 1467676800,
1467763200, 1467849600, 1467936000, 1468195200, 1468281600, 1468368000,
1468454400, 1468540800, 1468800000, 1468886400, 1468972800, 1469059200,
1469145600, 1469404800, 1469491200, 1469577600, 1469664000, 1469750400,
1470009600, 1470096000, 1470182400, 1470268800, 1470355200, 1470614400,
1470700800, 1470787200, 1470873600, 1470960000, 1471219200, 1471305600,
1471392000, 1471478400, 1471564800, 1471824000, 1471910400, 1471996800,
1472083200, 1472169600, 1472428800, 1472515200, 1472601600, 1472688000,
1472774400, 1473033600, 1473120000, 1473206400, 1473292800, 1473379200,
1473638400, 1473724800, 1473811200, 1473897600, 1473984000, 1474243200,
1474329600, 1474416000, 1474502400, 1474588800, 1474848000, 1474934400,
1475020800, 1475107200, 1475193600, 1475452800, 1475539200, 1475625600,
1475712000, 1475798400, 1476057600, 1476144000, 1476230400, 1476316800,
1476403200, 1476662400, 1476748800, 1476835200, 1476921600, 1477008000,
1477267200, 1477353600, 1477440000, 1477526400, 1477612800, 1477872000,
1477958400, 1478044800, 1478131200, 1478217600, 1478476800, 1478563200,
1478649600, 1478736000, 1478822400, 1479081600, 1479168000, 1479254400,
1479340800, 1479427200, 1479686400, 1479772800, 1479859200, 1479945600,
1480032000, 1480291200, 1480377600, 1480464000, 1480550400, 1480636800,
1480896000, 1480982400, 1481068800, 1481155200, 1481241600, 1481500800,
1481587200, 1481673600, 1481760000, 1481846400, 1482105600, 1482192000,
1482278400, 1482364800, 1482451200, 1482710400, 1482796800, 1482883200,
1482969600, 1483056000, 1483315200, 1483401600, 1483488000, 1483574400,
1483660800, 1483920000, 1484006400, 1484092800, 1484179200, 1484265600,
1484524800, 1484611200, 1484697600, 1484784000, 1484870400, 1485129600,
1485216000, 1485302400, 1485388800, 1485475200, 1485734400, 1485820800,
1485907200, 1485993600, 1486080000, 1486339200, 1486425600, 1486512000,
1486598400, 1486684800, 1486944000, 1487030400, 1487116800, 1487203200,
1487289600, 1487548800, 1487635200, 1487721600, 1487808000, 1487894400,
1488153600, 1488240000, 1488326400, 1488412800, 1488499200, 1488758400,
1488844800, 1488931200, 1489017600, 1489104000, 1489363200, 1489449600,
1489536000, 1489622400, 1489708800, 1489968000, 1490054400, 1490140800,
1490227200, 1490313600, 1490572800, 1490659200, 1490745600, 1490832000
), tclass = "Date"), .Dim = c(259L, 2L), .Dimnames = list(NULL,
c("DHR.US", "GIS.US")))
So I dug up the code that is run when VaR is called with the parameters you specified. Please see below.
As you read my answer please note that this domain is out side of my skill set and the math is a bit over my head, however it appears that the major difference between your code and theirs is they sort the data and select the transposed cross product identified by alpha. Where as you use quantile. For some reason this means you select adjacent numbers - it appears to be basically a rounding error. Please see the following excel screen shot of your daily_whatif_scenario to illustrate this point:
Code for VaR:
VaR.historical.portfolio = function(R,p,w)
{
alpha = .setalphaprob(p)
portret = c();
T = dim(R)[1]
N = dim(R)[2]
for( t in c(1:T) ){
portret = c(portret,sum(w*as.numeric(R[t,])))
}
hVaR = -1* sort(portret)[floor(alpha*T)]
return(hVaR)
}
Where:
.setalphaprob = function (p)
{
if ( p >= 0.51 ) {
# looks like p was a percent like .99
alpha <- 1-p
} else {
alpha = p
}
return (alpha)
}
I have a data frame with the first column is like this
df<- structure(list(phenelzine = c(-0.0269, 0.0217, 0.4609, -0.0011,
0.0016, -0.0334, 0.5159, 0.4687, -0.5715, -0.1466, 0.1096, 0.1006,
-0.1021, 0.0093, -0.2616, 0.1496, 0.4463, -0.0833, 0.0573, 0.0602,
0.1345, -0.0911, -0.1444, -0.0624, -0.2009, -0.0579, 0.1701,
-0.161, -0.376, 0.1239, -0.1529, -0.1217, 0.2659, 0.1975, 0.0598,
-0.1397, 0.1959, -0.0342, -0.0771, 0.0017, 0.0562, 0.0605, -0.0976,
0.3257, -0.0385, -0.0687, 0.3087, 0.0992, -0.2253, -0.3171, -0.055,
0.2071, -0.1928, -0.113, -0.2142), denatonium.benzoate = c(-0.1734,
-0.2142, 0.2142, -0.1376, -0.0129, 0.0254, 0.06, 0.1768, 0.2295,
0.1772, 0.1978, -0.0556, 0.0971, 0.0979, 0.2073, 0.061, 0.2782,
-0.048, -0.0825, 0.1922, -0.0395, 0.1594, -0.2117, -0.0999, 5e-04,
-0.0595, -0.2083, 0.1004, -0.1279, -0.0613, -0.0576, -0.0567,
0.1006, -0.0213, -0.1109, 0.0313, 0.0641, -0.2919, -0.0119, 0.1644,
-0.1197, -0.1138, 0.078, -0.2729, -0.022, -0.1864, -0.0709, 0.0211,
0.0183, 0.0175, 0.1655, 0.0064, -0.0701, 0.0274, 0.1163), triamterene = c(-0.0253,
0.2896, 0.1606, -0.086, -0.1886, 0.0355, 0.0552, 0.0733, 0.0131,
0.6887, -0.0161, -0.3962, 0.1294, 0.0316, -0.1851, -0.1193, 0.0308,
-0.1663, 0.0417, -0.0568, 0.1047, 0.0162, 0.3497, 0.0708, -0.0317,
-0.1471, -0.1588, -0.4929, 0.078, 0.1263, -0.2024, -0.0021, -0.3611,
-0.0494, -0.4211, -0.0226, 0.5045, -0.0757, 0.0328, -0.0198,
-0.2622, 0.2297, -0.0454, 0.4094, 0.0826, -0.4326, 0.0316, 0.3048,
-0.2047, 0.1124, -0.2369, 0.1803, -0.1735, 0.2399, -0.0509),
talampicillin = c(-0.1761, -0.0355, 0.0295, 0.1979, 0.0545,
0.0673, -0.0797, -0.1566, 0.0496, 0.2623, -0.197, 0.0192,
-0.2673, 0.0687, -0.0058, -0.4072, -0.06, -0.2315, 0.2532,
-0.0395, 0.0911, -0.1348, -0.018, 0.3084, 0.3751, 0.1659,
-0.1882, 0.1874, -0.0979, -0.0829, -0.1693, 0.0179, -0.0264,
0.1013, 0.4272, 0.1253, -0.0733, 0, 0.1552, -0.3395, -0.0738,
0, 0, -0.1746, -0.2039, 0.2907, 0.0363, 0.2168, -0.1428,
-0.2898, 0.0793, 0.349, -0.043, 0.1547, -0.0557), triamcinolone = c(0.0115,
0.3329, -0.0752, 0.2784, -0.5543, 0.0139, -0.6692, -0.4599,
-4e-04, 0.0115, 0.0624, -0.2127, 0.1339, 0.1186, -0.0732,
0.1033, -0.0672, 0.0038, -0.0445, -0.2484, 0.0075, 0.1113,
0.3911, 0.0205, -0.0126, -0.0763, 0.0073, 0.4081, -0.2826,
-0.0348, 0.0957, -0.0069, 0.0103, 0.0607, 0.7144, 0.0012,
0.0216, 0.1009, -0.0395, -0.0432, 0.08, -0.1039, -0.38, 0.0532,
-0.0404, 0.4977, 0.0735, -0.0793, -0.4312, 0.0332, -0.0964,
0.1067, -0.0468, 0.0052, 0.1366), sulfaphenazole = c(-0.0886,
0.1081, 0.2002, 0.0625, 0.0403, 0.0256, -0.0074, -0.0678,
-0.0393, 0.3201, 0.4213, -0.3058, -0.1228, 0.0797, 0.0591,
-0.0541, -0.0721, -0.1914, -0.3058, 0.4353, 0.176, -0.1903,
0.142, -0.078, 0.0747, -0.3066, 0.2954, -0.2864, 0.0823,
-0.2976, 0.0354, 0.1075, 0.0696, 0.0413, -0.1505, -0.0958,
0.0996, 0.3221, -0.0582, 0.0723, 0.016, -0.3852, 0.0542,
0.0229, 0.1946, -0.0557, -0.0675, -0.2177, 0.2971, 0.3417,
-0.4023, -0.2623, 0.0103, -0.3864, 0.591), procyclidine = c(0.03,
0.0372, 0.335, 0.1666, -0.0048, -0.1153, 0.1449, -0.0488,
0.1038, -0.0245, 0.1008, 0.0194, -0.0315, -0.0325, 0.0161,
0.0316, 0.19, -0.3043, -0.1044, 0.2325, -0.0602, 0.0076,
0.1493, -0.049, 0.0593, -0.0343, 0.0667, 0.1323, 0.2388,
0.0962, -0.2329, -0.2198, 0.0395, -0.0078, 0.1016, 0.03,
-0.1293, -0.0076, -0.1876, -0.2648, 0.0044, 0.0609, 0.0403,
-0.0914, -0.1242, -0.0627, 0.067, 0.0665, 0.1154, 0.286,
-0.2009, 0.2039, -0.0567, -0.0365, -0.0198), pentoxifylline = c(0,
0.3439, -0.0614, 0.0181, -0.0149, -0.0216, -0.1211, -0.1816,
-0.0204, 0.1023, -0.0059, -0.008, -0.1121, 0.2029, 0.052,
-0.0935, 0, 0.0595, 0.0271, -0.0482, 0.0246, -0.1369, 0.1106,
0.4988, -0.0599, 0.0021, -0.0233, 0.2809, 5e-04, -0.0204,
0.0586, 0.0278, 0.0246, 0.0534, 0.0165, -0.1816, -0.071,
-0.0296, -0.1173, 0.0814, -0.0672, 0.0946, -0.2164, 0.0299,
-0.1341, 0.0439, 0.1124, 0.0125, -0.4091, -0.1134, 0.0098,
0.1957, 0.0044, 0.0922, 0.1851), suloctidil = c(-0.0772,
-0.1651, -0.0543, -0.037, 0.2182, -0.1884, 0.1866, 0.2013,
-0.3388, 0.0493, 0.0223, 0.3441, 0.0887, -0.1477, -0.0719,
-0.029, -0.0162, 0.0602, 0.1497, -0.0699, 0.1472, 0.3213,
-0.1266, 0.9143, 0.072, 0.4574, -0.1897, -0.7059, 0.3822,
-0.1055, -0.0744, 0.179, -0.5096, -0.4966, -0.8764, -0.3903,
0.174, 0.2181, -0.122, -0.0097, -0.2281, -0.1387, -0.0506,
0.1179, 0.1911, -0.9006, -0.0161, -0.2298, 0.1777, -0.1341,
-0.0264, -0.3478, -0.188, 0.035, 0.0441), etacrynic.acid = c(0.1017,
-0.4238, -0.0089, -0.1116, 0.1265, -0.0529, -0.121, -0.0243,
-0.0033, -1e-04, 0.0952, 0.4075, -0.3078, 0.1265, -0.0612,
-0.1239, 0.0381, 0.0491, 0.1062, -0.0398, 0.2044, 0.0341,
-0.0654, 0.2705, 0.0408, 0.0064, -0.0858, -0.19, -0.4934,
-0.0524, 2e-04, -0.1496, -0.1018, -0.3303, -0.665, -0.6764,
0, -0.0248, -0.091, 0.052, -0.0327, 0.2288, 0.8196, -0.1288,
0.038, -0.4468, 0.0013, -0.2137, 0.6752, 0.1275, 0.0198,
0.1209, -0.0735, 1e-04, -0.0706), diphenylpyraline = c(0.0495,
-0.1318, -0.0723, -0.0485, 0.0175, -0.1585, 0.0054, 0.0565,
-0.0637, 0.0366, 0.0155, 0.1003, -0.11, -0.129, 0.0673, -0.0519,
0.0284, -0.1663, 0.0224, -0.1806, -0.1265, 0.1559, -0.1337,
-0.0892, 0.08, -0.2713, -0.0064, -0.0193, -0.2862, -0.0627,
0.1242, 0.0607, -0.1815, -0.107, 0.038, -0.0171, 0.1978,
0.1513, -0.1016, -0.086, -0.0019, 0.042, -0.0073, -0.0751,
-0.0388, 0.0844, -0.0678, -0.0556, 0.1765, -0.0377, -0.0143,
0.3352, 0.0206, 0.1197, -0.1429), carbenoxolone = c(0.0769,
0.0987, 0.1992, -0.016, -0.0276, 0.2596, -0.0086, -0.0038,
-0.2432, -0.0237, 0.0696, -0.1436, 0.1942, -0.0411, -0.0948,
0.2645, -0.114, -0.0887, -0.6041, 0.0061, 0.0875, -0.1449,
-0.1735, -0.0606, -0.0785, 0.3483, 0.1196, 0.2659, 0.0614,
-0.1372, 0.1543, 0.0434, -0.0253, -0.0028, 0.0956, 0.3397,
-0.0771, 0.2627, -0.0028, 0.1337, 0.0046, -0.0757, -0.0112,
-0.084, 0.0312, 0.0935, -0.0709, 0.0829, -0.1061, -0.1504,
0.0122, 6e-04, 0.3138, 0.0632, 0.1019), arecoline = c(0.0185,
0.1999, -0.0313, -0.1868, -0.0626, 0.0298, 0.03, 0, -0.2209,
0.0101, -0.0693, -0.1656, -0.1048, 0.2098, 0.0393, -0.1354,
0.0328, -0.0311, -0.1967, -0.2653, 0.204, 0.1737, 0.36, -0.1034,
-0.3326, 0.0613, -0.2044, 0.1967, 0.0042, 0.2329, 0.2409,
-0.0335, 0.0068, 0.5101, 0.2002, 0.1867, 0.2134, -0.1072,
0.0854, 0.2414, -0.0653, 0.3851, 0.1358, -0.0102, 0.0268,
0.2497, -0.1376, 0.1798, 0.0427, 0.1593, -0.29, 0.0343, 0.2249,
-0.1301, -0.0987), chenodeoxycholic.acid = c(-0.0825, 0.2098,
-0.0911, -0.0287, 0.0473, 0.1348, -0.06, -0.0285, -0.1473,
-0.0162, -0.0863, 0.0652, -0.0256, 0.1345, -0.1175, -0.0141,
0.2985, 0.2963, 0.1925, -0.212, -0.2106, -0.1128, -0.3121,
0.0867, 0.0214, 0.0346, 0.0173, 0.1101, -0.3066, 0.1115,
0.0415, 0.285, 0.0787, -0.0985, -0.027, -0.2767, 0.1572,
-0.0518, 0.0815, 0.0168, -0.2047, -0.1517, -0.0076, -0.0359,
0.0596, -0.0706, 0.1006, 0.0099, 0.1661, -0.0435, 0.0331,
0.0996, 0.197, -0.0067, -0.0328), torasemide = c(0.0987,
0.1829, 0.1693, -0.0482, 0.0534, 0.1624, 0.0047, -0.1721,
0.248, 0.1715, -0.2109, -0.0909, 0.0513, -0.1358, 0.6297,
0.0762, -0.3473, -0.2189, -0.126, 0.3054, -0.0451, -0.2243,
0.0914, -0.0199, -6e-04, -0.1102, -0.0082, 0.0242, -0.053,
-0.0825, -0.1874, 0.1251, 0, -0.0457, -0.1179, -0.3169, 0.0559,
0.1999, 0.262, 0.0462, 0.1038, -0.0999, -0.1278, -0.1944,
0.0074, 0.1785, 0.0486, -0.027, -0.2748, -0.0428, 0.0696,
-0.1544, 0.117, 0.1665, 0), troglitazone = c(-0.1513, 0.2568,
0.1377, 0.2474, -0.0359, -0.0859, 0.2014, 0.096, 0.0751,
0.1304, -0.1376, -0.1718, 0.0063, 0.2732, -0.2237, 0.1951,
-0.0496, -0.0087, 0.0245, -0.1212, 0.194, -0.0971, -0.0184,
0.1071, 0.0129, -0.061, 0.1704, -0.0021, 0.3985, 0.0963,
0.1591, -0.381, 0.2277, 0.5395, 0.0611, -0.0204, 0.0614,
-0.2816, 0.0308, -0.0642, -0.1722, -0.0191, -0.0374, 0.0436,
-0.3296, 0.0379, -0.0517, 0.2978, 0.0834, -0.1304, 0.1039,
0.2279, 0.5988, 0.0424, -0.2677), mepenzolate.bromide = c(0.0392,
-0.0041, -0.0531, -0.1213, -0.0919, 0.3096, 0.0545, 0.0922,
0.112, 0.0606, -0.25, -0.0086, 0.2729, 0.1307, 0.2124, -0.039,
-0.1005, 0.0125, 0.0644, -0.0064, 0.0707, 0.1931, -0.5523,
-0.285, -0.3085, 0.0343, 0.0622, -0.1849, -0.0436, 0.1069,
0.072, 0.0312, 0.1016, -0.1611, -0.139, 0.0047, 0.0297, -0.1409,
-0.0834, -0.0087, 0.2839, -0.03, -0.0378, -0.0705, 0.0836,
-0.0824, 0.0803, -0.1378, 0.014, -0.0351, 0.0049, 0.0153,
-0.093, 0.0263, 0.0855), megestrol = c(-0.1106, 0.2547, -0.0488,
0, -0.0406, -0.0826, -0.1795, -0.3099, -0.0316, 0.1563, 0,
-0.1189, -0.0343, -0.0482, 0.8041, 0.5202, 0.0733, 0.0419,
0.2212, -0.0888, 0.0268, 0.0886, -0.0476, -0.3235, -0.1348,
-0.2665, 0.294, 0.122, -0.1695, 0.1186, 0.0927, 0, -0.0429,
-0.0492, 0.0963, -0.0934, 0.1289, 0.1447, -0.0308, -0.1145,
0.1861, -0.0464, 0.0992, 0, 0.0771, -0.1804, 0.0899, 0.1031,
0, 0.1035, 0.1404, 0, -0.1281, -0.2463, -0.2874), dexpropranolol = c(-0.1942,
-0.0051, 0.0561, 0.0166, 0.0029, -0.1707, 0.2173, 0.1178,
-0.0683, -0.2903, -0.2874, 0.1764, -0.1135, 0.2574, 0, -0.1194,
-0.4694, -0.285, 0.0364, -0.0362, 0.158, -0.0648, -0.0046,
0.1435, -0.0946, 0.11, 0.1712, 0.3853, 0.1692, -0.0977, 0.3577,
0.2089, 0.3066, -0.0788, 0.3092, 0.113, -0.1234, 0.5753,
0.0359, 0.1014, 0.2017, 0.2949, 0.019, -0.0855, 0.0428, 0.2171,
-0.0951, -0.0761, -0.1451, -0.2832, -0.2653, -0.166, 0.0376,
-0.0214, 0.0131), nipecotic.acid = c(0.2653, -0.3115, -0.0069,
0.118, -0.0998, -0.0289, 0.1051, 0.0132, -0.0028, -0.1072,
-0.1888, -0.0312, 0.3868, -0.0019, -0.1878, -0.0798, -0.3881,
-0.2162, -0.1189, 0.1569, -0.0537, 0.0196, -0.0013, -0.0818,
-0.1885, 0.2266, 0.2862, 0.1302, 0.0734, 0.0936, -0.0159,
-0.0974, -0.0253, 0.2637, -0.2069, 0.2298, -0.1518, -0.13,
-0.0344, -0.0623, -0.0307, 0.1186, -0.2535, 0.0387, 0.0374,
0.0443, -0.0783, 0.1588, -0.0239, 0.1104, 0.1518, 0.1921,
-0.0744, -0.0125, -0.19), omeprazole = c(0.0272, -0.3796,
-0.0125, 0.0585, -0.1073, -0.0761, 0.3838, 0.5699, 0.0027,
-0.3822, 0.1132, -0.1588, 0.2396, -0.217, 0.0434, 0.1336,
-0.0608, -0.0294, 0.1702, -0.2499, 0.0568, 0.0348, -0.0486,
0, 0.2792, -0.1318, 0.0249, 0.005, 0.1688, -0.3908, -0.2366,
-0.0204, -0.0672, -0.0181, -0.0514, 0.139, 0.0582, 0.109,
0.1018, -0.2249, 0.0432, -0.1882, 0.0818, -0.1082, -0.0456,
-0.0764, 0.4292, 0.0553, -0.1346, -0.2173, 0, 0.1391, 0.1908,
-0.0633, 0.0385), etanidazole = c(-0.1036, 0.0281, 0.0039,
0.0123, -0.2305, -0.0542, 0.0485, -0.0686, -0.1829, 0.2637,
-0.1158, -0.2029, 0.104, 0.3169, 0.1387, 0.0844, -0.1385,
-0.3046, 0.0706, -0.076, -0.1772, 0, -0.393, 0.0841, -0.2469,
-0.1261, 0.1085, 0.0265, 0.0141, 0.0183, 0.2398, -0.0353,
-0.5217, -0.1577, 0.4357, -0.1347, 0.0362, 0.4197, -0.0326,
0.1955, -0.0352, -0.0122, -0.0658, -0.1482, -0.1485, 0.3406,
0.0618, 0.0822, 0.2024, -0.3203, 0.1646, -0.1412, 0.3221,
-8e-04, 0.0472), acenocoumarol = c(-0.0087, 0.2005, -0.1051,
-0.067, -0.0655, -0.0467, -0.4081, -0.1085, -0.0556, 0.0145,
-0.0865, 0.1961, -0.1842, -0.0398, -0.0944, 0.046, 0.0632,
-0.1008, 0.0837, 0.0023, 0.0482, 0.0195, -0.2322, -0.0293,
0.0671, -8e-04, -0.013, -0.0905, -0.0019, -0.0333, 0.0421,
-0.0712, -0.0771, 0.1843, 0.0589, 0.0229, -0.0976, -0.0184,
0.1559, -0.0297, -0.1089, -0.0461, -0.0322, 0.1253, 0.0408,
-0.1147, 0.082, 0.1072, 0.1094, 0.1066, 0.104, 0.1947, 0.1116,
-0.1421, -0.0934), dacarbazine = c(-0.0065, -0.7082, 0.0979,
0.0048, -0.1409, 0.0541, -0.0793, -0.1102, -0.1513, -0.002,
-0.0898, 0.0443, 0.0686, -0.1122, -0.0239, 0.0126, -0.0093,
0.0366, 0.0061, -0.1217, 0.0259, -0.1117, -0.1178, -0.2467,
-0.1128, -0.0671, 0.0347, 0.4719, -0.3849, -0.0517, 0.0764,
0.2058, 0.3147, 0.3275, 0.551, 0.1175, 0.0383, -0.1004, 0.0425,
0.1717, 0.174, 0.0122, -0.1466, 0.0381, -0.0656, 0.2572,
-0.0851, 0.0996, -0.2686, -0.1501, 0.0994, 0.001, 0.0891,
0.0652, -0.2386), diphemanil.metilsulfate = c(-0.0312, -0.2611,
-0.0098, -0.216, 0.24, 0.0909, 0.1171, -0.156, 0.0986, -0.0946,
0.1027, -0.1465, 0.0611, 0.0637, 0.0677, -0.0017, -0.0884,
-0.0023, 0.012, -0.3062, -0.0396, -0.0197, -0.2654, -0.1362,
-0.1075, 0.0738, 0.0039, -0.047, -0.0562, -0.0751, 0.0048,
-0.0273, 0.1636, -0.003, 0.1565, 0.2033, -0.1181, -0.046,
0.0056, 0.0896, -0.0309, -0.0146, -0.0784, 0.002, 0.1111,
0.2156, 0.0915, 0.1174, 0.2337, -0.0151, -0.056, 0.0705,
0.0522, -0.0171, -0.3229), meprylcaine = c(-0.0382, -0.1127,
0, 0, 0.0964, 0.0097, -0.0231, -0.0747, 0.2147, 0.0618, -0.2313,
0.0878, -0.3681, 0.0497, -0.0105, 0.2084, 0.0492, 0.0423,
0.0634, -0.0457, -0.0883, 0.0104, 0.1429, -0.1485, 0.4838,
0.0139, 0.2511, -0.1276, -0.1517, -0.0574, -0.0525, -0.0053,
-0.0796, 0.1029, -0.0393, -0.0587, -0.1787, -0.2193, 0.1723,
0.1684, 0.1708, 0.1961, -0.027, 0.0921, -0.0293, -0.2186,
-0.1013, -0.043, 0.2408, -0.2149, 0.2316, -0.0559, 0.2917,
-0.0128, -0.2173), mevalolactone = c(-0.0393, -0.174, -0.1632,
0.4181, -0.038, 0.1698, -0.1771, -0.168, -0.3952, 0.1739,
0.1304, 0, -0.0317, -0.1822, -0.2021, -0.0743, 0.3155, 0.0782,
0.2533, -0.1159, 0.0135, 0.2274, -0.1785, -0.4384, -0.268,
0.1331, -0.4809, 0.6483, -0.7939, -0.1549, -0.0138, -0.0604,
0.043, 0.1208, 0.4608, 0.4028, 0.1985, 0.1172, 0.0787, 0.099,
-0.0329, 0.1553, -0.0503, -0.1355, 0.0382, 0.4655, -0.1948,
-0.1411, -0.0149, 0.0259, -0.4708, 0.0715, 0.0608, 0, 0.5629
), buspirone = c(-0.1276, -0.0097, -0.1365, 0.0801, -0.0499,
0.0499, 0.2245, 0.0739, -0.0485, -0.0042, -0.2356, -0.1325,
0, 0, -0.0756, 0.0099, 0.069, 0.062, 0.0404, 0.2093, -0.0973,
-0.237, -0.2371, 0.1403, -0.0553, -0.043, 0.0219, 0.0596,
0.0999, -0.1358, 0.1098, -0.06, 0.1429, -0.0758, 0.1528,
-0.0426, 0.1053, 0.2444, -0.0031, -0.2131, -0.1855, 0.144,
0.0037, 0.1078, 0.2742, 0.247, 0.0755, 0.0187, 0.0075, -0.0934,
-0.2814, 0.1086, -0.379, -0.1291, -0.0597), sulfafurazole = c(0.0336,
0.0083, 0.0309, 0.1891, -0.0991, 0.0259, 0.084, 0.0888, 0.0672,
-0.0859, -0.0196, -0.4085, 0.0676, 0.0357, -0.0486, -0.2493,
-0.053, -0.128, 0.103, -0.0471, 0.0839, 0.3382, 0.1353, 0.2906,
0.1022, -0.1326, -0.0457, 0.3148, 0.2374, 0.0719, 0.0469,
-0.2298, 0.1334, 0.1581, -0.1396, -0.2614, -2e-04, 0.0297,
-0.0349, -0.1307, -0.1516, -0.0526, 0.0468, 0.0534, -0.0268,
0.2099, -0.0794, 0.2252, 0.095, -0.1607, 0.1593, 0.0157,
0.0283, 0.1304, -8e-04), ciclopirox = c(0.0958, 0.2534, -0.1897,
-0.0457, 0.1704, -0.2632, -0.2596, -0.1076, 0.3008, 0.1348,
-0.0817, 0.2929, -0.2249, -0.1357, -0.0498, 0.3122, 0.1269,
0.0809, 0.05, -0.2514, -0.3665, -0.1104, 0.0793, 0.8682,
0.3056, 0.2149, -0.3415, 0.1955, 0.2518, -0.1525, 0.0429,
0.4573, -0.556, -0.2508, 0.0437, 0.0459, 0.13, 0.104, 8e-04,
-0.5053, -0.4061, -0.2273, 0.1871, 0.1646, -0.0224, 0.1323,
-0.1473, -0.0917, -0.214, 0.0844, -0.4498, 0.0313, 0.0385,
-0.1485, -0.0553)), .Names = c("phenelzine", "denatonium.benzoate",
"triamterene", "talampicillin", "triamcinolone", "sulfaphenazole",
"procyclidine", "pentoxifylline", "suloctidil", "etacrynic.acid",
"diphenylpyraline", "carbenoxolone", "arecoline", "chenodeoxycholic.acid",
"torasemide", "troglitazone", "mepenzolate.bromide", "megestrol",
"dexpropranolol", "nipecotic.acid", "omeprazole", "etanidazole",
"acenocoumarol", "dacarbazine", "diphemanil.metilsulfate", "meprylcaine",
"mevalolactone", "buspirone", "sulfafurazole", "ciclopirox"), row.names = c("200665_s_at",
"201125_s_at", "201150_s_at", "201162_at", "201560_at", "202133_at",
"202435_s_at", "202436_s_at", "202766_s_at", "203083_at", "203238_s_at",
"203640_at", "204223_at", "204468_s_at", "204589_at", "204619_s_at",
"205381_at", "205422_s_at", "205713_s_at", "205941_s_at", "208626_s_at",
"209581_at", "209747_at", "209875_s_at", "209894_at", "210004_at",
"210078_s_at", "210495_x_at", "210511_s_at", "210517_s_at", "210809_s_at",
"211071_s_at", "211597_s_at", "211709_s_at", "211719_x_at", "212464_s_at",
"212488_at", "212667_at", "213125_at", "213351_s_at", "213352_at",
"213413_at", "213656_s_at", "214770_at", "214927_at", "216442_x_at",
"217428_s_at", "217949_s_at", "218718_at", "219054_at", "219087_at",
"219179_at", "219922_s_at", "222379_at", "37022_at"), class = "data.frame")
I want to heatmap all the columns based on only those rows that have mean of higher than 0.02.
what I do is as follows:
tab <- melt(transform(df, rowname = row.names(df)))
t<- data.frame(sort(rowMeans(df)))
t[t[,1] > 0.02,, drop=FALSE]
library(ggplot2)
ggplot(subset(tab, rowname %in% rownames(t)[t > 0.02]),
aes(x = rowname, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
but it does not do the job, it does put the x axis as it wants not based on the order, for example I want to be from 0.02 to 0.05
so the other looks like
204619_s_at 0.02055667
211709_s_at 0.02357333
216442_x_at 0.02364000
201162_at 0.02389667
217949_s_at 0.03153333
201150_s_at 0.03251000
204468_s_at 0.03279000
211719_x_at 0.03447667
219922_s_at 0.03968667
212667_at 0.04798667
212488_at 0.04833333
209875_s_at 0.05173333
203083_at 0.05493667
210495_x_at 0.05858333
219179_at 0.05932667
in axis and not what it shows
You can try
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
tab1 <- subset(tab, rowname %in% rownames(t)[t > 0.02])
tab1$rowname <- factor(tab1$rowname, levels=rn)
library(ggplot2)
ggplot(tab1,aes(x = rowname, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient2(high="red",mid="white",low="blue") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
Or keeping most of the steps within the %>%
library(dplyr)
library(tidyr)
library(ggplot2)
bind_cols(data.frame(rowname=row.names(df)), df) %>%
filter(rowMeans(.[-1]) >0.02) %>%
gather(variable, value,-rowname) %>%
mutate(rowname=factor(rowname, levels=rn)) %>%
ggplot(., aes(x=rowname, y=variable, fill=value))+
geom_tile()+
scale_fill_gradient2(high='red', mid='white', low='blue')+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
xlab('x axis') +
ylab('y axis')
I have a data frame like below
df<- structure(list(phenelzine = c(-0.0269, 0.0217, 0.4609, -0.0011,
0.0016, -0.0334, 0.5159, 0.4687, -0.5715, -0.1466, 0.1096, 0.1006,
-0.1021, 0.0093, -0.2616), denatonium.benzoate = c(-0.1734, -0.2142,
0.2142, -0.1376, -0.0129, 0.0254, 0.06, 0.1768, 0.2295, 0.1772,
0.1978, -0.0556, 0.0971, 0.0979, 0.2073), triamterene = c(-0.0253,
0.2896, 0.1606, -0.086, -0.1886, 0.0355, 0.0552, 0.0733, 0.0131,
0.6887, -0.0161, -0.3962, 0.1294, 0.0316, -0.1851), talampicillin = c(-0.1761,
-0.0355, 0.0295, 0.1979, 0.0545, 0.0673, -0.0797, -0.1566, 0.0496,
0.2623, -0.197, 0.0192, -0.2673, 0.0687, -0.0058), triamcinolone = c(0.0115,
0.3329, -0.0752, 0.2784, -0.5543, 0.0139, -0.6692, -0.4599, -4e-04,
0.0115, 0.0624, -0.2127, 0.1339, 0.1186, -0.0732), sulfaphenazole = c(-0.0886,
0.1081, 0.2002, 0.0625, 0.0403, 0.0256, -0.0074, -0.0678, -0.0393,
0.3201, 0.4213, -0.3058, -0.1228, 0.0797, 0.0591), procyclidine = c(0.03,
0.0372, 0.335, 0.1666, -0.0048, -0.1153, 0.1449, -0.0488, 0.1038,
-0.0245, 0.1008, 0.0194, -0.0315, -0.0325, 0.0161), pentoxifylline = c(0,
0.3439, -0.0614, 0.0181, -0.0149, -0.0216, -0.1211, -0.1816,
-0.0204, 0.1023, -0.0059, -0.008, -0.1121, 0.2029, 0.052), suloctidil = c(-0.0772,
-0.1651, -0.0543, -0.037, 0.2182, -0.1884, 0.1866, 0.2013, -0.3388,
0.0493, 0.0223, 0.3441, 0.0887, -0.1477, -0.0719), etacrynic.acid = c(0.1017,
-0.4238, -0.0089, -0.1116, 0.1265, -0.0529, -0.121, -0.0243,
-0.0033, -1e-04, 0.0952, 0.4075, -0.3078, 0.1265, -0.0612), diphenylpyraline = c(0.0495,
-0.1318, -0.0723, -0.0485, 0.0175, -0.1585, 0.0054, 0.0565, -0.0637,
0.0366, 0.0155, 0.1003, -0.11, -0.129, 0.0673), carbenoxolone = c(0.0769,
0.0987, 0.1992, -0.016, -0.0276, 0.2596, -0.0086, -0.0038, -0.2432,
-0.0237, 0.0696, -0.1436, 0.1942, -0.0411, -0.0948), arecoline = c(0.0185,
0.1999, -0.0313, -0.1868, -0.0626, 0.0298, 0.03, 0, -0.2209,
0.0101, -0.0693, -0.1656, -0.1048, 0.2098, 0.0393), chenodeoxycholic.acid = c(-0.0825,
0.2098, -0.0911, -0.0287, 0.0473, 0.1348, -0.06, -0.0285, -0.1473,
-0.0162, -0.0863, 0.0652, -0.0256, 0.1345, -0.1175), torasemide = c(0.0987,
0.1829, 0.1693, -0.0482, 0.0534, 0.1624, 0.0047, -0.1721, 0.248,
0.1715, -0.2109, -0.0909, 0.0513, -0.1358, 0.6297), troglitazone = c(-0.1513,
0.2568, 0.1377, 0.2474, -0.0359, -0.0859, 0.2014, 0.096, 0.0751,
0.1304, -0.1376, -0.1718, 0.0063, 0.2732, -0.2237), mepenzolate.bromide = c(0.0392,
-0.0041, -0.0531, -0.1213, -0.0919, 0.3096, 0.0545, 0.0922, 0.112,
0.0606, -0.25, -0.0086, 0.2729, 0.1307, 0.2124), megestrol = c(-0.1106,
0.2547, -0.0488, 0, -0.0406, -0.0826, -0.1795, -0.3099, -0.0316,
0.1563, 0, -0.1189, -0.0343, -0.0482, 0.8041), dexpropranolol = c(-0.1942,
-0.0051, 0.0561, 0.0166, 0.0029, -0.1707, 0.2173, 0.1178, -0.0683,
-0.2903, -0.2874, 0.1764, -0.1135, 0.2574, 0), nipecotic.acid = c(0.2653,
-0.3115, -0.0069, 0.118, -0.0998, -0.0289, 0.1051, 0.0132, -0.0028,
-0.1072, -0.1888, -0.0312, 0.3868, -0.0019, -0.1878), omeprazole = c(0.0272,
-0.3796, -0.0125, 0.0585, -0.1073, -0.0761, 0.3838, 0.5699, 0.0027,
-0.3822, 0.1132, -0.1588, 0.2396, -0.217, 0.0434), etanidazole = c(-0.1036,
0.0281, 0.0039, 0.0123, -0.2305, -0.0542, 0.0485, -0.0686, -0.1829,
0.2637, -0.1158, -0.2029, 0.104, 0.3169, 0.1387), acenocoumarol = c(-0.0087,
0.2005, -0.1051, -0.067, -0.0655, -0.0467, -0.4081, -0.1085,
-0.0556, 0.0145, -0.0865, 0.1961, -0.1842, -0.0398, -0.0944),
dacarbazine = c(-0.0065, -0.7082, 0.0979, 0.0048, -0.1409,
0.0541, -0.0793, -0.1102, -0.1513, -0.002, -0.0898, 0.0443,
0.0686, -0.1122, -0.0239), diphemanil.metilsulfate = c(-0.0312,
-0.2611, -0.0098, -0.216, 0.24, 0.0909, 0.1171, -0.156, 0.0986,
-0.0946, 0.1027, -0.1465, 0.0611, 0.0637, 0.0677), meprylcaine = c(-0.0382,
-0.1127, 0, 0, 0.0964, 0.0097, -0.0231, -0.0747, 0.2147,
0.0618, -0.2313, 0.0878, -0.3681, 0.0497, -0.0105), mevalolactone = c(-0.0393,
-0.174, -0.1632, 0.4181, -0.038, 0.1698, -0.1771, -0.168,
-0.3952, 0.1739, 0.1304, 0, -0.0317, -0.1822, -0.2021), buspirone = c(-0.1276,
-0.0097, -0.1365, 0.0801, -0.0499, 0.0499, 0.2245, 0.0739,
-0.0485, -0.0042, -0.2356, -0.1325, 0, 0, -0.0756), sulfafurazole = c(0.0336,
0.0083, 0.0309, 0.1891, -0.0991, 0.0259, 0.084, 0.0888, 0.0672,
-0.0859, -0.0196, -0.4085, 0.0676, 0.0357, -0.0486), ciclopirox = c(0.0958,
0.2534, -0.1897, -0.0457, 0.1704, -0.2632, -0.2596, -0.1076,
0.3008, 0.1348, -0.0817, 0.2929, -0.2249, -0.1357, -0.0498
)), .Names = c("phenelzine", "denatonium.benzoate", "triamterene",
"talampicillin", "triamcinolone", "sulfaphenazole", "procyclidine",
"pentoxifylline", "suloctidil", "etacrynic.acid", "diphenylpyraline",
"carbenoxolone", "arecoline", "chenodeoxycholic.acid", "torasemide",
"troglitazone", "mepenzolate.bromide", "megestrol", "dexpropranolol",
"nipecotic.acid", "omeprazole", "etanidazole", "acenocoumarol",
"dacarbazine", "diphemanil.metilsulfate", "meprylcaine", "mevalolactone",
"buspirone", "sulfafurazole", "ciclopirox"), row.names = c("200665_s_at",
"201125_s_at", "201150_s_at", "201162_at", "201560_at", "202133_at",
"202435_s_at", "202436_s_at", "202766_s_at", "203083_at", "203238_s_at",
"203640_at", "204223_at", "204468_s_at", "204589_at"), class = "data.frame")
what I want to do, is after I melt the data, I only keep those that t>0.02 but not all, I want to be able to say, the last 10, or the last 5 or a specific number I like , This should apply on all variables and not only one variable
what i do is as follows:
# sort the mean of the df
t<- data.frame(sort(rowMeans(df)))
# select only those that are higher than 0.02
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
# melt the data frame
tab <- melt(transform(df, rowname = row.names(df)))
# select only those that are higher than 0.02
tab1 <- subset(tab, rowname %in% rownames(t)[t > 0.02])
You can use tailto get the last rows of a vector or a data.frame:
tail(tab1 ,5) ## the last five rows
tail(t,5) ## the last five conditions rows