-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-analysis-summaries.Rmd
247 lines (190 loc) · 5.18 KB
/
03-analysis-summaries.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
---
title: "Summary statistics, 2016-2019"
date: "`r Sys.Date()`"
output:
html_document:
df_print: paged
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_dir = "docs") })
---
By **Christian McDonald**, Assistant Professor of Practice\
School of Journalism and Media, Moody College of Communication\
University of Texas at Austin
----
## Purpose of the notebook
Each of the other analysis notebooks in the project export two types of summaries for each measure: statewide rates and averages of hospital rates. This notebook simply brings them all together into a single data file for export.
There are some additional data combinations for interactives.
```{r setup, echo=T, results='hide', message=F, warning=F}
library(fs)
library(tidyverse)
library(jsonlite)
library(janitor)
# suppresses grouping warning
options(dplyr.summarise.inform = FALSE)
```
## Summary imports
```{r imports}
# set up folder
data_dir <- "data-processed"
# find the files
summary_files <- dir_ls(data_dir, recurse = TRUE, regexp = "_summary")
# peek at them
summary_files
# read and combine
summary_data <- summary_files %>%
map_dfr(read_rds) %>%
arrange(SUMMARY, CATEGORY, YR)
# finished data
summary_data
```
### Summary exports
```{r exports_summary}
summary_data %>%
write_csv("exports/summary_data.csv")
```
## Interactives exports
### Import pcsec and epi rates, providers
```{r hospitals_import}
pcsec <- read_rds("data-processed/ahrq_pcsec_rate_hosp_yr.rds")
epi <- read_rds("data-processed/lf_epi_rate_hosp_yr.rds")
vbac <- read_rds("data-processed/ahrq_vbac_rate_hosp_yr.rds")
providers_full <- read_rds("data-processed/providers_full.rds")
```
### Hospitals table
Filters and assembles 2019 data for hospitals table.
```{r hosp_table_2019}
## filter and select for 2019
pcsec_2019 <- pcsec %>%
filter(YR == 2019) %>%
ungroup() %>%
select(THCIC_ID, PCRATE)
epi_2019 <- epi %>%
filter(YR == 2019) %>%
ungroup() %>%
select(THCIC_ID, EPIRATE)
vbac_2019 <- vbac %>%
filter(YR == 2019) %>%
ungroup() %>%
select(THCIC_ID, VBACRATE)
```
### Assemble table for output
We are starting with the providers_full here, but some of our hospitals are filtered out earlier because they didn't have data (or enough) in 2019, so they might not have a rate.
```{r hosp_table_assemble}
# assemble
table_2019 <- providers_full %>%
rename(PROVIDER_NAME = PROVIDER_NAME_CLEANED) %>%
filter(!is.na(PROVIDER_CITY)) %>%
left_join(pcsec_2019, by = "THCIC_ID") %>%
left_join(epi_2019, by = "THCIC_ID") %>%
# filter out cities without either measure
filter(
!(
is.na(PCRATE) &
is.na(EPIRATE)
)
)
# write
table_2019 %>%
write_csv("exports/table_2019.csv")
# peek
table_2019
```
## Data export: charts by year
### Assemble Texas averages
```{r chart_texas}
chart_texas <- summary_data %>%
filter(
SUMMARY == "TX",
CATEGORY %in% c("EPISIOTOMY", "PRIMARY_CESAREAN", "VAGINAL_BIRTH_AFTER_CESAREAN")
) %>%
select(-MEASUREMENT) %>%
pivot_wider(names_from = CATEGORY, values_from = VALUE) %>%
rename(
EPIRATE = EPISIOTOMY,
PCRATE = PRIMARY_CESAREAN,
VBACRATE = VAGINAL_BIRTH_AFTER_CESAREAN,
THCIC_ID = SUMMARY
) %>%
mutate(
PROVIDER_NAME = "Texas",
PROVIDER_CITY = "",
PROVIDER_ADDRESS = ""
) %>%
# select to order
select(
YR, THCIC_ID, PROVIDER_NAME, PROVIDER_CITY, PROVIDER_ADDRESS, PCRATE, EPIRATE, VBACRATE
) %>%
arrange(YR)
# peek
chart_texas
```
### Assemble charts by year
Again, some hospitals won't have rates if they didn't have enough deliveries for that year.
```{r chart_rates}
# build table from data
chart_rates <- providers_full %>%
rename(PROVIDER_NAME = PROVIDER_NAME_CLEANED) %>%
ungroup() %>%
left_join(
epi %>% ungroup() %>% select(YR, THCIC_ID, EPIRATE)
) %>%
left_join(
pcsec %>% ungroup() %>% select(YR, THCIC_ID, PCRATE)
) %>%
left_join(
vbac %>% ungroup() %>% select(YR, THCIC_ID, VBACRATE)
) %>%
filter(
# filters closed/old hospitals
!is.na(PROVIDER_CITY),
!(
is.na(PCRATE) &
is.na(EPIRATE)
)
)
# peek
chart_rates
```
### Bind Texas to rates
And write to JSON.
```{r chart_data}
chart_data <- chart_texas %>%
bind_rows(chart_rates)
chart_data %>%
write_json("exports/chart_data.json")
# peek
chart_data %>% head(12)
```
#### Chart data for print
Right now there are now special needs for print beyond format, but updates can go here if they arise.
```{r chart_data_print}
# set new df
chart_data_print <- chart_data
# write the data
chart_data_print %>%
write_csv("exports/chart_data_print.csv")
# peek at the data
chart_data_print %>% head(20)
```
### Alt bind method for json
This adds the Texas values for a given year to each hospitals' yearly data.
```{r chart_data_v2}
chart_data_v2 <- chart_rates %>%
left_join(
chart_texas %>%
select(YR, PCRATE, EPIRATE, VBACRATE) %>%
rename(
TXPCRATE = PCRATE,
TXEPIRATE = EPIRATE,
TXVBACRATE = VBACRATE
),
by = "YR")
chart_data_v2 %>%
write_json("exports/chart_data_v2.json")
## peek
chart_data_v2
```
## Closing
```{r close}
# A klaxon to indicate the processing is complete
beepr::beep(4)
```