Issues when obtaining RMSE and R2 from predictions on test data - r
I have a training data (train.dat) and test data (test.dat). I would like to run my linear regression, elastic, tree and random forest model on the test data after training it on the training data.
From there, I would like to get the RMSE and R2 to observe the predictive accuracy of the model. However, I have 2 issues:
When I train my random forest model on test data, I get the error: Error in predict.randomForest(modelFit, newdata) : missing values in newdata. Other models are fine
I can only extract the RMSE and R2 values for my tree model, while the other models return NAs. A screenshot is below
Can anyone tell me what has gone wrong with my code?
My code:
library(caret)
set.seed(10345678)
tr.Control <- trainControl(method = "repeatedcv",
number = 10,
repeats = 5,
classProbs = FALSE,
)
lm7 <- train(Lifeexp ~ . + Govthealth*Privhealth,
data = train.dat,
method = 'lm',
trControl = tr.Control,
preProc = c("center", "scale")
)
alpha.vec <- seq(0, 1, by = 0.1)
lambda.vec2 <- seq(0, 30, length.out = 50)
elastic.grid <- expand.grid(alpha = alpha.vec, lambda = lambda.vec2)
elastic4 <- train(Lifeexp ~ ., data = train.dat,
method = 'glmnet',
trControl = tr.Control,
verbose = FALSE,
tuneGrid = elastic.grid,
preProc = c("center", "scale")
)
cp.vec <- seq(10^-4, 0.1, length.out = 50 )
tree2 <- train(Lifeexp ~ ., data = train.dat
, trControl = tr.Control
, method = "rpart"
, preProcess = c("center", "scale")
, tuneGrid = expand.grid(cp = cp.vec)
)
rf1 <- train(
Lifeexp ~ .,
data = train.dat,
method = "rf",
trControl = tr.Control ,
preProc = c("center", "scale"),
ntree = 1000,
tuneGrid = expand.grid(mtry = seq(1, ncol(train.dat)-1)
)
)
regression.pred <- predict(lm7, newdata = test.dat, na.action = na.pass, type = "raw")
elastic.pred <- predict(elastic4, newdata = test.dat, na.action = na.pass, type = "raw")
tree.pred <- predict(tree2, newdata = test.dat, na.action = na.pass, type = "raw")
rf.pred <- predict(rf1, newdata = test.dat, na.action = na.pass, type = "raw")
test.statistics <- data.frame(
RMSE.regression = RMSE(regression.pred, test.dat$Lifeexp),
Rsquare.regression = R2(regression.pred, test.dat$Lifeexp),
RMSE.el = RMSE(elastic.pred, test.dat$Lifeexp),
Rsquare.el = R2(elastic.pred, test.dat$Lifeexp),
RMSE.tree = RMSE(tree.pred, test.dat$Lifeexp),
Rsquare.tree = R2(tree.pred, test.dat$Lifeexp)
)
test.statistics
train.dat:
structure(list(GDP = c(402.1030419, 442.2030419, 543.3030419,
520.8966027, 254.2432569, 124.4608003, 341.5541149, 772.3135303,
478.6685897, 191.8789042, 592.4010975, 1033.912431, 138.4288795,
622.4988457, 642.7767443, 317.3893069, 269.8711377, 709.5819646,
585.07655, 780.190201, 3122.362815, 3893.596078, 1166.610276,
1674.825261, 3690.113268, 4241.788782, 2441.741991, 4043.662051,
9040.566251, 963.8417858, 2234.579866, 10330.61561, 1944.137621,
2136.440243, 567.5286729, 567.930736, 2292.445156, 2028.18197,
371.6785662, 519.5343268, 987.409723, 1482.403063, 1196.586858,
1955.588006, 6941.235848, 1038.90854, 3102.713363, 3139.966054,
3032.427138, 7328.615629, 869.6965166, 2799.648876, 617.2304355,
1126.683318, 4094.362119, 7708.100996, 10385.96443, 11683.94962,
718.1878292, 3243.231125, 3100.280468, 11286.24302, 8920.762105,
201.4671636, 785.5022829, 1510.324871, 1831.001912, 8141.913127,
12027.36588, 6967.24523, 7691.345097, 3233.295943, 367.5566093,
1357.563719, 1489.876911, 977.2736357, 1508.942737, 2007.736363,
5076.342992, 7273.563207, 948.3318545, 2146.996385, 95.18825018,
390.0933261, 2566.59695, 52022.1256, 57373.68668, 19095.467,
28149.87001, 39435.8399, 20600.37525, 23041.53473, 44141.87814,
47518.63604, 24190.24962, 46232.98962, 26891.44645, 61350.34791,
28364.64508, 50152.34014, 22303.96133, 23635.92922, 41531.9342,
47603.02763, 9600.18513, 12042.95373, 26917.75898, 20324.25356,
20087.59199, 36000.52012, 25423.07201, 32018.06325, 43024.92384,
73191.11632, 12663.36453, 30693.59308, 18440.37852, 38577.38166,
33994.40657, 21290.86038, 50950.03434, 53024.05921, 13663.02162,
13641.10272, 41945.33167, 1731.209509, 4492.727604, 11861.75616,
47236.96023, 23509.54339, 26123.97387, 74605.77451), Health = c(22.23474948,
36.44474948, 45.58774948, 46.38774948, 3.333203815, 5.359203815,
16.69390488, 19.46990488, 33.22835541, 5.300580788, 29.97179604,
33.59179604, 5.971383095, 62.66848373, 67.22848373, 8.23568,
14.98141193, 32.6487999, 10.22661548, 16.19961548, 92.18703461,
98.65987461, 143.7665911, 159.7515106, 308.6578979, 402.5568979,
99.5689502, 111.4155502, 292.8907166, 198.2263198, 221.1403198,
705.336568, 176.6524443, 200.7054443, 12.56211728, 17.72411728,
76.7208786, 98.4562786, 9.55682529, 16.01162529, 26.5686245,
33.565445, 69.66563616, 89.45643616, 275.2236792, 32.77552414,
122.5689168, 198.7124574, 221.7829742, 539.567627, 43.70681763,
108.6149597, 33.2254878, 42.36598, 60.2569, 705.1993408, 891.1377563,
992.5689563, 31.84200096, 77.2356478, 277.45864, 891.7641602,
932.325129, 15.23564, 54.30473709, 74.231488, 200.564125, 665.2514038,
755.36985, 384.9183044, 445.20158, 262.5267029, 11.56898, 45.25077438,
109.0749969, 122.02145, 42.568412, 62.25963211, 172.0576935,
200.562134, 91.17743683, 120.236549, 11.23587, 18.82835197, 99.23568,
4952.777344, 5236.3654, 1101.36589, 1674.2854, 3309.480957, 1654.5687,
1845.321045, 4449.542969, 5000.36545, 1998.634277, 6054.23658,
1900.2356, 7025.36987, 1000.5689, 5036.2356, 1233.36545, 2334.651855,
4597.244629, 5698.2547, 1500.3698, 2000.23564, 2573.740234, 3002.36547,
1520.453613, 3214.546387, 1569.3254, 2873.848145, 3644.802734,
4587.235478, 1122.02145, 2211.019043, 462.5890808, 1061.365601,
1256.56897, 1987.2145, 5186.632813, 6547.2356, 990.32658, 1053.891602,
4201.3698, 122.02145, 238.0044861, 712.2356, 1513.565918, 2015.18042,
2985.23, 8021.80957), Govthealth = c(1.25689, 2.032658, 2.495758057,
2.965478, 1.985478, 2.209019899, 2.882325411, 3.21458, 7.3134408,
1.032568, 5.433434963, 7.235478, 1.239725351, 8.535984039, 10.323589,
1.236589, 3.562868595, 4.673761368, 2.32547, 4.648055553, 23.70949936,
33.235687, 51025478, 71.8605423, 205.9026794, 295.2356, 31.2587,
51.99817276, 154.70401, 56.32588, 73.30036926, 399.23568, 66.3265,
99.82849121, 2.23568, 3.246135235, 10.43734169, 15.235478, 3.569877,
5.623521328, 5.849419594, 8.32665, 35.3654457, 44.96020508, 195.3657,
14.55177689, 35.235698, 61.02356, 81.59127045, 284.7705994, 23.43979454,
43.92045593, 22.36587, 30.42416763, 181.3415375, 385.9675598,
576.0806274, 602.3258, 25.36730576, 66.235687, 92.2147, 401.4833984,
502.3698, 2.0214578, 10.70767879, 15.36987, 112.3698, 481.0765686,
502.36987, 226.7909851, 300.65478, 55.95266342, 2.36547, 11.85855961,
35.50076675, 45.235698, 25.36954, 34.36005783, 126.9312592, 156.3257,
23.53768349, 39.235687, 4.235687, 6.570708275, 45.36987, 3399.406006,
4500.321547, 990.36547, 1368.160278, 2804.857178, 1000.365, 1375.334717,
3458.573975, 4120.325, 1456.037842, 4100.368, 1500.36578, 6925.325445,
990.58795, 4125.25658, 998.25998, 1827.566895, 3482.541016, 4800.3256,
989.325, 1254.325, 1756.99939, 1998.23569, 1104.429321, 2521.927002,
1800.3256, 2315.543701, 2931.431641, 331.0256, 548.32, 1388.55896,
351.3133545, 898.4367065, 997.02145, 956.32547, 3488.651855,
4400.23556, 558.36987, 785.0509033, 3000.3658, 100.36987, 162.3498688,
162.365, 543.0645752, 1458.283813, 2000.3694, 2495.23877), Privhealth = c(14.3698,
25.36698, 36.01279831, 49.36875, 1.23569, 2.278559208, 8.061329842,
10.3658, 5.059076786, 3.25698, 20.38587761, 30.65877, 4.726452827,
22.79703331, 32.65878, 6.32589, 10.38636589, 19.33849907, 8.326589,
11.07592678, 67.27728271, 74.23658, 63.235698, 83.74517059, 88.83229828,
96.32568, 49.32658, 59.41738892, 138.1631165, 100.23564, 147.8399658,
300.23568, 71.02584, 90.6206665, 8.365984, 11.47062778, 61.48280716,
74.254785, 7.235647, 10.26313496, 19.40570831, 23.65879, 33.25478,
44.17641068, 189.32658, 17.06592751, 75.325689, 89.32658, 136.7345276,
238.6507721, 19.86775017, 63.43461227, 7.325478, 19.23568, 25.321547,
319.0157471, 311.9694214, 442.03695, 3.889117956, 15.3654, 115.02365,
488.0875244, 552.0325698, 10.3658, 36.04922485, 45.362154, 45.23548,
182.7733917, 202.3654, 142.2067719, 202.325, 197.0276337, 9.32658,
32.95304871, 70.28269196, 90.3256, 15.021457, 27.89465141, 44.9021492,
60.32568, 43.03323364, 60.325845, 8.325698, 11.45799065, 60.32568,
1553.358765, 2330.2354, 201.0214578, 305.5347595, 503.7982178,
301.23565, 469.9864197, 990.9689331, 1200.36987, 542.5964966,
1823.021457, 312.0215478, 1100.32145, 301.02145, 1100.3256, 320.365478,
507.0849609, 1114.720093, 2001.23548, 401.14567, 662.03214, 816.2644653,
998.32546, 416.0243225, 692.6192017, 402.32564, 558.3044434,
713.3709106, 998.32658, 302.0214, 793.8995972, 111.2757187, 162.9289398,
212.3657, 442.32598, 1698.060913, 2226.32568, 145.2365, 268.8859863,
902.32568, 42.36587, 75.64861298, 332.65478, 970.5014648, 556.8964233,
700.32658, 5526.447266), Population = c(12412308L, 20779953L,
29185507L, 37172386L, 47887865L, 66224804L, 87639964L, 109224559L,
14539612L, 18905478L, 27013212L, 28087871L, 6216341L, 32428167L,
42723139L, 8449913L, 10946445L, 15049353L, 181413402L, 211513823L,
241834215L, 267663435L, 3565890L, 5122493L, 7261539L, 9956011L,
18029824L, 23194257L, 28208035L, 223158L, 279398L, 515696L, 1432905L,
1794571L, 95212450L, 122283850L, 158503197L, 195874740L, 107647921L,
142343578L, 179424641L, 212215030L, 22071433L, 26459944L, 31989256L,
77991755L, 106651922L, 36800509L, 44967708L, 51216964L, 18777601L,
20261737L, 3286542L, 3089027L, 2913021L, 36870787L, 40788453L,
44494502L, 591021L, 754394L, 149003223L, 195713635L, 209469333L,
8975597L, 14312212L, 16249798L, 3119433L, 4577378L, 4999441L,
70878L, 71625L, 3786695L, 873277798L, 1234281170L, 34545013L,
41801533L, 56558186L, 62952642L, 67195028L, 69428524L, 12697723L,
14439018L, 67988862L, 79910412L, 95540395L, 22031750L, 24982688L,
57247586L, 58892514L, 62766365L, 9967379L, 10251250L, 10895586L,
11433256L, 30685730L, 37057765L, 5140939L, 5793636L, 4986431L,
5515525L, 79433029L, 82211508L, 81776930L, 82905782L, 10196792L,
10805808L, 11121341L, 10731726L, 56942108L, 59277417L, 254826L,
281205L, 318041L, 352721L, 4660000L, 7623600L, 2045123L, 2991884L,
4137309L, 14951510L, 16615394L, 17231624L, 3329800L, 3857700L,
4841000L, 38110782L, 38258629L, 3047132L, 5076732L, 2048583L,
2073894L, 7824909L), Lifeexp = c(50.331, 55.841, 61.028, 64.486,
47.099, 51.941, 61.627, 66.24, 55.564, 54.404, 67.611, 70.478,
61.974, 57.099, 62.973, 45.746, 48.069, 55.251, 62.32, 65.772,
69.205, 71.509, 69.872, 71.73, 73.428, 74.405, 70.865, 72.594,
74.493, 61.529, 70.173, 78.627, 61.608, 52.192, 45.9, 46.267,
50.896, 54.332, 60.1, 62.82, 65.264, 67.114, 66.165, 71.111,
76.516, 68.793, 71.095, 63.307, 56.048, 57.669, 71.333, 75.439,
71.836, 73.955, 76.562, 73.576, 75.278, 76.52, 60.884, 71.46,
66.343, 73.619, 75.672, 53.595, 66.56, 69.57, 75.654, 78.769,
80.095, 74.619, 77.672, 71.46, 57.865, 66.693, 62.764, 65.095,
70.248, 70.623, 74.184, 76.931, 50.64, 61.195, 70.551, 73.025,
75.317, 81.69512195, 82.74878049, 75.8804878, 77.74146341, 80.40243902,
76.05195122, 77.72195122, 80.18292683, 81.59512195, 79.13658537,
81.94878049, 74.80536585, 81.35121951, 74.81317073, 81.83414634,
75.2277561, 77.92682927, 79.98780488, 80.99268293, 76.93902439,
77.88780488, 80.38780488, 81.28780488, 79.77804878, 82.03658537,
78.03634146, 79.65365854, 81.89756098, 82.66097561, 76.60731707,
81.60243902, 73.142, 74.358, 75.398, 76.87804878, 80.70243902,
81.76097561, 75.37804878, 78.63658537, 81.85853659, 70.8902439,
73.74878049, 75.29512195, 81.54146341, 79.42195122, 81.02926829,
82.24634146), Govted = c(1.23568, 2.31245, 3.47945, 5.32658,
2.365, 3.98311, 4.49659, 6.32547, 3.5398, 1.023568, 3.63172,
5.16365, 2.32871, 2.38901, 2.52076, 1.23568, 2.97156, 3.34389,
0.984578, 1.36589, 2.81228, 4.326587, 1.2365897, 1.9654789, 2.3658,
3.58851, 3.23568, 5.97161, 4.96645, 1.23568, 3.21548, 6.32547,
2.32657, 6.99139, 1.32658, 2.012457, 3.214587, 4.235687, 2.51681,
1.83782, 2.28687, 3.9854587, 2.36587, 3.22803, 3.71993, 3.26766,
5.32568, 5.12579, 5.44358, 5.72174, 2.36578, 1.71774, 2.3265,
3.43017, 2.65897, 4.58031, 5.01971, 6.32658, 5.51379, 6.64043,
2.36587, 5.6488, 6.32658, 1.235687, 1.53379, 2.16286, 3.24578,
6.63445, 7.02824, 2.36578, 3.325478, 3.215487, 3.23568, 3.37769,
2.32657, 3.23654, 3.323568, 5.25346, 3.50844, 5.32658, 1.54406,
4.60449, 3.326589, 4.235478, 4.17277, 5.55006, 6.32365, 4.05552,
4.06533, 5.74164, 4.021547, 5.32658, 6.40799, 6.9874564, 5.442,
6.32658, 7.32658, 8.9854587, 5.33591, 7.32658, 3.0215478, 3.21547,
4.91368, 6.3265, 2.04608, 3.23019, 4.32658, 5.023658, 4.29886,
4.35239, 4.25224, 6.44717, 6.97848, 7.235689, 5.43073, 5.54157,
2.985467, 3.124578, 3.32652, 5.22879, 5.48909, 4.236587, 5.321457,
6.323658, 7.5698745, 3.26587, 4.9936, 2.325647, 3.08044, 5.56251,
5.965871, 4.92605)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 11L, 13L, 15L, 16L, 18L, 23L, 24L, 25L, 26L, 27L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, 44L, 45L,
46L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 60L, 62L,
64L, 65L, 66L, 67L, 70L, 71L, 73L, 74L, 75L, 78L, 79L, 80L, 82L,
84L, 85L, 87L, 88L, 89L, 91L, 92L, 93L, 95L, 96L, 99L, 100L,
103L, 105L, 107L, 111L, 112L, 113L, 114L, 115L, 116L, 119L, 120L,
121L, 122L, 124L, 127L, 128L, 129L, 130L, 131L, 133L, 134L, 135L,
136L, 138L, 140L, 141L, 144L, 145L, 148L, 149L, 150L, 151L, 152L,
153L, 154L, 155L, 156L, 158L, 159L, 161L, 162L, 163L, 164L, 165L,
167L, 170L, 171L, 172L, 173L, 175L, 176L, 177L, 178L, 180L, 181L,
182L, 185L, 187L, 191L, 192L, 195L), class = "data.frame")
Test.dat:
structure(list(GDP = c(199.9863423, 156.3857186, 389.3980332,
229.4902871, 497.6320261, 749.552711, 826.6215305, 248.0293672,
261.8689977, 899.6599081, 11373.233, 7076.662423, 5324.61704,
5931.453886, 5082.354757, 715.9137121, 2124.05677, 6374.028196,
463.6186318, 4102.48135, 5268.848504, 4333.482973, 564.7796095,
2258.183141, 3749.75325, 302.5771636, 3772.870012, 2860.43156,
4787.780171, 1614.640122, 749.9085236, 4717.143026, 443.3141934,
2009.978857, 483.952592, 366.1728076, 841.9729898, 563.0577411,
1317.890706, 18211.27459, 21679.24784, 42943.90227, 21448.36196,
47450.31847, 30743.54768, 58041.39844, 24285.46682, 46459.97325,
20825.78421, 34483.204, 21043.57493, 41715.02928, 8794.631229,
26149.41108, 33692.01083, 12599.53358, 15420.91116, 23852.32703,
64581.94402, 9107.477079, 10201.30354, 38428.3855, 37868.296,
82796.54716), Health = c(6.22435541, 8.909747124, 39.22274712,
8.625580788, 4.22284155, 42.34384155, 47.44484155, 10.74555809,
18.80055809, 45.32365, 324.6654166, 602.659668, 504.5536499,
594.8854499, 239.3392792, 22.55662414, 91.84031677, 624.335527,
30.56891763, 128.3355597, 74.23569, 505.4589408, 22.23569, 69.80043793,
311.6526794, 19.73552704, 251.0935822, 211.589745, 250.7455292,
35.25698, 47.90106964, 292.54782, 18.56432343, 70.5685123, 10.56888,
17.38329887, 50.66987, 75.201547, 78.18682861, 1022.5487, 1632.427612,
4002.325, 1452.369, 5044.135254, 2496.047119, 6011.536621, 1655.866211,
4099.587891, 1125.365, 4400.325, 1496.87854, 3000.23568, 336.2356,
2023.143677, 3216.223633, 809.1994019, 956.21547, 820.6981812,
1989.235, 446.3265, 796.6470337, 2985.12, 3737.802979, 9658.23
), Govthealth = c(2.65987, 3.350677967, 8.32365, 1.337858081,
0.235689, 8.714180946, 11.02365, 2.356894, 4.656533241, 5.958777,
198.23568, 319.1759033, 207.0215302, 302.654789, 123.2336197,
9.32658, 29.2992878, 300.5689, 12.02589, 52.658912, 22.03256,
222.325689, 16.3258, 50.29269791, 129.758316, 3.900079966, 163.0175018,
102.369, 156.8104706, 4.36987, 5.465222836, 75.36987, 3.839128733,
14.32589, 3.25478, 5.880064487, 12.36547, 18.02584, 30.97570801,
990.365478, 1116.231445, 3201.0245, 996.598723, 3721.796387,
2074.39917, 5042.459961, 1229.708252, 3167.418213, 889.32658,
3698.23598, 944.5585938, 1998.02365, 200.365778, 1396.733398,
2517.370117, 577.3640747, 662.32589, 298.1834717, 702.369, 456.325,
568.7339478, 889.36547, 1045.900513, 3987.3654), Privhealth = c(1.36589,
1.832908154, 7.325698, 5.431494236, 2.36589, 29.85413742, 35.3698,
4.23568, 8.9836483, 22.3658, 152.36589, 263.3545532, 225.5363922,
301.325478, 111.575592, 10.23568, 60.89479446, 336.02145, 12.36587,
75.36987, 34.3265, 223.02145, 2.0215478, 11.81901455, 180.9026947,
15.41190529, 85.28456879, 45.321478, 86.49634552, 25.36987, 39.00668716,
220.32145, 14.22738075, 49.326545, 7.02145, 11.50323391, 20.36587,
33.021456, 45.45627975, 400.23568, 516.1798096, NA, 400.32547,
1322.338745, 421.6481018, 969.076416, 426.0691833, 931.8737793,
302.1245, 886.02154, 517.4750366, 889.32547, 90.3256, 626.4102173,
698.8658447, 231.8352966, 301.0324, 522.5147705, 1236.021458,
117.3658, 227.9130707, 1965.3256, 2691.985107, 6600.3256), Population = c(9404500L,
11148758L, 18143315L, 23941110L, 5283814L, 7527394L, 9100837L,
17354392L, 23650172L, 19077690L, 31528585L, 365734L, 2118874L,
2448255L, 29027674L, 61895160L, 93966780L, 57779622L, 17325773L,
21670000L, 2866376L, 32618651L, 530804L, 685503L, 174790340L,
12155239L, 3962372L, 70419L, 69650L, 4802000L, 4077131L, 3726549L,
1056575549L, 1352617328L, 20147590L, 27275015L, 10432421L, 11881477L,
87967651L, 17065100L, 19153000L, 66460344L, 27691138L, 34004889L,
5339616L, 5547683L, 5176209L, 5363352L, 56719240L, 60421760L,
6289000L, 8882800L, 2095344L, 15925513L, 4350700L, 38042794L,
37974750L, 4027887L, 5638676L, 1998161L, 1988925L, 6715519L,
7184250L, 8513227L), Lifeexp = c(46.096, 45.09, 63.798, 62.288,
58.824, 68.736, 70.879, 45.853, 46.229, 58.893, 75.997, 75.905,
56.665, 63.373, 74.41, 66.366, 69.823, 63.857, 69.509, 76.812,
78.458, 71.594, 52.878, 68.384, 70.116, 58.432, 77.452, 66.843,
71.116, 70.386, 69.902, 73.6, 62.505, 69.416, 55.5, 58.472, 58.1,
44.649, 74.837, 76.99463415, 79.23414634, 81.35609756, 77.42195122,
81.24634146, 76.59268293, 79.1, 77.46585366, 79.87073171, 76.97073171,
82.94634146, 78.95365854, 82.80243902, 72.15, 77.98780488, 80.70243902,
76.24634146, 77.75365854, 77.95121951, 83.14634146, 73.20487805,
75.41219512, 77.24243902, 79.6804878, 83.55121951), Govted = c(3.27054,
5.24797, 4.71484, 2.97515, 1.36587, 4.00675, 6.32547, 1.023658,
2.46167, 4.32658, 4.53477, 4.11747, 8.34961, 10.23547, 2.8673,
2.36587, 5.326545, 6.15899, 2.41093, 2.11189, 2.46866, 1.06738,
3.21547, 4.02447, 3.94893, 1.65599, 4.68696, 1.856231, 2.032145,
1.56897, 2.18109, 4.236587, 4.32479, 5.326587, 0.36589, 1.01218,
1.45426, 2.36589, 5.13722, 4.6764, 4.89147, 7.3265, 5.99199,
5.36993, 8.08434, 8.55955, 5.71688, 6.54071, 3.325687, 5.32658,
6.12262, 6.32658, 1.326587, 4.58512, 7.00241, 5.06843, 6.32547,
3.3213, 5.32658, 3.32365, 4.32657, 4.52294, 4.7814, 5.9658745
)), row.names = c(9L, 10L, 12L, 14L, 17L, 19L, 20L, 21L, 22L,
28L, 40L, 43L, 47L, 48L, 59L, 61L, 63L, 68L, 69L, 72L, 76L, 77L,
81L, 83L, 86L, 90L, 94L, 97L, 98L, 101L, 102L, 104L, 106L, 108L,
109L, 110L, 117L, 118L, 123L, 125L, 126L, 132L, 137L, 139L, 142L,
143L, 146L, 147L, 157L, 160L, 166L, 168L, 169L, 174L, 179L, 183L,
184L, 186L, 188L, 189L, 190L, 193L, 194L, 196L), class = "data.frame")
You can use the following code
#Remove the NA from the data freme
test.dat <- na.omit(test.dat)
regression.pred <- predict(lm7, newdata = test.dat, type = "raw")
elastic.pred <- predict(elastic4, newdata = test.dat, type = "raw")
tree.pred <- predict(tree2, newdata = test.dat, type = "raw")
rf.pred <- predict(rf1, newdata = test.dat, type = "raw")
test.statistics <- data.frame(
RMSE.regression = RMSE(regression.pred, test.dat$Lifeexp),
Rsquare.regression = R2(regression.pred, test.dat$Lifeexp),
RMSE.el = RMSE(elastic.pred, test.dat$Lifeexp),
Rsquare.el = R2(elastic.pred, test.dat$Lifeexp),
RMSE.tree = RMSE(tree.pred, test.dat$Lifeexp),
Rsquare.tree = R2(tree.pred, test.dat$Lifeexp)
)
test.statistics
# RMSE.regression Rsquare.regression RMSE.el Rsquare.el RMSE.tree Rsquare.tree
# 1 8.201072 0.3910896 8.115006 0.3984145 6.369994 0.6446954
Related
Replace row names with the column value of another data frame based on column matches
I want to replace the row names of meth.kirp.cpg with anno$V1 if the existing row names matches anno$V2. library(tidyverse) rownames(meth.kirp.cpg) <- meth.kirp.cpg %>% rowwise() %>% mutate(out = anno$V1[str_which(colnames(meth.kirp.cpg), anno$V2)]) Traceback: Error in `mutate()`: ℹ In argument: `out = anno$V1[str_which(rownames(meth.kirp.cpg), anno$V2)]`. ℹ In row 1. Caused by error in `str_detect()`: ! Can't recycle `string` (size 142513) to match `pattern` (size 365860). Run `rlang::last_error()` to see where the error occurred. Example data: meth.kirp.cpg > dput(meth.kirp.cpg[1:100,1:2]) structure(list(TCGA.2K.A9WE.01A = c(0.461440642939772, 0.143910373119058, 0.847164847154162, 0.737361955793681, 0.716794733144112, 0.351877113536983, 0.248986769373366, 0.0121360989202765, 0.876303885229884, 0.944311384947134, 0.0490407302658151, 0.0200484962577958, 0.0623434271852525, 0.489865398138095, 0.920994933496615, 0.92663932531651, 0.0149191766670711, 0.884749685210921, 0.446591784140497, 0.91113228700911, 0.912199953863369, 0.908167409366654, 0.386721526377863, 0.0386737340626713, 0.0347492896507038, 0.98309370597552, 0.0176080612232509, 0.91878387167279, 0.743683318738873, 0.939148492241393, 0.722471943330892, 0.613143449419421, 0.0111202783577944, 0.843823786705695, 0.836431557867031, 0.390282953982417, 0.027408710286304, 0.0222349236137297, 0.657221610108816, 0.861848830221141, 0.0433751011272091, 0.0281247935879252, 0.938960776959358, 0.919825831744144, 0.922071582222369, 0.874732275907705, 0.0287898761495033, 0.0266947996996682, 0.922915821025777, 0.95009866012662, 0.964858875373814, 0.106451342824246, 0.406100902807456, 0.0421684244823044, 0.0341277368595181, 0.805451068725895, 0.147595746750675, 0.602617067494429, 0.90660866745333, 0.922313274809095, 0.462291286891102, 0.502857899902497, 0.0292904155423265, 0.835117565787527, 0.146789494933407, 0.06805696389495, 0.970563583145203, 0.0379479981289824, 0.058526761439653, 0.938993650169269, 0.44761099556807, 0.558961729061086, 0.939778576056268, 0.0728795533192928, 0.812084345787681, 0.899377654465699, 0.940111049552295, 0.838186810388758, 0.715121288990262, 0.897506380407565, 0.0929678061732199, 0.99024632582796, 0.055583745670494, 0.835146654988372, 0.973309086845447, 0.651216797099359, 0.0218535991986461, 0.0999671036378156, 0.790540668893094, 0.980591855409854, 0.567883806155822, 0.774816434396113, 0.904434807209845, 0.16641097147085, 0.0102686285230525, 0.65243489007093, 0.917594420539083, 0.0147831247626457, 0.844679485594683, 0.65566679452182), TCGA.2Z.A9J1.01A = c(0.595894468074615, 0.0807243779293262, 0.867305510246114, 0.70680600651273, 0.217862460492399, 0.169408257004071, 0.173115013795265, 0.0108902025634162, 0.813866558997356, 0.938576461648791, 0.0426568318037534, 0.0133187057875756, 0.0540543120983417, 0.317547629906197, 0.89911570032979, 0.525131175543627, 0.0152198596492253, 0.586968687135673, 0.49896100615873, 0.946718072906056, 0.859306039060091, 0.91185524112895, 0.28077646371254, 0.0413484993379312, 0.169193526857136, 0.941230054689418, 0.0164701153466769, 0.928402415411224, 0.736184540407898, 0.946288965623826, 0.312150292032857, 0.403171876971832, 0.0091246246912222, 0.535149883791691, 0.801041308364712, 0.171664264695538, 0.022737572168221, 0.0164834707992085, 0.34399568227201, 0.690016503202975, 0.0390842331750004, 0.0270854886242561, 0.888936631403145, 0.911902815624012, 0.858247513475469, 0.877113632682254, 0.0342892379505875, 0.0387268488822914, 0.922299785913074, 0.926130065834329, 0.975692332236198, 0.105415153493416, 0.127593519059119, 0.0540003798276299, 0.030980833881057, 0.914299941557146, 0.0512267439881511, 0.307325891435045, 0.941037265659174, 0.927078967007025, 0.48873418258592, 0.259006924115841, 0.0278764868641079, 0.87768067729952, 0.302640875302654, 0.0706384569300761, 0.968762634771395, 0.0364352674378962, 0.0441231506131831, 0.8307385629478, 0.242575477196221, 0.513439830376976, 0.932449172188782, 0.0526229004254996, 0.81314353054328, 0.778591104943176, 0.95668645045373, 0.453172059602829, 0.250129171963381, 0.863470213940097, 0.0994627135023581, 0.989489689575077, 0.0472116225581592, 0.911407225108748, 0.825189076107663, 0.578029414148402, 0.018058167343065, 0.0855852777154159, 0.819733395638372, 0.988287891473147, 0.255899615791521, 0.643359326354994, 0.491979154678761, 0.0978562004864199, 0.0105671614378101, 0.48897100984416, 0.9024550858788, 0.0131702158217202, 0.81328537816321, 0.85890307119103)), row.names = c("cg00000029", "cg00000165", "cg00000236", "cg00000289", "cg00000292", "cg00000321", "cg00000363", "cg00000622", "cg00000658", "cg00000721", "cg00000734", "cg00000769", "cg00000905", "cg00000924", "cg00000948", "cg00000957", "cg00001245", "cg00001249", "cg00001261", "cg00001349", "cg00001364", "cg00001446", "cg00001510", "cg00001582", "cg00001583", "cg00001687", "cg00001747", "cg00001791", "cg00001809", "cg00001854", "cg00001874", "cg00002033", "cg00002116", "cg00002145", "cg00002190", "cg00002224", "cg00002236", "cg00002406", "cg00002426", "cg00002449", "cg00002464", "cg00002490", "cg00002531", "cg00002591", "cg00002593", "cg00002597", "cg00002660", "cg00002719", "cg00002769", "cg00002808", "cg00002809", "cg00002810", "cg00002837", "cg00003091", "cg00003173", "cg00003181", "cg00003287", "cg00003345", "cg00003513", "cg00003529", "cg00003578", "cg00003625", "cg00003784", "cg00003969", "cg00003994", "cg00004055", "cg00004067", "cg00004072", "cg00004082", "cg00004089", "cg00004105", "cg00004121", "cg00004192", "cg00004207", "cg00004209", "cg00004429", "cg00004533", "cg00004562", "cg00004608", "cg00004773", "cg00004818", "cg00004883", "cg00004939", "cg00004963", "cg00004979", "cg00004996", "cg00005010", "cg00005040", "cg00005072", "cg00005083", "cg00005112", "cg00005166", "cg00005215", "cg00005297", "cg00005306", "cg00005390", "cg00005437", "cg00005543", "cg00005617", "cg00005619"), class = "data.frame") anno > dput(anno[1:100,]) structure(list(V1 = c("TSPY4", "TTTY14", "TMSB4Y", "TBL1Y", "TMSB4Y", "TSPY4", "RPS4Y2", "EIF1AY", "PCDH11Y", "TBL1Y", "ZFY", "FAM197Y2", "TTTY14", "TSPY4", "ZFY", "NLGN4Y", "EIF1AY", "TSPY4", "TBL1Y", "UTY", "PRKY", "ZFY", "CD24", "PRKY", "TSPY1", "CYorf15A", "TSPY2", "TTTY15", "RPS4Y2", "UTY", "CYorf15A", "RPS4Y2", "TSPY2", "TBL1Y", "TSPY3", "DDX3Y", "CYorf15A", "ZFY", "RBMY1F", "DDX3Y", "RPS4Y2", "ZFY", "DDX3Y", "TTTY15", "BCORL2", "PCDH11Y", "KDM5D", "TTTY14", "EIF1AY", "DDX3Y", "LOC100101121", "CYorf15A", "TTTY15", "TSPY1", "TSPY1", "FAM197Y2", "TSPY4", "TMSB4Y", "DDX3Y", "TTTY15", "TTTY20", "NLGN4Y", "TSPY4", "CYorf15A", "RPS4Y2", "KDM5D", "RBMY1J", "EIF1AY", "KDM5D", "ZFY", "TGIF2LY", "HMGN5", "EBP", "UBL4A", "WDR13", "MTM1", "BCOR", "ZCCHC12", "FTHL17", "PORCN", "NAA10", "PCDH11X", "ARSE", "DOCK11", "PDK3", "LONRF3", "MAGIX", "PCYT1B", "SLC6A8", "UBE2A", "TAF9B", "STARD8", "BCOR", "ZIC3", "IL1RAPL2", "TMSB4X", "CLCN5", "LOC100133957", "SCML1", "GNL3L"), V2 = c("cg00050873", "cg00212031", "cg00214611", "cg01707559", "cg02004872", "cg02011394", "cg02050847", "cg02233190", "cg02494853", "cg02839557", "cg02842889", "cg03052502", "cg03244189", "cg03443143", "cg03683899", "cg03706273", "cg03750315", "cg04016144", "cg04042030", "cg04448376", "cg04689676", "cg04840163", "cg05230942", "cg05480730", "cg05544622", "cg05621349", "cg05865243", "cg05890011", "cg06322277", "cg06479204", "cg07731488", "cg07747963", "cg08242338", "cg08921682", "cg09350919", "cg09856092", "cg10076560", "cg10213302", "cg10267609", "cg10698069", "cg10841270", "cg11131351", "cg14180491", "cg14741114", "cg15027426", "cg15295597", "cg15329860", "cg15345074", "cg15422579", "cg15429127", "cg15682806", "cg15682993", "cg15746461", "cg15810474", "cg15935877", "cg17834650", "cg17837162", "cg18032798", "cg18077436", "cg25032547", "cg25071634", "cg25518695", "cg25705492", "cg25756647", "cg26058907", "cg26517491", "cg26983430", "cg26983535", "cg27049643", "cg27433982", "cg27539833", "cg00008945", "cg00011200", "cg00011891", "cg00014152", "cg00016522", "cg00016934", "cg00018261", "cg00021786", "cg00026186", "cg00072288", "cg00072839", "cg00074638", "cg00112256", "cg00114625", "cg00114913", "cg00116709", "cg00139317", "cg00140085", "cg00142683", "cg00192980", "cg00200463", "cg00206414", "cg00240113", "cg00241296", "cg00241907", "cg00264378", "cg00265812", "cg00266918", "cg00360365")), row.names = c(1L, 2L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 16L, 17L, 18L, 20L, 21L, 23L, 25L, 27L, 29L, 31L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 45L, 47L, 48L, 50L, 51L, 52L, 54L, 56L, 57L, 58L, 61L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 82L, 83L, 85L, 86L, 87L, 88L, 89L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 122L, 123L, 124L, 125L), class = "data.frame")
There is no match between your cpg dataframe and annotation dataframe: table(rownames(meth.kirp.cpg) %in% anno$V2) # FALSE # 100 Below should work with your full data, assuming there is are no duplicates and all rownames are in annotation dataframe: rownames(meth.kirp.cpg) <- anno$V2[ match(rownames(meth.kirp.cpg), anno$V2) ]
Event Analysis around Earnings Announcement Dates with multiple firms and multiple dates
I am trying to compute Cumulative Abnormal Returns per firm around the EPS announcement dates by using the EvReturn function from the ererpackage. I have a dataframe (df) with column Date (time-series), 5 columns with daily firms returns, and last column with the market index returns. The second dataframe (Dates_clean), consists of 2 columns, one referring to the firms and the second with the announcement dates. This dataframe has been tidied and cleaned from NAs and it is possible that a firm is not present due to missing observations regarding EPS dates. df looks as follow (subset only for the last 2 years, daily observations of returns): df <-- structure(list(Date = c("2021-12-31", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07", "2022-01-10", "2022-01-11", "2022-01-12", "2022-01-13", "2022-01-14", "2022-01-17", "2022-01-18", "2022-01-19", "2022-01-20", "2022-01-21", "2022-01-24", "2022-01-25", "2022-01-26", "2022-01-27", "2022-01-28", "2022-01-31", "2022-02-01", "2022-02-02", "2022-02-03", "2022-02-04", "2022-02-07", "2022-02-08", "2022-02-09", "2022-02-10", "2022-02-11", "2022-02-14", "2022-02-15", "2022-02-16", "2022-02-17", "2022-02-18", "2022-02-21", "2022-02-22", "2022-02-23", "2022-02-24", "2022-02-25", "2022-02-28", "2022-03-01", "2022-03-02", "2022-03-03", "2022-03-04", "2022-03-07", "2022-03-08", "2022-03-09", "2022-03-10", "2022-03-11", "2022-03-14", "2022-03-15", "2022-03-16", "2022-03-17", "2022-03-18", "2022-03-21", "2022-03-22", "2022-03-23", "2022-03-24", "2022-03-25", "2022-03-28", "2022-03-29", "2022-03-30", "2022-03-31", "2022-04-01", "2022-04-04", "2022-04-05", "2022-04-06", "2022-04-07", "2022-04-08", "2022-04-11", "2022-04-12", "2022-04-13", "2022-04-14", "2022-04-15", "2022-04-18", "2022-04-19", "2022-04-20", "2022-04-21", "2022-04-22", "2022-04-25", "2022-04-26", "2022-04-27", "2022-04-28", "2022-04-29"), NESN = c(NA, 0.0128217636632681, 0.0055390098819148, -0.0303446403908639, -0.00353202700477517, -0.0148639604729367, -0.0242468464841654, 0.000833755941585634, 0.000566825263230264, -0.00107292453477992, -0.00413304805849857, 0.00944796286389749, 0.00606019163539706, -0.0178671560381302, 0.0102682549157, 0.00238042231089342, -0.0264129023440169, -0.00359737479380895, -0.00057237456191328, 0.00371815995136426, -0.00208920373247656, 0.00360658333406638, 0.00460158995889248, 0.0124240943672786, -0.0192088934850052, -0.0116772543954452, 0.00161803666364402, -0.00947063835830442, 0.00910418738855001, -0.0111621216023017, 0.0149520456912964, -0.00898947983118181, -0.000580331235212728, -0.00085760228693943, 0.00337970744966198, 0.00873269055978332, 0.0104415116340701, -0.00548153587914391, 0.00615346747892431, -0.0248914439483823, 0.0173285069170668, 0.0237445174472881, 0.0114592474061839, -0.00044228969975324, -0.0129341888051192, -0.0039655856135451, -0.0259827935397877, -0.0295994170754248, 0.0307311936266654, -0.013299455406402, -0.0056093783764315, 0.0110647313950434, 0.00334040819250792, 0.00425756006997746, 0.0172069005359381, 0.0251378343192163, -0.0021649080340248, 0.00189626896268957, -0.0135983085238801, 0.0114262007450368, -0.00381128173575684, 0.0058674672957324, -0.00307865494333059, 0.00175366558880397, 0.00248499184478623, 0.00721501950695935, 0.0140475807883897, 0.0150789811679537, -0.0124887026538493, 0.00613195773358854, 0.0132145840052262, -0.0015996735360132, -0.0066051386016398, 0.00478929567729036, 0.000712513206063692, 0, -0.00234061707177347, -0.0254790654941593, -0.002887253994175, 0.0031573171246464, 0.0188337863652812, 0.0128193479643504, -0.0131137915005016, 0.0142879572931387, 0.00363369724621165, -0.00110402156089173 ), MC = c(NA, 0.00550261575073563, 0.0177834448996657, 0.0188176125853796, -0.0407649022737596, -0.0155420098162522, -0.0183010694192803, 0.0103886467592562, -0.00183070448319778, -0.0263867607751148, -0.021159281495444, 0.0162863512416866, -0.0195218616791354, 0.036701342749085, 0.00888605545163479, -0.00951769122788382, -0.049340634153576, 0.00633645237423841, 0.0248878672659074, 0.0152138699844748, 0.0322766524097042, 0.00865438181820477, 0.0132850798110877, -0.00396012072659391, -0.0241332685589187, -0.00786778247404551, 0.0141615838200913, -0.0139641131076824, 0.014870269120445, -0.0145131154740279, -0.0300199660152931, -0.0218975214448938, 0.0358204424148594, -0.00922132830585842, -0.00509031265552273, 0.00321590363749413, -0.0205452396116511, -0.00446266058653078, -0.00508069260599331, -0.0449083741501392, 0.0482780235448934, -0.0100514536376457, -0.0516743602589363, 0.0194952092513918, -0.0322887147335422, -0.0641394765595972, -0.0173068505872533, -0.0311734655958205, 0.0954377559421478, -0.0243947811500892, 0.0119067888938236, 0.00638796543754672, -0.0145319835423444, 0.065932734214486, 0.00143122890265102, 0.0152422943022248, -0.0096967499833438, 0.0168987761680872, -0.0304396645441839, 0.000640877885772717, 0.00208067872578832, 0.0154956918516587, 0.0567879413578825, -0.0168206261325913, -0.0168057506728613, 0.000308592594485901, 0.0220126129867861, -0.00451860424097195, -0.0373729875542405, -0.0191766740018862, 0.0123403885706372, -0.0186802220420382, 0.0177444777777833, 0.005072438614252, 0.00930500067891504, 0, -3.12499902288543e-07, -0.00921874999999994, 0.0129321873521526, 0.0172808575677141, -0.0211199942209961, -0.0375232958098812, -0.0134827788364557, 0.0153135163323701, 0.000972753811222749, 0.00372650801247842 ), ASML = c(NA, -0.00707457495736874, -0.0290725339785473, -0.0152647901460136, -0.0184823371590401, 0.00971814435287954, -0.0640693722781605, 0.00755262856381633, 0.0307819825808584, 0.0255294675755942, -0.0310802702293709, 0.0217999099657287, -0.0225541062341073, -0.0236980117549149, 0.0220375314655912, -0.0167179739743669, -0.070396438711375, -0.0162393217912211, 0.0253696004759689, -0.0127093712930013, -0.0283213085062449, 0.0496373256889857, 0.0104341989959946, -0.00316405782212825, -0.0412701616473979, -0.0167302195887068, 0.014887800465154, -0.0106529882383843, 0.0393645189761693, -0.00798216711956512, -0.024996058038682, -0.017207904689711, 0.0235835268894051, 0.00174602988969963, 0.00749259452866369, -0.0134901418194397, -0.0343618513323983, 0.0248730936819173, -0.000354295774261049, -0.0108096737888227, 0.0553563040643841, 0.0166350336725456, -0.0362331061055623, 0.010395360497353, -0.0169754801097395, -0.069247700897907, 0.0131186232688818, -0.0436560986000191, 0.077563666368923, -0.0210021464720349, -0.00476696090336659, -0.0152907148120854, 0.011037414017806, 0.0712443030593835, 0.0100189998239766, 0.0430985046864196, 0.00459075408780252, 0.0236664062345713, -0.0212058320781517, 0.00293240036892017, 0.00730826701315568, 0.00403160465895103, 0.0165410283377181, -0.0170617639615278, -0.0196080007058823, -0.00540967213114751, 0.0176357313934843, -0.0294781484344746, -0.0457273107021545, -0.00769569645056023, 0.00334966691029259, -0.029861744320177, 0.0146653992395438, 0.010171668543006, -0.0130715444394179, 0, -1.7898690185536e-07, 0.00519062108466084, 0.0530628561253561, -0.0125130178368247, -0.0219178119722282, -0.0411412887152115, -0.0334124520723025, -0.00132262939176431, 0.0378286505877248, -0.00127556088046676), ROG = c(NA, -0.0016563124362825, -0.0167576047110743, 0.00558547220350492, -0.00931186868686873, 0.00929598170989054, -0.0189468728798304, 0.0102378674073382, -0.0211679240011176, -0.0090002397780341, 0.00835899311866473, 0.00677858618059668, -0.004236109928897, 0.00246186811339411, -3.6951028517751e-05, -0.0085303180179872, -0.0453608483871986, 0.00297013943738533, 0.00575500793484429, 0.0165975968466054, 0.00233697546175771, -0.000929104641140599, 0.0127388162493018, -0.00699098766109441, -0.034148341582287, -0.0182875241992058, 0.0008188551362458, 0.0038028755990831, 0.0290255912422577, -0.00518866385230854, -0.014558705998831, -0.0111877742472386, 0.0114612566678807, 0.00773075782606858, -0.00964711322222001, -0.00537533349502783, -0.00277687179752917, 0.0100685911308367, 0.00239370555303364, -0.00492692631648861, 0.0156943530061375, 0.0141076879880042, 0.0095079601184076, -0.00810543484286608, -0.0153314409260049, -0.00265576875393958, 0.011543968321482, 0.00650858137799504, 0.0172871323248192, -0.013768790740987, 0.00885575961597662, 0.0135187375923218, -5.44893487662845e-05, -0.00846638540982525, 0.00148964049045319, 0.0260458184590857, 0.0049176082464939, -0.000655460348850556, -0.000953002940294323, 0.0201022930361616, 0.00785770864045432, -0.00585620372821327, -0.0105159715950733, -0.00101256130850647, -0.00614261872165789, 0.0145685186996185, 0.0366446438948085, 0.00785569119190255, 0.0111315542528179, 0.0105840795630807, 0.0110330061523778, 0.00756508782135668, -0.0247558224043232, 0.000999685813030116, -0.00142410234860157, 0, -0.00234052114911543, -0.0406737559105963, -0.0119804035990065, -0.0253254619925773, 0.00693062609568162, -0.00655543146327209, -0.0173983359583453, 0.0239373293877456, -0.00889126611629498, -2.8255212379813e-06), DJSTOXX = c(NA, 0.00448956330284256, 0.00822265035062086, 0.000665968984872922, -0.0125074340644002, -0.00391672453668146, -0.014833902655213, 0.00842939391029529, 0.00646896140565878, -0.000302344092258267, -0.0100667827031564, 0.00696648315220894, -0.00975618817245349, 0.00231978423713897, 0.00508630710398661, -0.0184133486433087, -0.0381245455216094, 0.00708217470094685, 0.0167954411441527, 0.00646040726886765, -0.0101525109126204, 0.00715709523318564, 0.0127472893082297, 0.0045192172025077, -0.0175615871465199, -0.0138360753686276, 0.0067878981966738, 0.000120356858084092, 0.0171767249252694, -0.00207254104941135, -0.00589392210453665, -0.0183275230158848, 0.0143092365731592, 0.000447004322467581, -0.0068709568459443, -0.00805937763694919, -0.0130249474835507, 0.000692602999960323, -0.00277069175010591, -0.0328295068963999, 0.0331852560597776, -0.000926074963563228, -0.0237052175314, 0.00896542681851686, -0.020101628384252, -0.0356274007682458, -0.0110105316066746, -0.00508229969266472, 0.0468249263880254, -0.0168766270684341, 0.00950095407560014, 0.0120160585194342, -0.00282797911787214, 0.0306352270637984, 0.00455346192440631, 0.0091122594851849, 0.000435550608561153, 0.00846535252146574, -0.0100600682445028, -0.00211439307007666, 0.00104840080252311, 0.00137141546833175, 0.0174494900565429, -0.00411821862698636, -0.00940915708728995, 0.00543807309261601, 0.00839116898191072, 0.00191698129536877, -0.015334581522056, -0.0020812772770139, 0.0130697264521857, -0.00588544107183153, -0.0035024167766855, 0.000289061644585642, 0.00664430735011456, 0, 0, -0.00769436536709167, 0.00837864552170919, 0.00318842343654246, -0.0178911495356058, -0.0180825084765205, -0.00902690097368308, 0.00728639570524336, 0.00620287637010186, 0.00743961741666777)), class = "data.frame", row.names = c(NA, -86L)) Whereas, Dates_clean is: Dates_clean <-- structure(list(stock = c("NESN", "NESN", "MC", "ASML", "ASML", "ROG", "ROG"), EPS = structure(c(19103, 19040, 19019, 19102, 19011, 19107, 19026), class = "Date")), row.names = c("33", "130", "260", "293", "390", "423", "520"), class = "data.frame", na.action = structure(c(`NA` = 1L, NA.1 = 2L, NA.2 = 3L, NA.3 = 4L, NA.4 = 5L, NA.5 = 6L, NA.6 = 7L, NA.7 = 8L, NA.8 = 9L, NA.9 = 10L, NA.10 = 11L, NA.11 = 12L, NA.12 = 13L, NA.13 = 14L, NA.14 = 15L, NA.15 = 16L, NA.16 = 17L, NA.17 = 18L, NA.18 = 19L, NA.19 = 20L, NA.20 = 21L, NA.21 = 22L, NA.22 = 23L, NA.23 = 24L, NA.24 = 25L, NA.25 = 27L, NA.26 = 28L, NA.27 = 29L, NA.28 = 30L, NA.29 = 31L, NA.30 = 32L, NA.31 = 33L, NA.32 = 34L, NA.33 = 35L, NA.34 = 36L, NA.35 = 37L, NA.36 = 38L, NA.37 = 39L, NA.38 = 40L, NA.39 = 41L, NA.40 = 42L, NA.41 = 43L, NA.42 = 44L, NA.43 = 45L, NA.44 = 46L, NA.45 = 47L, NA.46 = 48L, NA.47 = 49L, NA.48 = 50L, NA.49 = 51L, NA.50 = 52L, NA.51 = 53L, NA.52 = 54L, NA.53 = 55L, NA.54 = 57L, NA.55 = 58L, NA.56 = 59L, NA.57 = 60L, NA.58 = 61L, NA.59 = 62L, NA.60 = 63L, NA.61 = 64L, NA.62 = 65L, NA.63 = 66L, NA.64 = 67L, NA.65 = 68L, NA.66 = 69L, NA.67 = 70L, NA.68 = 71L, NA.69 = 72L, NA.70 = 73L, NA.71 = 74L, NA.72 = 75L, NA.73 = 76L, NA.74 = 77L, NA.75 = 78L, NA.76 = 79L, NA.77 = 80L, NA.78 = 81L, NA.79 = 82L, NA.80 = 83L, NA.81 = 84L, NA.82 = 85L, NA.83 = 86L, NA.84 = 87L, NA.85 = 88L, NA.86 = 89L, NA.87 = 90L, NA.88 = 91L, NA.89 = 92L, NA.90 = 93L, NA.91 = 94L, NA.92 = 95L, NA.93 = 96L, NA.94 = 97L, NA.95 = 98L, NA.96 = 99L, NA.97 = 100L, NA.98 = 101L, NA.99 = 102L, NA.100 = 103L, NA.101 = 104L, NA.102 = 106L, NA.103 = 107L, NA.104 = 108L, NA.105 = 109L, NA.106 = 110L, NA.107 = 111L, NA.108 = 112L, NA.109 = 113L, NA.110 = 114L, NA.111 = 115L, NA.112 = 116L, NA.113 = 117L, NA.114 = 118L, NA.115 = 119L, NA.116 = 120L, NA.117 = 121L, NA.118 = 122L, NA.119 = 124L, NA.120 = 125L, NA.121 = 126L, NA.122 = 127L, NA.123 = 128L, NA.124 = 129L, NA.125 = 130L, NA.126 = 131L, NA.127 = 132L, NA.128 = 133L, NA.129 = 134L, NA.130 = 135L, NA.131 = 136L, NA.132 = 137L, NA.133 = 138L, NA.134 = 139L, NA.135 = 140L, NA.136 = 141L, NA.137 = 142L, NA.138 = 143L, NA.139 = 144L, NA.140 = 145L, NA.141 = 146L, NA.142 = 147L, NA.143 = 148L, NA.144 = 149L, NA.145 = 150L, NA.146 = 151L, NA.147 = 152L, NA.148 = 153L, NA.149 = 155L, NA.150 = 156L, NA.151 = 157L, NA.152 = 158L, NA.153 = 159L, NA.154 = 160L, NA.155 = 161L, NA.156 = 162L, NA.157 = 163L, NA.158 = 164L, NA.159 = 165L, NA.160 = 166L, NA.161 = 167L, NA.162 = 168L, NA.163 = 169L, NA.164 = 170L, NA.165 = 171L, NA.166 = 172L, NA.167 = 173L, NA.168 = 174L, NA.169 = 175L, NA.170 = 177L, NA.171 = 178L, NA.172 = 179L, NA.173 = 180L, NA.174 = 181L, NA.175 = 182L, NA.176 = 183L, NA.177 = 184L, NA.178 = 185L, NA.179 = 186L, NA.180 = 187L, NA.181 = 188L, NA.182 = 189L, NA.183 = 190L, NA.184 = 191L, NA.185 = 192L, NA.186 = 193L, NA.187 = 194L, NA.188 = 195L, NA.189 = 196L, NA.190 = 197L, NA.191 = 198L, NA.192 = 199L, NA.193 = 200L, NA.194 = 201L, NA.195 = 202L, NA.196 = 203L, NA.197 = 204L, NA.198 = 205L), class = "omit")) Those are subsets of my original dataframes, encompassing more than 20 years of daily observations and 250 firms. My goal is to compute automatically cumulative abnormal returns for each firm around each earnings announcement date. I was able to compute the simple 1 firm or multiple firms with 1 event date in common for all. I am struggling in telling R how to match each date and firm with the dataframe containing returns and then compute the cumulative abnormal returns. Until now I have tried this code: hh2 <- list() for(i in Dates_clean[2]){ firms <- colnames(df)[2:6] hh2[[i]] <- evReturn(y = df, firm = firms, event.date = i, y.date = "Date", index = "DJSTOXX", event.win = 3, est.win = 100, digits = 4)} Error in xj[i] : only 0's may be mixed with negative subscripts Any help is highly appreciated.
Boxplot labelling outliers returns an error using data rownames
I am trying to label the outliers in my boxplot using the text function so I can find out from which class the outliers are coming from. I've stored the rownames of my data in variable "rownames" using names(vehData) to get the row names. When I apply this however, I get an error. ERROR: Error in which(removeOutliers1 == bxpdat$out, arr.ind = TRUE) : 'list' object cannot be coerced to type 'double' Completely new to R programming. Completely not sure how to fix this or what I am doing wrong Thanks in advance for any help! library(reshape2) vehData <- structure( list( Samples = 1:6, Comp = c(95L, 91L, 104L, 93L, 85L, 107L), Circ = c(48L, 41L, 50L, 41L, 44L, 57L), D.Circ = c(83L, 84L, 106L, 82L, 70L, 106L), Rad.Ra = c(178L, 141L, 209L, 159L, 205L, 172L), Pr.Axis.Ra = c(72L, 57L, 66L, 63L, 103L, 50L), Max.L.Ra = c(10L, 9L, 10L, 9L, 52L, 6L), Scat.Ra = c(162L, 149L, 207L, 144L, 149L, 255L), Elong = c(42L, 45L, 32L, 46L, 45L, 26L), Pr.Axis.Rect = c(20L, 19L, 23L, 19L, 19L, 28L), Max.L.Rect = c(159L, 143L, 158L, 143L, 144L, 169L), Sc.Var.Maxis = c(176L, 170L, 223L, 160L, 241L, 280L), Sc.Var.maxis = c(379L, 330L, 635L, 309L, 325L, 957L), Ra.Gyr = c(184L, 158L, 220L, 127L, 188L, 264L), Skew.Maxis = c(70L, 72L, 73L, 63L, 127L, 85L), Skew.maxis = c(6L, 9L, 14L, 6L, 9L, 5L), Kurt.maxis = c(16L, 14L, 9L, 10L, 11L, 9L), Kurt.Maxis = c(187L, 189L, 188L, 199L, 180L, 181L), Holl.Ra = c(197L, 199L, 196L, 207L, 183L, 183L), Class = c("van", "van", "saab", "van", "bus", "bus") ), row.names = c(NA, 6L), class = "data.frame") #Remove outliers removeOutliers <- function(data) { OutVals <- boxplot(data)$out remOutliers <- sapply(data, function(x) x[!x %in% OutVals]) return (remOutliers) } vehDataRemove1 <- vehData[, -1] vehDataRemove2 <- vehDataRemove1[,-19] vehData <- vehDataRemove2 vehClass <- vehData$Class rownames <- names(vehData) #column names #Begin removing outliers removeOutliers1 <- removeOutliers(vehData) bxpdat <- boxplot(removeOutliers1) #Also tried using vehicles$Class instead of rownames but get the same error text(bxpdat$group, bxpdat$out, rownames[which(removeOutliers1 == bxpdat$out, arr.ind = TRUE)[,1]], pos = 4) The boxplot looks like this. I am trying to label the outliers based on the x axis e.g. "Comp", "Circ", "D.Circ", "Rad.Ra", "Max.L.Ra" etc.. & by vehicle class "Van", "Bus" .. Crammed text issue when identifying class
If it is the outliers in the 2nd boxplot, it would be: bxpdat <- boxplot(removeOutliers1) text(bxpdat$group, bxpdat$out, bxpdat$names[bxpdat$group], pos = 4) Maybe looks better like this, if you adjust the margin and flip the labels: par(mar=c(8,3.5,3.5,3.5)) bxpdat = boxplot(removeOutliers1,las=2,cex=0.5) text(bxpdat$group, bxpdat$out, bxpdat$names[bxpdat$group], pos = 4,cex=0.5)
I understood the question differently to #StupidWolf. I thought the goal was to replace points indicating outliers with the text of the vehicle class (bus, van or saab). If you simply print the variable name (e.g. Skew.maxis), then you might as well have simply plotted the outliers as points. Unless I'm missing something. Here is code to answer the question as I understood it, for what it's worth (beginning after defining removeOutliers): # CHANGE: Create vehClass vector before removing Class from the dataframe vehClass <- vehData$Class vehDataRemove1 <- vehData[, -1] vehDataRemove2 <- vehDataRemove1[,-19] vehData <- vehDataRemove2 #Begin removing outliers removeOutliers1 <- removeOutliers(vehData) bxpdat <- boxplot(removeOutliers1) # use boxplot(vehData) if you plot all the outliers as points # loop over columns n_plot <- 1; set.seed(123) # only plot n_plot randomly-chosen outliers for(i in 1:ncol(vehData)){ # find out which row indices were removed as outliers diffInd <- which(vehData[[i]] %in% setdiff(vehData[[i]], removeOutliers1[[i]])) # if none were, then don't add any outlier text if(length(diffInd) == 0) next print(i) print(paste0("l:", length(diffInd))) if(length(diffInd) > n_plot){ diffIndPlot <- sample(diffInd, n_plot, replace = FALSE) } else diffIndPlot <- diffInd text(x = i, y = vehData[[i]][diffIndPlot], labels = paste0(vehClass[diffIndPlot], ": ", vehData[[i]][diffIndPlot])) }
How could I insert a histogram into a `geom_smooth` plot?
I am trying to mimic some figures from journal papers. Here is an example from Schlenker and Roberts (2009). I'd like to add a similar histogram to my own plot. Please see below. Is it possible to achieve this task with ggplot? Thanks. See a dput data below. rh represents x axis and yhat1 indicates the y axis. > dput(df.m[,c('rh','yhat1')]) structure(list(rh = c(11L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L), yhat1 = c(0.0097784, 0.111762325, 0.0887123966666667, 0.24714677, 0.079887235, 0.162714825, 0.24789043, 0.107558165, 0.182885584545455, 0.136690964444444, 0.159203683333333, 0.5156053805, 0.587034213636364, 0.233377613, 0.31531245, 0.4778449572, 0.212574774137931, 0.2274105676, 0.253733041707317, 0.560999839354839, 0.224892959444444, 0.392268151304348, 0.351498776603774, 0.366547010727273, 0.35013903469697, 0.382026272372881, 0.510611202461538, 0.391176294871795, 0.423356474328358, 0.380316089137931, 0.459821489651163, 0.388949226593407, 0.506833284166667, 0.459263999259259, 0.558535709906542, 0.745323656071429, 0.60167464606383, 0.72210854266129, 0.695203745656566, 0.638265557105263, 0.52373110503876, 0.611695133046875, 0.963833986386555, 0.803060819275362, 0.837984669112426, 0.7931166204, 0.870764136976744, 1.21005393820225, 0.862845527777778, 1.028402381125, 1.2077895633526, 1.01176334204082, 1.08139833964706, 0.90346288, 1.05871937863014, 1.27788244930233, 1.16250975336634, 1.1450916525, 1.4412301412, 1.21264826238281, 1.35417930411504, 1.18588206727273, 1.40277204710084, 1.33194569259259, 1.18413544210084, 1.22718163528571, 1.33992107226667, 1.44770425268156, 1.43974964777778, 1.26656031551351, 1.58998655363636, 1.29994566024272, 1.46398530493902, 1.26061274530055, 1.30718501225275, 1.20523443567901, 1.23789593428571, 1.34433582230769, 1.36438752851852, 1.5915544857037, 1.10979387898438, 1.31898147708661, 1.426120105, 1.52075980155738, 1.40629729460177, 0.9048366681, 1.2973945580531, 1.37696154192982)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -88L))
Hopefully this can get you started: library(ggplot2) breaks <- 20 maxcount <- max(table(cut(df.m$rh,breaks = 20))) + 1 ggplot(data = df.m, aes(x = rh)) + stat_smooth(formula = y ~ x, aes(y = yhat1 * 10 + maxcount), method = "loess") + scale_y_continuous(breaks = c(0,5), "Exposure (Days)", sec.axis = sec_axis(~ (. - maxcount) /10, "Log of Daily Confirmed Case Counts")) + geom_histogram(bins = breaks, color = "black", fill = "green") + geom_segment(aes(x = 85, xend = 85, y = 0 + maxcount, yend = Inf), col = "red", linetype = "dashed") + labs(x = "Relative Humidity Percentage") + theme_classic() + theme(axis.line.y.left = element_line(color= "green"), axis.title.y.left = element_text(hjust = 0.05, color = "green"))
Trouble with date column in R (time series)
Hi I am working with a table in r. The first column consists of the date(monthly) and the following columns contain different return data on several portfolios. I downloaded the package PerformanceAnalytics and therefore I need this data to be read a time series. This is what I tried to do. It has worked with a sheet before. But now I always get this error. But I only changed the return data nothing else. I dont understand why it wont read the date correctly. > library(PerformanceAnalytics) Loading required package: xts Loading required package: zoo Attaching package: ‘zoo’ The following objects are masked from ‘package:base’: as.Date, as.Date.numeric Package PerformanceAnalytics (1.4.3541) loaded. Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson, GPL-2 | GPL-3 http://r-forge.r-project.org/projects/returnanalytics/ > #load file into R > FactorR <- read.table("~/Desktop/Rfiles/FactorRegression.csv",header=TRUE,sep=";") > > #Time Series (first column date) > FactorR_xts <- xts(x = FactorR[, -1],order.by = as.Date(FactorR$Date)) Error in charToDate(x) : character string is not in a standard unambiguous format I attached the dput function so you can see what kind of data I am talking about.(I did not include all data because it would be too much) put(FactorR) structure(list(Date = structure(c(203L, 55L, 5L, 142L, 70L, 35L, 85L, 167L, 178L, 102L, 105L, 116L, 204L, 26L, 2L, 143L, 71L, 9L, 145L, 36L, 157L, 169L, 19L, 181L, 107L, 192L, 122L, 7L, 30L, 60L, 146L, 17L, 158L, 90L, 92L, 182L, 49L, 193L, 123L, 8L, 133L, 61L, 72L, 76L, 159L, 41L, 93L, 183L, 22L, 194L, 53L, 3L, 134L, 62L, 147L, 77L, 87L, 170L, 94L, 46L, 108L, 195L, 124L, 9L, 135L, 32L, 148L, 78L, 39L, 171L, 95L, 184L, 109L, 118L, 125L, 10L, 136L, 16L, 149L, 79L, 160L, 172L, 45L, 185L, 110L, 52L, 126L, 11L, 57L, 63L, 150L, 37L, 161L, 173L, 20L, 186L, 111L, 196L, 127L, 28L, 137L, 64L, 73L, 80L, 162L, 42L, 96L, 187L, 23L, 197L, 54L, 4L, 138L, 65L, 34L, 81L, 163L, 174L, 97L, 104L, 112L, 198L, 25L, 1L, 139L, 66L, 151L, 82L, 88L, 175L, 98L, 47L, 113L, 199L, 128L, 12L, 140L, 33L, 152L, 83L, 40L, 176L, 99L, 188L, 114L, 119L, 129L, 29L, 58L, 67L, 153L, 38L, 164L, 177L, 21L, 189L, 115L, 200L, 130L, 13L, 31L, 68L, 154L, 18L, 165L, 91L, 100L, 190L, 50L, 201L, 131L, 14L, 141L, 69L, 74L, 84L, 166L, 43L, 101L, 191L, 24L, 202L), .Label = c("26.02.10", "26.02.99", "27.02.04", "27.02.09", "27.02.98", "28.02.01", "28.02.02", "28.02.03", "28.02.05", "28.02.06", "28.02.07", "28.02.11", "28.02.13", "28.02.14", "28.04.00", "28.04.06", "28.06.02", "28.06.13", "28.09.01", "28.09.07", "28.09.12", "28.11.03", "28.11.08", "28.11.14", "29.01.10", "29.01.99", "29.02.00", "29.02.08", "29.02.12", "29.03.02", "29.03.13", "29.04.05", "29.04.11", "29.05.09", "29.05.98", "29.06.01", "29.06.07", "29.06.12", "29.07.05", "29.07.11", "29.08.03", "29.08.08", "29.08.14", "29.09.00", "29.09.06", "29.10.04", "29.10.10", "29.10.99", "29.11.02", "29.11.13", "29.12.00", "29.12.06", "30.01.04", "30.01.09", "30.01.98", "30.03.01", "30.03.07", "30.03.12", "30.04.01", "30.04.02", "30.04.03", "30.04.04", "30.04.07", "30.04.08", "30.04.09", "30.04.10", "30.04.12", "30.04.13", "30.04.14", "30.04.98", "30.04.99", "30.05.03", "30.05.08", "30.05.14", "30.06.00", "30.06.03", "30.06.04", "30.06.05", "30.06.06", "30.06.08", "30.06.09", "30.06.10", "30.06.11", "30.06.14", "30.06.98", "30.06.99", "30.07.04", "30.07.10", "30.07.99", "30.08.02", "30.08.13", "30.09.02", "30.09.03", "30.09.04", "30.09.05", "30.09.08", "30.09.09", "30.09.10", "30.09.11", "30.09.13", "30.09.14", "30.09.98", "30.09.99", "30.10.09", "30.10.98", "30.11.00", "30.11.01", "30.11.04", "30.11.05", "30.11.06", "30.11.07", "30.11.09", "30.11.10", "30.11.11", "30.11.12", "30.11.98", "30.11.99", "30.12.05", "30.12.11", "31.01.00", "31.01.01", "31.01.02", "31.01.03", "31.01.05", "31.01.06", "31.01.07", "31.01.08", "31.01.11", "31.01.12", "31.01.13", "31.01.14", "31.03.00", "31.03.03", "31.03.04", "31.03.05", "31.03.06", "31.03.08", "31.03.09", "31.03.10", "31.03.11", "31.03.14", "31.03.98", "31.03.99", "31.05.00", "31.05.01", "31.05.02", "31.05.04", "31.05.05", "31.05.06", "31.05.07", "31.05.10", "31.05.11", "31.05.12", "31.05.13", "31.05.99", "31.07.00", "31.07.01", "31.07.02", "31.07.03", "31.07.06", "31.07.07", "31.07.08", "31.07.09", "31.07.12", "31.07.13", "31.07.14", "31.07.98", "31.08.00", "31.08.01", "31.08.04", "31.08.05", "31.08.06", "31.08.07", "31.08.09", "31.08.10", "31.08.11", "31.08.12", "31.08.98", "31.08.99", "31.10.00", "31.10.01", "31.10.02", "31.10.03", "31.10.05", "31.10.06", "31.10.07", "31.10.08", "31.10.11", "31.10.12", "31.10.13", "31.10.14", "31.12.01", "31.12.02", "31.12.03", "31.12.04", "31.12.07", "31.12.08", "31.12.09", "31.12.10", "31.12.12", "31.12.13", "31.12.14", "31.12.97", "31.12.98", "31.12.99"), class = "factor"), T1V = c(2.647778077, 2.210168532, 5.184543047, 8.040141376, 1.375197787, 5.254693278, 0.238583717, -0.897572167, -6.812178155, -4.904778447, 1.445454477, 4.362544312, 0.577758687, -1.049345994, -0.862978469, 1.496311077, 1.535298083, 0.288034989, 1.002503645, -0.677737904, 1.148733333, -0.068879397, -0.933636437, 1.952957927, 0.864593373, 0.69587105, 1.566383785, 0.201725025, 0.108433102, 1.121251221, 0.697840536, -0.341798507, 1.750353464, -0.336236355, -0.173630687, 0.405227621, 0.407442779, 0.301534209, -0.252288427, -2.197112455, 0.4182172, 2.417270431, -1.777693712, 0.333608117, -0.963997684, -6.639419411, 0.258711011, 0.186660625, 1.075364953, -0.260546877, -0.144517713, 2.614703924, 1.592532166, 0.247679225, -2.45731793, -4.605964615, -0.051317674, -2.162348318, -2.094287999, 1.053871887, 0.775032852, -2.409925349, -1.24731202, 0.20137383, 2.9796142, 1.18379607, 0.530516718, 0.687770774, 2.425813597, 1.070508498, 1.594988715, 2.577337728, 1.735724627, 4.753962343, 1.817757107, 0.287317513, 2.122250222, 0.509726992, 1.623651005, -0.629218412, 1.413071621, 1.466153048, -0.032322501, 1.570878067, 2.495539535, 4.669928369, 2.540314459, 1.351671444, -0.511289999, . .. .... .... 1.637709345, 0.949670725, -0.380310863, -1.434786801, 0.546588731, -1.680930574, -1.497671033, 2.134405674, 0.189844698), T3R = c(0.440505512, 5.325647834, 8.837385281, 21.10071908, 4.5326005, 6.606732343, -4.488433652, -1.304513421, -27.57526532, -19.22941607, 13.12560656, 10.95535151, -2.960696646, -1.282931055, -4.047714673, 4.325802659, 13.34806221, -3.940632325, 2.668465326, -2.035239493, 2.265868534, 2.901646772, 1.555938816, 8.725598107, 11.1111256, 15.10307892, 10.71764649, -1.860936247, -3.235221339, -0.718662895, 2.928862379, 1.567574208, 0.098434872, -2.639317291, -4.334738565, -7.662240412, -1.392672778, -0.249440069, -7.519374824, -12.54244192, 3.211494367, -1.798924417, -9.750103402, -17.47336517, -13.59092267, -30.85835803, 6.627120118, 13.84521564, 1.224167247, -4.282226202, -3.879824851, 11.0002882, -1.633862571, 0.728697276, -15.20216478, -21.43439457, -9.173494124, -27.72510655, -1.643806123, 15.30080078, -11.42185815, -10.86780424, -10.08529262, 0.158622664, 19.07560852, 4.410459583, 6.983702045, 9.726738752, 11.96532368, 0.865241128, 10.52710826, 1.824183803, 0.051281172, 7.643560265, 3.857934445, -4.269747269, 0.193491252, -1.127403274, -1.145642636, -4.336023223, -4.750288798, 1.386568693, -3.058304715, 3.87811701, 6.007778471, 6.972611825, 7.139746344, 4.366307305, -4.231872029, 0.465995363, 3.370806119, 6.055047349, 1.589337466, 6.641594709, -5.834167246, 0.500189653, 3.001936466, 5.665564573, 6.219235151, 4.696735739, 3.597032279, -6.95415108, -2.658694701, 0.700309545, 3.870252718, 4.059903633, 4.129877722, 2.850231626, 6.026897131, 11.42913672, -1.40600749, 4.68987461, 6.138984252, 0.859683472, -0.783511946, -2.061859604, -7.537614888, -3.971992672, 2.743416779, -13.26388813, 1.902781239, -21.73358064, 5.433251961, -6.426065721, 5.500056238, 1.813441355, -11.11515726, -5.234823589, 2.582946217, -16.67855167, -36.66711169, -12.46637364, -5.211445441, -8.572591139, -17.88276043, 2.956958358, 25.59635755, 9.043196394, -1.052072638, 8.698101054, 11.55426061, 6.544403365, -4.495701412, -3.156245124, -1.293693294, 5.803543849, -0.762197087, 8.000348105, 2.646959488, -12.09434448, -2.563082034, 1.466128125, -1.863374559, 4.699135454, 3.622459782, -1.706221195, 4.038651722, 2.817603386, 1.027156327, -1.486388335, 0.168641413, -3.888501653, -9.915080583, -11.88374941, -13.56634471, -10.51374661, 3.846951996, -11.50943308, 2.074359943, 7.548294859, 6.711539857, 1.806850477, -0.576496993, -9.21065397, -4.154519223, 3.525193617, -0.24777096, 3.601168094, 0.143557195, -6.368196817, 5.231960646, 6.810400741, 3.672507394, -2.556477674, -2.869519924, 4.479135652, -5.380429829, 1.713023169, 3.396652152, 4.922622663, 4.040155598, 1.512006061, 0.24907751, 4.496251525, 0.92375895, -0.774870584, -3.784012139, 5.614058853, 5.327086162, -0.706470295, 0.771043886, -4.377376587, -2.491251246, 3.172560156, -2.082216546)), .Names = c("Date", "T1V", "T2V", "T3V", "T1MV", "T2MV", "T3MV", "T1BTM", "T2BTM", "T3BTM", "T1MOM", "T2MOM", "T3MOM", "Rm", "SMB", "HML", "MOM", "T1R", "T2R", "T3R"), class = "data.frame", row.names = c(NA, -205L)) I would be very happy if anyone could help me.
You need to specify your date format (see ?as.Date): dates <- c("26.02.10", "26.02.99", "27.02.04", "27.02.09", "27.02.98", "28.02.01", "28.02.02", "28.02.03") as.Date(dates, "%d.%m.%y")