CDF is not starting from zero - r

I have a data without any infinite value like below:
data<-c(6.87, 0.11, 0, 0.03, 0.08, 0, 0.01, 0, 0, 0.13, 0.17, 0, 0.53,
0.01, 2.69, 0, 0, 0, 0, 0, 0, 5.44, 4.71, 3.57, 0.38, 0.31, 0.45,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.09, 0.43, 0, 0.23, 2.31, 0,
0.01, 0.96, 1.28, 0.07, 0, 0, 0, 0, 0.07, 0, 0, 0.03, 5.99, 0.08,
6.23, 2.95, 0.04, 0, 0.98, 0.03, 17.2, 0, 0, 0.25, 1.99, 0, 0,
0, 0, 0, 0, 0, 0, 0.01, 0, 0, 0, 1.94, 1.06, 0.05, 0, 0, 0, 0,
0, 0.02, 0, 0.02, 0.05, 0.66, 0.57, 0.54, 0, 0.11, 0.75, 0.65,
0, 7.35, 0.22, 0, 1.97, 0, 0, 0, 0, 0, 0, 0, 3.78, 0.28, 0.06,
0.09, 0.03, 0.01, 0.14, 0.02, 0, 1.82, 0.04, 0, 0.01, 0, 0, 0,
9.52, 10.38, 29.09, 0.1, 0.4, 0, 0.97, 0.57, 0.33, 0.16, 40.19,
1.06, 5.02, 0.01, 0.79, 4.78, 0.44, 15.29, 7.26, 0.05, 0.01,
6.47, 37.66, 0, 0.4, 0.15, 1.42, 0, 0.07, 0.01, 0, 0, 0.01, 7.43,
0, 0.03, 0.02, 0.15, 0.09, 0.44, 1.45, 0.03, 0, 0.01, 0.01, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09, 0, 0.01, 0, 0, 0, 0, 0, 0, 0.04,
1.52, 0.01, 0, 0.27, 0.01, 1.11, 2.41, 0, 0, 0.15, 0, 0, 1.28,
0.02, 0.16, 0.17, 1.74, 3.75, 0.05, 0.18, 0.07, 0.39, 0, 0, 0,
0.45, 0.78, 0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0.05, 0, 0, 0, 0, 0.09,
0.17, 0, 0.27, 0.04, 0.02, 0.88, 1.23, 0.01, 0.01, 0.13, 0.6,
0.02, 0.4, 0, 0, 0.09, 0, 0, 0, 0, 0, 0, 0, 0.12, 0, 0.02, 0.02,
0, 0.01, 0, 0, 0, 0, 0.05, 0, 0, 0, 2.71, 0.49, 0, 0, 0, 0.84,
0.04, 0, 0, 0.02, 0.06, 0, 0, 0.02, 0.15, 0, 0.48, 5.34, 5.23,
0.22, 2.37, 1.55, 3.29, 0, 0, 0.08, 0.03, 0.05, 0.06, 7.62, 0.84,
0.48, 0.04, 0, 0, 0, 0, 0.6, 0, 0.02, 0.08, 0, 0, 0, 0, 0, 0,
0, 0, 0.11, 0.1, 0, 0.01, 0, 0.24, 0, 2.4, 1.57, 0.12, 0.03,
0, 0, 0, 0, 0.08, 0.56, 0, 0.09, 0.01, 0.09, 0.01, 0, 0, 0, 0,
0, 0, 0, 0.01, 0, 0, 0, 0, 0, 0.03, 0, 0, 0, 0.38, 0.01, 0.03,
0.14, 0.03, 0, 0, 0, 0, 0, 0, 0, 0, 0.08, 0, 6.1, 0, 0, 0, 0.31,
0.01, 0.03, 0.2, 0.03, 0.02, 0.08, 0.12, 0, 5.79, 0.91, 0.95,
0, 0.39, 1.3, 0, 0, 0, 0, 0.01, 0, 0.64, 0.02, 0, 1.14, 0.24,
0, 0, 0.04, 0, 0, 0, 1.28, 0, 0.01, 0.02, 2.18, 0.05, 0, 0, 0,
0, 0, 0.01, 0.02, 0.19, 3.9, 0.01, 0.03, 0, 5.93, 0.88, 0, 0,
0, 0, 0.07, 0, 4.06, 0.41, 0.04, 0, 0, 0.08, 2.21, 0.03, 0, 8.07,
0, 0, 0.66, 1.73, 0.17, 0.02, 1.37, 4.68, 0.26, 0.09, 0.2, 0.03,
0, 0, 0, 0, 2.74, 0.1, 0.42, 0.65, 0.53, 0, 0, 0, 0, 0, 0.01,
0.16, 0.17, 0, 0, 0.01, 0, 0, 0, 0.6, 0, 0, 0.09, 0.01, 0, 0,
0.69, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.13, 0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
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.15, 1.41, 0, 0, 0, 0, 0)
When I try to plot CDF, curve is not starting from zero. It starts with 0.57. I read that infinite and zero values can cause this problem. To overcome this I replaced zeros with very small values.
data[data==0]<-0.000001
plot(ecdf(data), xlim = c(min(data),max(data)))
But still I am getting same result. Why is that happening?

I'm not sure it makes much sense to show empirical cdf for values outside of observed data. All values that are lower than the minimum of data will have ecdf of zero. You could try something like:
f = ecdf(data)
curve(f, min(data) - 0.01, max(data))

Like it is said in comments to the question 1 and 2, there are many zeros in your data, representing 57% of the total data points.
mean(data == 0)
#[1] 0.5794045
If you plot the ECDF and an horizontal line at that ordinate, you will see that the ECDF starts there.
plot(ecdf(data))
abline(h = mean(data == 0))

Related

Predictive models with different sized time series

