-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaysOfCode.Rmd
More file actions
256 lines (172 loc) · 11.1 KB
/
DaysOfCode.Rmd
File metadata and controls
256 lines (172 loc) · 11.1 KB
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
---
title: "Days of Code Austin 2018"
author: "Carlos Santillan"
date: "February 10, 2018"
#output: md_document
output: html_document
---
# Analysis of Opiod drug overdoses data from the CDC
Carlos Santillan
csantill@gmail.com
https://www.linkedin.com/in/carlos-santillan/
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r libraries ,echo=FALSE, Warning=FALSE,message=FALSE}
library(knitr)
library(tidyverse)
library(ggplot2)
library(fiftystater)
data("fifty_states") # this line is optional due to lazy data loading
```
## Load the data
Load CDC data
Dataset: Underlying Cause of Death, 1999-2016
### Load Monthly Data
Group By: State, Year, Month, ICD Chapter
ICD-10 Codes:
* X40 (Accidental poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics)
* X41 (Accidental poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified)
* X42 (Accidental poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified)
* X43 (Accidental poisoning by and exposure to other drugs acting on the autonomic nervous system)
* X44 (Accidental poisoning by and exposure to other and unspecified drugs, medicaments and biological substances)
* X60 (Intentional self-poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics)
* X61 (Intentional self-poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified)
* X62 (Intentional self-poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified)
* X63 (Intentional self-poisoning by and exposure to other drugs acting on the autonomic nervous system)
* X64 (Intentional self-poisoning by and exposure to other and unspecified drugs, medicaments and biological substances)
* Y10 (Poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics, undetermined intent)
* Y11 (Poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified, undetermined intent),
* Y12 (Poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified, undetermined intent)
* Y13 (Poisoning by and exposure to other drugs acting on the autonomic nervous system, undetermined intent)
* Y14 (Poisoning by and exposure to other and unspecified drugs, medicaments and biological substances, undetermined intent)
Source:
Centers for Disease Control and Prevention, National Center for Health Statistics. Underlying Cause of Death 1999-2016 on CDC WONDER Online Database, released December, 2017. Data are from the Multiple Cause of Death Files, 1999-2016, as
compiled from data provided by the 57 vital statistics jurisdictions through the Vital Statistics Cooperative Program. Accessed at http://wonder.cdc.gov/ucd-icd10.html on Feb 10, 2018 4:36:12 PM
* Note: Year, Month data for the state of South Dakota and North Dakota was unavailable due to CDC Suppression, data was inputed by floor (AnnualDeaths/ 12 ) for each year
Use of this data allowed under https://wonder.cdc.gov/DataUse.html#
CDC Data was generated from the following Query https://wonder.cdc.gov/controller/saved/D76/D29F799
```{r loadmonthlydata, echo=FALSE}
cdcMonthlyStateData <- read_tsv("data/statefull_2018.txt")
colnames(cdcMonthlyStateData) <- make.names(colnames(cdcMonthlyStateData)) # Set standard names (remove spaces, etc)
cdcMonthlyStateData$Notes <- cdcMonthlyStateData$Notes <- NULL
cdcMonthlyStateData$Deaths <- as.integer(cdcMonthlyStateData$Deaths)
cdcMonthlyStateData <- cdcMonthlyStateData %>% separate(Month.Code,c("yr","Month.Code"))
cdcMonthlyStateData$Year<- as.character(cdcMonthlyStateData$Year)
cdcMonthlyStateData$YearMonth <- paste(cdcMonthlyStateData$Year , cdcMonthlyStateData$Month.Code,sep="-")
cdcMonthlyStateData <- cdcMonthlyStateData %>% select(
State,YearMonth,Year,Month.Code,Deaths)
#save the RDS
saveRDS(cdcMonthlyStateData,"data/cdcMonthlyStateData.rds")
saveRDS(cdcMonthlyStateData,"shiny/data/cdcMonthlyStateData.rds")
```
### Load Annual Data
Group By: State, Year, ICD Chapter
ICD-10 Codes:
* X40 (Accidental poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics)
* X41 (Accidental poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified)
* X42 (Accidental poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified)
* X43 (Accidental poisoning by and exposure to other drugs acting on the autonomic nervous system)
* X44 (Accidental poisoning by and exposure to other and unspecified drugs, medicaments and biological substances)
* X60 (Intentional self-poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics)
* X61 (Intentional self-poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified)
* X62 (Intentional self-poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified)
* X63 (Intentional self-poisoning by and exposure to other drugs acting on the autonomic nervous system)
* X64 (Intentional self-poisoning by and exposure to other and unspecified drugs, medicaments and biological substances)
* Y10 (Poisoning by and exposure to nonopioid analgesics, antipyretics and antirheumatics, undetermined intent)
* Y11 (Poisoning by and exposure to antiepileptic, sedative-hypnotic, antiparkinsonism and psychotropic drugs, not elsewhere classified, undetermined intent),
* Y12 (Poisoning by and exposure to narcotics and psychodysleptics [hallucinogens], not elsewhere classified, undetermined intent)
* Y13 (Poisoning by and exposure to other drugs acting on the autonomic nervous system, undetermined intent)
* Y14 (Poisoning by and exposure to other and unspecified drugs, medicaments and biological substances, undetermined intent)
Source:
Centers for Disease Control and Prevention, National Center for Health Statistics. Underlying Cause of Death"
"1999-2016 on CDC WONDER Online Database, released December, 2017. Data are from the Multiple Cause of Death Files, 1999-2016, as"
"compiled from data provided by the 57 vital statistics jurisdictions through the Vital Statistics Cooperative Program. Accessed"
"at http://wonder.cdc.gov/ucd-icd10.html on Feb 13, 2018 11:26:09 AM"
Use of this data allowed under https://wonder.cdc.gov/DataUse.html#
CDC Data was generated from the following Query https://wonder.cdc.gov/controller/saved/D76/D29F799
https://wonder.cdc.gov/controller/saved/D76/D30F044
```{r loadAnnualdata, echo=FALSE}
cdcAnnualData <- read_tsv("data/statefull_annual_2018.txt")
colnames(cdcAnnualData) <- make.names(colnames(cdcAnnualData)) # Set standard names (remove spaces, etc)
cdcAnnualData$Notes <- cdcAnnualData$Notes <- NULL
cdcAnnualData$Year<- as.character(cdcAnnualData$Year)
#str(cdcAnnualData)
cdcAnnualData <- cdcAnnualData %>% select(State,Year,Deaths,Population,Crude.Rate,Age.Adjusted.Rate)
cdcAnnualData %<>% arrange(State,desc(Year)) %>% group_by(State) %>%mutate(PercentChange = round(((Deaths - lead(Deaths)) * 100 / lead(Deaths)),digits=2))
# Calculate the US totals
#us_agg_data <- cdcAnnualData %>% group_by(Year) %>% summarise(Deaths=sum(Deaths), Population = #sum(Population),Crude.Rate = format((sum(Deaths) / sum(Population)) * 100000, big_mark = "," ,digits = 2))
#us_agg_data ["State"] <- "US Aggregated"
#cdcAnnualData <- cdcAnnualData %>% rbind(us_agg_data)
```
## Load Geo data
Latitudeo and Longitude Coordinates for each state were downloaded from https://inkplant.com/code/state-latitudes-longitudes
```{r loadGeoData, echo=FALSE}
state.geom <- read.csv("data/state_geom.csv")
state.geom$name = as.character(state.geom$name)
saveRDS(state.geom,"data/state_geom.rds")
saveRDS(state.geom,"shiny/data/state_geom.rds")
# Join geocoordinates with CDC data
cdcAnnualData <- left_join(cdcAnnualData,state.geom,by=c('State'='name'))
cdcAnnualData <- cdcAnnualData %>% select(State,Year,Deaths,Population,PercentChange,Crude.Rate,Age.Adjusted.Rate,Lat=latitude,Long=longitude)
saveRDS(cdcAnnualData,"data/cdcAnnualData.rds")
saveRDS(cdcAnnualData,"shiny/data/cdcAnnualData.rds")
cdcMonthlyStateData <- left_join(cdcMonthlyStateData,state.geom,by=c('State'='name'))
cdcMonthlyStateData <- cdcMonthlyStateData %>% select(State,YearMonth,Year,Month.Code,Deaths,Lat=latitude,Long=longitude)
```
### Exploration
```{r exploreMonthly, echo=FALSE}
str(cdcMonthlyStateData)
head(cdcMonthlyStateData)
summary(cdcMonthlyStateData)
print("Deaths reported in 2016 ")
print(cdcMonthlyStateData %>% filter(Year=="2016") %>% select(Deaths) %>%sum)
print("Deaths reported in the state of Texas 2016 ")
kable( cdcMonthlyStateData %>% filter(Year=="2016",State=="Texas") %>% select(Deaths) %>%sum)
```
```{r plottexas, echo=FALSE}
grp_state <- cdcMonthlyStateData %>% group_by(State,YearMonth)%>% select(State,YearMonth,Deaths)
tx <- filter(grp_state,State=="Texas")
head(tx)
ggplot(tx,aes(x=YearMonth, y=Deaths))+
geom_point() +
ggtitle("Deaths attributed to opiods for the state of Texas","1999-2016")
ggplot(tx,aes(x=YearMonth, y=Deaths,group=1))+
geom_line(stat="identity") +
stat_smooth(method='loess') +
ggtitle("Deaths attributed to opiods for the state of Texas","1999-2016")
```
```{r plottus, echo=FALSE}
grp_us <- cdcAnnualData %>% group_by(Year)%>% select(Year,Deaths) %>%summarise(Deaths= sum(Deaths))
grp_us2 <- grp_us %>% arrange(desc(Year)) %>% mutate (PercentChange = (Deaths-lead(Deaths)) * 100 / lead(Deaths))
kable(grp_us2 %>% arrange(desc(Year)),format.args = list(decimal.mark = ".", big.mark = ","),caption="Deaths attributed to opiods in the United State")
ggplot(grp_us,aes(x=Year, y=Deaths))+
geom_point() +
ggtitle("Deaths attributed to opiods in the United States","1999-2016")
ggplot(grp_us,aes(x=Year, y=Deaths,group=1))+
geom_line() +
stat_smooth(method="glm") +
ggtitle("Deaths attributed to opiods in the United States","1999-2016")
```
```{r plotmap, echo=FALSE}
grp_state2 <-cdcAnnualData %>% filter(Year=="2016") %>%group_by(State)%>% summarise(Deaths= sum(Deaths)) %>% arrange(desc(Deaths))
kable(head(grp_state2),format.args = list(decimal.mark = " ", big.mark = ","),caption="States with Highest number of deaths for 2016")
kable(tail(grp_state2),format.args = list(decimal.mark = " ", big.mark = ","),caption="States with Lowest number of deaths for 2016")
grp_state2$State <- tolower(grp_state2$State)
p <- ggplot(grp_state2, aes(map_id = State)) +
# map points to the fifty_states shape data
scale_fill_gradient(low="white", high="red")+
# Diverging color scheme
geom_map(aes(fill = log10(Deaths)), map = fifty_states) +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") +
theme(legend.position = "bottom",
panel.background = element_blank())
p + fifty_states_inset_boxes()
```
``` {r}
sessionInfo(package = NULL)
```