I am using quantregForest to perform quantile regression. Since I already have a script which uses randomForest, at first I simply tried to replace all my calls to randomForest
with quantregForest
and I thought I would be able to achieve similar predictions and error metrics.
To my surprise, this was not the case, with randomForest performing much different (and much better) than quantregForest.
I then tried to test with a sample data what was the issue (if any), and realized that with the sample data the results are quite similar. Now, I wonder what is the reason why with my data I don't obtain similar results.
I am sharing both tests so that hopefully anyone can jump in and enlighten me on what's going on.
library(randomForest)
library(quantregForest)
## EVALUATION METRIC FUNCITON
explained_var <- function(obs, pred) {
sse <- sum((obs - pred) ^ 2)
mse <- sse / length(obs)
var.spde0 <- var(obs)
RSquared <- 1 - (mse / var.spde0)
return(RSquared)
}
## TEST WITH SAMPLE DATA
data(airquality)
data <- na.omit(airquality)
target <- "Ozone"
p <- colnames(data[, colnames(data) != target])
x <- data[, colnames(data) %in% p]
y <- data[, colnames(data) == target]
set.seed(17)
rf <- randomForest(x, y, mtry=3)
pred.rf <- predict(rf, x)
set.seed(17)
qrf <- quantregForest(x, y, mtry=3)
pred.qrf <- predict(qrf, x)
plot(pred.rf, pred.qrf[, "quantile= 0.5"])
explained_var(data[, target], pred.rf)
explained_var(data[, target], pred.qrf[, "quantile= 0.5"])
## TEST WITH MY ACTUAL DATA
data <- structure(c(805, 359, 10680, 2262, 511, 771, 479, 875, 6964,
7743, 8566, 1362, 2171, 5550, 15679, 15264, 9295, 2283, 16061,
22886, 22612, 15929, 8916, 22344, 21294, 16979, 8435, 8882, 23461,
5923, 5583, 2283, 3083, 852, 556, 199, 1276, 1883, 1774, 5348,
1526, 4059, 5703, 6877, 9589, 2439, 3930, 8751, 13710, 13523,
0, 719, 681, 17062, 10137, 5351, 3103, 2262, 217, 232, 1922,
1114, 6964, 9757, 8566, 15679, 14597, 26125, 15264, 6732, 4354,
2283, 16061, 9637, 3828, 9675, 22344, 21294, 8435, 12870, 22997,
23461, 5923, 977, 3244, 1811, 199, 60, 1276, 1740, 2228, 5442,
5348, 2385, 5703, 10123, 7220, 9589, 1358, 3930, 71.4672983240059,
74.9198152134087, 81.9748541641424, 81.3789818300981, 81.9018162181432,
82.306150433418, 83.5710787598859, 83.6022058822685, 91.9745920058279,
92.177882576848, 93.7015991058775, 92.969738685972, 94.0845956761658,
94.3871918286028, 95.539367006126, 102.648983496307, 102.878767023783,
100.822592044532, 101.211308969233, 102.888930163146, 101.41486338154,
100.194237577834, 100, 103.691742474797, 104.810200398337, 105.081459779634,
105.658533706422, 107.066864722412, 109.8, 115.524770338304,
116.339447326764, 117.153505611092, 113.584039598905, 113.742763470202,
111.301658519173, 110.651789542492, 114.979007670775, 114.518217436604,
115.858309684512, 87.136, 89.061, 92.995, 98.02, 104.217, 107.685,
113.896, 118.532, 121.116, 131.471, 133.483, -12.453857, 2.627739,
10.294222, 13.808369, 13.729789, 11.05466, 11.547364, 6.506183,
5.066044, 6.339626, 6.50363, 5.225325, 9.705699, 9.096802, 8.809451,
9.548807, 10.658337, 6.818086, 5.936759, 2.947278, 1.380448,
-0.58437, -2.580623, 0.780271, 3.347968, 4.877748, 6.115633,
7.896962, 5.890785, 9.36788, 10.723921, 10.87936, 5.435571, -1.623142,
-4.995025, -3.473387, -0.413473, 0.140873, 4.705319, 12.924873,
12.87974, 10.896275, 9.872646, 4.964755, -1.59944, 2.94889, 12.892184,
12.711813, 21.4542, 23.785, 5.758738, 3.692827, 2.651698, 1.979174,
4.320553, 2.420242, 6.846502, 5.708376, 10.47081, 5.960546, 2.023324,
3.199712, 4.17889, 1.182266, -3.025575, 0.137166, 2.067003, 5.326766,
9.991956, 6.617062, 7.498435, 7.252982, 7.368301, 2.391988, -0.185234,
0.902366, 5.888848, 5.485053, 4.30281, 5.803252, 4.249248, 1.847162,
8.300537, 1.005001, 8.532007, 7.1677, 1.34825, 1.969291, -2.365834,
0.846709, 1.710687, -1.875916, 2.350934, 7.492073, 6.813673,
13.926611, 14.430876, 14.453089, 13.464248, 15.333133, 9.218577,
6.710891, 10.954855, 15.447809, 15.221554, 12.722883, 14.715703,
14.860707, 5.02454, 7.445601, 8.674921, 8.12602, 8.141969, 7.930163,
8.38038, 8.955148, 9.089494, 7.62761, 7.158901, 5.862085, 6.107298,
6.374446, 6.475524, 5.475055, 3.766777, 3.888304, 4.225037, 4.303394,
5.376844, 6.936538, 7.366514, 7.506334, 7.616502, 7.085899, 5.983623,
6.180173, 5.33112, 5.443108, 5.420325, 5.092378, 6.543894, 6.077348,
5.730803, 5.513068, 4.872824, 5.15625, 5.22216, 5.426504, 6.337451,
6.235169, 1.327782, -1.244555, 1.951754, 5.040705, 4.712911,
4.877019, 5.198488, 5.654572, 3.096761, 3.699563, 3.972976, 2.33718,
-1.820208, -5.07591, -5.340972, -7.215595, -4.796942, -2.321996,
-2.469738, -2.042629, -1.179673, -1.707933, 3.470262, 4.361841,
0.652995, 1.994654, 1.510602, 0.9817, -2.818533, -0.367611, -1.760629,
-1.57131, 0.915267, 3.394129, 5.474692, 4.501784, -3.240785,
-4.266969, -8.685754, 5.407742, 3.811551, 7.119581, 9.064532,
5.152202, 3.05267, 5.521755, 7.827914, 9.898174, 25.257635, 26.585744,
0.639221, 0.903577, 3.091027, 3.187305, 2.86402, 3.543284, 3.908825,
3.386684, 4.945598, 4.158416, 2.837573, 2.81827, 2.810078, 2.674308,
3.13093, 1.14177, 1.323251, 0.372093, -0.551978, 0.275229, 1.121495,
0.93633, 2.539981, 1.555352, 1.94085, 2.133581, 1.459854, 1.270417,
-0.540541, 0, -0.803571, -0.359712, 0.902527, 1.645338, 0.99278,
0.630631, 1.893598, 2.885482, 3.849597, 2.956522, 3.292894, 4.137931,
3.589744, 2.951096, 4.391892, 4.221854, 4.070026, 4.676677, 9.82801,
11.79821, 1.472841, 6.718912, 11.836783, 15.898909, 25.709607,
9.05762, 7.764579, 8.201088, 4.746932, 4.132983, 4.776813, 2.98657,
1.050668, 1.542468, -0.099334, -4.192771, -0.165434, -3.087394,
-1.382122, -1.378621, 1.157874, 3.304686, 2.585513, 5.763776,
3.384095, 5.358027, 4.705771, -1.118568, -2.05919, -2.231126,
-5.810989, -2.869076, 3.98776, 0.905365, 0.03739, -0.485754,
1.262192, -0.415565, 2.506336, 3.607033, 4.761905, 4.591454,
2.146872, 5.208613, 7.03289, 8.779789, 8.915835, 11.401768, 8.440897,
7.453209, 22.346787, 18.092592, 16.32671, 20.486843, 21.660653,
19.031784, 16.662976, 8.933911, 7.857115, 9.345785, 9.019837,
8.979081, 13.862944, 12.980833, 15.344746, 13.405133, 11.709088,
11.803316, 11.517679, 8.401285, 8.016625, 9.150574, 9.810513,
14.641483, 12.511891, 12.080331, 17.595185, 19.316216, 28.348572,
43.574038, 41.09749, 40.289323, 39.562645, 11.849703, 10.136963,
9.641545, 12.5001, 15.424718, 9.027388, -0.654163, 0.13022, -4.085596,
3.576235, 1.088659, 1.760079, 6.594, 11.580856, 8.117877, 19.722498,
25.968992, 18.568383, 17.844052, 27.086202, 30.980984, 32.210481,
33.505397, 31.425392, 25.382328, 13.484474, 13.879603, 13.386605,
11.992726, 13.124146, 12.186457, 11.703648, 10.999458, 10.700577,
2.962496, 1.521551, -1.498747, 9.918065, 10.581814, 10.276264,
22.561524, 9.998095, 10.288427, 11.45816, 11.861566, 12.934786,
13.165752, 13.239061, 13.584854, 13.958301, 14.316469, 14.044076,
13.480028, 11.601599, 11.464572, 11.417678, 13.331946, 13.456125,
12.722104, 12.614558, 13.971946, 16.922664, 17.276725, 15.955127,
15.240389, 14.640779, 12.493157, 11.111111, 8.527132, 19.047619,
25.650558, 19.728615, 26.736598, 25.533479, 25.824272, 48.93706,
51.900154, 59.040554, 55.415671, 51.93109, 50.499072, 45.954002,
15.903802, 10.816898, 8.728333, 7.560141, 12.584119, 9.958636,
9.552752, 10.014957, 6.046335, 7.452608, 7.006717, 5.031857,
7.197291, 7.001057, 17.463447, 16.447507, 14.334213, 10.658486,
6.288348, 12.647899, 15.078958, 56.06989, 62.779872, 71.89311,
11.952007, 14.867608, 24.863388, 56.864564, 67.07264, 61.500412,
59.190372, 61.332441, 46.86576, 40.770633, 43.511257, 9.704761,
9.327586, 9.046318, 7.41218, 6.905203, 5.91303, 5.427525, 4.476935,
5.957071, 5.421398, 4.367089, 3.848273, 2.826883, 1.82659, 1.334151,
-0.485143, -0.697181, 1.089918, 1.513317, 2.813067, 0.298219,
-0.266586, 1.822791, 3.776308, 3.580392, 3.32178, -1.219164,
-4.35945, -6.544755, 0.595238, -1.315789, -2.774923, -3.703704,
-2.520161, 1.902748, 2.648305, 1.968912, 0.724638, -0.722394,
-0.206398, -2.070393, -1.446281, -1.22825, 0.6, 4.136505, 10.272537,
5.358948, 8.290155, 7.157058, 8.613861, -1.315789, 1.642336,
5.40054, 0.257511, -1.384083, 0.620567, 3.321364, 1.623932, 4.362703,
3.483432, 2.818104, 3.225806, 2.104377, 1.574151, 1.403799, -0.913621,
-2.302632, -2.446982, -1.302932, 1.575456, 1.744186, 2.093802,
2.514669, 0.979592, 1.061224, 3.69155, 4.66067, 3.1477, 2.586904,
-1.111994, -0.630418, -0.703125, -0.390016, 2.981467, 2.596381,
1.513944, -3.098373, -2.039216, -1.177394, 0.114844, -1.659949,
1.517047, -0.241276, -2.059631, 1.270772, -1.072125, 0.968054,
2.862784, 9.4, 9.417476, -1.5, -2.07, -4.32, -0.56, 2.8, 4.93,
0.11, 11.37, 5.31, 1.37, 1.55, 6.14, 8.85, 5.57, 6.71, -5.44,
-7.65, -7.64, -8.96, -12.35, -9.47, -10.62, -7.55, 2.97, 13.36,
18.32, 42.23, 66.7, 65.12, 59.99, 48.3, 40.95, 6.82, -13.08,
-18.89, -19.66, -14.76, -15.65, -19.58, 24.81, 38.11, 62.12,
47.54, 62.03, 49.99, 27.61, 21.02, 19.73, 13.37, 11.18, 12.725207,
13.881474, 20.327775, 18.342986, 16.890357, 16.081053, 17.393343,
9.688959, 7.62322, 6.904355, 5.117021, 6.224441, 8.93588, 7.47678,
9.521595, 9.528388, 6.779661, 6.471746, 4.405868, 2.530138, 1.19128,
-0.476483, -1.048741, 1.440654, 2.4689, 2.450702, 1.24283, 2.448869,
2.428074, 3.908119, 4.552697, 3.053195, 3.236969, 0.986028, 3.228903,
3.517544, 1.584636, 4.519478, 7.879705, 12.09214, 14.522115,
12.887968, 15.130427, 11.580484, 14.827254, 9.963327, 8.320985,
10.467734, 15.304798, 17.038065, 3.615675, 0.290316, 9.612278,
4.08874, 4.302048, 8.331068, 11.64105, 5.132272, 54.494295, 49.02577,
58.868737, 74.156898, 155.556071, 158.016304, 148.819789, 60.06319,
47.43299, -5.001556, -7.156328, 10.173523, 24.944215, 24.280533,
34.303568, 57.470193, 57.138073, 53.129133, 64.585052, 64.190582,
46.592821, 32.802954, 25.899403, 16.538018, 4.863528, -0.018432,
0.589235, -0.431025, 6.116208, 7.900795, 13.791603, 97.407267,
161.360947, 174.523877, 191.539712, 138.844643, 110.235956, 63.062881,
54.899454, 52.048643, 39.880294, 38.597559, 5.77286, 4.269472,
4.950716, 3.58448, 4.000951, 4.327782, 4.54294, 5.587523, 8.197352,
6.208559, 7.306885, 7.558833, 7.463545, 5.68505, 6.768042, 3.510181,
5.109944, 6.006986, 5.572127, 3.323999, 6.571791, 6.893587, 6.220531,
3.201263, 1.05779, 1.222288, 3.603639, 3.758056, 3.641122, 5.66528,
4.042778, 3.977761, 6.908439, 7.93183, 8.485569, 8.087771, 7.131337,
8.165735, 6.487861, 4.727564, 4.305443, 3.849238, 4.340963, 4.150653,
5.967865, 7.413127, 7.003012, 6.88351, 8.708487, 8.641975, 562,
52, 131, 652, 557, 298, 359, 17062, 1630, 2421, 1816, 2015, 217,
1177, 1922, 8566, 1362, 5550, 15679, 22778, 25031, 26125, 15264,
22886, 22612, 15929, 9637, 3828, 22344, 27390, 13554, 8435, 12870,
6266, 2283, 4150, 1811, 1822, 199, 1411, 583, 1649, 1740, 4262,
5348, 4059, 1479, 5703, 6877, 7220, 3, 1, 2, 3, 4, 4, 1, 1, 3,
3, 4, 4, 1, 1, 2, 4, 4, 1, 2, 2, 3, 3, 4, 2, 3, 3, 4, 1, 2, 4,
4, 4, 2, 3, 4, 1, 3, 3, 1, 4, 4, 1, 2, 3, 4, 1, 2, 2, 3, 4), class = c("xts",
"zoo"), index = structure(c(1280620800, 1293840000, 1304208000,
1314835200, 1317427200, 1322697600, 1325376000, 1330560000, 1372636800,
1375315200, 1380585600, 1383264000, 1388534400, 1393632000, 1396310400,
1412121600, 1414800000, 1425168000, 1427846400, 1433116800, 1438387200,
1441065600, 1443657600, 1464739200, 1470009600, 1472688000, 1480550400,
1485907200, 1496275200, 1506816000, 1509494400, 1512086400, 1522540800,
1535760000, 1543622400, 1546300800, 1561939200, 1564617600, 1577836800,
1604188800, 1606780800, 1612137600, 1619827200, 1630454400, 1635724800,
1643673600, 1648771200, 1651363200, 1661990400, 1664582400), tzone = "UTC", tclass = "yearmon"), dim = c(50L,
20L), dimnames = list(NULL, c("CMr", "X6M_CMr", "X6M_AFG_value.infl_fcpi_m",
"X6M_AFG_fpi.faostat", "X6M_DZA_fpi.faostat", "X6M_BGD_fpi.faostat",
"X6M_BFA_fpi.faostat", "X6M_CMR_fpi.faostat", "X6M_CIV_fpi.faostat",
"X6M_EGY_fpi.faostat", "X6M_GIN_fpi.faostat", "X6M_IRN_fpi.faostat",
"X6M_IRQ_fpi.faostat", "X6M_MAR_fpi.faostat", "X6M_NGA_inflation.food_price",
"X6M_PAK_fpi.faostat", "X6M_SYR_fpi.faostat", "X6M_TUN_fpi.faostat",
"arrivals.1yr", "quarter")))
target <- "CMr"
p <- colnames(data[, colnames(data) != target])
x <- data[, colnames(data) %in% p]
y <- data[, colnames(data) == target]
set.seed(17)
rf <- randomForest(x, y, mtry=3)
pred.rf <- predict(rf, x)
set.seed(17)
qrf <- quantregForest(x, y, mtry=3)
pred.qrf <- predict(qrf, x)
plot(pred.rf, pred.qrf[, "quantile= 0.5"])
explained_var(data[, target], pred.rf)
explained_var(data[, target], pred.qrf[, "quantile= 0.5"])
I may have found the problem and I am posting this as an answer rather than an edit since unfortunately my code is so long it cannot fit as an addition to my question.
The second data I am using is of class "xts" "zoo"
. If I have like a classical matrix (by removing the time index part), things work as I would expect. Sincerely, I don't know why this is happening but I am glad I solved my issue for now.
Example with the new data as a matrix.
library(randomForest)
library(quantregForest)
## EVALUATION METRIC FUNCITON
explained_var <- function(obs, pred) {
sse <- sum((obs - pred) ^ 2)
mse <- sse / length(obs)
var.spde0 <- var(obs)
RSquared <- 1 - (mse / var.spde0)
return(RSquared)
}
## TEST WITH MY ACTUAL DATA
data <- structure(c(805, 359, 10680, 2262, 511, 771, 479, 875, 6964,
7743, 8566, 1362, 2171, 5550, 15679, 15264, 9295, 2283, 16061,
22886, 22612, 15929, 8916, 22344, 21294, 16979, 8435, 8882, 23461,
5923, 5583, 2283, 3083, 852, 556, 199, 1276, 1883, 1774, 5348,
1526, 4059, 5703, 6877, 9589, 2439, 3930, 8751, 13710, 13523,
0, 719, 681, 17062, 10137, 5351, 3103, 2262, 217, 232, 1922,
1114, 6964, 9757, 8566, 15679, 14597, 26125, 15264, 6732, 4354,
2283, 16061, 9637, 3828, 9675, 22344, 21294, 8435, 12870, 22997,
23461, 5923, 977, 3244, 1811, 199, 60, 1276, 1740, 2228, 5442,
5348, 2385, 5703, 10123, 7220, 9589, 1358, 3930, 71.4672983240059,
74.9198152134087, 81.9748541641424, 81.3789818300981, 81.9018162181432,
82.306150433418, 83.5710787598859, 83.6022058822685, 91.9745920058279,
92.177882576848, 93.7015991058775, 92.969738685972, 94.0845956761658,
94.3871918286028, 95.539367006126, 102.648983496307, 102.878767023783,
100.822592044532, 101.211308969233, 102.888930163146, 101.41486338154,
100.194237577834, 100, 103.691742474797, 104.810200398337, 105.081459779634,
105.658533706422, 107.066864722412, 109.8, 115.524770338304,
116.339447326764, 117.153505611092, 113.584039598905, 113.742763470202,
111.301658519173, 110.651789542492, 114.979007670775, 114.518217436604,
115.858309684512, 87.136, 89.061, 92.995, 98.02, 104.217, 107.685,
113.896, 118.532, 121.116, 131.471, 133.483, -12.453857, 2.627739,
10.294222, 13.808369, 13.729789, 11.05466, 11.547364, 6.506183,
5.066044, 6.339626, 6.50363, 5.225325, 9.705699, 9.096802, 8.809451,
9.548807, 10.658337, 6.818086, 5.936759, 2.947278, 1.380448,
-0.58437, -2.580623, 0.780271, 3.347968, 4.877748, 6.115633,
7.896962, 5.890785, 9.36788, 10.723921, 10.87936, 5.435571, -1.623142,
-4.995025, -3.473387, -0.413473, 0.140873, 4.705319, 12.924873,
12.87974, 10.896275, 9.872646, 4.964755, -1.59944, 2.94889, 12.892184,
12.711813, 21.4542, 23.785, 5.758738, 3.692827, 2.651698, 1.979174,
4.320553, 2.420242, 6.846502, 5.708376, 10.47081, 5.960546, 2.023324,
3.199712, 4.17889, 1.182266, -3.025575, 0.137166, 2.067003, 5.326766,
9.991956, 6.617062, 7.498435, 7.252982, 7.368301, 2.391988, -0.185234,
0.902366, 5.888848, 5.485053, 4.30281, 5.803252, 4.249248, 1.847162,
8.300537, 1.005001, 8.532007, 7.1677, 1.34825, 1.969291, -2.365834,
0.846709, 1.710687, -1.875916, 2.350934, 7.492073, 6.813673,
13.926611, 14.430876, 14.453089, 13.464248, 15.333133, 9.218577,
6.710891, 10.954855, 15.447809, 15.221554, 12.722883, 14.715703,
14.860707, 5.02454, 7.445601, 8.674921, 8.12602, 8.141969, 7.930163,
8.38038, 8.955148, 9.089494, 7.62761, 7.158901, 5.862085, 6.107298,
6.374446, 6.475524, 5.475055, 3.766777, 3.888304, 4.225037, 4.303394,
5.376844, 6.936538, 7.366514, 7.506334, 7.616502, 7.085899, 5.983623,
6.180173, 5.33112, 5.443108, 5.420325, 5.092378, 6.543894, 6.077348,
5.730803, 5.513068, 4.872824, 5.15625, 5.22216, 5.426504, 6.337451,
6.235169, 1.327782, -1.244555, 1.951754, 5.040705, 4.712911,
4.877019, 5.198488, 5.654572, 3.096761, 3.699563, 3.972976, 2.33718,
-1.820208, -5.07591, -5.340972, -7.215595, -4.796942, -2.321996,
-2.469738, -2.042629, -1.179673, -1.707933, 3.470262, 4.361841,
0.652995, 1.994654, 1.510602, 0.9817, -2.818533, -0.367611, -1.760629,
-1.57131, 0.915267, 3.394129, 5.474692, 4.501784, -3.240785,
-4.266969, -8.685754, 5.407742, 3.811551, 7.119581, 9.064532,
5.152202, 3.05267, 5.521755, 7.827914, 9.898174, 25.257635, 26.585744,
0.639221, 0.903577, 3.091027, 3.187305, 2.86402, 3.543284, 3.908825,
3.386684, 4.945598, 4.158416, 2.837573, 2.81827, 2.810078, 2.674308,
3.13093, 1.14177, 1.323251, 0.372093, -0.551978, 0.275229, 1.121495,
0.93633, 2.539981, 1.555352, 1.94085, 2.133581, 1.459854, 1.270417,
-0.540541, 0, -0.803571, -0.359712, 0.902527, 1.645338, 0.99278,
0.630631, 1.893598, 2.885482, 3.849597, 2.956522, 3.292894, 4.137931,
3.589744, 2.951096, 4.391892, 4.221854, 4.070026, 4.676677, 9.82801,
11.79821, 1.472841, 6.718912, 11.836783, 15.898909, 25.709607,
9.05762, 7.764579, 8.201088, 4.746932, 4.132983, 4.776813, 2.98657,
1.050668, 1.542468, -0.099334, -4.192771, -0.165434, -3.087394,
-1.382122, -1.378621, 1.157874, 3.304686, 2.585513, 5.763776,
3.384095, 5.358027, 4.705771, -1.118568, -2.05919, -2.231126,
-5.810989, -2.869076, 3.98776, 0.905365, 0.03739, -0.485754,
1.262192, -0.415565, 2.506336, 3.607033, 4.761905, 4.591454,
2.146872, 5.208613, 7.03289, 8.779789, 8.915835, 11.401768, 8.440897,
7.453209, 22.346787, 18.092592, 16.32671, 20.486843, 21.660653,
19.031784, 16.662976, 8.933911, 7.857115, 9.345785, 9.019837,
8.979081, 13.862944, 12.980833, 15.344746, 13.405133, 11.709088,
11.803316, 11.517679, 8.401285, 8.016625, 9.150574, 9.810513,
14.641483, 12.511891, 12.080331, 17.595185, 19.316216, 28.348572,
43.574038, 41.09749, 40.289323, 39.562645, 11.849703, 10.136963,
9.641545, 12.5001, 15.424718, 9.027388, -0.654163, 0.13022, -4.085596,
3.576235, 1.088659, 1.760079, 6.594, 11.580856, 8.117877, 19.722498,
25.968992, 18.568383, 17.844052, 27.086202, 30.980984, 32.210481,
33.505397, 31.425392, 25.382328, 13.484474, 13.879603, 13.386605,
11.992726, 13.124146, 12.186457, 11.703648, 10.999458, 10.700577,
2.962496, 1.521551, -1.498747, 9.918065, 10.581814, 10.276264,
22.561524, 9.998095, 10.288427, 11.45816, 11.861566, 12.934786,
13.165752, 13.239061, 13.584854, 13.958301, 14.316469, 14.044076,
13.480028, 11.601599, 11.464572, 11.417678, 13.331946, 13.456125,
12.722104, 12.614558, 13.971946, 16.922664, 17.276725, 15.955127,
15.240389, 14.640779, 12.493157, 11.111111, 8.527132, 19.047619,
25.650558, 19.728615, 26.736598, 25.533479, 25.824272, 48.93706,
51.900154, 59.040554, 55.415671, 51.93109, 50.499072, 45.954002,
15.903802, 10.816898, 8.728333, 7.560141, 12.584119, 9.958636,
9.552752, 10.014957, 6.046335, 7.452608, 7.006717, 5.031857,
7.197291, 7.001057, 17.463447, 16.447507, 14.334213, 10.658486,
6.288348, 12.647899, 15.078958, 56.06989, 62.779872, 71.89311,
11.952007, 14.867608, 24.863388, 56.864564, 67.07264, 61.500412,
59.190372, 61.332441, 46.86576, 40.770633, 43.511257, 9.704761,
9.327586, 9.046318, 7.41218, 6.905203, 5.91303, 5.427525, 4.476935,
5.957071, 5.421398, 4.367089, 3.848273, 2.826883, 1.82659, 1.334151,
-0.485143, -0.697181, 1.089918, 1.513317, 2.813067, 0.298219,
-0.266586, 1.822791, 3.776308, 3.580392, 3.32178, -1.219164,
-4.35945, -6.544755, 0.595238, -1.315789, -2.774923, -3.703704,
-2.520161, 1.902748, 2.648305, 1.968912, 0.724638, -0.722394,
-0.206398, -2.070393, -1.446281, -1.22825, 0.6, 4.136505, 10.272537,
5.358948, 8.290155, 7.157058, 8.613861, -1.315789, 1.642336,
5.40054, 0.257511, -1.384083, 0.620567, 3.321364, 1.623932, 4.362703,
3.483432, 2.818104, 3.225806, 2.104377, 1.574151, 1.403799, -0.913621,
-2.302632, -2.446982, -1.302932, 1.575456, 1.744186, 2.093802,
2.514669, 0.979592, 1.061224, 3.69155, 4.66067, 3.1477, 2.586904,
-1.111994, -0.630418, -0.703125, -0.390016, 2.981467, 2.596381,
1.513944, -3.098373, -2.039216, -1.177394, 0.114844, -1.659949,
1.517047, -0.241276, -2.059631, 1.270772, -1.072125, 0.968054,
2.862784, 9.4, 9.417476, -1.5, -2.07, -4.32, -0.56, 2.8, 4.93,
0.11, 11.37, 5.31, 1.37, 1.55, 6.14, 8.85, 5.57, 6.71, -5.44,
-7.65, -7.64, -8.96, -12.35, -9.47, -10.62, -7.55, 2.97, 13.36,
18.32, 42.23, 66.7, 65.12, 59.99, 48.3, 40.95, 6.82, -13.08,
-18.89, -19.66, -14.76, -15.65, -19.58, 24.81, 38.11, 62.12,
47.54, 62.03, 49.99, 27.61, 21.02, 19.73, 13.37, 11.18, 12.725207,
13.881474, 20.327775, 18.342986, 16.890357, 16.081053, 17.393343,
9.688959, 7.62322, 6.904355, 5.117021, 6.224441, 8.93588, 7.47678,
9.521595, 9.528388, 6.779661, 6.471746, 4.405868, 2.530138, 1.19128,
-0.476483, -1.048741, 1.440654, 2.4689, 2.450702, 1.24283, 2.448869,
2.428074, 3.908119, 4.552697, 3.053195, 3.236969, 0.986028, 3.228903,
3.517544, 1.584636, 4.519478, 7.879705, 12.09214, 14.522115,
12.887968, 15.130427, 11.580484, 14.827254, 9.963327, 8.320985,
10.467734, 15.304798, 17.038065, 3.615675, 0.290316, 9.612278,
4.08874, 4.302048, 8.331068, 11.64105, 5.132272, 54.494295, 49.02577,
58.868737, 74.156898, 155.556071, 158.016304, 148.819789, 60.06319,
47.43299, -5.001556, -7.156328, 10.173523, 24.944215, 24.280533,
34.303568, 57.470193, 57.138073, 53.129133, 64.585052, 64.190582,
46.592821, 32.802954, 25.899403, 16.538018, 4.863528, -0.018432,
0.589235, -0.431025, 6.116208, 7.900795, 13.791603, 97.407267,
161.360947, 174.523877, 191.539712, 138.844643, 110.235956, 63.062881,
54.899454, 52.048643, 39.880294, 38.597559, 5.77286, 4.269472,
4.950716, 3.58448, 4.000951, 4.327782, 4.54294, 5.587523, 8.197352,
6.208559, 7.306885, 7.558833, 7.463545, 5.68505, 6.768042, 3.510181,
5.109944, 6.006986, 5.572127, 3.323999, 6.571791, 6.893587, 6.220531,
3.201263, 1.05779, 1.222288, 3.603639, 3.758056, 3.641122, 5.66528,
4.042778, 3.977761, 6.908439, 7.93183, 8.485569, 8.087771, 7.131337,
8.165735, 6.487861, 4.727564, 4.305443, 3.849238, 4.340963, 4.150653,
5.967865, 7.413127, 7.003012, 6.88351, 8.708487, 8.641975, 562,
52, 131, 652, 557, 298, 359, 17062, 1630, 2421, 1816, 2015, 217,
1177, 1922, 8566, 1362, 5550, 15679, 22778, 25031, 26125, 15264,
22886, 22612, 15929, 9637, 3828, 22344, 27390, 13554, 8435, 12870,
6266, 2283, 4150, 1811, 1822, 199, 1411, 583, 1649, 1740, 4262,
5348, 4059, 1479, 5703, 6877, 7220, 3, 1, 2, 3, 4, 4, 1, 1, 3,
3, 4, 4, 1, 1, 2, 4, 4, 1, 2, 2, 3, 3, 4, 2, 3, 3, 4, 1, 2, 4,
4, 4, 2, 3, 4, 1, 3, 3, 1, 4, 4, 1, 2, 3, 4, 1, 2, 2, 3, 4), dim = c(50L, 20L), dimnames = list(NULL, c("CMr", "X6M_CMr", "X6M_AFG_value.infl_fcpi_m",
"X6M_AFG_fpi.faostat", "X6M_DZA_fpi.faostat", "X6M_BGD_fpi.faostat",
"X6M_BFA_fpi.faostat", "X6M_CMR_fpi.faostat", "X6M_CIV_fpi.faostat",
"X6M_EGY_fpi.faostat", "X6M_GIN_fpi.faostat", "X6M_IRN_fpi.faostat",
"X6M_IRQ_fpi.faostat", "X6M_MAR_fpi.faostat", "X6M_NGA_inflation.food_price",
"X6M_PAK_fpi.faostat", "X6M_SYR_fpi.faostat", "X6M_TUN_fpi.faostat",
"arrivals.1yr", "quarter")))
target <- "CMr"
p <- colnames(data[, colnames(data) != target])
x <- data[, colnames(data) %in% p]
y <- data[, colnames(data) == target]
set.seed(17)
rf <- randomForest(x, y, mtry=3)
pred.rf <- predict(rf, x)
set.seed(17)
qrf <- quantregForest(x, y, mtry=3)
pred.qrf <- predict(qrf, x)
plot(pred.rf, pred.qrf[, "quantile= 0.5"])
explained_var(data[, target], pred.rf)
explained_var(data[, target], pred.qrf[, "quantile= 0.5"])