I'm trying to estimate an ARIMA, VAR and Neural Network models in R, to compare the quality of their predictions regarding unemployment, using the data regarding several economic indicators and COVID-19 related information which i would like to use as exogenous information. I would also like these models to use the rolling window approach, if possible.
My economic information extends from 2007 to 2021 (monthly) while the COVID data only comes from 2020, which obviously poses problems regarding the size of the time series.
Since i am trying to use rolling windows, every time I try to include COVID information i get the following error.
Error in solve.default(Sigma) :
system is computationally singular: reciprocal condition number = 2.6396e-19
Which I assume is because the covid information is all 0's (since we are using data from 2007) and therefore the model assumes a perfect correlation for that information.
Is there any way I can combine these 2 time series (the economic one from 2007 and the covid one) in the models mentioned, while using the rolling window approach?
dput version
structure(list(date = c("02/2007", "03/2007", "04/2007", "05/2007",
"06/2007", "07/2007", "08/2007", "09/2007", "10/2007", "11/2007",
"12/2007", "01/2008", "02/2008", "03/2008", "04/2008", "05/2008",
"06/2008", "07/2008", "08/2008", "09/2008", "10/2008", "11/2008",
"12/2008", "01/2009", "02/2009", "03/2009", "04/2009", "05/2009",
"06/2009", "07/2009", "08/2009", "09/2009", "10/2009", "11/2009",
"12/2009", "01/2010", "02/2010", "03/2010", "04/2010", "05/2010",
"06/2010", "07/2010", "08/2010", "09/2010", "10/2010", "11/2010",
"12/2010", "01/2011", "02/2011", "03/2011", "04/2011", "05/2011",
"06/2011", "07/2011", "08/2011", "09/2011", "10/2011", "11/2011",
"12/2011", "01/2012", "02/2012", "03/2012", "04/2012", "05/2012",
"06/2012", "07/2012", "08/2012", "09/2012", "10/2012", "11/2012",
"12/2012", "01/2013", "02/2013", "03/2013", "04/2013", "05/2013",
"06/2013", "07/2013", "08/2013", "09/2013", "10/2013", "11/2013",
"12/2013", "01/2014", "02/2014", "03/2014", "04/2014", "05/2014",
"06/2014", "07/2014", "08/2014", "09/2014", "10/2014", "11/2014",
"12/2014", "01/2015", "02/2015", "03/2015", "04/2015", "05/2015",
"06/2015", "07/2015", "08/2015", "09/2015", "10/2015", "11/2015",
"12/2015", "01/2016", "02/2016", "03/2016", "04/2016", "05/2016",
"06/2016", "07/2016", "08/2016", "09/2016", "10/2016", "11/2016",
"12/2016", "01/2017", "02/2017", "03/2017", "04/2017", "05/2017",
"06/2017", "07/2017", "08/2017", "09/2017", "10/2017", "11/2017",
"12/2017", "01/2018", "02/2018", "03/2018", "04/2018", "05/2018",
"06/2018", "07/2018", "08/2018", "09/2018", "10/2018", "11/2018",
"12/2018", "01/2019", "02/2019", "03/2019", "04/2019", "05/2019",
"06/2019", "07/2019", "08/2019", "09/2019", "10/2019", "11/2019",
"12/2019", "01/2020", "02/2020", "03/2020", "04/2020", "05/2020",
"06/2020", "07/2020", "08/2020", "09/2020", "10/2020", "11/2020",
"12/2020", "01/2021", "02/2021", "03/2021", "04/2021", "05/2021",
"06/2021", "07/2021", "08/2021", "09/2021", "10/2021", "11/2021"
), unemp = c(2.25752508, -1.0989011, 2.1978022, -2.1978022, -1.0989011,
-0.05972289, -1.08695652, -6.43115942, -1.00945017, 0, -1.07388316,
-3.04982818, -1.96717862, 1.96717862, -1.87441425, 2.90534208,
3.06056701, -2.02963918, 3.07130584, 2.08333333, 0, 0, 5.43478261,
5.6763285, 3.49563046, 2.05992509, 3.33333333, 0.87912088, 0.62625546,
4.58158018, -0.55504163, 1.06382979, 0, -1.06382979, -2.85150252,
-4.68041237, 0.62745098, -3.38935574, 0.5026455, -0.28619529,
-1.29525342, -2.95239548, -1.03740503, -1.76613018, -2.5210084,
2.59222333, -1.7518872, 1.7518872, -2.6894523, 1.65289256, -3.43801653,
-0.8503937, -2.3744125, 0.7751938, 2.29576625, 3.90578414, 4.94647471,
1.3640873, 4.04773622, -0.92273622, 3.125, 0.45673077, 3.84615385,
-0.95126248, 1.72049325, 0.56958309, 0.71246819, -3.64508393,
0.92207924, -2.02585375, -1.25114155, 0.66666667, -3.8008658,
-2.882786, -4.77313832, 0, -2.51458002, -1.30135713, -7.81499203,
-1.75438596, -3.87759171, -2.16419816, -3.70910671, -2.12494688,
0.46748831, -0.63842848, 0.38701623, -1.12359551, 1.43499197,
-1.14285714, -0.07142857, 0.31385281, 1.64714631, 0.44583317,
2.8365708, 2.38646704, -1.2987013, -1.44395079, -1.39857093,
-1.51117827, -0.67114094, 1.15965849, 3.67636416, 2.73483341,
-1.37718397, -0.78108941, -1.36176773, -2.05167173, 1.97459054,
1.16173728, -0.21885522, 0.47348485, -0.86122047, -0.78740157,
-0.78740157, -1.36727095, -1.66545543, 0.55803571, -3.31428571,
1.3398374, -3.09467611, 0.50237969, 0.36506406, 0.38394668, 0.55958863,
-1.04021115, -1.05878635, -1.78571429, 0.327654, -2.97315664,
2.71485077, -0.97087379, -0.66990291, -0.23232323, -1.50994481,
0.53174173, -1.32138858, 1.61887142, 2.80193237, -1.61616162,
1.79704017, 3.85706183, -0.21077989, 1.03395062, 1.96428571,
1.12781955, 4.93871665, 1.27473364, 2.65873016, -1.42857143,
-2.73641851, 6.98011599, -1.47058824, 2.94117647, 1.49253731,
1.49253731, -2.98507463, -4.54545455, 0.06784261, -5.97014925,
22.3880597, 10.78697422, 3.42657343, -6.75086108, -4.25146992,
-9.26916221, -4.41176471, -0.02131287, 3.09617918, 0.21645022,
4.61309524, 7.29166667, -26, -9.18518519, -4.65221319, 3.17073171,
4.21052632, 3.28947368), regist_un = c(-7.6, -8.1, -10.4, -13,
-12.2, -10.8, -10.2, -11.3, -12, -13.2, -13.8, -12.7, -11.6,
-11.4, -8.2, -3.6, -1.6, -2, -0.5, -0.7, 0.5, 2.9, 6.6, 12.1,
17.7, 23.8, 27.3, 27.6, 28.1, 30.1, 28.7, 29.1, 29.1, 28.2, 26.1,
25.1, 19.6, 18.1, 16.1, 14.6, 12.7, 10.3, 9.6, 8.9, 6.4, 4.4,
3.3, -0.5, -1, -3.5, -5, -5.4, -6, -4.4, -3, -0.3, 3, 6.7, 11.7,
14.4, 16.6, 19.8, 21, 20.8, 24.5, 25, 26.3, 23.4, 22.5, 19.6,
17.4, 16.05866431, 14.1343296, 11.04394749, 11.07092871, 9.66638699,
6.80821419, 4.99845882, 3.21403698, 2.00992748, -0.01381295,
-0.82689753, -2.83078075, -4.69352568, -5.22666645, -6.07571945,
-8.30308904, -9.4986526, -10.86351863, -11.10348947, -10.1911332,
-11.56954866, -12.86335954, -13.57419377, -13.31634168, -12.71367749,
-13.78692468, -14.3833581, -14.16732657, -12.93820022, -12.73630773,
-12.91458502, -14.04113868, -12.63480706, -10.48461147, -7.99771938,
-7.25281958, -7.35380587, -4.68547808, -2.62950703, -1.82217091,
-3.44938365, -4.66108643, -6.57689723, -7.04795734, -8.83698741,
-9.49043411, -11.59763744, -13.07912754, -13.26308777, -15.34204052,
-18.01521541, -19.89096413, -19.19477791, -18.26531051, -16.35403878,
-16.14554408, -16.34837215, -17.53504461, -16.81810893, -16.32660251,
-16.00691286, -17.02626382, -16.57334233, -16.61939724, -18.99258341,
-20.51560419, -20.5844694, -19.149043, -17.49773014, -17.38241663,
-17.23274637, -16.0328503, -15.58626266, -15.29940386, -15.14205448,
-14.5670108, -12.85161091, -10.29016682, -10.0720839, -10.00068018,
-11.10920973, -10.23871997, -8.6402685, -8.42184435, -8.61357235,
-7.91941687, 2.99152725, 22.12769269, 34.00159255, 36.37735545,
37.00494467, 34.50234942, 36.14288275, 34.50948107, 30.17574135,
29.55791318, 32.38134753, 36.84886013, 25.91626159, 8.04566645,
-1.65087765, -7.08027492, -9.47650638, -9.99850976, -12.4400864,
-12.85751101, -13.15709526), electr_cons = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1.52573055, -13.77356375, -2.91314706, -8.07503923,
5.64741668, 2.14652942, 8.96942648, -12.90473041, 6.7462353,
-1.99320589, 3.5519951, 3.96085785, 2.90812218, -3.01852589,
-0.72990857, 6.62024931, -4.03278316, 15.15581887, -5.80240675,
6.15355971, -8.9434497, 7.09174981, -3.03231017, 5.99309871,
0.12505412, 7.98920263, 4.97957359, -13.41437565, 3.1251824,
-19.61649962, 8.42404224, 3.9274678, -0.69310101, -6.20882422,
2.92246113, 0.84784002, -5.54698038, -0.34784001, -2.23860664,
2.52721219, 1.51556136, 0.11030442, -6.11617537, -0.35384086,
3.73968268, 1.36305933, -4.2866283, -3.01881185, 8.07963944,
5.51292808, -4.35131302, 3.6602547, -6.44713838, 1.39859892,
1.30415104, -1.60930464, -0.40894902, -0.56603623, 3.40891108,
-1.80100326, -5.44803444, -6.44187786, 38.45132631, -4.07719031,
0.61965483, -1.49863173, 3.30109363, 1.19224911, -2.92700391,
16.92568044, -15.15157455, 3.48316945, -1.00483719, -40.57315028,
9.7659042, 4.78461676, 0.28264713, -0.87145969, -3.86558313,
-0.0679219, 2.45947602, -9.41849532, 8.07657479, 3.93419348,
1.94861355, 52.56477551, -54.55955271, -3.99890956, 4.60669488,
1.67468875, 0.32376412, -0.80237405, -4.03956352, 1.8608071,
-0.74354869, -6.79369666, -2.74137491, 5.6219074, 5.13307485,
2.09278167, -8.97792005, 0.23895157, 2.34350504, 2.01565156,
0.34461909, -5.39315134, 3.70375643, 1.35911336, 0.14440375,
-10.59505494, 2.44997231, -3.43575023, 8.45149168, 1.82580942,
-5.09746181, 0.54849722, -0.40588822, 2.90913923, -2.59092777,
2.87179578, 3.5472478, 5.50635214, 3.45884957, 0.5574311, -9.17310051,
-2.3069345, 1.01792404, 3.39829408, 2.18975374, -3.29888986,
3.36520805, -6.06857013, 3.69912561, -5.88831257, -5.59447201,
6.93463825, 5.18518235, -4.45047426, 3.48886956, -4.22410882,
0.36229398, 4.38850288, 2.44283293, -4.13337598, 0.07853854,
0.33111425, -1.50291612, -10.73449339, -2.31367763, 6.06925576,
7.25078064, -1.2508886, 0.69265168, -1.6952393, -5.45510355,
10.36136532, 0.81292421, -5.78213732, 0.29608406, 8.58995306,
3.33976066, -16.05532167, 4.59091993, 2.96362702, -1.21232785,
0.44224015, 4.78743418), inflation = c(-0.3, 0.1, 0.4, -0.4,
0, -0.1, -0.4, 0.1, 0.5, 0.3, -0.1, 0.2, 0, 0.3, -0.7, 0.3, 0.6,
-0.3, 0, 0.1, -0.7, -1.1, -0.6, -0.7, 0, -0.7, 0.1, -0.8, -0.3,
0.1, 0.3, -0.6, 0.2, 0.8, 0.7, 0.2, 0.1, 0.4, 0.1, 0.4, 0, 0.8,
0.1, 0, 0.3, -0.1, 0.2, 1.2, -0.1, 0.4, 0.1, -0.3, -0.4, -0.3,
-0.2, 0.7, 0.5, -0.2, -0.3, -0.2, 0.2, -0.4, -0.2, -0.2, 0, 0.1,
0.3, -0.2, -0.8, -0.2, 0.2, -1.7, -0.2, 0.5, -0.3, 0.5, 0.3,
-0.4, -0.6, 0.1, -0.3, 0.1, 0.1, -0.1, -0.2, -0.3, 0.3, -0.2,
0.1, -0.5, 0.6, 0.1, 0.1, 0, -0.4, -0.1, 0.3, 0.5, 0.1, 0.5,
-0.2, -0.1, 0, 0.2, -0.2, -0.1, -0.3, 0.4, -0.5, 0.3, 0, -0.1,
0.3, 0, 0.1, -0.1, 0.4, -0.6, 0.4, 0.4, 0.3, -0.2, 1, -0.7, -0.7,
0, 0.3, 0.3, 0.3, -0.1, -0.2, -0.5, -0.4, 0.1, -0.5, 1.1, 0.6,
0.2, -0.9, 0.5, -1, 0.1, -0.3, 0, 0.3, -0.1, 0.1, -0.6, 0.4,
-1.4, 0.6, -0.2, 0.2, 0.3, 0.2, 0.4, -0.3, -0.4, -0.2, -0.5,
0.8, -0.3, -0.1, -0.6, 0.2, 0.2, 0.1, 0.5, 0.1, -0.2, -0.2, 0.6,
-1.1, 1.7, 0.2, 0, 0.5, 0.8), indust_prod = c(-2.572274344, -2.787547763,
2.430571433, -3.345301558, -0.463876872, -4.282068938, -1.120792803,
1.572826809, 5.587675149, -4.689662112, -4.039000653, 6.048760143,
3.054565546, -8.324475243, 8.938629302, -8.359749373, 1.285898904,
2.787686994, -1.18689554, -1.044345864, -5.377152698, 0.417765217,
-2.016437912, -6.076899004, 0.004832982, 6.553009253, -3.861674081,
2.856277, -0.305740286, 3.884000992, 0.228026533, 3.160838283,
0.718539054, -0.177278754, 5.861419366, 1.635627332, -0.233559319,
1.656666214, -3.416861953, 3.08593964, 0.219887065, -5.252665393,
2.67414747, -3.078208264, -1.786603334, 5.055713134, -0.487827087,
-1.085759889, 1.558281917, 1.522276844, -0.649720104, -2.25624012,
-3.000400858, 1.287503915, 2.146782202, -2.414634004, -0.710817622,
-2.037943864, -3.422580841, 2.574492816, -0.989720019, -3.140953611,
-2.00903906, 5.044475953, 2.421105335, 1.312884435, -0.423167539,
-1.618876184, 0.749480391, -0.067574147, 0.665361763, 2.736011629,
-0.984538393, 2.734504118, 3.689697632, -4.285546756, -1.453623444,
-3.036406761, 0.371745852, 5.268465577, 2.014992273, 0.230769423,
1.479245498, -0.601620521, 0.435743581, -6.919825179, 3.973589167,
-1.18054296, 1.009209474, 3.4645508, -4.268359859, -1.38055936,
1.398352479, -1.380441077, -0.945043656, -3.070867046, 2.286824653,
4.15932548, -0.885666142, 1.66749454, 1.079797346, 1.268988373,
-3.757792572, 3.297688911, 0.757515843, -3.344176468, 0.937671829,
1.942358369, -0.363516768, -2.258151209, 1.602633167, -3.43640148,
1.319433782, -3.471816444, 6.680682523, -3.215319966, -4.672113236,
6.216354428, 0.969772263, -0.213391145, -3.34142316, 5.648021789,
-8.289444236, 8.300362011, -2.663437754, 3.039228136, 3.239437295,
-6.66030595, 1.185659472, -1.499248124, -3.292614657, 2.681309878,
-0.579981109, 0.867209022, 2.022041722, -6.479958297, 1.680604654,
-0.873272771, -2.875794511, 3.56198993, 0.706252639, -3.546626564,
1.706203079, -1.238940266, 0.694831062, -4.576557295, 5.589573409,
1.887484409, -6.283451003, 3.968287705, -4.024275523, -0.142379113,
3.389769739, 5.34662785, -1.707063421, -1.962359729, 1.648794981,
-5.778920692, -8.801594747, 6.391085381, 3.735161238, 0.512586559,
1.347387952, 0.083401305, -0.958210945, 0.28448621, -0.398402291,
0.174892606, -2.923332835, 9.40208252, 8.440250585, -5.118029161,
-4.652533609, -1.031496063, 0.253718285, 0.116959064, -0.767472053,
-2.656293514), construction = c(-5.97969161, -4.79269684, -5.74581431,
-5.80595875, -3.91928599, -4.68384075, 2.11180124, -2.08333333,
-2.12510024, -2.80786201, -2.94938222, -2.5189924, -3.04, -3.39592489,
-2.50302786, -4.54176805, -5.77544426, -5.52825553, 2.83860503,
-6.58756137, -7.37402704, -6.9748246, -9.32238193, -11.64889253,
-10.35478548, -10.21505376, -11.09730849, -8.41121495, -8.65837977,
-11.66016472, -15.14195584, -9.46123522, -8.97832817, -9.40550133,
-10.00905797, -8.58867224, -10.86056144, -11.60755412, -10.75919888,
-11.03896104, -12.57625528, -8.83218842, -15.24163569, -12.48185776,
-12.97376093, -13.36924584, -11.67589331, -11.93499238, -10.22199277,
-11.04742053, -10.96033403, -13.3472367, -11.27214171, -12.86329386,
-14.03508772, -13.0458817, -14.40536013, -15.65856416, -14.52991453,
-13.49480969, -15.12363427, -15.58289397, -18.22977726, -17.74969916,
-16.57592257, -16.86226065, -14.66836735, -16.52892562, -16.1121983,
-15.8847185, -16.46666667, -19.2, -19.85094851, -17.4184594,
-16.27240143, -14.55742502, -15.88107324, -15.15601783, -15.8445441,
-16.22239147, -15.08553655, -13.14741036, -14.44533121, -14.68646865,
-11.3271344, -14.45378151, -9.93150685, -11.81506849, -10.94827586,
-10.07005254, -8.25932504, -6.18181818, -6.04395604, -6.05504587,
-3.91791045, -0.87040619, -4.09914204, 0.09823183, -3.8973384,
-2.23300971, -2.80735721, -2.72638754, -1.839303, -3.87596899,
-3.89863548, -4.4921875, -6.69902913, -5.75609756, -3.77733598,
-5.69185476, -3.85756677, -4.27010924, -5.37848606, -3.5035035,
-6.01577909, -3.83064516, -2.63691684, -2.04498978, 0.52029136,
3.10559006, 2.58264463, 1.144641, -0.30864198, 0.20746888, 2.31578947,
1.34854772, 2.51836306, 2.72536688, 1.66666667, 1.67014614, 3.51966874,
2.10843373, 1.91339376, 3.08641975, 3.81836945, 4.24430642, 3.90946502,
3.48004094, 4.81064483, 4.18367347, 4.4057377, 3.28542094, 1.7,
1.08161259, 1.97628458, 3.79241517, 5.96421471, 3.17775571, 2.27722772,
3.26409496, 2.44140625, 1.76297747, 2.15897939, 8.41012196, -0.16302847,
0.76178712, -4.69372663, -17.51199295, -45.16320079, -36.28914654,
-16.06074568, -4.87295907, 7.5668007, 3.72748757, 7.02862225,
3.38149083, 1.75109309, 4.74865906, 14.84745984, 34.803423, 100.52114863,
67.60757235, 30.44713435, 18.17227737, 11.63694169, 9.69843425,
4.74314833, -4.52314658), exports = c(24.96318942, 23.36969334,
22.92995604, 10.65726657, 16.4160009, 15.06347523, 7.05135338,
15.02299212, 19.0326442, 18.98558329, 3.33296617, 23.07105097,
24.1601689, 19.72166595, 27.16602809, 24.3702412, 14.56065717,
23.81003502, 12.51014556, 1.73165902, -13.84780944, -22.58391171,
-20.80753018, -33.06565254, -36.85899284, -37.79190796, -39.34913459,
-33.38661304, -27.85334908, -28.37492746, -23.27801369, -13.6431818,
6.74411365, 11.84361899, 13.51797098, 20.8751474, 22.95402278,
28.37464897, 21.54264152, 8.61516289, 3.13570326, 4.20220684,
8.75273789, 5.92066231, 6.95366577, 4.26020533, 11.40501619,
9.87457947, 21.13174127, 16.42066897, 31.89439842, 33.06799105,
34.07732157, 29.29939609, 27.02846164, 20.81549686, 14.50583432,
16.11199422, 5.25642553, 5.67979421, 5.03884106, 5.91383786,
-6.92432553, -4.54670446, -5.43453018, -11.06914116, -4.30401515,
-5.89475834, -6.20354881, -5.22134237, -1.09985899, 9.43369798,
1.66688812, 1.20138491, 6.90689277, 7.75020233, 10.13668917,
11.30827244, 9.29930475, 10.9321974, 9.07936123, 15.05633842,
13.19202501, 5.4705525, 6.82591085, 2.44716859, 4.11748271, 5.06863218,
6.22577349, 6.12185947, 0.58671313, -3.10270985, 1.18732102,
-8.2784308, -6.9219884, -15.34433626, -14.14267264, -15.499663,
-17.52534249, -13.08561877, -13.30851885, -14.85330263, -14.60224224,
-11.87824448, -10.99332931, -13.12023756, -13.1469306, -6.67165565,
-4.86353483, -1.18866693, 5.19617018, -5.56720735, -1.31601522,
2.27149134, -0.32052319, 6.34015708, -2.21439899, 8.54617508,
7.90296929, 8.97464479, 8.46001663, 14.8780712, 0.80639894, 12.94240637,
3.50598117, 9.14630966, 19.69171985, 15.802644, 16.51140186,
21.66597738, 20.28010486, 25.27245738, 22.10541531, 15.94575338,
26.38566548, 13.20690115, 17.00548484, 12.09162042, 0.06482127,
2.03901271, 0.44242727, -9.82586651, 0.27668163, -2.78174059,
-2.82866966, -3.82970378, -5.71082564, 2.43947926, -5.13803015,
-5.57470758, -5.42985341, -2.92568853, 3.29088281, 0.70260049,
-5.31805275, -3.60733326, -1.34953732, -13.82572968, -43.79388438,
-37.96950775, -25.84963925, -17.45358624, 1.37712882, -6.52424698,
-1.90571756, -7.56995756, 4.05542391, -3.88890971, 0.22947427,
18.0580403, 83.71922617, 69.97805207, 37.59500165, 29.96692408,
15.45334076, 19.1358433, 18.69987106, 1.361784), new_licences = c(-8.68185938,
-0.02872738, -4.63917526, -1.28723796, 6.61294804, 22.56276492,
0.7781103, 4.79877409, 10.14401124, 8.24945593, 12.81146637,
9.55541727, 22.9280397, 5.57950192, 13.32309582, 5.15754737,
-8.87412041, -4.33625964, 4.81079247, 6.37217177, -6.40984757,
-3.33605579, 37.89418817, -43.23130325, -42.15352673, -42.12746655,
-33.9205377, -33.62858734, -23.67129034, -20.50889878, -14.90326237,
-12.50180871, 3.46190541, 1.31288184, -17.85106785, 61.84546971,
53.12063809, 86.83179182, 33.04076778, 46.80088462, 62.54917879,
18.35461489, 13.50837139, 15.08185877, 1.04729285, 19.36104696,
61.72782699, -9.18395384, -12.57976299, -20.58231247, -11.10426044,
-23.56363636, -34.11195205, -29.70589684, -31.85551458, -33.86262394,
-40.44064924, -48.64559819, -59.99715485, -47.36404205, -48.59228363,
-49.05969361, -41.7394923, -27.49762131, -37.00874636, -35.13210456,
-33.07096298, -30.93970668, -19.15289482, -25.34798535, -43.46550498,
0.89093261, 8.91046074, -0.19703412, -4.26190476, -5.38995126,
17.88392113, 17.14563526, 12.84454245, 15.91945886, 23.0810884,
23.51044441, 35.77606542, 31.90428714, 40.30863376, 47.04904406,
53.12111415, 36.55008422, 23.55712603, 30.67416767, 26.05438854,
31.72750712, 29.72943247, 33.01929625, 37.87352328, 27.84796458,
35.64994785, 41.8951385, 21.94250447, 33.10840226, 33.91801716,
9.71134166, 21.90931404, 30.04017719, 16.31199661, 14.13943169,
10.39986559, 17.71959459, 25.98727895, 31.75638663, 6.44645711,
13.57863178, 10.93394077, 0.59826311, 12.84306453, 10.63138715,
8.87090896, 23.54216507, 29.26495206, 7.82034725, 4.63800277,
-1.80663693, 17.80530531, 13.51986946, 6.23716632, 12.3673104,
12.0950324, 6.38739706, 6.44081414, 6.66263238, 0.41205557, -2.8280543,
10.1373204, 7.07467283, 13.80775358, -0.3255401, 5.34750745,
13.59549283, 28.0137388, -14.04051962, -12.215373, -12.25462385,
-5.28197913, 7.40258851, -9.21869735, -10.48961104, -1.44190387,
-3.61384459, -3.27574344, -7.6398978, -18.62443557, 13.99263957,
12.13098309, 6.03905341, 9.71096119, -8.04004081, 7.45041892,
-57.44578313, -86.98451778, -74.73596198, -56.22999407, -17.50379692,
-0.14475271, -9.42437148, -12.58866381, -27.8902439, -19.59833014,
-30.46522915, -58.98435572, 19.84711212, 438.70498363, 190.21076468,
70.96424702, -18.97560655, -35.80575018, -18.2011224, -22.68440675,
-7.59343819), investm = c(9.59206261, 10.09104451, 8.49259555,
11.05912367, 11.33035356, 10.69893187, 9.27221449, 6.36319052,
5.1145808, 3.14675108, -2.0583252, -5.9801462, -8.54458193, -11.85690298,
-12.73704068, -15.30334665, -16.51488054, -18.55445193, -18.78407228,
-20.33141371, -23.20479413, -23.70413199, -25.82349247, -23.49151634,
-23.20331483, -21.76656318, -19.69767216, -16.1163604, -15.56801204,
-10.63893647, -6.90914634, -1.33860382, 5.00317034, 9.14637266,
13.66111398, 14.90618555, 16.27492141, 18.9726911, 17.81129998,
11.93953654, 9.97602683, 6.72450506, 3.35538125, 1.22492917,
-0.25529345, -3.58030117, -5.66383325, -5.99451638, -5.59778107,
-8.22207902, -8.75620416, -6.82634766, -7.76850362, -8.81317285,
-10.30877298, -12.31016477, -12.44698993, -11.49249967, -9.6953666,
-10.58305192, -9.65103885, -8.93856304, -8.31371275, -8.81656192,
-7.08877565, -5.92517826, -3.13301918, -0.90715848, 0.17047088,
0.71130641, 3.09817756, 6.3772002, 6.05805695, 4.99307084, 4.93907049,
7.50839619, 8.21051614, 7.46196337, 6.59668824, 6.44846351, 4.4735673,
3.33064046, -0.20557392, -0.17865147, -0.07705874, 1.55447287,
0.59002986, -0.47146863, 0.62804213, -2.08346753, -2.73661788,
-3.90829617, -3.78220665, -4.01170424, -5.17638734, -6.16018899,
-5.69641901, -6.49065187, -5.75373139, -6.16938142, -10.39770764,
-7.95466132, -7.83823783, -8.7925605, -7.90867493, -5.97676105,
-5.228471, -6.84892555, -8.8243345, -9.50405058, -10.14943257,
-10.02532808, -8.25385595, -7.41211911, -6.72331539, -4.65132561,
-3.88085377, -5.35115101, -3.6992884, 3.41808435, 5.01032011,
6.4338955, 7.44842984, 7.96282188, 10.42036183, 9.85846999, 10.3384518,
12.3886042, 13.55244814, 14.4281047, 12.93327424, 7.14080025,
6.37943542, 4.68820186, 5.38710032, 4.61464096, 3.14205599, 2.9423131,
2.15746296, 0.09387282, -2.38577599, -2.97124266, -4.32001028,
-4.33596506, -3.36175388, -1.65514585, -1.1147413, -1.1116245,
0.03453284, 1.51801102, 1.97354903, 3.29586522, 4.83238917, 5.70220137,
7.46051222, 7.48040345, 6.30281751, 0.14703508, -0.15754408,
0.73361141, 0.474155, 0.46358707, 1.64385862, 1.15369522, 0.67754708,
4.63841367, 6.78461903, 8.58994132, 10.23754398, 17.32260776,
19.2077649, 20.72874275, 21.76681016, 22.19165981, 23.17276241,
23.11683188, 23.85460849, 20.34415852), household_consupt = c(0,
0, 1.359168191, 0, 0, -0.342789739, 0, 0, 1.790564565, 0, 0,
-0.482967554, 0, 0, -1.762057837, 0, 0, -0.536837331, 0, 0, -4.302408991,
0, 0, -4.488751839, 0, 0, -1.381433473, 0, 0, 0.30585643, 0,
0, 3.227950235, 0, 0, 5.377633558, 0, 0, 1.854863297, 0, 0, -0.352069414,
0, 0, -0.701775487, 0, 0, -3.526502117, 0, 0, -2.191789397, 0,
0, -0.956817085, 0, 0, -2.394047662, 0, 0, 1.250192652, 0, 0,
-0.572344375, 0, 0, 0.753471833, 0, 0, 0.070143601, 0, 0, -0.298510806,
0, 0, 3.154164092, 0, 0, 0.883890768, 0, 0, 2.625083331, 0, 0,
0.256046586, 0, 0, -0.856863549, 0, 0, 0.590803335, 0, 0, -0.813925723,
0, 0, 0.125528578, 0, 0, 1.468506379, 0, 0, -0.670352866, 0,
0, -0.362744845, 0, 0, 1.32210668, 0, 0, -1.215018014, 0, 0,
0.331687133, 0, 0, 1.532560625, 0, 0, -0.533138522, 0, 0, -0.422657587,
0, 0, 0.037693685, 0, 0, -0.657257657, 0, 0, 0.489966584, 0,
0, 0.938809848, 0, 0, -0.343317849, 0, 0, 0.626641626, 0, 0,
0.067372918, 0, 0, -0.634514601, 0, 0, 0.004316699, 0, 0, -0.456951802,
0, 0, -3.945597519, 0, 0, -16.828812208, 0, 0, 12.869510817,
0, 0, -1.316414045, 0, 0, -1.582469509, 0, 0, 26.887239149, 0,
0, -14.346872459, 0, 0, 2.145073044, 0), covid_total_cases = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 236.490662, 29.76642, 29.664615,
21.193137, 13.588659, 30.217886, 87.020465, 110.973322, 38.789711,
74.173149, 11.664696, 2.132837, 1.797567, 1.506289, 3.587828,
10.127144, 7.154014, 3.020636, 1.99873, 5.189378), covid_total_vaccinations = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 964.859996,
163.568194, 94.43678, 87.634082, 73.551864, 47.399777, 42.470628,
20.985756, 8.109421, 1.48542, 7.961336)), class = "data.frame", row.names = c(NA,
-178L))
Code:
VAR_data_Total = data
f = function(data){
m1 = vars::VAR(data[,1:10], p=3, exogen = data[,11:12])
s1 = summary(m1)
return(s1$varresult$unemp$coefficients)
}
zoo::rollapply(VAR_data_Total, width = 41, FUN = f, by.column = FALSE)

