-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervention_processing.R
180 lines (154 loc) · 5 KB
/
intervention_processing.R
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
library(tidyverse); theme_set(theme_minimal())
# import raw intervention data
data_path = "data/intervention_googlesheet_raw_20200702.csv"
raw = read_csv(data_path)[ 1:51, ] # exclude total, notes rows
fips = read_csv("data/FIPS.csv")
# parse date when data was pulled from the source
dataDate = as.Date(gsub('\\.csv$','', str_extract(data_path, "[0-9]{8}\\.csv")), format="%Y%m%d")
#' Scope down to only columns about policies that are directly
#' involved in limiting transmission.
# : (1) enforce, (2) release column
int.full = list(
"school_close" = c(
"Date closed K-12 schools"
, "")
,"daycare_close" = c(
"Closed day cares"
, "")
,"ban_nursing_home_visit" = c(
"Date banned visitors to nursing homes"
, "")
,"stay_home" = c(
"Stay at home/ shelter in place"
, "End/relax stay at home/shelter in place")
,"close_business" = c(
"Closed non-essential businesses"
, "Began to reopen businesses")
,"close_restaurant" = c(
"Closed restaurants except take out"
, "Reopen restaurants")
,"close_gym" = c(
"Closed gyms"
, "Reopened gyms")
,"close_movie" = c(
"Closed movie theaters"
, "Reopened movie theaters")
,"face_mask_public" = c(
"Mandate face mask use by all individuals in public spaces"
, "")
,"face_mask_business" = c(
"Mandate face mask use by employees in public-facing businesses"
, "")
,"state_of_emergency" = c(
"State of emergency"
, "")
,"suspend_medical" = c(
"Suspended elective medical/dental procedures"
, "Resumed elective medical procedures")
)
int.full.labels = c(
"school_close" = "School closure"
,"daycare_close" = "Daycare closure"
,"ban_nursing_home_visit" = "Nursing home visit ban"
,"stay_home" = "Stay at home"
,"close_business" = "Close non-essential businesses"
,"close_restaurant" = "Close restaurants"
,"close_gym" = "Close gyms"
,"close_movie" = "Close movie theaters"
,"face_mask_public" = "Face mask mandated in public"
,"face_mask_business" = "Face mask mandated in businesses"
,"state_of_emergency" = "Declared state of emergency"
,"suspend_medical" = "Suspend non-essential medical services"
)
dat = lapply(names(int.full), function(int){
x = int.full[[int]]
dateStart = raw[[x[1]]] %>%
# fix 2-digit years
gsub(pattern = "/20$", replacement = "/2020") %>%
as.Date("%m/%d/%Y") %>%
as.character
if(x[2]==""){
dateEnd = ifelse(is.na(dateStart), NA, as.character(dataDate))
} else {
dateEnd = raw[[x[2]]] %>%
# fix 2-digit years
gsub(pattern = "/20$", replacement = "/2020") %>%
as.Date("%m/%d/%Y") %>%
as.character
dateEnd = ifelse(is.na(dateEnd) & !is.na(dateStart), as.character(dataDate), dateEnd)
}
data.frame(
state_name = raw$State
, intervention = int
, dateStart = dateStart
, dateEnd = dateEnd
)
}) %>%
do.call(what = rbind) %>%
mutate(
dateStart = as.Date(dateStart)
, dateEnd = as.Date(dateEnd)
)
#' Filtering out interventions with start date later than data retrieval date.
dat = dat %>% filter(dateStart <= dateEnd, !is.na(dateStart))
#' # Cluster interventions based on co-occurrence
dat$dates = with(dat,
mapply( function(tmin,tmax) {
if( is.na(tmin)|is.na(tmax) ){ return( character(0) ) }
seq(tmin, tmax, by = "day") %>% as.character
}
, tmin = dateStart
, tmax = dateEnd
))
dat = dat %>%
select(state_name, intervention, dates) %>%
unnest(cols = dates) %>%
mutate(on = 1) %>%
spread(intervention, on, fill = 0) %>%
mutate(dates = as.Date(dates))
dat.clust = dat %>%
select(-state_name, -dates)
colnames(dat.clust) = int.full.labels[colnames(dat.clust)]
pdf("output/plot/fig_s2.pdf", height = 8, width = 9)
dat.clust[ , -which(colnames(dat.clust)=="Declared state of emergency")] %>%
as.matrix %>%
t %>%
dist %>%
hclust %>%
plot
dev.off()
#' # Group interventions
int.groups = list(
"school_close" = "school_close"
, "state_of_emergency" = "state_of_emergency"
, "daycare_close" = "daycare_close"
, "face_mask" = c(
"face_mask_public"
, "face_mask_business"
)
, "lifestyle_close" = c(
"close_restaurant"
, "close_gym"
, "close_movie"
)
, "lv3_stay_home" = c(
"stay_home"
, "close_business"
)
, "ban_nursing_home_visit" = "ban_nursing_home_visit"
, "suspend_medical" = "suspend_medical"
)
# export start and end dates
x = data.frame(intgr = names(int.groups))
x$intervention = int.groups
x %>%
unnest(col= intervention) %>%
right_join(intStartEnd, by = "intervention") %>%
select(-intervention) %>%
rename(intervention = intgr) %>%
group_by(state_name, intervention) %>%
summarize(
dateStart = min(dateStart)
, dateEnd = max(dateEnd)
) %>%
write_csv("data/intervention_googlesheet.csv")