With the following code, I can calculate percentage changes in selected indicators over time. The code is not efficient nor practical to integrate it into Manipulate
function I already have.
pch[x1_, x2_] := Quantity[((x2 - x1)/x1)*100, "Percent"];
jdata[GroupBy["country"], GroupBy["time"], All, {"EVI"}]["Bhutan"] // Normal
jdata[GroupBy["country"], GroupBy["time"], All, {"Exposure index"}]["Bhutan"] // Normal
extracts the data for "EVI" and "Exposure index" indicators for Bhutan.
<|2012.` -> {<|"EVI" -> 44.19255725212609`|>},
2015.` -> {<|"EVI" -> 40.19403209871271`|>},
2018.` -> {<|"EVI" -> 36.26105903025079`|>},
2021.` -> {<|"EVI" -> 25.73725117803039`|>}|>
<|2012.` -> {<|"Exposure index" -> 42.47413422316401`|>},
2015.` -> {<|"Exposure index" -> 38.87846003288048`|>},
2018.` -> {<|"Exposure index" -> 39.04960632190836`|>},
2021.` -> {<|"Exposure index" ->
Missing["KeyAbsent", "Exposure index"]|>}|>
I then calculate the percentage change using:
(* percentage change in "EVI" in Bhutan *)
x1 = jdata[GroupBy["country"], GroupBy["time"], All, {"EVI"}][
"Bhutan", First, 1, "EVI"]
x2 = jdata[GroupBy["country"], GroupBy["time"], All, {"EVI"}][
"Bhutan", Last, 1, "EVI"]
pch[x1, x2]
which generates:
Quantity[-41.7611, "Percent"]
I repeat the same operation for "Exposure index" indicator:
(* percentage change in "Exposure index" in Bhutan *)
x1 = jdata[GroupBy["country"], GroupBy["time"],
All, {"Exposure index"}]["Bhutan", First, 1, "Exposure index"]
x2 = jdata[GroupBy["country"], GroupBy["time"], All, {"Exposure index"}]["Bhutan", Last, 1, "Exposure index"]
pch[x1, x2]
This does not work because the last element of the indicator is missing:
2.35437 (-42.4741 + Missing["KeyAbsent", "Exposure index"])
My questions/requests:
I like to make the percentage change calculations automatically not in the way I illustrated above because I have many indicators to calculate percentage change over time. If possible, calculate the percentage changes for all indicators, not one by one, as I did above.
On the way, I encounter a
Missing[Key]
problem because sometimes the first and sometimes the last year's data are missing. In that case, I want to calculate the percentage change over the existing data.My
code
is not efficient and I would like to have a better code to calculate all the percentage changes in all the indicators in the dataset at once.
Any help?