2
$\begingroup$

This code works as expected for 30 countries:

DateListLogPlot[Callout[Tooltip[10^5 #2/#1["Population"], CommonName[#1]], #1] & @@@ 
 Normal@ResourceData["Epidemic Data for Novel Coronavirus COVID-19", "WorldCountries"][
 TakeLargestBy[#ConfirmedCases["LastValue"] &, 30]][All, {#Country, #ConfirmedCases} &], 
 Sequence[PlotRange -> All, GridLines -> Automatic, AspectRatio -> 1.5, ImageSize -> 500, 
 PlotLabel -> "estimated confirmed cases per 100K people (log \scale)"]]

I wish to construct the plot for a set of selected countries:

cList={"Japan","France","Brazil","UnitedKingdom"};

EDIT

Using the following Manipulate, I can get COVID-19 confirmed cases across countries and plot the moving average time series of daily confirmed cases across countries:

(*Single country analysis: most recent observation and plot of time series Confirmed Cases*)

ResourceRemove[ResourceObject["Epidemic Data for Novel Coronavirus COVID-19"]];
covid = ResourceData[ResourceObject["Epidemic Data for Novel Coronavirus COVID-19"]];
cList={"Japan","France","Brazil","UnitedKingdom"};

Manipulate[
 Row[{
   ListLinePlot[
    covid[
      SelectFirst[#Country == 
         Entity["Country", cList[[country]]] &], {"ConfirmedCases"}][
     All, MovingAverage[Differences[#], Quantity[1, "Weeks"]] &], 
    Joined -> True, PlotRange -> All, ImageSize -> 400
    ],
   Last[N[
     Normal[covid[
          SelectFirst[#Country == 
             Entity["Country", 
              cList[[country]]] &], {"ConfirmedCases"}][All, 
         MovingAverage[Differences[#], 
           Quantity[1, "Weeks"]] &][[1]]][[All, 2]]]]
   }],
 {{country, 1, "country : "}, Thread[Range[Length[cList]] -> cList], 
  ControlType -> PopupMenu},
 TrackedSymbols :> {country},
 FrameLabel -> {{"", ""}, {"", 
    Style["Time series of confirmed cases and the most recent 
    observation", Larger, Bold, Black]}}
 ]

In my actual list of countries, there are four countries with state-level data. I developed (Thanks to @LouisB) the following Manipulate that extracts the total state-level data:

 cListext={"Unitedstates", "Australia", "Canada", "China"};
 Manipulate[
 case = Entity["Country", cListext[[country]]];
 data = covid[
     Select[(#Country == case && 
         Not[Head[#AdministrativeDivision] === Missing]) &]][[All, 
     4]] // Total;
 Row[{
   DateListPlot[
      MovingAverage[Differences[data], Quantity[1, "Weeks"]], 
      ImageSize -> 400, PlotRange -> {All, All}, 
      PlotLabel -> case["Name"], 
      GridLines -> True] & /@ {"ConfirmedCases"},
   Last[N[
     Normal@MovingAverage[Differences[data], 
          Quantity[1, "Weeks"]] &[[1]][[All, 2]]]]
   }],
 {{country, 1, "country : "}, 
  Thread[Range[Length[cListext]] -> cListext], 
  ControlType -> PopupMenu},
 TrackedSymbols :> {country},
 FrameLabel -> {{"", ""}, {"", Style["Aggregating cumulative state data into country-level data", Larger, Bold, Black]}}
 ]

Both of these Manipulate codes work as expected. My goal is to combine the two Manipulate codes in a single Manipulate and be able to get times series country-level daily confirmed cases, and construct a table with two columns: {cListCombined, "very last observation of confirmed cases"}

cListCombined={cList, cListext}//Flatten

EDIT 2

Instead of:

covid = ResourceData[ResourceObject["Epidemic Data for Novel Coronavirus COVID-19"]];

if I use WorldCountries in extracting the data:

covid = ResourceData["Epidemic Data for Novel Coronavirus COVID-19", 
   "WorldCountries"];

the first Manipulate operation performs what I aim to, and no need for the second Manipulate (pointed out by @LouisB).

$\endgroup$
9
  • 1
    $\begingroup$ Instead of TakeLargestBy[#ConfirmedCases["LastValue"] &, 30] try Select[MemberQ[Entity["Country", #] & /@ cList, #Country] &] $\endgroup$
    – LouisB
    Commented Apr 28, 2021 at 4:40
  • $\begingroup$ @LouisB: Thanks for your help. It works. I like to accept it if you post it as an answer. $\endgroup$ Commented Apr 28, 2021 at 13:33
  • $\begingroup$ @LouisB: I wonder if you can show me how to get the "Country" level data only. I face a problem with countries having various states like the USA, China, etc. I do not want to get state-level data. I just need country-level total numbers of COVID. $\endgroup$ Commented Apr 28, 2021 at 17:12
  • $\begingroup$ Yes, I will look at the country-level problem. $\endgroup$
    – LouisB
    Commented Apr 28, 2021 at 17:31
  • 1
    $\begingroup$ To my knowledge, the best way to get country-level data is to use "WorldCountries", as you have done in your question. I don't understand how you are getting state-level data, if you are using "WorldCountries". Please consider adding a brief example of the state-level versus country-level problem to your question. Also, my previous answer was rather uninformed, since I did not know about "WorldCountries" at that time. $\endgroup$
    – LouisB
    Commented Apr 28, 2021 at 20:07

1 Answer 1

1
$\begingroup$

Here is my answer to my own question above.

ResourceRemove[ResourceObject["Epidemic Data for Novel Coronavirus COVID-19"]];
covid = ResourceData["Epidemic Data for Novel Coronavirus COVID-19", "WorldCountries"];

allcountries = {"Afghanistan", "AlandIslands", "Albania", "Algeria", 
   "AmericanSamoa", "Andorra", "Angola", "Anguilla", "Antarctica", 
   "AntiguaBarbuda", "Argentina", "Armenia", "Aruba", "Australia", 
   "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", 
   "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", 
   "Bhutan", "Bolivia", "BonaireSintEustatiusAndSaba", 
   "BosniaHerzegovina", "Botswana", "BouvetIsland", "Brazil", 
   "BritishIndianOceanTerritory", "BritishVirginIslands", "Brunei", 
   "Bulgaria", "BurkinaFaso", "Burundi", "Cambodia", "Cameroon", 
   "Canada", "CapeVerde", "CaymanIslands", "CentralAfricanRepublic", 
   "Chad", "Chile", "China", "ChristmasIsland", "CocosKeelingIslands",
    "Colombia", "Comoros", "CookIslands", "CostaRica", "Croatia", 
   "Cuba", "Curacao", "Cyprus", "CzechRepublic", 
   "DemocraticRepublicCongo", "Denmark", "Djibouti", "Dominica", 
   "DominicanRepublic", "EastTimor", "Ecuador", "Egypt", "ElSalvador",
    "EquatorialGuinea", "Eritrea", "Estonia", "Ethiopia", 
   "FalklandIslands", "FaroeIslands", "Fiji", "Finland", "France", 
   "FrenchGuiana", "FrenchPolynesia", 
   "FrenchSouthernAndAntarcticLands", "Gabon", "Gambia", "GazaStrip", 
   "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", 
   "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea",
    "GuineaBissau", "Guyana", "Haiti", "Honduras", "HongKong", 
   "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", 
   "Ireland", "IsleOfMan", "Israel", "Italy", "IvoryCoast", "Jamaica",
    "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", 
   "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", 
   "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", 
   "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", 
   "Malaysia", "Maldives", "Mali", "Malta", "MarshallIslands", 
   "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", 
   "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", 
   "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", 
   "Nauru", "Nepal", "Netherlands", "NewCaledonia", "NewZealand", 
   "Nicaragua", "Niger", "Nigeria", "Niue", "NorfolkIsland", 
   "NorthKorea", "NorthernMarianaIslands", "Norway", "Oman", 
   "Pakistan", "Palau", "Panama", "PapuaNewGuinea", "Paraguay", 
   "Peru", "Philippines", "PitcairnIslands", "Poland", "Portugal", 
   "PuertoRico", "Qatar", "RepublicCongo", "Reunion", "Romania", 
   "Russia", "Rwanda", "SaintBarthelemy", "SaintHelena", 
   "SaintKittsNevis", "SaintLucia", "SaintMartin", 
   "SaintPierreMiquelon", "SaintVincentGrenadines", "Samoa", 
   "SanMarino", "SaoTomePrincipe", "SaudiArabia", "Senegal", "Serbia",
    "Seychelles", "SierraLeone", "Singapore", "SintMaarten", 
   "Slovakia", "Slovenia", "SolomonIslands", "Somalia", "SouthAfrica",
    "SouthGeorgiaAndTheSouthSandwichIslands", "SouthKorea", 
   "SouthSudan", "Spain", "SriLanka", "Sudan", "Suriname", "Svalbard",
    "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", 
   "Tajikistan", "Tanzania", "Thailand", "Togo", "Tokelau", "Tonga", 
   "TrinidadTobago", "Tunisia", "Turkey", "Turkmenistan", 
   "TurksCaicosIslands", "Tuvalu", "Uganda", "Ukraine", 
   "UnitedArabEmirates", "UnitedKingdom", "UnitedStates", 
   "UnitedStatesMinorOutlyingIslands", "UnitedStatesVirginIslands", 
   "Uruguay", "Uzbekistan", "Vanuatu", "VaticanCity", "Venezuela", 
   "Vietnam", "WallisFutuna", "WestBank", "WesternSahara", "Yemen", 
   "Zambia", "Zimbabwe"};

cList = {"Australia", "Austria", "Belgium", "Canada", "Chile", 
   "CzechRepublic", "Denmark", "Estonia", "Finland", "France", 
   "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Israel", 
   "Italy", "Japan", "SouthKorea", "Latvia", "Lithuania", 
   "Luxembourg", "Netherlands", "NewZealand", "Norway", "Poland", 
   "Portugal", "Slovakia", "Slovenia", "Spain", "Sweden", 
   "Switzerland", "Turkey", "UnitedKingdom", "UnitedStates", 
   "Argentina", "Brazil", "Brunei", "Bulgaria", "Cambodia", 
   "Colombia", "CostaRica", "Croatia", "Cyprus", "India", "Indonesia",
    "HongKong", "Kazakhstan", "Malaysia", "Malta", "Morocco", "Peru", 
   "Philippines", "Romania", "Russia", "SaudiArabia", "Singapore", 
   "SouthAfrica", "Taiwan", "Thailand", "Tunisia", "Vietnam", 
   "Mexico", "China"};

RoW = {"Afghanistan", "AlandIslands", "Albania", "Algeria", 
   "AmericanSamoa", "Andorra", "Angola", "Anguilla", "Antarctica", 
   "AntiguaBarbuda", "Armenia", "Aruba", "Azerbaijan", "Bahamas", 
   "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belize", "Benin", 
   "Bermuda", "Bhutan", "Bolivia", "BonaireSintEustatiusAndSaba", 
   "BosniaHerzegovina", "Botswana", "BouvetIsland", 
   "BritishIndianOceanTerritory", "BritishVirginIslands", 
   "BurkinaFaso", "Burundi", "Cameroon", "CapeVerde", "CaymanIslands",
    "CentralAfricanRepublic", "Chad", "ChristmasIsland", 
   "CocosKeelingIslands", "Comoros", "CookIslands", "Cuba", "Curacao",
    "DemocraticRepublicCongo", "Djibouti", "Dominica", 
   "DominicanRepublic", "EastTimor", "Ecuador", "Egypt", "ElSalvador",
    "EquatorialGuinea", "Eritrea", "Ethiopia", "FalklandIslands", 
   "FaroeIslands", "Fiji", "FrenchGuiana", "FrenchPolynesia", 
   "FrenchSouthernAndAntarcticLands", "Gabon", "Gambia", "GazaStrip", 
   "Georgia", "Ghana", "Gibraltar", "Greenland", "Grenada", 
   "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", 
   "GuineaBissau", "Guyana", "Haiti", "Honduras", "Iran", "Iraq", 
   "IsleOfMan", "IvoryCoast", "Jamaica", "Jersey", "Jordan", "Kenya", 
   "Kiribati", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Lebanon", 
   "Lesotho", "Liberia", "Libya", "Liechtenstein", "Macau", 
   "Macedonia", "Madagascar", "Malawi", "Maldives", "Mali", 
   "MarshallIslands", "Martinique", "Mauritania", "Mauritius", 
   "Mayotte", "Micronesia", "Moldova", "Monaco", "Mongolia", 
   "Montenegro", "Montserrat", "Mozambique", "Myanmar", "Namibia", 
   "Nauru", "Nepal", "NewCaledonia", "Nicaragua", "Niger", "Nigeria", 
   "Niue", "NorfolkIsland", "NorthernMarianaIslands", "NorthKorea", 
   "Oman", "Pakistan", "Palau", "Panama", "PapuaNewGuinea", 
   "Paraguay", "PitcairnIslands", "PuertoRico", "Qatar", 
   "RepublicCongo", "Reunion", "Rwanda", "SaintBarthelemy", 
   "SaintHelena", "SaintKittsNevis", "SaintLucia", "SaintMartin", 
   "SaintPierreMiquelon", "SaintVincentGrenadines", "Samoa", 
   "SanMarino", "SaoTomePrincipe", "Senegal", "Serbia", "Seychelles", 
   "SierraLeone", "SintMaarten", "SolomonIslands", "Somalia", 
   "SouthGeorgiaAndTheSouthSandwichIslands", "SouthSudan", "SriLanka",
    "Sudan", "Suriname", "Svalbard", "Swaziland", "Syria", 
   "Tajikistan", "Tanzania", "Togo", "Tokelau", "Tonga", 
   "TrinidadTobago", "Turkmenistan", "TurksCaicosIslands", "Tuvalu", 
   "Uganda", "Ukraine", "UnitedArabEmirates", 
   "UnitedStatesMinorOutlyingIslands", "UnitedStatesVirginIslands", 
   "Uruguay", "Uzbekistan", "Vanuatu", "VaticanCity", "Venezuela", 
   "WallisFutuna", "WestBank", "WesternSahara", "Yemen", "Zambia", 
   "Zimbabwe"};

(*final demand adjustment function*)
delta[infBar_, delBar_, infObs_] := Piecewise[{{1, infObs <= 0.1*infBar},
    {delBar*(1 + (infObs/infBar - 0.1))/(delBar + (infObs/infBar - 0.1)), 
    infObs > 0.1*infBar} }];

(*Rest of the World COVID-19 Confirmed Cases*)
bir = Manipulate[
   Module[
    {tsRoW, aggRoW, maRoW},
    tsRoW = 
     Normal@covid[
         Select[MemberQ[Entity["Country", #] & /@ RoW, #Country] &]][
        All, {#Country, #ConfirmedCases} &][[All, 2]];
    aggRoW = Normal[TimeSeriesThread[Total, tsRoW]][[All, 2]];
    maRoW = 
     Select[MovingAverage[Differences[aggRoW], 7] // N, 
      FreeQ[#, _Missing] &];
    {
     ListLinePlot[maRoW, Joined -> True, ImageSize -> 400, 
      PlotRange -> All, Filling -> Axis, GridLines -> Automatic],
     delta[infBar, delBar, #] & /@ maRoW // 
      Mean     (*annual FD adjustment factor*)
     }
    ],
   Spacer[40],
   Delimiter,
   {{infBar, 20000, "infection threshold : "}, 5000, 100000, 5000},
   {{delBar, .65, "max rate of reduction in demand : "}, 0, 1, .05},
   TrackedSymbols :> {infBar, delBar},
   FrameLabel -> {{"", ""}, {"", 
      Style["Time series of confirmed cases in the RoW and demand \
adjustment factor", Larger, Bold, Black]}}
   ];

(*Single country most recent data on confirmed cases*)
iki = Manipulate[
   Module[
    {veri, maveri, infObs},
    veri = 
     covid[SelectFirst[#Country == 
         Entity["Country", cList[[country]]] &], {"ConfirmedCases"}];
    maveri = 
     veri[All, MovingAverage[Differences[#], Quantity[1, "Weeks"]] &];
    Row[{
      DateListPlot[maveri, Joined -> True, PlotRange -> All, 
       ImageSize -> 400, Filling -> Axis, GridLines -> Automatic],
      {
       infObs = 
        Select[N[Normal[maveri[[1]]][[All, 2]]], FreeQ[#, _Missing] &];
       delta[infBar, delBar, #] & /@ infObs // 
        Mean     (*annual FD adjustment factor*)
       }
      }]
    ],
   Spacer[40],
   Delimiter,
   {{country, 1, "country : "}, Thread[Range[Length[cList]] -> cList],
     ControlType -> PopupMenu},
   {{infBar, 20000, "infection threshold : "}, 5000, 100000, 5000},
   {{delBar, .65, "max rate of reduction in demand : "}, 0, 1, .05},
   TrackedSymbols :> {country, infBar, delBar},
   FrameLabel -> {{"", ""}, {"", 
      Style["Time series of confirmed cases across countries and \
demand adjustment factor", Larger, Bold, Black]}}
   ];

(*Final demand adjustment parameters across countries*)
Manipulate[
 Do[
  veri = covid[
    SelectFirst[#Country == 
       Entity["Country", cList[[i]]] &], {"ConfirmedCases"}];
  maveri = 
   veri[All, MovingAverage[Differences[#], Quantity[1, "Weeks"]] &];
  out = Select[N@Flatten@Normal[maveri][[All, 2]][[1, 1]], 
    FreeQ[#, _Missing] &];
  del[i] = delta[infBar, delBar, #] & /@ out // Mean, {i, 
   Length[cList]}
  ];
 adjFact = Table[{cList[[i]], del[i]}, {i, Length[cList]}];
 TableForm[adjFact, TableHeadings -> {None, {"", "Adj-Factor"}}],
 Spacer[40],
 Delimiter,
 {{infBar, 20000, "infection threshold : "}, 5000, 100000, 5000},
 {{delBar, .65, "max rate of reduction in demand : "}, 0, 1, .05},
 TrackedSymbols :> {infBar, delBar},
 FrameLabel -> {{"", ""}, {"", 
    Style["Final demand adjustment parameters across countries", 
     Larger, Bold, Black]}}
 ];

With the above code one can do the following 3 tasks:

  1. Time series of the daily 7-day moving average of confirmed infections across 64 countries (rest of the world is calculated as a separate country);

  2. The country-level final demand adjustment parameter is calculated for each country; and

  3. Every time this code is run, the most recent time series and final demand adjustment parameters are automatically calculated.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.