Calculate the S1 sum for variance of Moran's I

I would like to calculate the S1 term for the variance of Moran's I. The following is the formula to calculate the S1
where w_{ij} is an element in the spatial weights matrix.
My spatial weights matrix is as following:-
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.111111111111111, 0.1, 0.1, 0.166666666666667,
0, 0.1, 0.111111111111111, 0.125, 0.166666666666667, 0, 0, 0.1,
0.111111111111111, 0, 0, 0.166666666666667, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0.0909090909090909,
0, 0, 0.1, 0.1, 0.166666666666667, 0, 0.1, 0.111111111111111,
0.125, 0.166666666666667, 0, 0, 0.1, 0, 0, 0, 0, 0, 0, 0.0909090909090909,
0, 0.111111111111111, 0, 0.1, 0, 0, 0.1, 0.111111111111111, 0.125,
0.166666666666667, 0, 0, 0.1, 0.111111111111111, 0, 0, 0.166666666666667,
0, 0, 0.0909090909090909, 0, 0.111111111111111, 0.1, 0, 0, 0.333333333333333,
0.1, 0.111111111111111, 0.125, 0, 0, 0, 0.1, 0.111111111111111,
0, 0, 0.166666666666667, 0, 0, 0.0909090909090909, 0, 0.111111111111111,
0, 0, 0, 0, 0.1, 0.111111111111111, 0, 0.166666666666667, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0.111111111111111, 0, 0, 0.166666666666667, 0, 0, 0.0909090909090909,
0, 0.111111111111111, 0.1, 0.1, 0.166666666666667, 0, 0, 0.111111111111111,
0.125, 0.166666666666667, 0, 0, 0.1, 0.111111111111111, 0, 0,
0, 0, 0, 0.0909090909090909, 0, 0.111111111111111, 0.1, 0.1,
0.166666666666667, 0, 0.1, 0, 0.125, 0, 0, 0, 0.1, 0.111111111111111,
0, 0, 0, 0, 0, 0.0909090909090909, 0, 0.111111111111111, 0.1,
0.1, 0, 0, 0.1, 0.111111111111111, 0, 0, 0, 0, 0.1, 0.111111111111111,
0, 0, 0, 0, 0, 0.0909090909090909, 0, 0.111111111111111, 0.1,
0, 0.166666666666667, 0, 0.1, 0, 0, 0, 0, 0, 0.1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0.0909090909090909, 0, 0.111111111111111, 0.1, 0.1, 0, 0, 0.1,
0.111111111111111, 0.125, 0.166666666666667, 0, 0, 0, 0.111111111111111,
0, 0, 0.166666666666667, 0, 0, 0.0909090909090909, 0, 0, 0.1,
0.1, 0, 0.333333333333333, 0.1, 0.111111111111111, 0.125, 0,
0, 0, 0.1, 0, 0, 0, 0.166666666666667, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.166666666666667,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0909090909090909,
0, 0, 0.1, 0.1, 0, 0.333333333333333, 0, 0, 0, 0, 0, 0, 0.1,
0.111111111111111, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(20L, 20L), .Dimnames = list(
c("AUS", "BEL", "CAN", "CHE", "DEU", "DNK", "ESP", "FIN",
"FRA", "GBR", "IRL", "ITA", "JPN", "KOR", "NLD", "NOR", "NZL",
"PRT", "SWE", "USA"), c("AUS", "BEL", "CAN", "CHE", "DEU",
"DNK", "ESP", "FIN", "FRA", "GBR", "IRL", "ITA", "JPN", "KOR",
"NLD", "NOR", "NZL", "PRT", "SWE", "USA")))
I can do this by using a for loop within a for loop, but I am thinking that there might be a more efficient way to do this. Maybe using lapply or something else.
Thanks in advance.
Define a squared sum function and use outer.
squared_sum <- function(x, y) (x + y)^2
sum(outer(W, W, squared_sum))/2
#[1] 3037.03
for me, the solution looks like this
set.seed(1)
m <- matrix(sample(c(0, 1), size = 16, replace = TRUE), nrow = 4)
diag(m) <- 0
m
#> [,1] [,2] [,3] [,4]
#> [1,] 0 1 1 0
#> [2,] 1 0 1 0
#> [3,] 0 0 0 0
#> [4,] 0 0 0 0
S1 <- 0.5 * sum((m + t(m))^2)
S1
#> [1] 6
Created on 2021-10-07 by the reprex package (v2.0.1)
Using expand.grid
squared_sum <- function(x, y) (x + y)^2
with(expand.grid(W, W) , sum(squared_sum(Var1, Var2)))/2
[1] 3037.03

