Skip to content

Commit

Permalink
add cod_check_summary function and refactor outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Jun 30, 2024
1 parent d611709 commit 376f2de
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 26 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(cod_check_code_sex_icd11)
export(cod_check_code_sex_icd11_)
export(cod_check_code_structure_icd10)
export(cod_check_code_structure_icd11)
export(cod_check_code_summary)
export(cod_check_code_unlikely_icd10)
export(cod_check_code_unlikely_icd11)
export(cod_check_codedit_input)
Expand Down
92 changes: 70 additions & 22 deletions R/cod_check_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cod_check_code <- function(cod, version = c("icd10", "icd11"), sex) {
cod_check_code_unlikely, cod_check_code_sex
) |>
dplyr::mutate(
cod_check = rowSums(
cod_check_code = rowSums(
data.frame(
.data$cod_check_structure,
.data$cod_check_ill_defined,
Expand All @@ -59,7 +59,15 @@ cod_check_code <- function(cod, version = c("icd10", "icd11"), sex) {
),
na.rm = TRUE
) |>
(\(x) ifelse(x == 0, 0, 1))()
(\(x) ifelse(x == 0, 0, 1))(),
cod_check_code_note = ifelse(
cod_check_code == 0,
"No issues found in CoD code",
"Issues found in CoD code"
) |>
factor(
levels = c("No issues found in CoD code", "Issues found in CoD code")
)
)
}

Expand Down Expand Up @@ -239,20 +247,6 @@ cod_check_code_structure_icd11 <- function(cod) {
#' @export
#'
cod_check_code_ill_defined_icd10 <- function(cod) {
# I46.1
# I46.9
# I50.-
# I95.9
# I99
# J96.0
# J96.9
# P28.5
# R00-R57.1,
# R57.8-R64,
# R65.2-R65.3,
# R68.0-R94,
# R96-R99

set1 <- ifelse(
cod %in% c("I46.1", "I46.9", "I95.9", "I99", "J96.0", "P28.5"), 1L, 0L
)
Expand All @@ -274,7 +268,16 @@ cod_check_code_ill_defined_icd10 <- function(cod) {
"CoD code is an ill-defined code"
)

tibble(cod_check, cod_check_note)
tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is an ill-defined code"
)
)
)
}


Expand All @@ -294,7 +297,16 @@ cod_check_code_ill_defined_icd11 <- function(cod) {
"CoD code is an ill-defined code"
)

tibble::tibble(cod_check, cod_check_note)
tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is an ill-defined code"
)
)
)
}


Expand All @@ -312,7 +324,16 @@ cod_check_code_unlikely_icd10 <- function(cod) {
"CoD code is an unlikely cause-of-death"
)

tibble(cod_check, cod_check_note)
tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is an unlikely cause-of-death"
)
)
)
}

#'
Expand All @@ -331,7 +352,16 @@ cod_check_code_unlikely_icd11 <- function(cod) {
"CoD code is an unlikely cause-of-death"
)

tibble::tibble(cod_check, cod_check_note)
tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is an unlikely cause-of-death"
)
)
)
}

#'
Expand All @@ -356,7 +386,16 @@ cod_check_code_sex_icd10_ <- function(cod, sex) {
"CoD code is not appropriate for person's sex"
)

tibble::tibble(cod_check, cod_check_note)
tibble::tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is not appropriate for person's sex"
)
)
)
}


Expand Down Expand Up @@ -397,7 +436,16 @@ cod_check_code_sex_icd11_ <- function(cod, sex) {
"CoD code is not appropriate for person's sex"
)

tibble::tibble(cod_check, cod_check_note)
tibble::tibble(cod_check, cod_check_note) |>
dplyr::mutate(
cod_check_note = factor(
x = cod_check_note,
levels = c(
"No issues found in CoD code",
"CoD code is not appropriate for person's sex"
)
)
)
}


