Skip to content

Instantly share code, notes, and snippets.

@gka

gka/.gitignore

Last active Sep 22, 2020
Embed
What would you like to do?
covid19 berlin cases
.Rhistory
.Rproj.user
covid19-berlin.Rproj
.Renviron
date new new.deaths new.avg pred
2020-03-01 - - - -
2020-03-02 2 - - 35.39561364079998
2020-03-03 3 - - 41.43811900273269
2020-03-04 6 - - 47.33769769377706
2020-03-06 7 - 8.142857142857142 53.07894256760853
2020-03-07 9 - 16.428571428571427 58.64644647790257
2020-03-08 20 - 21.714285714285715 64.02480227833462
2020-03-10 10 - 35.857142857142854 69.19860282258013
2020-03-11 60 - 37.714285714285715 74.15244096431454
2020-03-13 40 - 43.42857142857143 78.87090955721331
2020-03-14 105 - 47.857142857142854 83.33860145495186
2020-03-15 20 - 65.85714285714286 87.54010951120564
2020-03-16 49 - 81.42857142857143 91.46002657965015
2020-03-17 51 - 101.42857142857143 95.08294551396081
2020-03-18 136 - 108.85714285714286 98.4289810626824
2020-03-19 169 - 112.57142857142857 101.53525920441629
2020-03-20 180 - 126.71428571428571 104.41188963792277
2020-03-21 157 0 148.85714285714286 107.06898206196213
2020-03-22 46 0 160.85714285714286 109.51664617529467
2020-03-23 148 1 178.42857142857142 111.76499167668065
2020-03-24 206 1 183.42857142857142 113.82412826488039
2020-03-25 220 1 187.42857142857142 115.70416563865416
2020-03-26 292 4 198.71428571428572 117.41521349676225
2020-03-27 215 0 194.57142857142858 118.96738153796494
2020-03-28 185 1 193.14285714285714 120.37077946102256
2020-03-29 125 2 192.57142857142858 121.63551696469534
2020-03-30 119 2 183.71428571428572 122.77170374774363
2020-03-31 196 2 190.57142857142858 123.67436464557028
2020-04-01 216 2 184.14285714285714 124.25177513204626
2020-04-02 230 3 175 124.53392086363391
2020-04-03 263 2 183 124.55078749679558
2020-04-04 140 2 180.14285714285714 124.33236068799366
2020-04-05 61 2 174.14285714285714 123.90862609369049
2020-04-06 175 2 162 123.30956937034838
2020-04-07 176 4 137.14285714285714 122.56517617442978
2020-04-08 174 5 132.42857142857142 121.70543216239696
2020-04-09 145 5 128 120.7603229907123
2020-04-10 89 4 115 119.7598343158382
2020-04-11 107 4 99.71428571428571 118.73395179423697
2020-04-12 30 1 94 117.71266108237097
2020-04-13 84 5 87.71428571428571 116.18344397696215
2020-04-14 69 6 92.14285714285714 113.72089153284888
2020-04-15 134 12 90.57142857142857 110.48565329366808
2020-04-16 101 10 91.71428571428571 106.63837880305678
2020-04-17 120 7 85.42857142857143 102.33971760465192
2020-04-18 96 1 86.42857142857143 97.75031924209055
2020-04-19 38 2 69.28571428571429 93.03083325900958
2020-04-20 40 3 72.14285714285714 88.34190919904601
2020-04-21 76 8 63 83.8441966058368
2020-04-22 14 - 60 79.69834502301896
2020-04-23 121 - 59.857142857142854 76.06500399422944
2020-04-24 56 1 58.857142857142854 73.10482306310523
2020-04-25 75 10 56.714285714285715 70.37341293231893
2020-04-26 37 2 66.57142857142857 67.36190295472669
2020-04-27 33 2 57.857142857142854 64.13422064924085
2020-04-28 61 10 57.714285714285715 60.75429353477376
2020-04-29 83 10 51.857142857142854 57.28604913023776
2020-04-30 60 2 51.42857142857143 53.79341495454518
2020-05-01 55 3 51.285714285714285 50.34031852660836
2020-05-02 34 2 49.714285714285715 46.990687365339646
2020-05-03 34 0 46 43.808448989651374
2020-05-04 32 0 46 40.857530918455886
2020-05-05 50 5 42.857142857142854 38.201860670665525
2020-05-06 57 3 40.857142857142854 35.90536576519263
2020-05-07 60 1 37.57142857142857 34.03197372094953
2020-05-08 33 1 33.285714285714285 32.52485771066673
2020-05-09 20 1 29.714285714285715 31.26566701039179
2020-05-10 11 0 27.857142857142858 30.226043294012644
2020-05-11 2 0 27.142857142857142 29.37762823541726
2020-05-12 25 5 26.857142857142858 28.692063508493554
2020-05-13 44 3 26.714285714285715 28.140990787129507
2020-05-14 55 4 26.714285714285715 27.696051745213037
2020-05-15 31 4 29.285714285714285 27.3288880566321
2020-05-16 19 0 29.857142857142858 27.011141395274635
2020-05-17 11 -1 30.571428571428573 26.7144534350286
2020-05-18 20 2 27 26.41046584978192
2020-05-19 29 4 26.714285714285715 26.07082031342256
2020-05-20 49 0 27.571428571428573 25.88946524124237
2020-05-21 30 4 26.714285714285715 26.044188702495802
2020-05-22 29 0 25.285714285714285 26.482391852014707
2020-05-23 25 1 24.142857142857142 27.151475844630934
2020-05-24 5 0 21.428571428571427 27.99884183517631
2020-05-25 10 0 22.142857142857142 28.97189097848269
2020-05-26 21 2 22.142857142857142 30.01802442938192
2020-05-27 30 2 23.285714285714285 31.08464334270583
2020-05-28 35 1 24.857142857142858 32.11914887328628
2020-05-29 29 1 26.714285714285715 33.0689421759551
2020-05-30 33 1 28.714285714285715 33.881424405544145
2020-05-31 16 0 28.285714285714285 34.50399671688525
2020-06-01 23 0 29.714285714285715 34.88406026481026
2020-06-02 35 0 28.142857142857142 35.28203770538156
2020-06-03 27 2 28.142857142857142 35.955845186679944
2020-06-04 45 0 28.571428571428573 36.84912410156482
2020-06-05 18 1 29 37.90551584289562
2020-06-06 33 4 29.714285714285715 39.068661803531754
2020-06-07 19 0 33.57142857142857 40.28220337633267
2020-06-08 26 0 31.285714285714285 41.489781954157785
2020-06-09 40 1 41.142857142857146 42.63503892986651
2020-06-10 54 2 40.714285714285715 43.66161569631829
2020-06-11 29 0 43.714285714285715 44.513153646372544
2020-06-12 87 0 46.714285714285715 45.13329417288869
2020-06-13 30 0 45.714285714285715 45.465678668726156
2020-06-14 40 0 48.42857142857143 45.60207840681158
2020-06-15 47 0 62.57142857142857 45.676885156415494
2020-06-16 33 1 68.71428571428571 45.69767105491315
2020-06-17 73 1 74.57142857142857 45.67200823967984
2020-06-18 128 1 73 45.60746884809082
2020-06-19 130 0 78.14285714285714 45.51162501752135
2020-06-20 71 0 81.85714285714286 45.39204888534671
2020-06-21 29 0 78 45.25631258894217
2020-06-22 83 0 70.42857142857143 45.11198826568298
2020-06-23 59 0 58.857142857142854 44.96664805294443
2020-06-24 46 1 52 44.827864088101784
2020-06-25 75 0 49 44.703208508530295
2020-06-26 49 1 43.42857142857143 44.600253451605234
2020-06-27 23 0 38.285714285714285 44.35732689423219
2020-06-28 8 0 40.142857142857146 43.8415822375048
2020-06-29 44 1 35.57142857142857 43.10382975508047
2020-06-30 23 0 35.42857142857143 42.19487972061659
2020-07-01 59 0 38 41.16554240777056
2020-07-02 43 0 39.285714285714285 40.06662809019978
2020-07-03 48 0 34.57142857142857 38.94894704156161
2020-07-04 41 1 37 37.86330953551349
2020-07-05 17 0 33.57142857142857 36.860525845712786
2020-07-06 11 0 34.42857142857143 35.9914062458169
2020-07-07 40 0 32 35.306761009483246
2020-07-08 35 1 29 34.85740041036918
2020-07-09 49 0 26.714285714285715 34.69413472213213
2020-07-10 31 0 27.714285714285715 34.66219301369997
2020-07-11 20 1 24.571428571428573 34.58308993163362
2020-07-12 1 0 23.142857142857142 34.472064116039974
2020-07-13 18 2 19.857142857142858 34.34435420702596
2020-07-14 18 1 18.714285714285715 34.21519884469846
2020-07-15 25 1 19.428571428571427 34.0998366691644
2020-07-16 26 0 20.142857142857142 34.013506320530674
2020-07-17 23 0 20.857142857142858 33.97144643890419
2020-07-18 25 0 23.285714285714285 33.988895664391876
2020-07-19 6 0 23 34.0810926371006
2020-07-20 23 0 24.142857142857142 34.26327599713732
2020-07-21 35 1 29.857142857142858 34.5506843846089
2020-07-22 23 0 31.142857142857142 34.958556439622264
2020-07-23 34 0 31.714285714285715 35.581047652389486
2020-07-24 63 1 31.142857142857142 36.471180870307045
2020-07-25 34 0 33.42857142857143 37.58249576925848
2020-07-26 10 0 41.42857142857143 38.86853202512735
2020-07-27 19 0 42.57142857142857 40.28282931379719
2020-07-28 51 0 45.285714285714285 41.77892731115154
2020-07-29 79 0 46.857142857142854 43.310365693073955
2020-07-30 42 0 46.714285714285715 44.83068413544795
2020-07-31 82 0 49.57142857142857 46.29342231415709
2020-08-01 45 0 46.857142857142854 47.65211990508492
2020-08-02 9 0 48.57142857142857 48.86031658411496
2020-08-03 39 0 50.142857142857146 49.87155202713078
2020-08-04 32 0 52.285714285714285 50.75681297598739
2020-08-05 91 1 52.714285714285715 51.6222537488776
2020-08-06 53 0 52.857142857142854 52.47416538619165
2020-08-07 97 0 53.285714285714285 53.31883892831979
2020-08-08 48 0 64.57142857142857 54.16256541565226
2020-08-09 10 0 69.42857142857143 55.011635888579285
2020-08-10 42 0 80.57142857142857 55.87234138749113
2020-08-11 111 0 79.42857142857143 56.75097295277801
2020-08-12 125 0 78.57142857142857 57.65382162483018
2020-08-13 131 0 79.28571428571429 58.58717844403788
2020-08-14 89 0 82.71428571428571 59.55733445079135
2020-08-15 42 0 77.57142857142857 60.57058068548082
2020-08-16 15 0 69.57142857142857 61.63320818849653
2020-08-17 66 0 62.285714285714285 62.73202836873971
2020-08-18 75 0 65.57142857142857 63.84951453076828
2020-08-19 69 0 68.42857142857143 64.98545055845756
2020-08-20 80 2 67.85714285714286 66.13962033568289
2020-08-21 112 0 66.71428571428571 67.31180774631957
2020-08-22 62 0 65.42857142857143 68.50179667424298
2020-08-23 11 0 67.28571428571429 69.70937100332839
2020-08-24 58 0 66.85714285714286 70.93431461745114
2020-08-25 66 0 67.14285714285714 72.17641140048654
2020-08-26 82 0 66.57142857142857 73.43544523630996
2020-08-27 77 0 65.85714285714286 74.71120000879667
2020-08-28 114 0 68.57142857142857 76.00345960182203
2020-08-29 58 0 70.71428571428571 77.31261397215414
2020-08-30 6 0 71 78.63915545685494
2020-08-31 77 0 74 79.98302151024043
2020-09-01 81 0 71.14285714285714 81.34414958662667
2020-09-02 84 0 74.28571428571429 82.72247714032966
2020-09-03 98 0 76.85714285714286 84.11794162566542
2020-09-04 94 0 75.14285714285714 85.53048049695003
2020-09-05 80 0 76 86.96003120849946
2020-09-06 24 0 74.28571428571429 88.40653121462978
2020-09-07 65 0 80.14285714285714 89.869917969657
2020-09-08 87 0 84.71428571428571 91.35012892789715
2020-09-09 72 0 78.71428571428571 92.84710154366626
2020-09-10 139 0 77.42857142857143 94.36077327128035
2020-09-11 126 0 80.28571428571429 95.8937532110423
2020-09-12 38 0 83.42857142857143 97.44837785507411
2020-09-13 15 0 91.71428571428571 99.02417574542055
2020-09-14 85 0 91.85714285714286 100.62067542412636
2020-09-15 109 1 100.42857142857143 102.23740543323636
2020-09-16 130 0 115.85714285714286 103.87389431479525
2020-09-17 140 0 120 105.52967061084783
2020-09-18 186 0 129 107.20426286343884
2020-09-19 146 0 130.14285714285714 108.89719961461307
2020-09-20 44 0 - 110.60800940641522
2020-09-21 148 0 - 112.33622078089014
2020-09-22 117 1 - 114.08136228008253
needs(tidyverse, ggtext, zoo, modelr)
source('./write_datawrapper.R')
# load data from Jakub's Github repository
covid19 <- read_csv('https://raw.githubusercontent.com/jakubvalenta/covid-berlin-data/master/covid_berlin_data_incl_hospitalized.csv') %>%
# compute new cases
mutate(new=cases - lag(cases),
# compute new deaths
new.deaths=deaths-lag(deaths),
# compute rolling average over 7
new.avg=rollmean(new, 7, align = 'center', na.pad = T),
# add row number index
index=row_number())
# compute Loess prediction for new cases
mod <- loess(new ~ index, covid19, na.action='na.exclude', span=0.5)
# store dataset
covid19 %>%
add_predictions(mod) %>%
select(date, new, new.deaths, new.avg, pred) %>%
mutate(pred=ifelse(pred<0, NA, pred)) %>%
write_datawrapper('RnWgL') %>%
write_csv('covid19-cases-berlin.csv', na = '-')
# create R plot for reference
png('covid19-berlin-new.png', width =20, height=15, units='cm',
res=120)
covid19 %>%
add_predictions(mod) %>%
ggplot(aes(date, new)) +
geom_col(fill='#aeebd9') +
geom_col(aes(y=new.deaths), fill='#15607a') +
geom_hline(aes(yintercept=0)) +
geom_area(aes(y=new.avg), fill='#29a793', alpha=0.2) +
geom_line(aes(y=new.avg), color='#29a793', size=1) +
geom_line(aes(y=pred), color='#000000', span=0.45, se=F, linetype=3, size=0.7) +
scale_x_date(name='')+
scale_y_continuous(name='', limits = c(0, NA))+
labs(title='New COVID-19 cases in Berlin per day',
subtitle = paste('**bars** = reported number of new <b style="color:#7bd1b8">cases</b> and <b style="color:#15607a">deaths</b> per day',
'**lines** = 7-day average number of cases (solid) + loess smooth (dotted)', sep='<br>'),
caption='Source: Pressemitteilungen der Senatsverwaltung für Gesundheit, Pflege und Gleichstellung\nData via https://github.com/jakubvalenta/covid-berlin-data') +
theme_minimal() +
theme(text = element_text(size = 12),
plot.title = element_markdown(size = 17, face = 'bold'),
plot.subtitle = element_markdown(size = 12),
plot.caption = element_text(color='#777777'),
plot.margin=unit(c(0.4,0.2,0.49,0),'cm'))
dev.off()
# compute different moving average spans
covid19 %>%
select(date, index, new) %>%
mutate(zero=0,
average3=rollmean(new, 3, align = 'center', na.pad = T),
average7=rollmean(new, 7, align = 'center', na.pad = T),
average10=rollmean(new, 10, align = 'center', na.pad = T),
average14=rollmean(new, 14, align = 'center', na.pad = T),
average21=rollmean(new, 21, align = 'center', na.pad = T)) %>%
write_datawrapper('RBL4y') %>%
ggplot(aes(date, new)) +
geom_col(fill='#aeebd9') +
geom_hline(aes(yintercept=0)) +
geom_line(aes(y=average3), color=1, size=1) +
geom_line(aes(y=average7), color=1, size=1) +
geom_line(aes(y=average10), color=4, size=1) +
geom_line(aes(y=average14), color=2, size=1) +
geom_line(aes(y=average21), color=3, size=1) +
scale_x_date(name='')+
scale_y_continuous(name='', limits = c(0, NA))+
theme_minimal()
# different moving average alignments
covid19 %>%
select(date, index, new) %>%
add_predictions(mod) %>%
mutate(pred=ifelse(pred<0, NA, pred)) %>%
mutate(zero=0,
average.left=rollmean(new, 14, align = 'left', na.pad = T),
average.center=rollmean(new, 14, align = 'center', na.pad = T),
average.right=rollmean(new, 14, align = 'right', na.pad = T)) %>%
write_datawrapper('MiExN') %>%
ggplot(aes(date, new)) +
geom_col(fill='#aeebd9') +
geom_hline(aes(yintercept=0)) +
geom_line(aes(y=pred), color=1, size=0.5) +
geom_line(aes(y=average.left), color=1, size=1) +
geom_line(aes(y=average.center), color=1, size=1) +
geom_line(aes(y=average.right), color=4, size=1) +
scale_x_date(name='')+
scale_y_continuous(name='', limits = c(0, NA))+
theme_minimal()
covid19 %>%
select(date, new) %>%
mutate(zero=0,
average3=rollmean(new, 3, align = 'center', na.pad = T),
average7=rollmean(new, 7, align = 'center', na.pad = T),
average14=rollmean(new, 14, align = 'center', na.pad = T),
average21=rollmean(new, 21, align = 'center', na.pad = T)) %>%
write_datawrapper('RBL4y') %>%
ggplot(aes(date, new)) +
geom_col(fill='#aeebd9') +
geom_hline(aes(yintercept=0)) +
geom_line(aes(y=average3), color=1, size=1) +
geom_line(aes(y=average7), color=1, size=1) +
geom_line(aes(y=average14), color=2, size=1) +
geom_line(aes(y=average21), color=3, size=1) +
scale_x_date(name='')+
scale_y_continuous(name='', limits = c(0, NA))+
theme_minimal()
covid19 %>%
ggplot(aes(date, hospitalized)) +
geom_col(fill='#aeebd9') +
geom_col(aes(y=icu), fill='#29a793') +
theme_minimal()
needs(tidyverse)
covid19 <- read_csv('https://raw.githubusercontent.com/jakubvalenta/covid-berlin-data/master/covid_berlin_data.csv')
DAYS <- c(
'0'='Sunday',
'1'='Monday',
'2'='Tuesday',
'3'='Wednesday',
'4'='Thursday',
'5'='Friday',
'6'='Saturday')
new_cases <- covid19 %>%
mutate(weekday = DAYS[format(date, '%w')],
new=cases - lag(cases))
new_cases.ts <- ts(new_cases$new, start=0, frequency = 7)
dts <- decompose(new_cases.ts, "multiplicative")
plot(dts)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
needs(httr)
write_datawrapper <- function(data, chartid) {
token <- Sys.getenv('dw_api_key')
r <- httr::PUT(
paste0('https://api.datawrapper.de/v3/charts/', chartid,'/data'),
add_headers(Authorization=paste0('Bearer ', token)),
encode = 'raw',
body=format_csv(data, na = '-'))
print(r)
data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.