Density Plot based on specific tolerance bands (normalized)

I'm having a huge data table with many different columns. Now I want to plot for some columns (EV, EH, ...) the densities based on a specific tolerance band (column pBu). In my sample below you can see that I'm having five different tolerance bands (0.00, 0.05, 0.10, 0.15 and 0.20), so for my density plot EV I would have five different densities in one plot. In addition, the y-axis should be normalized (range from 0 to 1).
How can I handle this with plotly?
Here my data structure:
structure(list(Year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L), GPNRPlan = c(100236L, 101554L,
111328L, 124213L, 127434L, 128509L, 130058L, 130192L, 130224L,
130309L, 130342L, 130351L, 130392L, 130402L, 130843L, 132067L,
132597L, 133874L, 133943L, 133950L), TB.Info = c("Below TB",
"Over TB", "In TB", "In TB", "In TB", "Below TB", "Over TB",
"Below TB", "Below TB", "Below TB", "Over TB", "Over TB", "In TB",
"In TB", "In TB", "In TB", "In TB", "Over TB", "In TB", "Over TB"
), EV = c(-33498.61, 0, -31117.17, 70950.43, -37984.99, 0, 0,
-93820830.01, -7852622.13, -245712.8, 1800050, 320263371.77,
-803069.3, -731544.77, -45841.06, -19931.59, 62286.48, 14652139.32,
296249.92, 173414.12), EK = c(311746.83, 0, 408503.01, 965723.51,
447176.86, 0, 0, 237703401.51, 11650300.16, 761470.17, 15514898.49,
791067269.75, 35591131, 10754272.33, 9496742.11, 512370.9, 1134032.95,
35318984.4, 5630139.9, 1111511.07), EH = c(345245.44, 0, 439620.18,
894773.08, 485161.85, 0, 0, 331524231.52, 19502922.3, 1007182.97,
13714848.49, 470803897.97, 36394200.3, 11485817.1, 9542583.17,
532302.49, 1071746.46, 20666845.08, 5333889.99, 938096.94), Peff = c(104.78,
0, 91.52, 112.18, 113.39, 0, 0, 86.18, 101.04, 104.39, 106.23,
86.4, 96.19, 86.38, 113.5, 115.88, 104.61, 96.31, 95.6, 101.71
), Qeff = c(-0.01, 0, 0, 0, 0, 0, 0, 0, -0.01, -0.01, 0, 0, 0,
0, 0.01, 0, 0, 0, 0, 0), Meff = c(-7.35, 0, -2.16, -2.35, 1.9,
0, 0, 0.94, -8.28, -6.9, -0.57, 0, -0.63, -0.21, 10.89, -3.73,
-1.63, 0.69, 0.88, 3.29), G = c(104.7, 0, 91.51, 112.14, 113.46,
0, 0, 86.19, 100.99, 104.37, 106.2, 86.4, 96.19, 86.38, 113.67,
115.83, 104.61, 96.29, 95.61, 101.76), D = c(3262.07, 0, 4770.44,
7947.26, 4254.06, 0, 0, 3818668.12, 191836.45, 9612.18, 127995.67,
5426568.38, 377446.8, 132384.92, 83629.36, 4575.32, 10225.66,
212630.83, 55721.94, 9204.12), pP = c(24.86, 0, 28.34, 37.62,
30.53, 0, 0, 27.95, 28.54, 37.74, 26.96, 32.78, 37.09, 32.87,
29.64, 36.81, 39.14, 24.61, 40.53, 38.33), rP = c(1.05, 0, 0.64,
0.4, 0.65, Inf, Inf, 0.64, 0.63, 0.39, 0.92, 0.36, 0.23, 0.38,
0.6, 0.46, 0.2, 0.89, 0.12, 0.21), rQ = c(-0.11, Inf, -0.08,
0.07, -0.09, -0.04, 0.05, -0.29, -0.41, -0.25, 0.12, 0.66, -0.03,
-0.08, -0.02, -0.05, 0.05, 0.69, 0.04, 0.17), rE = c(0.91, 0,
0.6, 0.22, 0.52, Inf, Inf, 0.61, 0.59, 0.22, 0.76, 0.36, 0.21,
0.36, 0.54, 0.26, 0.15, 0.85, 0.1, 0.17), dEHdD = c(105.84, 0,
92.15, 112.59, 114.05, 0, 0, 86.82, 101.66, 104.78, 107.15, 86.76,
96.42, 86.76, 114.11, 116.34, 104.81, 97.2, 95.72, 101.92), dEHdzH = c(149.99,
0, 174.85, 250, 157.6, 0, 0, 140353.13, 6999.39, 299.99, 5499.95,
165545.1, 10399.12, 4079.6, 2999.81, 150, 274.98, 8998.95, 1399.89,
249.98), dEHdrP = c(3262.07, 0, 4770.44, 7947.26, 4254.06, 0,
0, 3818668.12, 191836.45, 9612.18, 127995.67, 5426568.38, 377446.8,
132384.92, 83629.36, 4575.32, 10225.66, 212630.83, 55721.94,
9204.12), dEKdzV = c(135, 0, 160.54, 267.07, 143.61, 0, 0, 133335.47,
6649.42, 284.99, 6049.94, 198654.12, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 9448.9, 1461.51, 249.98), dEKdzTB = c(-1.13,
0, 0, 0, 0, 0, 0, -34015.93, -2514.12, -60.93, 109.72, 76326.42,
0, 0, 0, 0, 0, 5769.55, 0, 42.97), dEKdzH = c(135, 0, 160.54,
267.07, 143.61, 0, 0, 133335.47, 6649.42, 284.99, 6049.94, 198654.12,
10058.17, 3772.52, 2951.88, 142.86, 287.99, 9448.9, 1461.51,
249.98), dEKdrQ = c(348451.86, 0, 441620.17, 900981.67, 487701.61,
0, 0, 335596984.05, 19696287.61, 1017097.6, 13847581.34, 476478784,
36707738.02, 11575841.25, 9599815.68, 535717.91, 1080845.93,
20888792.33, 5386259.63, 948299.85), dEKdrP = c(3262.07, 0, 4770.44,
7947.26, 4254.06, 0, 0, 3818668.12, 191836.45, 9612.18, 127995.67,
5426568.38, 377446.8, 132384.92, 83629.36, 4575.32, 10225.66,
212630.83, 55721.94, 9204.12), dEKdrE = c(-24.56, 0, 0, 0, 0,
0, 0, -925490.96, -68906.02, -1952.36, 2553.36, 2501979.97, 0,
0, 0, 0, 0, 136325.23, 0, 1582.17), dEKdpP = c(133.87, 0, 160.54,
267.07, 143.61, 0, 0, 99319.54, 4135.3, 224.06, 6159.66, 274980.54,
10058.17, 3772.52, 2951.88, 142.86, 287.99, 15218.45, 1461.51,
292.95), dEKdD = c(95.57, 0, 85.63, 121.52, 105.12, 0, 0, 62.25,
60.73, 79.22, 121.21, 145.78, 94.29, 81.23, 113.56, 111.99, 110.9,
166.1, 101.04, 120.76), dEKdf = c(3327.91, 0, 4549.69, 10047.22,
4384.35, 0, 0, 2775981.18, 118021.52, 8456.02, 166064.35, 9013862.02,
373057.45, 124002.65, 87493.72, 5258.83, 11271.93, 374526.13,
59234.84, 11228.85), dEKdavgPh = c(-162.25, 0, -171.47, -328.66,
-170.41, 0, 0, -107505.38, -4532.49, -274.4, -7350.79, -287563.54,
-10693.86, -3995.68, -3297.26, -179.43, -314.73, -16536.87, -1543.22,
-317.73), dEVdzV = c(135, 0, 160.54, 267.07, 143.61, 0, 0, 133335.47,
6649.42, 284.99, 6049.94, 198654.12, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 9448.9, 1461.51, 249.98), dEVdzTB = c(-1.13,
0, 0, 0, 0, 0, 0, -34015.93, -2514.12, -60.93, 109.72, 76326.42,
0, 0, 0, 0, 0, 5769.55, 0, 42.97), dEVdzH = c(-15, 0, -14.32,
17.07, -14, 0, 0, -7017.66, -349.97, -15, 549.99, 33109.02, -340.95,
-307.09, -47.93, -7.13, 13.01, 449.95, 61.62, 0), dEVdrQ = c(348451.86,
0, 441620.17, 900981.67, 487701.61, 0, 0, 335596984.05, 19696287.61,
1017097.6, 13847581.34, 476478784, 36707738.02, 11575841.25,
9599815.68, 535717.91, 1080845.93, 20888792.33, 5386259.63, 948299.85
), dEVdrE = c(-24.56, 0, 0, 0, 0, 0, 0, -925490.96, -68906.02,
-1952.36, 2553.36, 2501979.97, 0, 0, 0, 0, 0, 136325.23, 0, 1582.17
), dEVdpP = c(133.87, 0, 160.54, 267.07, 143.61, 0, 0, 99319.54,
4135.3, 224.06, 6159.66, 274980.54, 10058.17, 3772.52, 2951.88,
142.86, 287.99, 15218.45, 1461.51, 292.95), dEVdD = c(-10.27,
0, -6.52, 8.93, -8.93, 0, 0, -24.57, -40.93, -25.56, 14.06, 59.02,
-2.13, -5.53, -0.55, -4.36, 6.09, 68.91, 5.32, 18.84), dEVdf = c(3327.91,
0, 4549.69, 10047.22, 4384.35, 0, 0, 2775981.18, 118021.52, 8456.02,
166064.35, 9013862.02, 373057.45, 124002.65, 87493.72, 5258.83,
11271.93, 374526.13, 59234.84, 11228.85), dEVdavgPh = c(-162.25,
0, -171.47, -328.66, -170.41, 0, 0, -107505.38, -4532.49, -274.4,
-7350.79, -287563.54, -10693.86, -3995.68, -3297.26, -179.43,
-314.73, -16536.87, -1543.22, -317.73), dPeffdrP = c(-1.04, 0,
-0.92, -1.08, -1.11, -0.94, -1.13, -0.88, -1.03, -1.02, -1.04,
-0.88, -0.98, -0.88, -1.16, -1.11, -1.06, -0.96, -0.97, -1.04
), dMeffdrP = c(0.04, 0.82, 0.01, 0.03, -0.04, -0.05, -0.04,
-0.01, 0.03, 0.01, 0.01, 0, 0, 0, -0.11, 0.03, 0, 0.01, -0.01,
-0.04), type = c("J/B", "SPOT", "J/B", "J/B", "J/B", "J/B", "J*/B",
"KEINE", "J*/B", "J/B", "J*/B", "KEINE", "J*/B", "J/B", "J*/B",
"J/B", "J/B", "J*/B", "J*/B", "J/B"), pBu = c(0.1, 0, 0.05, 0.1,
0.1, 0.05, 0.1, 0, 0.1, 0.15, 0.1, 0, 0.1, 0.1, 0.2, 0.15, 0.1,
0.05, 0.2, 0.2), pBo = c(0.1, 0, 0.05, 0.1, 0.1, 0.05, 0.1, 0,
0.1, 0.15, 0.1, 0, 0.1, 0.1, 0.2, 0.15, 0.1, 0.05, 0.2, 0.2),
zTBu = c(1.5, 0, 1.5, 1.5, 1.5, 1.5, 1.5, 0, 1.5, 5.4, 1.5,
0, 1.5, 3, 1.5, 6.5, 1.5, 1.5, 1.5, 1.5), zTBo = c(1.5, 0,
1.5, 1.5, 1.5, 1.5, 1.5, 0, 1.5, 5.4, 1.5, 0, 1.5, 3, 1.5,
6.5, 1.5, 1.5, 1.5, 1.5), zV = c(1.5, 0, 1.5, 1.5, 1.5, 1,
0.8, 0, 0.5, 1.5, 0.8, 0, 1.5, 0, 0.8, 1.5, 1.5, 0.7, 1.5,
1.5), TP = c(35.19, 0, 37.24, 31.29, 28.73, 27.37, 23.83,
33.99, 27.28, 33.36, 30.41, 29.61, 31.41, 28.22, 23.92, 30.64,
32.98, 31.39, 25.77, 27.42)), class = c("data.table", "data.frame"
), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x0000016781401ef0>)
library(ggplot2)
library(dplyr)
library(plotly)
(df %>%
mutate(pBu=as.factor(pBu)) %>%
ggplot(aes(x=EV)) +
geom_density(aes(y=..scaled..,color=pBu))) %>%
ggplotly()

