{"id":147,"date":"2021-01-26T07:49:02","date_gmt":"2021-01-26T06:49:02","guid":{"rendered":"https:\/\/lorentzen.ch\/?p=147"},"modified":"2021-01-29T07:48:54","modified_gmt":"2021-01-29T06:48:54","slug":"covid-19-deaths-per-mio","status":"publish","type":"post","link":"https:\/\/lorentzen.ch\/index.php\/2021\/01\/26\/covid-19-deaths-per-mio\/","title":{"rendered":"Covid-19 Deaths per Mio"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Lost in Translation between R and Python 2<\/h1>\n\n\n\n<p>Hello again!<\/p>\n\n\n\n<p>This is the next article in our series <strong>&#8220;Lost in Translation between R and Python&#8221;<\/strong>. The aim of this series is to provide high-quality R <strong>and<\/strong> Python 3 code to achieve some non-trivial tasks. If you are to learn R, check out the R tab below. Similarly, if you are to learn Python, the Python tab will be your friend.<\/p>\n\n\n\n<p>Post 1: <a href=\"https:\/\/lorentzen.ch\/index.php\/2021\/01\/07\/illustrating-the-central-limit-theorem\/\">https:\/\/lorentzen.ch\/index.php\/2021\/01\/07\/illustrating-the-central-limit-theorem\/<\/a><\/p>\n\n\n\n<p>In Post 2, we use a publicly available data of the European Centre for Disease Prevention and Control to calculate Covid-19 deaths per Mio persons over time and across countries . We will use slim Python and R codes to <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>fetch the data directly from the internet,<\/li><li>prepare and restructure it for plotting and<\/li><li>plot a curve per selected country.<\/li><\/ul>\n\n\n\n<p><em>Note that different countries use different definitions of whom to count as Covid-19 death and these definitions might also have changed over time. So be careful with comparisons!<\/em><\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" style=\"\" id=\"ub-tabbed-content-block-\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content--tab-0\" aria-controls=\"ub-tabbed-content--panel-0\" aria-selected=\"false\" class=\"wp-block-ub-tabbed-content-tab-title-wrap\" style=\"--ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><div role=\"tab\" id=\"ub-tabbed-content--tab-1\" aria-controls=\"ub-tabbed-content--panel-1\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">Python<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap ub-hide\" id=\"ub-tabbed-content-1220209b-dc35-4ec0-b827-78f0555418b9-panel-0\" aria-labelledby=\"ub-tabbed-content-1220209b-dc35-4ec0-b827-78f0555418b9-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>library(tidyverse)\n\n# Source and countries\nlink &lt;- \"https:\/\/opendata.ecdc.europa.eu\/covid19\/casedistribution\/csv\"\ncountries &lt;- c(\"Switzerland\", \"United_States_of_America\", \n               \"Germany\", \"Sweden\")\n\n# Import\ndf0 &lt;- read_csv(link)\n\n# Data prep\ndf &lt;- df0 %&gt;%\n  mutate(Date = lubridate::dmy(dateRep),\n         Deaths = deaths_weekly \/ (popData2019 \/ 1e6))  %&gt;%\n  rename(Country = countriesAndTerritories) %&gt;%\n  filter(Date &gt;= \"2020-03-01\",\n         Country %in% countries)\n\n# Plot\nggplot(df, aes(x = Date, y = Deaths, color = Country)) +\n  geom_line(size = 1) +\n  ylab(\"Weekly deaths per Mio\") +\n  theme(legend.position = c(0.2, 0.85))<\/pre><\/div>\n\n<\/div><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-1220209b-dc35-4ec0-b827-78f0555418b9-panel-1\" aria-labelledby=\"ub-tabbed-content-1220209b-dc35-4ec0-b827-78f0555418b9-tab-1\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"python\",\"mime\":\"text\/x-python\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"Python\",\"maxHeight\":\"400px\",\"modeName\":\"python\"}'>import pandas as pd\n\n# Source and countries\nurl = \"https:\/\/opendata.ecdc.europa.eu\/covid19\/casedistribution\/csv\"\ncountries = [\"Switzerland\", \"United_States_of_America\", \n             \"Germany\", \"Sweden\"]\n\n# Fetch data\ndf0 = pd.read_csv(url)\n# df0.head()\n\n# Prepare data\ndf = df0.assign(\n    Date=lambda x: pd.to_datetime(x[\"dateRep\"], format=\"%d\/%m\/%Y\"),\n    Deaths=lambda x: x[\"deaths_weekly\"] \/ x[\"popData2019\"] * 1e6,\n).rename(columns={\"countriesAndTerritories\": \"Country\"})\ndf = df.loc[\n    (df[\"Country\"].isin(countries)) &amp; (df[\"Date\"] &gt;= \"2020-03-01\"),\n    [\"Country\", \"Date\", \"Deaths\"],\n]\ndf = df.pivot(index=\"Date\", columns=\"Country\")\ndf = df.droplevel(0, axis=1)\n\n# Plot\nax = df.plot()\nax.set_ylabel('Weekly Covid-19 deaths per Mio');<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"954\" height=\"675\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2021\/01\/deaths_per_mio-1.png\" alt=\"\" class=\"wp-image-200\" srcset=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2021\/01\/deaths_per_mio-1.png 954w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2021\/01\/deaths_per_mio-1-300x212.png 300w, https:\/\/lorentzen.ch\/wp-content\/uploads\/2021\/01\/deaths_per_mio-1-768x543.png 768w\" sizes=\"auto, (max-width: 954px) 100vw, 954px\" \/><figcaption>Weekly Covid-19 deaths per Mio inhabitants as per January 26, 2021 (Python output).<\/figcaption><\/figure>\n\n\n\n<p>The code can be found on <a href=\"https:\/\/github.com\/mayer79\/covid\"><\/a><a href=\"https:\/\/github.com\/mayer79\/covid\">https:\/\/github.com\/mayer79\/covid<\/a> with some other analyses regarding viruses.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>For more feed, visit<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"http:\/\/www.r-bloggers.com\/\" target=\"_blank\">http:\/\/www.R-bloggers.com<\/a> <\/li><li><a href=\"https:\/\/python-bloggers.com\">https:\/\/www.python-bloggers.com<\/a><\/li><\/ul>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;R <-> Python&#8221; continued&#8230; How are normalized<br \/>\nCovid-19 deaths developing across country?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[10,6,5],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-statistics","tag-lost-in-translation","tag-python","tag-r"],"featured_image_src":null,"author_info":{"display_name":"Michael Mayer","author_link":"https:\/\/lorentzen.ch\/index.php\/author\/michael\/"},"_links":{"self":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":28,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":626,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/626"}],"wp:attachment":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}