In[]:=
ResourceRemove@ResourceObject["Epidemic Data for Novel Coronavirus COVID-19"]
In[]:=
data=ResourceData["Epidemic Data for Novel Coronavirus COVID-19"];
In[]:=
(*selectthefirst20countrynames*)countries=Select[DeleteDuplicates[Normal@data[All,"Country"]],Head[#]===Entity&][[1;;20,2]];(*definedefaultcountires*)defCount=First@Position[countries,#]&/@{"China","Italy","Netherlands"};(*numberofinfectionsthatdefinestartday*)nstart=100;allDat=Transpose[((*getthedata*)count=#;country=First@Normal[data[Select[MatchQ[Entity["Country",count],#Country]&],"ConfirmedCases"]];countryD=First@Normal[data[Select[MatchQ[Entity["Country",count],#Country]&],"Deaths"]];(*getthetimesincesstartofoutbreak20-1-2020*)times=DateObject/@country["Times"];time=QuantityMagnitude[DateDifference[{2020,1,20},times]];(*getthevalues*)val=country["Values"];valD=countryD["Values"];(*makethetimerelativetothefirstdayof>100infections*)day100=FirstPosition[Unitize[valD],1]/.Missing["NotFound"]{Length[valD]};day100=FirstPosition[Unitize[Floor[val,nstart]],1]/.Missing["NotFound"]{Length[val]};time100=time-First[day100];(*fitsigmoidalmodeltoinfections*)mod=Quiet@NonlinearModelFit[Transpose[{time100,val}],{a/(1+Exp[-c(t-t0)])},{{a,3Max[val]},b,{c,.01},{t0,0}},t];bands[x_]=mod["SinglePredictionBands",ConfidenceLevel.999];(*fitsigmoidalmodeltodeaths*)modD=Quiet@NonlinearModelFit[Transpose[{time100,valD}],{a/(1+Exp[-c(t-t0)])},{{a,3Max[val]},b,{c,.01},{t0,0}},t];bandsD[x_]=mod["SinglePredictionBands",ConfidenceLevel.999];(*calculatemortalitypercentage*)mort=100.valD/(val/.0Infinity);(*giveoutput*){count{Transpose[{time100,val}],{mod,bands}},count{Transpose[{time100,valD}],{modD,bandsD}},count{Transpose[{time100,mort}]}})&/@countries];
In[]:=
(*makealegendwithselectionoptions*)xt=ConstantArray[{},20];sel=TogglerBar[Dynamic[xt[[#[[1]]]]],{#}]&/@Thread[Range[Length[countries]]countries][[1;;20]];(*startwithchinaitalyandnetherlands,defCount*)xt[[Flatten[defCount]]]=defCount;(*definetheplotfunction,linearorlogarithmic*)plotFun={LogPlot,ListLogPlot};bar1=SetterBar[Dynamic[plotFun],{{LogPlot,ListLogPlot}->" Logarithmic ",{Plot,ListPlot}->" Linear "}];(*selectwhattoplot*)n=2;bar2=SetterBar[Dynamic[n],{1->" Infected ",2->" Deaths ",3" Mortality "}];(*defineplotscalingbar*)max=1;maxBar=SetterBar[Dynamic[max],{100,500,1000,5000,10000,50000,100000,1" Auto "}];(*definetheplotlabelasaselectionbar*)lab=Column[{Row[{"Method:",bar1," Data:",bar2}," "],Row[{"Scale:",maxBar}," "]},AlignmentCenter];Dynamic[(*selectionofcountries*)xx=Flatten[xt];(*gettheplotdata*)If[n3,mort=If[MemberQ[xx,#],allDat[[n,#,2,1]],{{0,0}}]&/@Range[20];mortc=Flatten[DeleteCases[mort,{{0,0}}],1];mortr=Range@@MinMax[mortc[[All,1]]];mortq=Thread[{mortr,GaussianFilter[#,3]}]&/@Transpose[(r=#;Quantile[Select[mortc,#[[1]]r&][[All,2]],{0.05,.5,.95}])&/@mortr];,fun=If[MemberQ[xx,#],allDat[[n,#,2,2,1]],{0}]&/@Range[20];dat=If[MemberQ[xx,#],allDat[[n,#,2,1]],{{0,0}}]&/@Range[20];];(*findtheplotscaling*)sc=If[plotFun[[1]]===LogPlot,12,1.2];maxp=If[max===1,scMax[dat[[All,All,2]]],max];(*maketheplots*)Show[If[n3,(*mortality*){ListLinePlot[mortq,PlotRange{{-5,50},{0,10}},PlotStyle{Directive[Dashed,Lighter@Gray],Black,Directive[Dashed,Lighter@Gray]},Filling{1{3}},ImageSize600,GridLinesAutomatic],ListLinePlot[mort,PlotRange{{-5,50},{0,10}},PlotLegendssel]}(*infectedordeath*),{plotFun[[2]][dat,PlotRange{{-5,50},{0.5,maxp}},ImageSize600,PlotLegendssel,JoinedTrue,GridLinesAutomatic],plotFun[[1]][Evaluate[#[t]&/@fun],{t,-50,150},PlotRange{{-5,50},Full},ImageSize600,PlotStyle->Dashed]}],FrameStyleDirective[Thick],LabelStyleDirective[{Bold,14,Black}],PlotLabellab,AxesFalse,FrameTrue,FrameLabel{"Days since number of infections is > "<>ToString[nstart],{"Number of confirmed infections","Number of confirmed deaths","Mortality percentage of confirmed cases"}[[n]]}]]