Expand Down
2 changes: 1 addition & 1 deletion R/cod_structure_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cod_structure_input <- function(df, sex, sex_code = c(1, 2),
if (is.null(id)) {
FreeId <- seq(from = 1, to = nrow(df))
} else {
FreeId <- id
FreeId <- df[[id]]
}

Sex <- cod_recode_sex(sex_value = df[[sex]], sex_code = sex_code)
Expand Down
57 changes: 57 additions & 0 deletions R/cod_summary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#'
#' Summarise cause-of-death check results
#'
#' @param cod_check A data.frame output of the various `cod_check_code_*`
#' functions
#' @param simplify Logical. Should output be converted into a data.frame?
#' Default is FALSE.
#'
#' @returns If `simplify` is FALSE (default), a list of summary check outputs.
#' Otherwise, a tabulated summary of check outputs.
#'
#' @examples
#' cod_check_code(cod_data_raw_example$code, version = "icd11", sex = "sex") |>
#' cod_check_code_summary()
#'
#' @rdname cod_check_code_summary
#' @export
#'

cod_check_code_summary <- function(cod_check, simplify = FALSE) {
cod_check_list <- list(
cod_check |> dplyr::select(dplyr::contains("note_structure")),
cod_check |> dplyr::select(dplyr::contains("note_ill")),
cod_check |> dplyr::select(dplyr::contains("note_unlikely")),
cod_check |> dplyr::select(dplyr::contains("note_sex")),
cod_check |> dplyr::select(dplyr::all_of("cod_check_code_note"))

Check warning on line 26 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L21-L26

Added lines #L21 - L26 were not covered by tests
) |>
(\(x)

Check warning on line 28 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L28

Added line #L28 was not covered by tests
{
names(x) <- c(
"Code structure", "Ill-defined code",
"Unlikely cause-of-death code", "Code not appropriate for sex",
"Overall"

Check warning on line 33 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L30-L33

Added lines #L30 - L33 were not covered by tests
)
x

Check warning on line 35 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L35

Added line #L35 was not covered by tests
}
)()

cod_check_summary <- lapply(
X = cod_check_list,
FUN = function(x) dplyr::count(
x,
dplyr::across(
dplyr::everything()

Check warning on line 44 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L39-L44

Added lines #L39 - L44 were not covered by tests
),
.drop = FALSE

Check warning on line 46 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L46

Added line #L46 was not covered by tests
) |>
dplyr::rename_with(.fn = function(x) c("cod_check_note", "n"))

Check warning on line 48 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L48

Added line #L48 was not covered by tests
)

if (simplify) {
cod_check_summary <- cod_check_summary |>
dplyr::bind_rows(.id = "cod_check_type")

Check warning on line 53 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L51-L53

Added lines #L51 - L53 were not covered by tests
}

cod_check_summary

Check warning on line 56 in R/cod_summary.R

View check run for this annotation

Codecov / codecov/patch

R/cod_summary.R#L56

Added line #L56 was not covered by tests
}
43 changes: 42 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,55 @@ library(codeditr)

Using the `icd10_example` dataset which is a dataset already formatted into a compatible structure required by the CoDEdit tool, we can perform a check on this dataset to see possible issues in its formatting and structure before using with the CoDEdit tool.

```{r use-case-1}
```{r use-case-1a}
cod_check_codedit_input(icd10_example)
```

2. Structure raw cause-of-death data for input into CoDEdit tool

Using the `cod_data_raw_example` dataset, we can format it into a compatible structure required by the CoDEdit tool.

```{r use-case-2a}
cod_structure_input(
df = cod_data_raw_example,
sex = "sex", dob = "dob", dod = "dod", code = "code", id = "id"
)
```

The output is a data.frame that can then be saved as an `.xlsx` file for use as input into the CoDEdit tool.

### CoDEdit tool replacement workflow

1. Perform all checks on cause-of-death data

The `cod_check_code()` function performs all the checks implemented by the CoDEdit tool.

```{r use-case-3}
cod_check_code(cod_data_raw_example$code, version = "icd11", sex = "sex")
```

Results of the per row cause-of-death checks can also be summarised to give a count of issues found in the dataset.

```{r use-case-4}
cod_check_code(cod_data_raw_example$code, version = "icd11", sex = "sex") |>
cod_check_code_summary()
```

2. Perform specific check types on cause-of-death data

The family of `cod_check_code_*` functions can be used to perform specific check types on the cause-of-death data.

```{r use-case-5}
### Perform code structure check on cause-of-death data ----
cod_check_code_structure_icd10(icd10_example$Code)
### Perform check for ill-defined codes on cause-of-death data ----
cod_check_code_ill_defined_icd11(cod_data_raw_example$code)
### Perform check for unlikely cause-of-death codes ----
cod_check_code_unlikely_icd11(cod_data_raw_example$code)
```

## Citation

If you find the `codeditr` package useful please cite using the suggested citation provided by a call to the `citation()` function as follows:
Expand Down
Loading

0 comments on commit 376f2de

Please sign in to comment.