-
Notifications
You must be signed in to change notification settings - Fork 0
/
n2013.Rmd
288 lines (161 loc) · 7.08 KB
/
n2013.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
---
title: "n2013"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(haven);library(tidyverse);library(survey)
```
```{r}
df <- read_csv("H:/cabg_pci/n2013need.csv")
```
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}
n13 <- df
pricabg <- as.character(c("V4581")) # prior MI
a <- pricabg
n13$pricabg <- with(n13, 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"))
n13 %>% count(pricabg)
```
Remove patients with prior CABG:
```{r remove prior CABG patients}
df2 <- n13 %>% filter(pricabg == "no")
dim(df2) # df2 contains only patients with primary CABG surgery and no prior CABG surgery.
```
```{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
df2$valve <- with(df2, 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"))
df2 %>% count(valve)
```
```{r remove valve patients too}
df3 <- df2 %>% filter(valve == "no")
dim(df3)
```
1. Keep only patients who underwent CABG; remove patients with underwent PCI:
```{r remove PCI patients}
df4 <- df3 %>% filter(cabg == "yes")
```
```{r change name of df to get more variables}
n13 <- df4
```
```{r prior conditions}
priormi <- as.character(c("412")) # prior MI
a <- priormi
n13$priormi <- with(n13, 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"))
n13 %>% count(priormi)
```
```{r}
priorpci <- as.character(c("V4582")) # prior PCI
a <- priorpci
n13$priorpci <- with(n13, 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"))
n13 %>% count(priorpci)
```
```{r}
chf <- as.character('4280','4281','4282','4283','4284','4285','4286','4287','4288') # ICD9 codes for CHF
a <- chf
n13$chf <- with(n13, 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"))
n13 %>% count(chf)
```
```{r}
shock <- as.character(c("78551")) # prior CABG
a <- shock
n13$shock <- with(n13, 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"))
n13 %>% count(shock)
```
```{r}
stemi <- as.character(c("41071")) # prior CABG
a <- stemi
n13$stemi <- with(n13, 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"))
n13 %>% count(stemi)
```
```{r change df name to get more variables }
df <- n13
```
```{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 var for n09 and convert them to lower case}
n13 <- df
names(n13)
names(n13) <- tolower(names(n13))
names(n13)
```
```{r getting the severity file and hospital files into the document}
sev <- read_stata("I:/NIS_2013_Severity.dta")
names(sev) <- tolower(names(sev))
names(sev)
sev2 <- sev %>% dplyr::select(hosp_nis, key_nis, cm_aids:cm_wghtloss)
names(sev2)
```
```{r merge with df and sev}
df2 <- left_join(n13, sev2, by = "key_nis")
names(df2)
```
1. Now we need to get the hospital information into the df:
```{r get hospital df}
hosp <- read_stata("I:/NIS_2013_Hospital.dta")
names(hosp) <- tolower(names(hosp))
names(hosp)
```
```{r merge both df together}
df2$hosp_nis <- df2$hosp_nis.x
df2$hosp_nis <- as.numeric(df2$hosp_nis)
hosp$hosp_nis <- as.numeric(hosp$hosp_nis)
df3 <- left_join(df2, hosp, by = "hosp_nis")
dim(df3)
```
```{r save df for now}
write_csv(df3, "H:/bita_nis/df2013.csv")
```
```{r}
m1 <- df3 %>% 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,
pci, bita, want, 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 df to folder}
write_csv(m1,"H:/bita_nis/df/m2013.csv")
```