forked from pohjois-savon-tietoallas/regstudies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnClassificationsExcelDemo.Rmd
347 lines (244 loc) · 12.7 KB
/
OwnClassificationsExcelDemo.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
---
title: "How to use own classifications with regstudies and count stay lengths"
author: "Markku Kuismin"
date: "Last compiled on `r Sys.setlocale(locale = 'English'); format(Sys.time(), '%d %B %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r klippy, echo=FALSE, include=TRUE}
klippy::klippy(position="right")
```
## {.tabset}
### Introduction
The classification of interest are stored in an external (Excel) file which contains strings in SQL LIKE\% -format. We want to extend the data set with dichotomous variables which describe the presence of a disease based on these external classification codes.
One can use the resulting table to compute simple statistics about the diseases, such as counts and proportions.
<a href="#top">Back to top</a>
### How to create dichotomous variables manually
In these illustrative example we use the built-in demo data sets of $\verb#regstudies#$:
1. $\verb#sample_regdata#$,
2. $\verb#sample_cohort#$.
First, load some packages,
```{r message=F, warning=F}
library(readxl)
library(tidyverse)
library(lubridate)
```
In this example we use the example data sets of $\verb#regstudies#$ so we first have to install the package,
```{r message=F, warning=F}
library(devtools)
#devtools::install_github("markkukuismin/regstudies")
library(regstudies)
```
The initial idea is to use the function $\verb#%like%#$ to use LIKE\% classifications and function $\verb#left_join0#$ to create the dichotomous variables,
```{r}
`%like%` <- function(x, pattern){
pattern <- ifelse(is.na(pattern),"-",pattern)
pattern <- sapply(pattern, function(z){
if (!substr(z, 1, 1) == "%") {
z <- paste("^", z, sep="")
} else {
z <- substr(z, 2, nchar(z) )
}
if (!substr(z, nchar(z), nchar(z)) == "%") {
z <- paste(z, "$", sep="")
} else {
z <- substr(z, 1, nchar(z)-1 )
}
return(z)
})
grepl(pattern=paste(pattern, collapse = "|"), x=x)
}
left_join0 <- function(x,y,...,fill = 0L){
z <- left_join(x,y,...)
tmp <- setdiff(names(z), names(x))
z <- replace_na(z, setNames(as.list(rep(fill, length(tmp))), tmp))
z
}
```
The external classification codes can be found in the file "taulukot_sairausluokitus.xlsx",
```{r message = F}
sl <- readxl::read_excel("Data/taulukot_sairausluokitus.xlsx")
slnim <- sl %>% select(sair=Lyhenne,Sairaus)
```
Load the built-in cohort and register data from the $\verb#regstudies#$ package and filter persons whose date of discharge ($\verb#disc_date#$ in the data set) was two years prior the index day ($\verb#postingdate#$), and after the admission date ($\verb#adm_date#$),
```{r}
dgraj <- regstudies::sample_regdata %>%
dplyr::inner_join(regstudies::sample_cohort) %>%
dplyr::filter(adm_date <= postingdate & disc_date + 365.25*2 >= postingdate)
```
Run the following loop to create dichotomous variables for each disease,
```{r message=F, warning=F}
idvar <- 'personid'
kooste <- regstudies::sample_cohort %>% select(personid)
for(i in 1:nrow(sl)){
sairdg <- unlist(strsplit(as.character(sl[i, 'sairdiag']), ", "))
sairdgpl <- unlist(strsplit(as.character(sl[i, 'sairdiagpl']), ", "))
lyh <- c(idvar, sl[i, 'Lyhenne'])
nimi <- sl[i, 'Sairaus']
tmpsair <- dgraj %>%
mutate(pois = CODE1 %like% sairdgpl, sair = if_else(pois, 0, as.numeric(CODE1 %like% sairdg))) %>%
group_by_at(idvar) %>%
summarise(CODE1 = max(sair))
attr(tmpsair[['CODE1']],'label') <- nimi
names(tmpsair) <- lyh
kooste <- kooste %>%
left_join0(tmpsair,fill=0)
}
```
After the loop is complete, each disease is defined as an dichotomous variable,
```{r}
dplyr::tibble(kooste)
```
One can compute counts and proportions from the data set, e.g, as follows,
```{r}
osuudet <- kooste %>%
summarise_at(
vars(-personid),
list(~ sum(., na.rm = TRUE), ~ mean(., na.rm = TRUE))
)
taul <- osuudet %>%
tidyr::pivot_longer(
cols=everything(),
names_to=c("Disease","stat"),
names_pattern="^(.*)_([s|m][u|e][m|a].*)"
) %>%
tidyr::pivot_wider(names_from=stat,values_from=value)
dplyr::tibble(taul)
```
<a href="#top">Back to top</a>
### Make dichotomous variables with $\verb#regstudies#$
Here we present how to create dichotomous variables for each disease with the functions of $\verb#regstudies#$.
We will go through the process as whole so we will load and filter the same data sets as in the procedures described in the section "How to create dichotomous variables manually".
Load the external classification codes,
```{r}
sl <- readxl::read_excel("Data/taulukot_sairausluokitus.xlsx")
```
Function $\verb#regstudies::make_regex#$ can make regular expressions from LIKE\% classification. To apply this function with function $\verb#regstudies::make_indicators#$ (see below) we have to do the following premodifications,
1. Add a $\verb#score#$ variable of all ones into the regular expression table. This variable is used to define each disease of interest as dichotomous variables. In particular, here these variables are coded as zeroes and ones,
2. rename some variables of the regular expression table:
i) rename the variable holding the regular expressions as "icd10",
ii) rename the variable holding the abbreviations of the diseases as "class" and the variable holding the diseases in plain language as "label".
Here is an example how to make the premodifications,
```{r}
sl_regexp <- sl %>%
regstudies::make_regex(classname = Lyhenne, diagnosis = sairdiag, diagnosis.rm = sairdiagpl) %>%
dplyr::mutate(score = 1) %>%
dplyr::rename(icd10 = regex, Lyhenne = label) %>%
dplyr::left_join(sl %>% select(Sairaus, Lyhenne), by = "Lyhenne") %>%
dplyr::rename(class = Lyhenne, label = Sairaus)
```
Although I stressed to add labels to the regular expression table before, labels are not needed to make the dichotomous variables.
Load the built-in cohort and register data from the $\verb#regstudies#$ package and filter persons whose date of discharge ($\verb#disc_date#$ in the data set) was two years prior the index day ($\verb#postingdate#$), and after the admission date ($\verb#adm_date#$),
Instead of looping, we apply the function $\verb#regstudies::make_indicators#$ to create the dichotomous variables.
Function $\verb#regstudies::make_indicators#$ is a multitasking procedure which takes cohort and register data as input data.
We can do all the same as before with the the function $\verb#regstudies::make_indicators#$,
1. filter date intervals within study interval,
2. add dichotomous variables to the data set.
Load the function,
Make a regular expression table from external file. Again, this is done just to show all the steps of the process,
```{r}
sl <- readxl::read_excel("Data/taulukot_sairausluokitus.xlsx")
```
```{r}
sl_regexp <- sl %>%
regstudies::make_regex(classname = Lyhenne, diagnosis = sairdiag, diagnosis.rm = sairdiagpl) %>%
dplyr::mutate(score = 1) %>%
dplyr::rename(icd10 = regex, Lyhenne = label) %>%
dplyr::left_join(sl %>% select(Sairaus, Lyhenne), by = "Lyhenne") %>%
dplyr::rename(class = Lyhenne, label = Sairaus)
```
We filter persons whose date of discharge is two years (parameter $\verb#time_before#$) prior the index day, and add dichotomous variables to the data set - also diseases which have zero frequency (parameter $\verb#add_zero_class#$ of the function),
```{r}
test <- regstudies::make_indicators(cohort_data = regstudies::sample_cohort,
reg_data = regstudies::sample_regdata,
adm_date = adm_date, disc_date = disc_date, index_date = postingdate,
time_before = 2*365.25, time_after = 0,
idnum = personid, codes = CODE1, diag_tbl = sl_regexp, add_zero_class = T)
```
The resulting tibble data frame is the same as the one constructed by looping,
```{r}
dplyr::tibble(test)
```
```{r}
dplyr::all_equal(kooste, test %>% dplyr::select(-postingdate, -gender), convert = T)
```
Compute the counts and proportions,
```{r message = F}
tidyr::pivot_longer(test, cols = colnames(test)[-c(1:3)]) %>%
dplyr::group_by(name) %>%
dplyr::summarise(sum = sum(value), '%' = mean(value)*100) %>%
dplyr::filter(sum != 0)
```
<p> </p>
#### Use built-in classification tables
<p> </p>
In addition to external classification file, one can also use the built-in classification tables of $\verb#regstudies#$ while using the function $\verb#regstudies::make_indicators#$ just by setting the parameter $\verb#diag_tbl#$ as $\verb#charlson#$ or $\verb#elixhauser#$,
```{r}
test.charlson <- regstudies::make_indicators(cohort_data = regstudies::sample_cohort,
reg_data = regstudies::sample_regdata,
adm_date = adm_date, disc_date = disc_date,
index_date = postingdate,
time_before = 2*365.25, time_after = 0,
idnum = personid, codes = CODE1, diag_tbl = charlson,
add_zero_class = T)
```
The dichotomous variables added to the register data are computed according to the table $\verb#regstudies::charlson_classes#$,
```{r}
dplyr::tibble(test.charlson)
```
<a href="#top">Back to top</a>
### Count the days in hospital with $\verb#regstudies::sum_stay_length#$
The function $\verb#regstudies::sum_stay_length#$ can be used to compute the number of hospital days from the index day during the washout period. The function also computes the days persons were hospitalized at a specific time length at the end of the washout period.
For example, compute the number of hospital days during last two years (730 days) from the $\verb#postingdate#$ of the example register data found in $\verb#regstudies#$ package. Moreover, compute also the number of hospital days during 60 days at the end of the two year washout period,
```{r}
Hosp.df <- regstudies::sample_cohort %>%
regstudies::sum_stay_length(., regstudies::sample_regdata,
idnum = personid,
adm_date = adm_date,
disc_date = disc_date,
index_date = postingdate,
wolen = 2*365,
ongoing_end_time = 60)
```
The tibble data frame is the cohort data ($\verb#sample_cohort#$) extended with two columns:
* $\verb#wo_total_time_hosp#$ which is the total number of hospitalization days during the two year washout period,
* $\verb#wo_end_time_hosp#$ which is the number of days during the last 60 days of the two year washout period,
```{r}
dplyr::tibble(Hosp.df)
```
This data set is easy to join with the data frame containing dichotomous variables for each disease,
```{r}
test <- test %>%
dplyr::left_join(Hosp.df %>% dplyr::select(-gender, -postingdate), by = "personid")
dplyr::tibble(test)
```
Just look at the last columns of the table,
```{r}
dplyr::tibble(test %>% select(last_col(offset=(8-1):0, everything())))
```
<p> </p>
#### Alternative procedure
<p> </p>
Package $\verb#regstudies#$ also includes alternative procedure to compute the hospitalization days. Moreover, this procedure also computes the total number of days persons have been in hospital care.
This is a greedy procedure and it will take some time it to finish,
```{r}
Hosp.df.1 <- regstudies::sample_cohort %>% sum_stay_length_v2(., user_data = regstudies::sample_regdata,
idnum = personid,
adm_date = adm_date,
disc_date = disc_date,
index_date = postingdate,
wolen = 2*365,
ongoing_end_time = 60
)
```
This returns $\verb#regstudies::sample_cohort#$ extended with three new columns:
* $\verb#wo_total_days#$ describes the number of days persons were hospitalized during the washout period,
* $\verb#wo_end_days#$ shows the number of days person was hospitalized during the end of the washout period,
* $\verb#total_days#$ is the total number of days person has been in hospital care,
```{r}
dplyr::tibble(Hosp.df.1)
```
Note: Although functions $\verb#sum_stay_length#$ and $\verb#sum_stay_length_v2#$ join data sets using $\verb#dplyr::left_join(.data, user_data)#$ without "$\verb#by=.#$" argument, one should be careful while joining data tables containing columns with identical names.
<a href="#top">Back to top</a>