-
Notifications
You must be signed in to change notification settings - Fork 0
/
n2015_1.Rmd
301 lines (159 loc) · 7.54 KB
/
n2015_1.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
---
title: "n2015.1"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(haven);library(tidyverse);library(survey)
```
```{r get the core data for Q1/Q3}
df <- read_stata("G:/NIS_2015_Core.dta")
names(df)
```
This is the first 3 quarters for 2015. This is in ICD9 format. The Core file does not contain the dx and pr codes. So, we need to get these codes and merge with df file first.
```{r get the data that contains dx and pr codes}
dx <- read_stata("G:/NIS_2015Q1Q3_DX_PR_GRPS.dta")
```
Merging data to get the dx and pr codes into the main dataframe:
```{r we need to merge}
# we need to merge and keep only those observations that are in the dx df as those are the ones that are q1 - q3
df1 <- left_join(dx, df, by = "KEY_NIS")
df1
```
Now, prior to further analysis, remove the variables that are not needed in this study.
Also:
1. Keep only primary surgery; remove patients with prior CABG
2. Remove patients with concomitant valve surgery
```{r}
n15.1 <- df1
pricabg <- as.character(c("V4581")) # prior MI
a <- pricabg
n15.1$pricabg <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
n15.1 %>% count(pricabg)
# isolate CABG cases from this dataframe.
cabg <- as.character(c('3610','3611','3612','3613','3614','3615','3617','3619'))
n15.1$cabg <- with(n15.1,ifelse((PR1 %in% cabg | PR2 %in% cabg | PR3 %in% cabg | PR4 %in% cabg | PR5 %in% cabg),"yes","no"))
n15.1 %>% count(cabg)
```
Remove patients with prior CABG:
```{r remove prior CABG patients}
df2 <- n15.1 %>% filter(cabg == "yes")
df3 <- df2 %>% filter(pricabg == "no")
dim(df3) # df2 contains only patients with primary CABG surgery and no prior CABG surgery.
```
Remove patients with concomitant valve surgery too:
```{r remove patients undergoing concomitant valve surgery}
# valve replacement/ valve repair
valve <- as.character(c('3511','3512','3513','3514','3521','3522','3523','3524','3526','3525','3527',
'3528'))
a <- valve
df3$valve <- with(df3, ifelse((PR1 %in% a | PR2 %in% a | PR3 %in% a | PR4 %in% a | PR4 %in% a | PR5 %in% a | PR6 %in% a | PR7 %in% a | PR8 %in% a | PR10 %in% a | PR11 %in% a | PR12 %in% a | PR13 %in% a | PR14 %in% a | PR15 %in% a), "yes","no"))
df3 %>% count(valve)
df4 <- df3 %>% filter(valve == "no")
dim(df4)
```
```{r change name of df to get more variables}
n15.1 <- df4
```
```{r prior conditions}
priormi <- as.character(c("412")) # prior MI
a <- priormi
n15.1$priormi <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
n15.1 %>% count(priormi)
```
```{r}
priorpci <- as.character(c("V4582")) # prior PCI
a <- priorpci
n15.1$priorpci <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a), "yes","no"))
n15.1 %>% count(priorpci)
```
```{r}
chf <- as.character('4280','4281','4282','4283','4284','4285','4286','4287','4288') # ICD9 codes for CHF
a <- chf
n15.1$chf <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a), "yes","no"))
n15.1 %>% count(chf)
```
```{r}
shock <- as.character(c("78551")) # shock
a <- shock
n15.1$shock <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
n15.1 %>% count(shock)
```
```{r}
stemi <- as.character(c("41071")) # stemi
a <- stemi
n15.1$stemi <- with(n15.1, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
n15.1 %>% count(stemi)
```
```{r change df name to get more variables }
df <- n15.1
```
```{r carotid disease}
carotid.d <- as.character(c("43310")) # carotid artery disease
a <- carotid.d
df$carotid <- with(df, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a), "yes","no"))
table(df$carotid)
```
```{r}
pristroke <- as.character(c("V1254","4380")) # prior stroke
a <- pristroke
df$pristroke <- with(df, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
table(df$pristroke)
```
```{r}
priicd <- as.character(c("V4502")) # prior ICD implant
a <- priicd
df$priicd <- with(df, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
table(df$priicd)
```
```{r}
dementia <- as.character(c("2900","2941","2942","2948","3310","3311","3312","33182")) # dementia
a <- dementia
df$dementia <- with(df, ifelse((DX1 %in% a | DX2 %in% a | DX3 %in% a | DX4 %in% a | DX4 %in% a | DX5 %in% a | DX6 %in% a | DX7 %in% a | DX8 %in% a | DX10 %in% a | DX11 %in% a | DX12 %in% a | DX13 %in% a | DX14 %in% a | DX15 %in% a ), "yes","no"))
table(df$dementia)
```
```{r get severity and hospital dataframes}
sev <- read_stata('G:/NIS_2015Q1Q3_Severity.dta')
hosp <- read_stata("G:/NIS_2015_Hospital.dta")
```
```{r get severity into the df}
# merge
df_1 <- merge(df, sev, by = "KEY_NIS")
df_2 <- merge(df_1, hosp, by = "HOSP_NIS")
names(df_2) <- tolower(names(df_2))
names(df_2)
```
```{r}
# use of BITA for CABG
bita <- as.character(c('3616'))
df_2$bita <- with(df_2,ifelse((pr1 %in% bita | pr2 %in% bita | pr3 %in% bita | pr4 %in% bita | pr5 %in% bita),"yes","no"))
table(df_2$bita)
```
```{r save df for now}
write_csv(df_2, "H:/bita_nis/df2015.1.csv")
```
```{r}
m1 <- df_2 %>% dplyr::select(age, died, discwt.x, dispuniform, drg, dx1,
dx2, dx3, dx4, dx5, dx6, dx7, dx8, dx9, dx10,
dx11, dx12, dx13, dx14, dx15, female, key_nis, los, nis_stratum.x,
pay1, pr1, pr2,
pr3, pr4, pr5, pr6, pr7, pr8, pr9,pr10, pr11,
pr12, pr13, pr14, pr15, race, totchg,
year.x, cabg,
bita, pricabg, valve, priormi, priorpci,
chf, shock, stemi, cm_aids, cm_alcohol,
cm_anemdef, cm_arth, cm_bldloss, cm_chf, cm_chrnlung,
cm_coag, cm_depress, cm_dm, cm_dmcx, cm_drug, cm_htn_c,
cm_hypothy, cm_liver, cm_lymph, cm_lytes, cm_mets,
cm_neuro, cm_obese, cm_para, cm_perivasc, cm_psych,
cm_pulmcirc, cm_renlfail, cm_tumor, cm_ulcer, cm_valve,
cm_wghtloss, hosp_nis, discwt.y,
hosp_bedsize, hosp_locteach,
hosp_region, nis_stratum.y, year.y)
names(m1)
```
```{r}
write_csv(m1, "H:/bita_nis/df/m2015_1.csv")
```