Sensitivity and specificity changes using a single threshold and a gradient of thresholds at 0.5 using pROC in R

I am trying to calculate ROC for a model of multi-class image. But since I didn't find any best way for multi-class classification, I have converted it to binary class. I have 31 classes of image. Using binary methods I am trying to find ROC of each 31 classes individually.
df <- read.xlsx("data.xlsx",sheetName = 1,header = F)
dn <- as.vector(df$X1) # 31 class
model_info <- read.csv("all_new.csv",stringsAsFactors = F) # details of
model output (Actual labels, Model labels, probabablity values)
head(model_info)
Actual_labels App_labels X1st
1 tinea cruris and corporis tinea cruris and corporis tinea cruris and corporis
2 tinea cruris and corporis tinea cruris and corporis tinea cruris and corporis
3 tinea cruris and corporis no diagnosis acne vulgaris
4 eczema eczema eczema
5 eczema no diagnosis psoriasis
6 folliculitis impetigo and pyodermas impetigo and pyodermas
X2nd X3rd X.st.. X2nd.. X3rd..
1 psoriasis herpes zoster 0.89 0.05 0.03
2 psoriasis eczema 0.89 0.03 0.02
3 psoriasis molluscum contagiosum 0.29 0.16 0.14
4 tinea cruris and corporis psoriasis 0.62 0.09 0.08
5 melasma tinea cruris and corporis 0.27 0.27 0.25
6 acne vulgaris psoriasis 0.73 0.07 0.03
head(dn)
[1] "acne vulgaris" "alopecia areata" "anogenital warts"
[4] "bullous pemphigoid" "candidiasis" "chicken pox"
App_call function basically converts the probability values to 0 or 1 based on whether model call is true or not
app_call <- function(cut_off, category){
labels_thr <- rep(0,nrow(app_res))
ind <- which(model_info$X.st.. >= cut_off) # index of instances
above threshold
true_val <- which(app_res$App.Diagnosis[ind] == category) # index of instances where actual labels are similar to model labels for 1st class out of 31 class.
labels_thr[ind[true_val]] <- 1
return(labels_thr)}
index0 <- grep(pattern = paste0("^",dn[i],"$"),x = model_info$Actual_labels)
actual_labels <- rep(0,nrow(model_info))
if(length(index)>= 1){
actual_labels[index0] <- 1
actual_labels[-index0] <- 0}
app_labels <- app_call(cut_off = 0.5,category = dn[i])
res <- roc(actual_labels,app_labels)
res1 <- roc(actual_labels,model_info$X.st..)
dput(actual_labels)
c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0,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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0)
dput(model_info$X.st..)
c(0.89, 0.89, 0.29, 0.62, 0.27, 0.73, 0.44, 0.7, 0.42, 0.56,
0.87, 0.19, 0.72, 0.54, 0.37, 0.46, 0.89, 0.89, 0.88, 0.2, 0.46,
0.75, 0.78, 0.66, 0.5, 0.67, 0.17, 0.85, 0.75, 0.58, 0.97, 0.71,
0.8, 0.29, 0.56, 0.44, 0.6, 0.36, 0.38, 0.31, 0.17, 0.35, 0.99,
0.19, 0.48, 0.51, 0.48, 0.92, 0.39, 0.14, 0.44, 0.55, 0.5, 0.43,
0.38, 0.27, 0.37, 0.47, 0.63, 0.4, 0.78, 0.96, 0.67, 0.7, 0.61,
0.44, 0.44, 0.48, 0.47, 0.35, 0.44, 0.34, 0.34, 0.6, 0.24, 0.49,
0.47, 0.35, 0.62, 0.76, 0.67, 0.51, 0.48, 0.72, 0.81, 0.9, 0.89,
0.92, 0.94, 0.2, 0.87, 0.98, 0.99, 0.98, 0.85, 0.89, 0.87, 0.62,
0.19, 0.86, 0.49, 0.5, 0.2, 0.48, 0.51, 0.49, 0.3, 0.11, 0.88,
0.35, 0.81, 0.59, 0.67, 0.31, 0.94, 0.22, 0.4, 0.52, 0.21, 0.31,
0.19, 0.79, 0.64, 0.33, 0.27, 0.19, 0.74, 0.78, 0.41, 0.35, 0.8,
0.39, 0.28, 0.92, 0.37, 0.66, 0.66, 0.44, 0.77, 0.49, 0.27, 0.32,
0.65, 0.21, 0.95, 0.51, 0.16, 0.33, 0.85, 0.26, 0.61, 0.25, 0.57,
0.26, 0.21, 0.45, 0.44, 0.83, 0.31, 0.35, 0.91, 0.34, 0.89, 0.77,
0.56, 0.31, 0.26, 0.84, 0.15, 0.27, 0.47, 0.73, 0.28, 0.44, 0.52,
0.65, 0.72, 0.38, 0.99, 0.46, 0.35, 0.25, 0.42, 0.82, 0.24, 0.56,
0.31, 0.52, 0.91, 0.94, 0.68, 0.21, 0.28, 0.68, 0.67, 0.61, 0.6,
0.43, 0.37, 0.28, 0.28, 0.24, 0.4, 0.92, 0.9, 0.45, 0.96, 0.53,
0.72, 0.26, 0.47, 0.47, 0.97, 0.49, 0.25, 0.51, 0.3, 0.92, 0.43,
0.33, 0.95, 0.25, 0.51, 0.98, 0.23, 0.51, 0.75, 0.84, 0.54, 0.5,
0.54, 0.33, 0.64, 0.29, 0.93, 0.13, 0.27, 0.93, 0.59, 0.27, 0.81,
0.57, 0.59, 0.47, 0.24, 0.53, 0.53, 0.43, 0.24, 0.94, 0.6, 0.7,
0.23, 0.69, 0.95, 0.95, 0.49, 0.73, 0.31, 0.94, 0.15, 0.85, 0.92,
0.34, 0.95, 0.91, 0.36, 0.55, 0.55, 0.29, 0.86, 0.31, 0.48, 0.48,
0.45, 0.5, 0.49, 0.3, 0.33, 0.39, 0.8, 0.42, 0.51, 0.52, 0.66,
0.19, 0.58, 0.94, 0.51, 0.39, 0.84, 0.95, 0.85, 0.72, 0.35, 0.83,
0.5, 0.91, 0.83, 0.61, 0.79, 0.5, 0.87, 0.3, 0.5, 0.53, 0.22,
0.82, 0.74, 0.73, 0.65, 0.88, 0.31, 0.75, 0.74, 0.92, 0.38, 0.47,
0.26, 0.77, 0.78, 0.82, 0.59, 0.59, 0.33, 0.67, 0.31, 0.67, 0.44,
0.77, 0.61, 0.44, 0.77, 0.83, 0.58, 0.6, 0.78, 0.76, 0.47, 0.72,
0.47, 0.29, 0.14, 0.32, 0.17, 0.56, 0.68, 0.3, 0.46, 0.56, 0.68,
0.61, 0.7, 0.23, 0.39, 0.79, 0.38, 0.32, 0.58, 0.46, 0.5, 0.57,
0.93, 0.4, 0.37, 0.75, 0.76, 0.36, 0.84, 0.19, 0.18, 0.94, 0.53,
0.53, 0.24, 0.23, 0.51, 0.53, 0.84, 0.23, 0.44, 0.85, 0.53, 0.23,
0.56, 0.26, 0.38, 0.78, 0.93, 0.65, 0.22, 0.52, 0.35, 0.47, 0.33,
0.31, 0.65, 0.72, 0.46, 0.44, 0.74, 0.92, 0.99, 0.72, 0.41, 0.18,
0.85, 0.89, 0.31, 0.4, 0.98, 0.46, 0.16, 0.58, 0.25, 0.21, 0.32,
0.43, 0.56, 0.34, 0.35, 0.7, 0.43, 0.17, 0.25, 0.33, 0.44, 0.44,
0.58, 0.74, 0.37, 0.68, 0.52, 0.8, 0.96, 0.52, 0.25, 0.81, 0.94,
1, 0.58, 0.42, 0.46, 0.41, 0.18, 0.37, 0.9, 0.54, 0.29, 0.38,
0.38, 0.53, 0.99, 0.57, 0.44, 0.33, 0.45, 0.95, 0.85, 0.75, 0.19,
0.97, 0.27, 0.94, 0.77, 0.79, 0.57, 0.33, 0.98, 0.47, 0.55, 0.27,
0.43, 0.66, 1, 0.62, 0.34, 0.81, 0.4, 0.56, 0.33, 0.25, 0.4,
0.25, 0.91, 0.28, 0.4, 0.73, 0.32, 0.49, 0.37, 0.19, 0.35, 0.29,
0.77, 0.36, 0.31, 0.85, 0.33, 0.61, 0.63, 0.41, 0.98, 0.28, 0.31,
0.91, 0.34, 0.24, 0.82, 0.46, 0.5, 0.39, 0.72, 0.67, 0.51, 0.41,
0.81, 0.74, 0.5, 0.97, 0.65, 0.44, 0.71, 0.35, 0.84, 0.97, 0.42,
0.75, 0.91, 0.61, 0.94, 0.48, 0.42, 0.63, 0.81, 0.83, 0.66, 0.55,
0.61, 0.41, 0.63, 1, 0.63, 0.41, 0.75, 0.27, 0.28, 0.24, 0.55,
0.35, 0.85, 0.97, 0.64, 0.79, 0.92, 0.47, 0.81, 0.23, 0.16, 0.75,
0.12, 0.43, 0.18, 0.69, 0.21, 0.39, 0.19, 0.85, 0.57, 0.97, 0.56,
0.81, 0.13, 0.4, 0.47, 0.95, 0.43, 0.9, 0.67, 0.36, 0.38, 0.83,
0.97, 0.48, 0.93, 0.67, 0.44, 0.34, 0.83, 0.77, 0.39, 0.56, 0.85,
0.55, 0.22, 0.48, 0.46, 0.59, 0.89, 0.99, 0.57, 0.96, 0.97, 0.95,
0.98, 0.24, 0.89, 0.5, 0.94, 0.6, 0.41, 0.71, 0.5, 0.2, 0.96,
0.18, 0.93, 0.92, 0.85, 0.92, 0.82, 0.48, 0.62, 0.53, 0.59, 0.38,
0.8, 0.49, 0.91, 0.58, 0.94, 0.68, 0.15, 0.96, 0.98, 0.89, 0.84,
0.5, 0.88, 0.29, 0.24, 0.31, 0.29, 0.33, 0.49, 0.33, 0.76, 0.54,
0.88, 0.78, 0.26, 0.52, 0.75, 0.97, 0.93, 0.27, 0.69, 0.19, 0.69,
0.2, 0.21, 0.84, 0.31, 0.19, 0.8, 0.6, 0.19, 0.51, 0.98, 0.27,
0.39, 0.77, 0.95, 0.73, 0.28, 0.79, 0.19, 0.98, 0.77, 0.31, 0.84,
0.35, 0.19, 0.26, 0.82, 0.63, 0.38, 0.38, 0.26, 0.63, 0.65, 0.55,
0.88, 0.6, 0.71, 0.85, 0.99, 0.28, 0.42, 0.65, 0.58, 0.97, 0.35,
0.36, 0.32, 0.79, 0.68, 0.39, 0.45, 0.71, 0.98, 0.34, 0.62, 0.24,
0.55, 0.43, 0.95, 0.32, 0.6, 0.63, 0.98, 0.2, 0.31, 0.9, 0.3,
0.32, 0.37, 0.52, 0.64, 0.9, 0.22, 0.31, 0.39, 0.21, 0.93, 0.64,
0.4, 0.96, 0.31, 0.46, 0.86, 0.56, 0.99, 0.83, 0.87, 0.36, 0.59,
0.98, 0.72, 0.21, 0.52, 0.17, 0.21, 0.42, 0.97, 0.34, 0.96, 0.18,
0.63, 0.45, 0.36, 0.31, 0.48, 0.94, 0.86, 0.16, 0.32, 0.97, 0.29,
0.9, 0.38, 0.88, 0.6, 0.17, 0.19, 0.44, 0.98, 0.35, 0.36, 0.2,
0.39, 0.53, 0.35, 0.57, 0.18, 0.26, 0.17, 0.77, 0.51, 1, 0.17,
0.57, 0.48, 0.58, 0.25, 0.32, 0.33, 0.76, 0.16, 0.13, 0.46, 0.44,
0.31, 0.56, 0.46, 0.6, 0.17, 0.36, 0.34, 0.44, 0.43, 0.86, 0.86,
0.44, 0.34, 0.92, 0.32, 0.78, 0.21, 0.46, 0.92, 0.27, 0.98, 0.52,
0.34, 0.27, 0.59, 0.45, 0.58, 0.27, 0.48, 0.21, 0.24, 0.29, 0.89,
0.25, 0.33, 0.96, 0.56, 0.29, 0.97, 0.98, 0.59, 0.28, 0.22, 0.76,
0.91, 0.92, 0.91, 0.94, 0.83, 0.48, 0.53, 0.56, 0.5, 0.75, 0.4,
0.98, 0.6, 0.74, 0.66, 0.97, 0.62, 0.99, 0.39, 0.89, 0.86, 0.66,
0.92, 0.34, 0.99, 0.69, 0.71, 0.8, 0.47, 0.5, 0.83, 0.83, 0.41,
0.72, 0.98, 0.76, 0.65, 0.71, 0.9, 0.9, 1, 0.4, 0.46, 0.35, 0.72,
0.92, 0.74, 0.44, 0.67, 0.97, 0.88, 0.84, 0.71, 0.45, 0.78, 0.9,
0.72, 0.57, 0.68, 0.85, 0.84, 0.46, 0.91, 0.53, 0.96, 0.49, 0.93,
0.49, 0.37, 0.95, 0.47, 0.87, 0.49, 0.58, 0.64, 0.84, 0.8, 0.49,
0.67, 0.75, 0.44, 0.87, 0.71, 0.47, 0.46, 0.83, 0.74, 0.99, 0.86,
0.64, 0.74, 0.43, 0.44, 0.57, 0.89, 0.67, 0.59, 0.89, 0.45, 0.62,
0.81, 0.93, 0.81, 0.98, 0.95, 0.63, 0.64, 0.96, 0.55, 0.49, 0.59,
0.47, 0.42, 0.6, 0.51, 0.4, 0.3, 0.29, 0.45, 0.94, 0.29, 0.33,
0.14, 0.71, 0.41, 0.6, 0.31, 0.95, 0.94, 0.87, 0.8, 0.53, 0.66,
0.71, 0.19, 0.49, 0.97, 0.48, 0.43, 0.38, 0.4, 0.22, 0.38, 0.27,
0.25, 0.45, 0.75, 0.38, 0.23, 0.92, 0.7, 0.68, 0.17, 0.39, 0.65,
0.38, 0.39, 0.21, 0.28, 0.55, 0.89, 0.24, 0.34, 0.92, 0.31, 0.64,
0.86, 0.94, 0.28, 0.43, 0.44, 0.82, 0.23, 0.81, 0.71, 0.53, 0.96,
0.9, 0.55, 0.83, 0.64, 0.51, 0.32, 0.66, 0.45, 0.72, 0.28, 0.34,
0.98, 0.76, 0.52, 0.95, 0.83, 0.47, 0.9, 0.31, 0.23, 0.61, 0.94,
0.61, 0.42, 0.34, 0.55, 0.33, 0.93, 0.24, 0.51, 0.65, 0.17, 0.81,
0.68, 0.51, 0.78, 0.37, 0.37, 0.99, 0.94, 0.64, 0.59, 0.61, 0.9,
0.88, 0.64, 0.49, 0.09, 0.51, NA, 0.86, 0.45, 0.61, 0.24, 0.85,
0.26, 0.29, 0.21, 0.66, 0.26, 0.47, 0.19, 0.99, 0.51, 0.91, 0.37,
0.56, 0.71, 0.47, 0.44, 0.48, 0.52, 0.22, 0.52, 0.29, 0.46, 0.54,
0.94, 0.24, 0.24, 0.47, 0.37, 0.9, 0.79, 0.81, 0.41, 0.38, 0.71,
0.34, 0.46, 0.23, 0.54, 0.43, 0.85, 0.56, 0.26, 0.9, 0.25, 0.3,
0.39, 0.89, 0.38, 0.18, 0.78, 0.37, 0.45, 0.51, 0.8, 0.61, 0.52,
0.84, 0.4, 0.31, 0.28, 0.24, 0.23, 0.43, 0.77, 0.78, 0.95, 0.9,
0.81, 0.15, 0.77, 0.77, 0.87, 0.75, 0.16, 0.49, 0.23, 0.93, 0.45,
0.33, 0.75, 0.32, 0.75, 0.41, 0.24, 0.46, 0.17, 0.41, 0.45, 0.48,
0.15, 0.66, 0.53, 0.75, 0.57, 0.46, 0.78, 0.24, 0.29, 0.95, 0.77,
0.66, 0.94, 0.27, 0.29, 0.58, 0.6, 0.46, 0.58, 0.84, 0.69, 0.47,
0.45, 0.48, 0.35, 0.89, 0.98, 0.93, 0.2, 0.94, 0.91, 0.75, 0.5,
0.44, 0.69, 0.8, 0.76, 0.85, 0.84, 0.72, 0.25, 0.73, 0.26, 0.93,
0.15, 0.33, 0.3, 0.6, 0.24, 0.21, 0.28, 0.51, 0.79, 0.77, 0.85,
0.52, 0.39, 0.68, 0.83, 0.36, 0.15, 0.87, 0.55)
res1 = roc(actual_labels,app_labels)
res2= roc(actual_labels,model_info$X.st..)
The calls in the actual label class where it is "1" and have have a probablity threshold (model_info$X.st..) value more than 0.5 is named as "1" for app_labels and rest all zero
Both res1 and res2 have different values for sensitivitiy and specificity.
A ROC curve shows the sensitivity and specificity tradeoff as the decision threshold of a classifier is varied. Typically ROC curve functions expect to get the prediction and the truth value as input.
This is exactly what you do when you run:
res2= roc(actual_labels,model_info$X.st..)
However your app_labels is of a very different nature: you have already merged in the "correct classification" aspect, which makes it more like a flattened contingency table than the "predictions" the ROC function expects. So you can no longer use a regular ROC function and need to calculate the sensitivity and specificity manually.
TP <- sum(app_labels & actual_labels)
TN <- sum(app_labels & !(actual_labels))
FP <- sum(!(app_labels) & !(actual_labels))
FN <- sum(!(app_labels) & actual_labels)
# Sensitivity:
TP / (TP+FN)
# Specificity:
TN / (TN + FP)

