Eurostat data statistics: total deaths by age group
The objective is to explore total number of deaths data all cases included provided by Eurostat website https://ec.europa.eu/eurostat
and to compare 2020 covid epidemic year with previous years.
Data is limited to European countries and the granularity is at country, week , sex , agegroup level.
The exercise introduces some new SQL functions like pivot , unpivot for wrangling and visualise data.
In order to load SQL functions evaluate “Main Code” cell from SQL_Operators post
https://community.wolfram.com/groups/-/m/t/2195893
and to compare 2020 covid epidemic year with previous years.
Data is limited to European countries and the granularity is at country, week , sex , agegroup level.
The exercise introduces some new SQL functions like pivot , unpivot for wrangling and visualise data.
In order to load SQL functions evaluate “Main Code” cell from SQL_Operators post
https://community.wolfram.com/groups/-/m/t/2195893
In[]:=
tbtotaldeaths=Import["https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/demo_r_mwk_05.tsv.gz","TSV"];
The initial structure is in TSV format with week columns transposed “age,sex,unit,geo\\time”,”2021W99”,”2021W08”,”2021W07”,”2021W06”,”2021W05”,”2021W04...”
I do some data transformation to get to a normalise structure like “age, sex, geo, time, value”
I do some data transformation to get to a normalise structure like “age, sex, geo, time, value”
tbtotaldeaths[[1]]//Take[#,10]&
Out[]=
{age,sex,unit,geo\time,2021W99,2021W08,2021W07,2021W06,2021W05,2021W04,2021W03,2021W02,2021W01}
(*getthelistoftransposedweekcolumns*)colsweeks=tbtotaldeaths[[1]]//Cases[#,x_String/;StringMatchQ[x,RegularExpression["^\\d{4}\\w*"]]"A"<>x]&;colsweeks//Take[#,10]&
Out[]=
{A2021W99,A2021W08,A2021W07,A2021W06,A2021W05,A2021W04,A2021W03,A2021W02,A2021W01,A2020W99}
(*maindatatransformationfromsourcestructure{"age,sex,unit,geo\\time","2021W99","2021W06","2021W05","2021W04"....}{age,sex,unit,geo,time,value}*)tbeurostatdeaths=tableSQL[tbtotaldeaths//Rest,Join[{"fixedcols"},colsweeks]]//unpivotSQL[#,Symbol/@colsweeks//Evaluate,{"week","value"}]&//(*unpivotweekcolumnstorowvalues*)whereSQL[#,!StringContainsQ[value//ToString,":"]]&//(*filteroutinvalidvalues*)rowMapSQL[#,{fixedcols},StringSplit[#,","]&]&//(*whereSQL[#,rowNumber<100000]&//*)rowTransformSQL[#,Association["age"fixedcols[[1]], "sex"fixedcols[[2]], "unit"fixedcols[[3]], "geo"fixedcols[[4]], "time"(DateObject[{week,{"A","Year","W","Week"}}]),(*convertsweekformattoDate.Thisisverytimeconsuming5minforaprox2Mrows*) "value"ReplaceAll[value,x_StringStringReplace[x,RegularExpression["(\\d+)\\s*p"]"$1", RegularExpression[":"]"0"]//ToExpression] ]]&;//AbsoluteTiming
Out[]=
{493.,Null}
Explore data and display as dataset
tbeurostatdeaths//whereSQL[#,sex"T"&&geo"UK"&&age"TOTAL"]&//(*orderBySQL[#,{-AbsoluteTime[time]}]&//*)tableSQLAsDataset
Out[]=
(*helpertabletogetcountrynamesfromcodes*)tbrefgeo=tbeurostatdeaths//projectSQL[#,{geo}]&//(*distinctvalues*)rowTransformSQL[#,Association["countrycode"geo, "country"Check[CountryData[geo,"Name"],geo]]]&;tbrefgeo//showTableSQL
Total Rows:37 Elapsed:1.×
-7
10
Out[]//TableForm=
countrycode | country |
AD | Andorra |
AL | Albania |
AM | Armenia |
AT | Austria |
BE | Belgium |
BG | Bulgaria |
CH | Switzerland |
CY | Cyprus |
CZ | Czech Republic |
DE | Germany |
Prepare raw data for reporting (aggregation , timeseries)
(*finalresultstable*)tbts=tbeurostatdeaths//whereSQL[#,sex"T"&&AbsoluteTime[time]>(DateObject[{2014,1,1}]//AbsoluteTime)]&//groupBySQL[#,{geo,age}]&//summarySQL[#,Association["ts"TimeSeries[value,{time}]]]&//joinSQL[#,tbrefgeo,geo==countrycode]&//(*jointotbrefgeotogetthecountryname*)selectSQL[#,{country,age,ts}]&;//AbsoluteTimingtbts//showTableSQL
Out[]=
{15.2285,Null}
Total Rows:769 Elapsed:3.×
-7
10
Out[]//TableForm=
country | age | ts |
Andorra | TOTAL | TimeSeries |
Albania | TOTAL | TimeSeries |
Armenia | TOTAL | TimeSeries |
Austria | TOTAL | TimeSeries |
Belgium | TOTAL | TimeSeries |
Bulgaria | TOTAL | TimeSeries |
Switzerland | TOTAL | TimeSeries |
Cyprus | TOTAL | TimeSeries |
Czech Republic | TOTAL | TimeSeries |
Germany | TOTAL | TimeSeries |
Plot by country /age group
In[]:=
(*customfunction*)tsTakeLargest[ts_TemporalData,n_Integer]:=Block[{min=Min[TakeLargest[ts//Values,n]]}, Cases[ts//Normal,{t_,v_}/;v≥min]//TimeSeries ];plotcustomts[ts_TemporalData]:=Block[{tsmax=ts//tsTakeLargest[#,1]&//Cases[#//Normal,{t_,v_}Callout[{t,v},DateString[t,{"Year","MonthNameShort"}],Above]]&}, {ts,tsmax}// DateListPlot[#,PlotRangeAll, Joined{True,False},PlotStyle{Automatic,{Red,PointSize[0.02]}},PlotLegends{None,"Max"}, ImageSizeMedium]& ];
Build a small interactive app using manipulate
Multiple selections on country/agegroup
Multiple selections on country/agegroup
Prepare raw data for reporting on multi year plots (aggregate by year , geo, age)
Plot by country/ age group with multi lines for years and one year span on time axes
I define a criteria True/False based on wether the max deaths is before year 2020 for given age group.
Explore the age groups more affected by 2020 covid epidemic
Explore the age groups more affected by 2020 covid epidemic

