forked from weinbergerlab/excess_pi_covid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
us_pi_excess_MANUSCRIPT.Rmd
1993 lines (1470 loc) · 72.4 KB
/
us_pi_excess_MANUSCRIPT.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Estimating the early death toll of COVID-19 in the United States"
author: "Dan Weinberger"
date: "4/17/2020"
output:
pdf_document:
keep_tex: true
fig_caption: true
html_document:
df_print: paged
html_document: null
toc: yes
toc_depth: 2
toc_float: yes
fig_caption: true
params:
agg.level: 'state_region'
n.days.filter: 20
---
```{r, include = F}
knitr::opts_chunk$set(
collapse = TRUE,
echo=F,
warning=FALSE,
message=FALSE,
comment = "#>",
dev=c('png','pdf'),
fig.path='./figures/'
)
knitr::opts_knit$set(eval.after = "fig.cap")
```
```{r, eval=F}
if (!require("devtools")) {
install.packages("devtools")
}
devtools::install_github("weinbergerlab/ExcessILI")
```
```{r setup}
library(ExcessILI)
library(cdcfluview)
library(reshape2)
library(ggplot2)
library(lubridate)
library(RColorBrewer)
library(plotly)
library(MMWRweek)
library(readr)
library(rjson)
library(htmlTable)
library(RSocrata)
library(pdftools)
library(readr)
library(gsubfn)
library(dplyr)
library(RCurl)
#library(jsonlite)
set.seed(123)
source('./functions/ts_plot_func.R')
```
```{r archivfunc}
# Using ExcessILI's data archiving functions, returns the most recent copy of
# output obtained by running a function or formula \code{f}, unless this
# copy doesn't exist or is older (by modification time) than \code{maxage}.
# In that case, \code{f} is run and the output is archived into the folder
# Data/'storeName' as an RDS file, using the function ExcessILI::storeRDS.
#
# @param storeName A string. The name of the folder to store output in
# @param f A function or formula taking no arguments. Formulas are coerced to
# functions.
# @param maxage How old can any existing archived file be before \code{f} is
# called again?
runIfExpired <- function(storeName, f, maxage=hours(24)) {
basepath <- "Data/"
mostRecent <- mostRecentTimestamp(storeName, basepath=basepath)
f <- rlang::as_function(f)
runAndArchive <- function() {
data <- f()
storeRDS(data, storeName, basepath)
data
}
if (is.na(mostRecent))
return(runAndArchive())
if (mostRecent %--% now() < maxage)
return(retrieveRDS(storeName, basepath))
runAndArchive()
}
```
```{r}
#Download latest PIC data--pulls from most recent Friday
days.sub <- wday(Sys.Date() + 1)
days.sub[days.sub==7] <- 0
last.fri <- as.character(Sys.Date() - days.sub)
fri.date <- paste0(substr(last.fri,6,7),substr(last.fri,9,10),substr(last.fri,1,4))
runIfExpired('',download_pic){
download_pic <- function() {
download.file(
paste0(
'https://www.cdc.gov/coronavirus/2019-ncov/covid-data/covidview/',
fri.date,
'/csv/nchs-mortality-report.csv'
),
'./Data/pic_provisional/pic.provisional.txt'
)
ds1 <- read.txt('./Data/pic_provisional/pic.provisional.txt')
return(ds1)
}
download_pic()
}
```
```{r states_to_plot}
plot.states <- c('CA','TX','FL', 'NY','PA','IL','GA','MI','LA', 'MA' )
exclude.states <- c('CT', 'NC') #no 2020 data for CT, NC only reports through early-March
```
```{r start_date_select}
#Date when start counting excess deaths: Start first week of Feb to Match CDC's COVIDview tables
count.start.date <- as.Date('2020-01-26')
```
```{r import_state_pi_data}
#Import the P&I data
## Download the mortality data
#pi.data.alt <- read.socrata("https://data.cdc.gov/resource/pp7x-dyj2.json") #only through end of 2019
#pi.data.alt <- pi.data.alt[pi.data.alt$geoid=='state',]
#ARCHIVE
pi.data <- runIfExpired('pi_mortality_state', ~pi_mortality(coverage_area='state'))
pi.data.last <- pi.data[pi.data$week_end==max(pi.data$week_end) & pi.data$geo_description=='State',]
last.date.avail.pi <-
max(pi.data$week_end)
last.date.obtained <-
last.date.avail.pi +6 #release date fr latest data (if weekly)
pi.data <- pi.data[pi.data$week_end <= (last.date.obtained-params$n.days.filter),]
pi.data <- pi.data[!(is.na(pi.data$week_end)),]
#pi.data.miss.states <- unique(pi.data.last$region_name[is.na(pi.data.last$total_pni)])
#pi.data.miss.states <- unique(pi.data.last$region_name)
pi.data.not.miss.states <- unique(pi.data.last$region_name)
#pi.data.not.miss.states <- unique(pi.data.last$region_name[!is.na(pi.data.last$total_pni)])
pi.data.not.miss.states <- state.abb[match(pi.data.not.miss.states, state.name)]
pi.data.not.miss.states <-
pi.data.not.miss.states[!is.na(pi.data.not.miss.states)]
```
```{r import_state_adjustments}
#State-level adjustments for under-reporting
adj <- read.csv('./outputs/report.proportion.state.csv')
adj.m <-melt(adj, id.vars='X' )
adj.m$state <- state.abb[match(adj.m$variable, state.name)]
adj.m$state[adj.m$variable=='New York City'] <- 'NYC'
last.pi.date <- max(pi.data$week_end)
adj.m$date <- NA
adj.m$date[adj.m$X==1] <- as.Date(last.pi.date)
adj.m$date[adj.m$X==2] <- as.Date(last.pi.date) - 7
adj.m$date[adj.m$X==3] <- as.Date(last.pi.date) - 14
adj.m$date[adj.m$X==4] <- as.Date(last.pi.date) - 21
adj.m$date <- as.Date(adj.m$date, origin='1970-01-01')
adj.m <- adj.m[, c('date', 'state','value')]
names(adj.m) <- c('date', 'state', 'reporting.adjustment')
```
```{r state_region_cw}
#cross walk file to map states to hhs regions
#
hhs_states <- cdcfluview::hhs_regions
hhs_states$state <- state.abb[match(hhs_states$state_or_territory, state.name)]
hhs_states$state_region <- hhs_states$region
hhs_states$state_region[hhs_states$state %in% plot.states] <- hhs_states$state[hhs_states$state %in% plot.states]
hhs_states <- hhs_states[,c('state','state_region')]
hhs_states$state_region <- gsub(' ','', hhs_states$state_region)
hhs_states.cw <- hhs_states[!is.na(hhs_states$state),]
#Get rid of states that have no data in recent time points
hhs_states.cw <-
hhs_states.cw[ hhs_states.cw$state %in% pi.data.not.miss.states,]
#Exclude CT
hhs_states.cw <-
hhs_states.cw[ !(hhs_states.cw$state %in% exclude.states),]
if(params$agg.level== 'state_region'){
hhs_states.cw.spl <- split(hhs_states.cw,
hhs_states.cw$state_region)
}else{
hhs_states.cw.spl <- split(hhs_states.cw,
hhs_states.cw$state)
}
hhs_states.cw.spl <-lapply(hhs_states.cw.spl, function(x) {
x$state_region.lab <- paste(x$state, collapse=',')
return(x)
}
)
hhs_states.cw <-do.call('rbind.data.frame',hhs_states.cw.spl)
hhs_states.cw$state_region[hhs_states.cw$state_region != hhs_states.cw$state] <-
hhs_states.cw$state_region.lab[hhs_states.cw$state_region != hhs_states.cw$state]
```
```{r import_covid_tracking}
#TESTING DATA FORMATTING
url.test<-"https://covidtracking.com/api/v1/states/daily.json"
#ARCHIVE
json_data <- runIfExpired('covidtracking_states_daily', ~fromJSON(file=url.test))
test.dates <- as.character(sapply(json_data,'[[','date'))
test.state <- sapply(json_data,'[[','state')
testN <- sapply(json_data, '[[', 'totalTestResultsIncrease')
testN <-sapply(testN, function(x){
if(is.null(x)){
x <-0
}
return(x)
}
)
deathN <- sapply(json_data, '[[', 'deathIncrease')
deathN <-sapply(deathN, function(x){
if(is.null(x)){
x <-0
}
return(x)
}
)
test.ds <- cbind.data.frame('state'=test.state,test.dates, 'testN.day'=testN, 'deathN.day'=deathN )
test.ds$test.date.wk <-floor_date(as.Date(test.ds$test.dates, '%Y%m%d'),'week')
test.ds <- merge(test.ds, hhs_states.cw, by='state')
test.ds.agg <- aggregate( test.ds[,c('testN.day','deathN.day')] , by=list('state'=test.ds$state_region,'date'=test.ds$test.date.wk), FUN=sum, na.rm=T)
names(test.ds.agg) <- c('state','date','testN','deathN')
pop1<-read.csv('./Data/nst-est2019-01.csv')
pop1$state_name <- substring(pop1$state_name,2)
pop1$state <- state.abb[match(pop1$state_name, state.name)]
pop1$census_bureau_pop_2019 <- gsub( ',', '',pop1$census_bureau_pop_2019)
pop1$census_bureau_pop_2019 <-as.numeric(pop1$census_bureau_pop_2019)
pop1.reg <- merge(pop1, hhs_states.cw, by='state')
pop2 <- aggregate(pop1.reg[,"census_bureau_pop_2019"], by=list('state'=pop1.reg$state_region), FUN=sum)
names(pop2) <- c('state','census_bureau_pop_2019')
test.ds2 <- merge(test.ds.agg, pop2, by='state')
test.ds2$test.week.per.capita <- test.ds2$testN/test.ds2$census_bureau_pop_2019*1000
names(test.ds2) <- c('state','date','testN','covid.track.death', 'pop2019',"test.week.per.capita")
test.ds3.spl <- test.ds2
```
```{r import_cdc_covid_view}
#official CDC data
url.cdc.covid<-"https://data.cdc.gov/resource/hc4f-j6nb.json"
#ARCHIVE
cdc.data <- runIfExpired('cdc_covid_data', ~fromJSON(file=url.cdc.covid, simplify=F))
cdc.pneum.and.covid <- sapply(cdc.data,'[[', "pneumonia_and_covid_deaths" )
cdc.total.death <- sapply(cdc.data, '[[', 'total_deaths')
cdc.covid.death <- sapply(cdc.data,'[[', "covid_deaths" )
cdc.end.wk <- sapply(cdc.data,'[[', "end_week" )
cdc.pneu.death <- sapply(cdc.data,'[[', "pneumonia_deaths" )
cdc.grp<-sapply(cdc.data,'[[', "group" )
cdc.ind<-sapply(cdc.data,'[[', "indicator" )
cdc.covid.death <-lapply(cdc.covid.death, function(x){ if(is.null(x)){ x<-NA}
return(x)
})
cdc.covid.death <-unlist(cdc.covid.death)
cdc.total.death <-lapply(cdc.total.death, function(x){ if(is.null(x)){ x<-NA}
return(x)
})
cdc.total.death <-unlist(cdc.total.death)
cdc.pneum.and.covid <-lapply(cdc.pneum.and.covid, function(x){ if(is.null(x)){ x<-NA}
return(x)
})
cdc.pneum.and.covid <-unlist(cdc.pneum.and.covid)
cdc.summary <- cbind.data.frame('grp'=cdc.grp,'ind'=cdc.ind,'NCHS Reported COVID-19 Deaths'=cdc.covid.death, 'NCHS Reported COVID-19 Deaths, with pneumonia code'=cdc.pneum.and.covid, 'total deaths'=cdc.total.death, 'week_end'=cdc.end.wk,'pneumonia'=cdc.pneu.death)
cdc.summary.wk <-
cdc.summary[cdc.summary$grp=='By week',]
cdc.summary.wk$wk_end <-
as.Date(substr(cdc.summary.wk$week_end,1,10), "%Y-%m-%d")
cdc.summary.wk <-
cdc.summary.wk[!is.na(cdc.summary.wk$wk_end),]
cdc.summary.wk <- cdc.summary.wk[, c('wk_end', 'NCHS Reported COVID-19 Deaths','NCHS Reported COVID-19 Deaths, with pneumonia code','total deaths','pneumonia')]
cdc.summary.state <- cdc.summary[cdc.summary$grp=='By state',]
cdc.summary.state$`NCHS Reported COVID-19 Deaths, with pneumonia code` <- as.numeric(as.character(cdc.summary.state$`NCHS Reported COVID-19 Deaths, with pneumonia code`))
cdc.summary.state$`NCHS Reported COVID-19 Deaths` <- as.numeric(as.character(cdc.summary.state$`NCHS Reported COVID-19 Deaths` ))
cdc.summary.state$covid.pct.pneum <- cdc.summary.state$`NCHS Reported COVID-19 Deaths, with pneumonia code`/cdc.summary.state$`NCHS Reported COVID-19 Deaths`
```
```{r import_national_pic}
## there are 3 sources of national data, each has pluses and minus: 1) NCHS provisional data, which is updated daily and has all deaths and pneumonia deaths and covid deaths but not combined PIC category. this is available since last week of Jan 2) Provisional data from another website on NCHS that is updated weekly and has combined pneumonia/influenza/COVID (PIC) and is avalable since week 40 of 2019 3) The weekly NCHS fluview mortality summary, provides P&I and all cause, but no COVID
nchs_covidview <- cdc.summary.wk
#starting 2019 wk 40, CDC provide combined PIC national
#https://www.cdc.gov/coronavirus/2019-ncov/covid-data/covidview/04172020/nchs-mortality-report.html
#CAUTION: THE PIC data seem to be less complete than the fluview data (even looking at total deaths)
pic1 <- read.csv('./Data/pic_provisional/pic.provisional.txt', skip=5)
pic1<- pic1[-1,]
pic1 <- apply(pic1,2, function(x)
gsub(',','',x))
pic1 <- apply(pic1,2, function(x)
as.numeric(as.character(x)))
pic1 <- as.data.frame(pic1)
pic1 <- pic1[,c("Year", "Week","Total.Deaths","Pneumonia..Influenza.or.COVID.19.Deaths")]
names(pic1) <- c('year', 'week', 'total_deaths_pic_denom','pic_deaths')
nchs_covidview <-
nchs_covidview[cdc.summary.wk$wk_end <
max(cdc.summary.wk$wk_end),]
nchs_covidview$`total deaths` <-
as.numeric(as.character(nchs_covidview$`total deaths`))
nchs_covidview$`pneumonia` <-
as.numeric(as.character(nchs_covidview$`pneumonia`))
nchs_covidview$`NCHS Reported COVID-19 Deaths` <-
as.numeric(as.character(nchs_covidview$`NCHS Reported COVID-19 Deaths`))
plot(nchs_covidview$wk_end, nchs_covidview$`total deaths`)
#Also import the latest national fluview data
#natl.all.data <- runIfExpired('PI_mortality_national', #~pi_mortality(coverage_area='national'))
#NOTE can't use national data as baseline bc missing WV, NC, CT
pi.data.exclude <- pi.data
pi.data.exclude$state <-
state.abb[match(pi.data.exclude$region_name, state.name)]
pi.data.exclude <-
pi.data.exclude[!(pi.data.exclude$state %in% exclude.states),]
natl.all.data <-
aggregate(pi.data.exclude[,c("total_pni", 'all_deaths')],
by=list('week_end'=pi.data.exclude$week_end,
'year_week_num'=pi.data.exclude$weeknumber ), FUN=sum)
natl.all.data$year <-
year(natl.all.data$week_end)
max.wk <-
unique(natl.all.data$year_week_num[natl.all.data$week_end==max(pi.data$week_end)])
wk.range <- c(10,max.wk)
#Replace 2020 data with the NCHS data, which are more up to date
#natl.all.data <-
# natl.all.data[natl.all.data$year <=2019,]
natl.all.data <-
natl.all.data[,c('year','week_end', 'year_week_num',"all_deaths","total_pni" )]
mmwr.wk1 <- MMWRweek(nchs_covidview$wk_end)
nchs_covidview$year_week_num <- mmwr.wk1$MMWRweek
nchs_covidview <- nchs_covidview[,c('wk_end','NCHS Reported COVID-19 Deaths', 'total deaths', 'pneumonia', 'year_week_num')]
names(nchs_covidview) <-
c('week_end','covid19.nchs', 'all_deaths_covidview',
'number_pneumonia','year_week_num')
nchs_covidview <-
nchs_covidview[,c('week_end', 'covid19.nchs','all_deaths_covidview')]
#nchs_covidview$year <- 2020
natl.all.data$year_week_num <-
as.numeric(as.character(natl.all.data$year_week_num))
#natl.all.data <- bind_rows(natl.all.data,nchs_covidview)
natl.all.data <-
merge(natl.all.data,
nchs_covidview[,c('week_end','covid19.nchs','all_deaths_covidview')],
by='week_end', all=T)
#Use 'all-death' column from covid view since this is updated daily
# natl.all.data$all_deaths[!is.na(natl.all.data$all_deaths_covidview)] <-
# natl.all.data$all_deaths_covidview[!is.na(natl.all.data$all_deaths_covidview)]
#
natl.all.data <-
merge(natl.all.data, pic1,
by.x=c('year','year_week_num'),
by.y=c('year','week') ,all=T)
#For P&I analysis, use the data on P&I&C
natl.all.data$pic_deaths[is.na(natl.all.data$pic_deaths)] <-
natl.all.data$total_pni[is.na(natl.all.data$pic_deaths)]
natl.all.data$total_deaths_pic_denom[is.na(natl.all.data$total_deaths_pic_denom)] <-
natl.all.data$all_deaths[is.na(natl.all.data$total_deaths_pic_denom)]
natl.all.data.sub <-
natl.all.data[natl.all.data$year_week_num >= wk.range[1] &
natl.all.data$year_week_num <=
wk.range[2],]
agg.historic <- aggregate(natl.all.data.sub[,c("all_deaths","total_pni", 'covid19.nchs')], by=list('year'=natl.all.data.sub$year), FUN=sum)
natl.all.data$state <- 'NATL'
natl.all.data <-
natl.all.data[order(natl.all.data$week_end),]
plot(natl.all.data$week_end, natl.all.data$all_deaths, type='l')
nrevvs.natl <- runIfExpired('nrevss_national', ~cdcfluview::who_nrevss(region = c("national")))
nrevvs.natl <- nrevvs.natl$clinical_labs
nrevvs.natl <- nrevvs.natl[,c('wk_date','percent_positive') ]
nrevvs.natl$week_end <- nrevvs.natl$wk_date+6
natl.all.data2 <- merge( natl.all.data, nrevvs.natl, by.x='week_end', by.y='week_end')
```
```{r}
#download the NREVSS data
#ARCHIVE
nrevvs.state <- runIfExpired('nrevss_state', ~cdcfluview::who_nrevss(region = c("state")))
clin <- nrevvs.state[["clinical_labs"]]
clin$state <- state.abb[match(clin$region, state.name)]
data(cdcfluview::hhs_regions)
cw.file <- cdcfluview::hhs_regions
clin2 <- merge(clin, hhs_states.cw,
by = "state")
clin2 <- merge(clin2, cw.file,
by.x = "region",
by.y = "state_or_territory")
clin2.subsetvars <-
c('region', 'region_number',
'year', 'week', 'wk_date',
'total_a','total_b',
'total_specimens','state_region')
clin2 <- clin2[, clin2.subsetvars]
names(clin2)[1:2] <- c("state", "hhs_region")
clin2$total_specimens <- as.numeric(clin2$total_specimens)
clin2$total_a <- as.numeric(clin2$total_a)
clin2$total_b <- as.numeric(clin2$total_b)
clin2 <- aggregate(clin2[, c('total_specimens','total_a','total_b')], by=list('state'=clin2$state_region, 'wk_date'=clin2$wk_date,'hhs_region'=clin2$hhs_region), FUN=sum, na.rm=T)
##Florida doesn't have ILI data, so use regions ILI dat
#
#ARCHIVE
nrevvs_hhs <- runIfExpired('nrevss_hhs', ~cdcfluview::who_nrevss(region = c("hhs")))
clin.hhs <- nrevvs_hhs[["clinical_labs"]]
clin.hhs.subsetvars <-
c('region',
'wk_date',
"total_a",'total_b',
'total_specimens')
clin.hhs <- clin.hhs[, clin.hhs.subsetvars]
clin.hhs$region <- as.numeric(gsub("Region ", "", clin.hhs$region))
names(clin.hhs) <-
c("hhs_region",
"wk_date",
"hhs_total_a",'hhs_total_b',
'hhs_total_specimens')
clin3 <- merge(clin2, clin.hhs,
by = c("hhs_region", "wk_date"))
#If no specimens for state, use HHS region estimates
clin3$total_specimens[clin3$total_specimens==0] <-
clin3$hhs_total_specimens[clin3$total_specimens==0]
clin3$total_a[clin3$total_specimens==0] <-
clin3$hhs_total_a[clin3$total_specimens==0]
clin3$total_b[clin3$total_specimens==0] <-
clin3$hhs_total_b[clin3$total_specimens==0]
clin3$total_specimens <- as.numeric(clin3$total_specimens)
clin3$total_a <- as.numeric(clin3$total_a)
clin3$total_b <- as.numeric(clin3$total_b)
clin3$flu_pct_adj <- (clin3$total_a + clin3$total_b)/clin3$total_specimens
clin4<-clin3[,c('state','flu_pct_adj', 'wk_date')]
clin4.lag1<-clin4
clin4.lag1$wk_date <- clin4$wk_date + days(7)
names(clin4.lag1) <-c('state','flu_pct_adj_lag1','wk_date')
clin4.lag2<-clin4
clin4.lag2$wk_date <- clin4$wk_date + days(14)
names(clin4.lag2) <-c('state','flu_pct_adj_lag2','wk_date')
clin4.lags <- merge(clin4, clin4.lag1, by=c('state','wk_date'))
clin4.lags <- merge(clin4.lags, clin4.lag2, by=c('state','wk_date'))
```
```{r, include=F}
#Format and fill mssings with 0s
pi.data$percent_complete[pi.data$percent_complete > 1] <- 1
pi.data$state <- state.abb[match(pi.data$region_name, state.name)]
pi.data$state[pi.data$region_name == 'New York City'] <- 'NYC'
pi.data <- pi.data[!(pi.data$state %in% exclude.states),]
pi.data <- merge(pi.data,hhs_states.cw, by='state', all=T)
pi.data$state_region[pi.data$state=='NYC'] <- 'NYC'
pi.data$pct.comp.wgt <- pi.data$percent_complete*pi.data$all_deaths
pi.data.ny.separate <- pi.data
#Combine NYC and NY state
pi.data$state_region[pi.data$state=='NYC'] <- 'NY'
pi.data.agg <- aggregate(pi.data[,c('total_pni','all_deaths','pct.comp.wgt')], by=list('state'=pi.data$state_region, 'week_start'=pi.data$week_start, 'week_end'=pi.data$week_end), FUN=sum, na.rm=T)
pi.data.agg$percent_complete <- pi.data.agg$pct.comp.wgt/pi.data.agg$all_deaths
spl1<-split(pi.data.agg, pi.data.agg$state)
min.state <- lapply(spl1, function(x){ x$miss.x<-min(x$total_pni, na.rm=T)
return(x)
})
pi.data.clean <- do.call('rbind.data.frame',min.state)
pi.data.clean <- pi.data.clean[!is.na(pi.data.clean$miss.x),]
pi.data.clean2<- merge( pi.data.clean,clin4.lags, by.x=c('week_start', 'state'), by.y=c('wk_date','state'))
states.cdc <- unique(pi.data.clean2$state)
date.print <- max(pi.data.clean2$week_end)
```
### Mortality data on deaths due to pneumonia & influenza through the week ending `r date.print`
Dan Weinberger,^1^ Ted Cohen,^1^ Forrest W. Crawford,^2^ Farzad Mostashari,^3^ Don Olson,^4^ Virginia E Pitzer,^1^ Nicholas G Reich,^5^ Marcus Russi,^1^ Lone Simonsen,^6^ Annie Watkins,^1^ Cecile Viboud^7^
^1^Department of Epidemiology of Microbial Diseases and the Public Health Modeling Unit, Yale School of Public Health, New Haven, CT
^2^Department of Biostatistics and the Public Health Modeling Unit, Yale School of Public Health, New Haven, CT; Yale Departments of Ecology and Evolutionary Biology, Statistics & Data Science, Yale School of Management
^3^Aledade, Inc
^4^Department of Health and Mental Hygiene, New York City, NY
^5^Department of Biostatistics and Epidemiology, School of Public Health and Health Sciences, University of Massachusetts, Amherst, MA
^6^Department of Science and Environment, Roskilde University, Denmark
^7^Division of International Epidemiology and Population Studies, Fogarty International Center, National Institutes of Health, Bethesda, MD
## IMPORTANT NOTE
Starting April 17, fluview started reporting death data with a 1 week lag, rather than a 2 week lag. Data from the recent weeks are highly incomplete and should be interpreted with caution
## Abstract
*Background*
Tracking the severity and public health impact of emerging diseases, such as the novel coronavirus diseases COVID-19, is a critical need. These efforts are often hampered by testing issues and reporting lags for key epidemiological data. Evaluating unexplained increases in deaths attributed to non-specific causes, such as pneumonia, can provide a more complete picture of the burden caused by COVID-19.
*Methods*
We evaluated increases in the occurrence of deaths due to P&I above a seasonal baseline across the United States and compared the rate of excess deaths to reported rates of death due to COVID-19, as well as state-level coronavirus testing rates and excess rates of influenza-like illness.
*Results*
There were notable increases in the rate of death due to pneumonia and influenza. In a number of states, these increases pre-dated the increase in testing rates and went uncounted. There was substantial variability between states in the discrepancy between reported rates of death due to COVID-19 and the estimated burden of excess deaths due to P&I. In some states, the burden of reported deaths was 10-fold lower than the burden of excess deaths due to P&I.
*Conclusions*
Given the lack of consistent virological testing, tracking spikes in deaths due to non-specific causes provides the most complete estimate of the burden of COVID-19. This approach provides a framework for tracking the progression of the epidemic and its public health impact.
## National analysis of P&I
```{r natl_pi_analysis, include=F}
#Run analysis
natl.all.data2$one <- 1
natl.all.data3 <-
natl.all.data2[!is.na(natl.all.data2$pic_deaths),]
set.seed(123)
excess_ac_natl <-
excessCases(ds = natl.all.data3,
datevar = "week_end",
statevar = "state",
adj.flu = "percent_positive",
denom.var = 'total_deaths_pic_denom',
use.syndromes = c("pic_deaths"),
extrapolation.date = '2020-03-01',
model.type='negbin',
time.res='week')
ds <- excess_ac_natl
dates1 <-
ds[[1]][[1]][[1]]$date
unexplained.cases <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "unexplained.cases")
pred <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "pred")
upi <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "upi")
lpi <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "lpi")
obs <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "y")
sum.pred.iter <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "sum.pred.iter")
sum.obs <-
excessExtract(ds = ds,
syndrome = 'pic_deaths',
extract.quantity = "sum.obs")
sum.cases.excess <-
sum(sum.obs) - sum.pred.iter
sum.excess.deaths.range.pi <-
quantile(sum.cases.excess[,1,1], probs=c(0.025,0.5,0.975))
plot(natl.all.data3$week_end, natl.all.data3$pic_deaths, type='l')
points(natl.all.data3$week_end,pred, type='l', col='gray')
res.pi1 <-cbind.data.frame('week_end'=natl.all.data3$week_end,
'pred'=pred[,1,1], 'upi'=upi[,1,1], 'lpi'=lpi[,1,1],'obs'=obs[,1,1],
'unexplained.cases'=unexplained.cases[,1,1])
mmwr.rs <- mmwr_week(res.pi1$week_end)
res.pi1<- cbind.data.frame(mmwr.rs, res.pi1)
#plot(res.pi$week_end, res.pi$unexplained.cases)
res.pi <- res.pi1[res.pi1$week_end<=as.Date('2020-04-04'),]
write.csv(res.pi, './outputs/p_i_natl_obs_expected.csv')
sum(res.pi$unexplained.cases[res.pi$mmwr_week>=10 &
res.pi$mmwr_week<=14 & res.pi$week_end>=as.Date('2020-01-01')])
```
Excess deaths due to pneumonia/influenza/covid nationally in the specified time period
```{r}
print(sum.excess.deaths.range.pi)
```
```{r natl_pi_plot}
yrange.pneu <- range(c(res.pi$pred, res.pi$obs, res.pi$upi,0))
plot(res.pi$week_end, res.pi$pred, type='l', ylim=yrange.pneu, col='gray', bty='l', main='P&I+covid deaths')
polygon(c(res.pi$week_end, rev(res.pi$week_end)), c(res.pi$lpi, rev(res.pi$upi)),col = rgb(0, 0, 0, alpha = 0.1), border = NA )
points(res.pi$week_end, res.pi$obs, type='l', col='red', bty='l')
```
## National analysis of all-cause
```{r, include=F}
#Run analysis
natl.all.data2$one <- 365000000/100000
natl.all.data3 <-
natl.all.data2[!is.na(natl.all.data2$total_pni),]
set.seed(123)
excess_ac_natl <-
excessCases(ds = natl.all.data3,
datevar = "week_end",
statevar = "state",
adj.flu = "percent_positive",
denom.var = 'one',
use.syndromes = c("all_deaths"),
extrapolation.date = '2020-03-01',
model.type='negbin',
time.res='week')
ds <- excess_ac_natl
dates1 <-
ds[[1]][[1]][[1]]$date
unexplained.cases <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "unexplained.cases")
pred <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "pred")
upi <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "upi")
lpi <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "lpi")
obs <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "y")
sum.obs.ac <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "sum.obs")
sum.pred.iter.ac <-
excessExtract(ds = ds,
syndrome = 'all_deaths',
extract.quantity = "sum.pred.iter")
sum.ac.excess <-
sum(sum.obs.ac) - sum.pred.iter.ac
sum.excess.deaths.range.ac <-
quantile(sum.ac.excess[,1,1], probs=c(0.025,0.5,0.975))
plot(natl.all.data3$week_end, natl.all.data3$all_deaths, type='l')
points(natl.all.data3$week_end,pred, type='l', col='gray')
res.ac1 <-cbind.data.frame('week_end'=natl.all.data3$week_end,
'pred'=pred[,1,1], 'upi'=upi[,1,1], 'lpi'=lpi[,1,1],'obs'=obs[,1,1],
'unexplained.cases'=unexplained.cases[,1,1])
mmwr.rs <- mmwr_week(res.ac1$week_end)
res.ac1<- cbind.data.frame(mmwr.rs, res.ac1)
#plot(res.ac$week_end, res.ac$unexplained.cases)
res.ac <- res.ac1[res.ac1$week_end<=as.Date('2020-04-04'),]
yrange.pneu <- range(c(res.ac$pred, res.ac$obs, res.ac$upi,0))
sum(res.ac$unexplained.cases[res.ac$mmwr_week>=10 & res.ac$mmwr_week<=14 & res.ac$week_end>=as.Date('2020-01-01')])
```
Total excess all cause deaths
```{r}
print(sum.excess.deaths.range.ac)
mean(sum.ac.excess>13000 & sum.ac.excess<17000)
mean(sum.ac.excess>12000 & sum.ac.excess<18000)
mean(sum.ac.excess>11000 & sum.ac.excess<19000)
mean(sum.ac.excess>10000 & sum.ac.excess<20000)
mean(sum.ac.excess>12000)
```
```{r plot_ac_natl}
plot(res.ac$week_end, res.ac$pred, type='l', ylim=yrange.pneu, col='gray')
polygon(c(res.ac$week_end, rev(res.ac$week_end)), c(res.ac$lpi, rev(res.ac$upi)),col = rgb(0, 0, 0, alpha = 0.1), border = NA )
points(res.ac$week_end, res.ac$obs, type='l', col='red', bty='l')
```
```{r summary_table_natl}
res.pi2 <- res.pi1
names(res.pi2) <-
c('year','week','day','week_end_date','baseline_pi','baseline_pi_upper', 'baseline_pi_lower', 'pneumonia_influenza_covid','excess_pneumonia_influenza_covid')
res.ac2 <- res.ac1
names(res.ac2) <-
c('year','week','day','week_end_date','baseline_all_cause','baseline_all_cause_upper', 'baseline_all-cause_lower', 'all_cause_deaths','excess_all_cause_deaths')
comb1 <- merge(res.ac2, res.pi2,
by=c('year','week','day','week_end_date'))
comb2 <- merge(comb1,
natl.all.data2[,c('week_end','covid19.nchs')],by.x='week_end_date', by.y='week_end')
write.csv(comb2,'./outputs/national_summary.csv' )
```
```{r pi_analysis_state, include=F}
#Run analysis
set.seed(123)
excess_deaths1.adjusted <-
excessCases(ds = pi.data.clean2[!(pi.data.clean2$state %in% exclude.states),],
datevar = "week_start",
statevar = "state",
denom.var = "all_deaths",
adj.flu = "flu_pct_adj_lag1",
#covs=c("flu_pct_adj", "flu_pct_adj_lag1", "flu_pct_adj_lag2"),
use.syndromes = c("total_pni"),
extrapolation.date = '2020-01-26',
time.res='week')
```
```{r, echo=F}
#dashboardPlot(excess_deaths1.adjusted)
```
```{r}
### Extract the quantities of interest
#Which syndrome do you want to plot, and over what time range?
syndrome.select <- 'total_pni' #which syndrome do you want to plot?
n.days<-52 #How many days to plot?
ds <- excess_deaths1.adjusted
#Extract the data needed to plot from the results
dates1 <-
ds[[1]][[1]][[1]]$date
unexplained.cases <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "unexplained.cases")
unexplained.log.rr <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "resid1")
denom <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "denom")
upi <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "upi")
lpi <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "lpi")
obs <-
excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "y")
pred<- excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "pred")
rr <- excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "resid1")
excess_deaths <- excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "unexplained.cases")
excess_deaths_var <- excessExtract(ds = ds,
syndrome = syndrome.select,
extract.quantity = "pred.var")
n.days <- 52
select.indices <- (length(dates1)-n.days):length(dates1)
dates<-dates1[select.indices]
states <- dimnames(pred)[[2]]
ages <- dimnames(pred)[[3]]
```
```{r summary_table_pi_state}
lpi.long <- melt(lpi[,,1])
names(lpi.long) <- c('index','state','lpi')
upi.long <- melt(upi[,,1])
names(upi.long) <- c('index','state','upi')
pred.long <- melt(lpi[,,1])
names(pred.long) <- c('index','state','pred')
obs.long <- melt(obs[,,1])
names(obs.long) <- c('index','state','obs')
excess_deaths.long <-
melt(excess_deaths[,,1])
dates1.long <- rep(dates1, times=ncol(lpi))
mmwr.dates <- MMWRweek(dates1.long)
names(mmwr.dates) <- c('year', 'week', 'day')
pi.summary.state <-
cbind.data.frame(mmwr.dates,
'week_end_date'=dates1.long+6,
'state'=pred.long[,2],
'baseline_pi'=pred.long[,3],
"baseline_pi_lower"=lpi.long[,3], "baseline_pi_upper"=upi.long[,3],
"pi_deaths"=obs.long[,3],
"excess_pi_deaths"=excess_deaths.long[,3]
)
```
```{r fig1, fig.width=7.3, fig.height=6.9, fig.cap=paste0('Figure 1: Observed weekly death rate vs seasonal baseline (+/-95% Prediction Interval). The black line shows the observed proportion of deaths that were due to Pneumonia & Influenza (P&I) per week. The red line and shaded area represent the 95% Prediction Interval. The latest P&I data is for the week ending', max(dates1)+6) }
par(mfrow=c(5,4), mar=c(2,4,2,1))
#states.cdc.order <- c(states.cdc[states.cdc %in% state.abb],
# states.cdc[!(states.cdc %in% state.abb)])
#plot.state.indices <- match(states.cdc.order,dimnames(pred)[[2]] )
plot.state.rank <- cbind.data.frame(state.index=1:dim(rr)[2],state.rank= rank(-rr[dim(rr)[1],,1]))
plot.state.rank <- plot.state.rank[order(plot.state.rank$state.rank),]
plot.state.indices <- plot.state.rank$state.index
for(i in plot.state.indices){
for(j in 1:dim(pred)[3]){
y.range1<-range(c( pred[select.indices,,]/denom[select.indices,,],obs[select.indices,,]/denom[select.indices,,], upi[select.indices,,]/denom[select.indices,,]))
if(states[i] %in% state.abb ){
state.name.plot <-
state.name[match(states[i],state.abb)]