How to get multiple sets of subset from a matrix for multiple conditions using loop?

I would like to ask a data manipulation question by R.
all_matrix<-structure(list(V1 = c(0.012, 0, 0, 0, 0.037, 0, 0, 0, 0.007, 0, 0.104, 0.149, 0.164, 0.258, 3.986, 0, 0.002, 0, 0, 0), V2 = c(0, 0.07, 0, 0, 0.017, 0, 0, 0, 0.025, 0, 2.322, 0.327, 0.134, 1.035, 2.732, 0.01, 1.097, 0.388, 0, 0), V3 = c(0, 0, 0, 0.005, 0, 0, 0, 0, 0, 0, 0.007, 0, 0, 0, 1.777, 0, 0.241, 0, 0, 0), V4 = c(0, 0, 0, 0.001, 0.003, 0, 0, 0, 0, 0, 0.207, 0.002, 0.003, 0.015, 0.032, 0, 0.007, 0, 0, 0), V5 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.001, 0, 0.101, 0, 0, 0.005, 0.01, 0, 0, 0, 0, 0), V6 = c(0, 0, 0, 0.003, 0.009, 0, 0, 0, 0.076, 0, 0.01, 0.006, 0, 0.091, 0.829, 0, 0.002, 0, 0, 0), V7 = c(0, 0, 0, 0, 0.026, 0, 0, 0, 0.351, 0, 1.849, 0.003, 0, 0.005, 0.998, 0.009, 0.18, 0, 0, 0), V8 = c(0, 0, 0.002, 0.047, 0.01, 0.003, 0, 0, 0.021, 0, 0.848, 0.007, 0.005, 0.206, 0.023, 0, 0.025, 0, 0, 0), V9 = c(0, 0, 0, 0.02, 0.013, 0, 0, 0, 0, 0, 0.008, 0, 0, 0, 0, 0, 0, 0, 0, 0), V10 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.012, 9.895, 4.362, 0, 0, 0, 0, 0), P_diff = c(-4.051, -3.769, -3.602, -7.563, -6.398, -1.816, 0.84, -1.91, 3.095, -1.544, 6.068, 16.469, 6.403, 9.9, 9.089, 18.977, 14.123, 3.103, 1.527, -2.902), PH_fold = c(0.144, 0.511, 0.686, 0.372, 0.582, 0.325, 1.312, 0.436, 1.061, 0.371, 1.119, 1.298, 1.134, 1.146, 1.123, 1.484, 1.204, 1.263, 1.843, 0.423), PNH_fold = c(6.933, 1.955, 1.459, 2.69, 1.718, 3.081, 0.762, 2.291, 0.943, 2.696, 0.894, 0.77, 0.882, 0.872, 0.891, 0.674, 0.831, 0.792, 0.543, 2.366)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "P_diff", "PH_fold", "PNH_fold"), class = "data.frame", row.names = c("S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20"))
sig1<-data.frame(subset(all_matrix, all_matrix$PH_fold >= 1.1 & all_matrix$P_diff >=1))##conditions are in range like all_matrix$PH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= 1,2,3,4......, upto 20###
sig2<-data.frame(subset(all_matrix, all_matrix$PNH_fold >= 1.1 & all_matrix$P_diff <= -1))##conditions are in range like all_matrix$PNH_fold >= 1.1,1.2,1.3,....,2 & all_matrix$P_diff >= -1,-2,-3,-4......, upto -20###
d <- function(x){sum((log(x[x>0]))*(x[x>0]))*(-1)}
sh1<-apply((sig1[,-c(5:7)]/100), 2, d)
sh2<-apply((sig2[,-c(5:7)]/100), 2, d)
count1<-apply(sig1[,-c(5:7)], 2, function(i) (sum(i > 0)))
count2<-apply(sig2[,-c(5:7)], 2, function(i) (sum(i > 0)))
G1<-(sig1*sh1)
G2<-(sig2*sh2)
G<-data.frame(G1/G2)
I would like to set a R code to calculate "G" for all subsets for all_matrix based on each condition as mentioned above.
So how can I get the subsets of a matrix using different conditions for each subset using loop for further process to calculate "G":
I want to use subset() function in loop:
Can anyone help me out?
Thank you!
I appreciate any replies!
If you have large dataframes, don't use subset, it's slower than any other option. The fastest is generally the indexing way.
I first create two indices with the conditions given. Then a list to hold the sub-dfs.
i1 <- data1$age < 15
i2 <- data1$age > 20
sub_data1 <- list()
sub_data1[['less_15']] <- data1[i1, ]
sub_data1[['over_20']] <- data1[i2, ]
Note that you can skip the indices creation code and simply do
sub_data1 <- list()
sub_data1[['less_15']] <- data1[data1$age < 15, ]
sub_data1[['over_20']] <- data1[data1$age > 20, ]

Resources