Skip to content

Instantly share code, notes, and snippets.

@gka gka/.gitignore

Last active Aug 14, 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 - - -
2020-03-03 3 - - 7.343756893916837
2020-03-04 6 - - 17.688599243743482
2020-03-06 7 - 8.142857142857142 27.69838242617483
2020-03-07 9 - 16.428571428571427 37.35106692768374
2020-03-08 20 - 21.714285714285715 46.624613234743094
2020-03-10 10 - 35.857142857142854 55.49698183382576
2020-03-11 60 - 37.714285714285715 63.9461332114046
2020-03-13 40 - 43.42857142857143 71.95002785395249
2020-03-14 105 - 47.857142857142854 79.4866262479423
2020-03-15 20 - 65.85714285714286 86.5338888798469
2020-03-16 49 - 81.42857142857143 93.1015258057767
2020-03-17 51 - 101.42857142857143 99.22784453312602
2020-03-18 136 - 108.85714285714286 104.93370172529345
2020-03-19 169 - 112.57142857142857 110.23995404567775
2020-03-20 180 - 126.71428571428571 115.16745815767753
2020-03-21 157 0 148.85714285714286 119.73707072469152
2020-03-22 46 0 160.85714285714286 123.9696484101183
2020-03-23 148 1 178.42857142857142 127.88604787735663
2020-03-24 206 1 183.42857142857142 131.50712578980514
2020-03-25 220 1 187.42857142857142 134.85373881086252
2020-03-26 292 4 198.71428571428572 137.79025081287418
2020-03-27 215 0 194.57142857142858 140.20080227378628
2020-03-28 185 1 193.14285714285714 142.1359147653984
2020-03-29 125 2 192.57142857142858 143.64610985951043
2020-03-30 119 2 183.71428571428572 144.78190912792195
2020-03-31 196 2 190.57142857142858 145.5938341424328
2020-04-01 216 2 184.14285714285714 146.13240647484253
2020-04-02 230 3 175 146.44814769695097
2020-04-03 263 2 183 146.59157938055776
2020-04-04 140 2 180.14285714285714 146.61322309746268
2020-04-05 61 2 174.14285714285714 145.9447893453248
2020-04-06 175 2 162 144.1261138985042
2020-04-07 176 4 137.14285714285714 141.36990624385186
2020-04-08 174 5 132.42857142857142 137.88887586821906
2020-04-09 145 5 128 133.89573225845672
2020-04-10 89 4 115 129.60318490141606
2020-04-11 107 4 99.71428571428571 125.2239432839481
2020-04-12 30 1 94 120.97071689290398
2020-04-13 84 5 87.71428571428571 117.05621521513476
2020-04-14 69 6 92.14285714285714 113.6931477374916
2020-04-15 134 12 90.57142857142857 110.1331282509601
2020-04-16 101 10 91.71428571428571 105.6402450815439
2020-04-17 120 7 85.42857142857143 100.44591951862083
2020-04-18 96 1 86.42857142857143 94.78157285156897
2020-04-19 38 2 69.28571428571429 88.87862636976614
2020-04-20 40 3 72.14285714285714 82.96850136259039
2020-04-21 76 8 63 77.28261911941956
2020-04-22 14 - 60 72.0524009296317
2020-04-23 121 - 59.857142857142854 67.50926808260468
2020-04-24 56 1 58.857142857142854 63.88464186771648
2020-04-25 75 10 56.714285714285715 60.86811095593204
2020-04-26 37 2 66.57142857142857 57.99176509918459
2020-04-27 33 2 57.857142857142854 55.25077720830445
2020-04-28 61 10 57.714285714285715 52.64032019412202
2020-04-29 83 10 51.857142857142854 50.1555669674676
2020-04-30 60 2 51.42857142857143 47.79169043917157
2020-05-01 55 3 51.285714285714285 45.54386352006426
2020-05-02 34 2 49.714285714285715 43.40725912097603
2020-05-03 34 0 46 41.37705015273722
2020-05-04 32 0 46 39.448409526178196
2020-05-05 50 5 42.857142857142854 37.68930012313371
2020-05-06 57 3 40.857142857142854 36.1516923383152
2020-05-07 60 1 37.57142857142857 34.806770351867996
2020-05-08 33 1 33.285714285714285 33.62571834393744
2020-05-09 20 1 29.714285714285715 32.579720494668884
2020-05-10 11 0 27.857142857142858 31.63996098420766
2020-05-11 2 0 27.142857142857142 30.777623992699105
2020-05-12 25 5 26.857142857142858 29.963893700288576
2020-05-13 44 3 26.714285714285715 29.1699542871214
2020-05-14 55 4 26.714285714285715 28.366989933342918
2020-05-15 31 4 29.285714285714285 27.5740845843158
2020-05-16 19 0 29.857142857142858 26.836570523302644
2020-05-17 11 -1 30.571428571428573 26.16500443729868
2020-05-18 20 2 27 25.56994301329918
2020-05-19 29 4 26.714285714285715 25.061942938299367
2020-05-20 49 0 27.571428571428573 24.65156089929449
2020-05-21 30 4 26.714285714285715 24.3493535832798
2020-05-22 29 0 25.285714285714285 24.165877677250535
2020-05-23 25 1 24.142857142857142 24.111689868201942
2020-05-24 5 0 21.428571428571427 24.197346843129267
2020-05-25 10 0 22.142857142857142 24.57804801837485
2020-05-26 21 2 22.142857142857142 25.350730509862437
2020-05-27 30 2 23.285714285714285 26.438557553959367
2020-05-28 35 1 24.857142857142858 27.764692387032976
2020-05-29 29 1 26.714285714285715 29.252298245450607
2020-05-30 33 1 28.714285714285715 30.824538365579606
2020-05-31 16 0 28.285714285714285 32.40457598378731
2020-06-01 23 0 29.714285714285715 33.915574336441054
2020-06-02 35 0 28.142857142857142 35.28069665990819
2020-06-03 27 2 28.142857142857142 36.42310619055605
2020-06-04 45 0 28.571428571428573 37.26596616475197
2020-06-05 18 1 29 38.13594305688632
2020-06-06 33 4 29.714285714285715 39.3483814719045
2020-06-07 19 0 33.57142857142857 40.80946184200643
2020-06-08 26 0 31.285714285714285 42.425364599392076
2020-06-09 40 1 41.142857142857146 44.102270176261335
2020-06-10 54 2 40.714285714285715 45.74635900481417
2020-06-11 29 0 43.714285714285715 47.26381151725047
2020-06-12 87 0 46.714285714285715 48.560808145770224
2020-06-13 30 0 45.714285714285715 49.54352932257331
2020-06-14 40 0 48.42857142857143 50.11815547985971
2020-06-15 47 0 62.57142857142857 50.4326151804914
2020-06-16 33 1 68.71428571428571 50.695707557108385
2020-06-17 73 1 74.57142857142857 50.904918896577506
2020-06-18 128 1 73 51.05773548576567
2020-06-19 130 0 78.14285714285714 51.1516436115397
2020-06-20 71 0 81.85714285714286 51.18412956076649
2020-06-21 29 0 78 51.15267962031292
2020-06-22 83 0 70.42857142857143 51.05478007704584
2020-06-23 59 0 58.857142857142854 50.88791721783212
2020-06-24 46 1 52 50.649577329538644
2020-06-25 75 0 49 50.11432835368719
2020-06-26 49 1 43.42857142857143 49.129172047279916
2020-06-27 23 0 38.285714285714285 47.80024542040413
2020-06-28 8 0 40.142857142857146 46.23368548314726
2020-06-29 44 1 35.57142857142857 44.535629245596645
2020-06-30 23 0 35.42857142857143 42.81221371783967
2020-07-01 59 0 38 41.16957590996368
2020-07-02 43 0 39.285714285714285 39.71385283205608
2020-07-03 48 0 34.57142857142857 38.5511814942042
2020-07-04 41 1 37 37.787698906495436
2020-07-05 17 0 33.57142857142857 37.13867221726521
2020-07-06 11 0 34.42857142857143 36.29475513673265
2020-07-07 40 0 32 35.32516451781068
2020-07-08 35 1 29 34.29911721341223
2020-07-09 49 0 26.714285714285715 33.28583007645023
2020-07-10 31 0 27.714285714285715 32.35451995983762
2020-07-11 20 1 24.571428571428573 31.57440371648728
2020-07-12 1 0 23.142857142857142 31.01469819931219
2020-07-13 18 2 19.857142857142858 30.74462026122524
2020-07-14 18 1 18.714285714285715 30.833386755139372
2020-07-15 25 1 19.428571428571427 31.12035158723106
2020-07-16 26 0 20.142857142857142 31.414587427529824
2020-07-17 23 0 20.857142857142858 31.739889274728153
2020-07-18 25 0 23.285714285714285 32.12005212751858
2020-07-19 6 0 23 32.57887098459357
2020-07-20 23 0 24.142857142857142 33.14014084464563
2020-07-21 35 1 29.857142857142858 33.82765670636727
2020-07-22 23 0 31.142857142857142 34.665213568450994
2020-07-23 34 0 31.714285714285715 35.676606429589285
2020-07-24 63 1 31.142857142857142 36.88563028847465
2020-07-25 34 0 33.42857142857143 38.24559485767631
2020-07-26 10 0 41.42857142857143 39.69766605385048
2020-07-27 19 0 42.57142857142857 41.24742318182016
2020-07-28 51 0 45.285714285714285 42.90044554640837
2020-07-29 79 0 46.857142857142854 44.66231245243809
2020-07-30 42 0 46.714285714285715 46.53860320473234
2020-07-31 82 0 49.57142857142857 48.534897108114095
2020-08-01 45 0 46.857142857142854 50.656773467406396
2020-08-02 9 0 48.57142857142857 52.909811587432216
2020-08-03 39 0 50.142857142857146 55.299590773014565
2020-08-04 32 0 52.285714285714285 57.83869785573271
2020-08-05 91 1 52.714285714285715 60.5313755626014
2020-08-06 53 0 52.857142857142854 63.370687041596796
2020-08-07 97 0 53.285714285714285 66.3496954406951
2020-08-08 48 0 64.57142857142857 69.4614639078725
2020-08-09 10 0 69.42857142857143 72.69905559110519
2020-08-10 42 0 80.57142857142857 76.05553363836935
2020-08-11 111 0 - 79.5239611976412
2020-08-12 125 0 - 83.09740141689693
2020-08-13 131 0 - 86.76891744411269
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.