Scoring Comorbidities per patient using the Comorbidity Package in R

539 views Asked by At

I am trying to score each patient in my data based using their ICD10 codes.

library(dplyr)
library(comorbidity)

set.seed(1)
x <- data.frame(
  pat_id = sample(100:999, size = 300, replace = TRUE),
  code = sample_diag(n = 300)
)

I can successfully create the Charlson scores and the output contains a column with the original patient ids pat_id

charlson <- comorbidity(x = x, id = "pat_id", code = "code", map = "charlson_icd10_quan", assign0 = TRUE, tidy.codes = TRUE)

I've created a row number ID so that I can join to the scores output

charlson_ids <- charlson %>% 
  mutate(id = row_number()) %>% 
  select(id,pat_id)

When I convert the charlson index to scores there is no patient id so I am assuming row 1 in the output from charlson above ties to row 1 in scores below??

scores <- score(charlson, weights = NULL, assign0 = FALSE)

scores_df <- data.frame(scores) %>% 
  mutate(id = row_number())

combined <- charlson_ids %>% 
  inner_join(scores_df, by = c("id"="id")) %>% 
  select(-id)

If anyone can suggest a more succinct way to get from individual ICD-10 codes per patient to a comorbidity score per patient I would be grateful for any feedback.

1

There are 1 answers

1
ellessenne On BEST ANSWER

the score() function returns a single value per row of charlson (the output of the comorbidity() function). Therefore, you can simplify the above as:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(comorbidity)

set.seed(1)
x <- data.frame(
  pat_id = sample(seq(5), size = 100, replace = TRUE),
  code = sample_diag(n = 100)
)
charlson <- comorbidity(x = x, id = "pat_id", code = "code", map = "charlson_icd10_quan", assign0 = TRUE, tidy.codes = TRUE)
charlson_with_score <- mutate(charlson, score = score(charlson, weights = NULL, assign0 = FALSE))
charlson_with_score
#>   pat_id ami chf pvd cevd dementia copd rheumd pud mld diab diabwc hp rend canc
#> 1      1   0   0   0    0        0    0      0   0   1    1      0  0    0    0
#> 2      2   0   0   0    0        0    0      0   0   0    0      0  0    0    1
#> 3      3   0   0   0    0        0    0      0   0   0    0      0  0    0    1
#> 4      4   0   1   0    0        0    0      0   0   0    0      0  0    0    1
#> 5      5   0   0   0    1        0    0      0   0   0    0      0  0    0    0
#>   msld metacanc aids score
#> 1    0        0    0     2
#> 2    0        0    0     1
#> 3    1        0    0     2
#> 4    0        0    0     2
#> 5    0        0    1     2

Created on 2022-03-15 by the reprex package (v2.0.1)

Alternatively, you can pipe all the way through:

x %>%
  comorbidity(id = "pat_id", code = "code", map = "charlson_icd10_quan", assign0 = TRUE, tidy.codes = TRUE) %>%
  mutate(score = score(x = ., weights = NULL, assign0 = FALSE))
#>   pat_id ami chf pvd cevd dementia copd rheumd pud mld diab diabwc hp rend canc
#> 1      1   0   0   0    0        0    0      0   0   1    1      0  0    0    0
#> 2      2   0   0   0    0        0    0      0   0   0    0      0  0    0    1
#> 3      3   0   0   0    0        0    0      0   0   0    0      0  0    0    1
#> 4      4   0   1   0    0        0    0      0   0   0    0      0  0    0    1
#> 5      5   0   0   0    1        0    0      0   0   0    0      0  0    0    0
#>   msld metacanc aids score
#> 1    0        0    0     2
#> 2    0        0    0     1
#> 3    1        0    0     2
#> 4    0        0    0     2
#> 5    0        0    1     2

...but that's just a matter of style, as you can see the results are the same.

Alessandro