Skip to content

Commit

Permalink
feat: construct_regex
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Jun 28, 2024
1 parent c3b5df3 commit 2cb4e3c
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 15 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ Depends:
R (>= 4.1.0)
Imports:
cli,
collapse,
dplyr,
fs,
kit,
pins,
purrr,
rlang,
strex,
stringfish,
stringr,
tidyr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ S3method(print,northstar)
export(assign_adjustments)
export(calculate_amounts)
export(carc_add_dash)
export(chr)
export(construct_regex)
export(data_dict)
export(get_example)
export(get_pin)
Expand Down
6 changes: 4 additions & 2 deletions R/standards.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#' search_descriptions(hcpcs_code = c("39503", "43116", "33935", "11646"))
#'
#' search_descriptions(
#' hcpcs_code = dplyr::tibble(code = c("A0021", "V5362", "J9264", "G8916")),
#' hcpcs_code = dplyr::tibble(
#' code = c("A0021", "V5362", "J9264", "G8916")),
#' column = "code")
#'
#' dplyr::tibble(code = c("A0021", "V5362", "J9264", "G8916")) |>
#' dplyr::tibble(
#' code = c("A0021", "V5362", "J9264", "G8916")) |>
#' search_descriptions(column = "code")
#'
#' @autoglobal
Expand Down
66 changes: 66 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,72 @@ get_example <- function(name = c("report", "practicum")) {

}

#' Alias for `as.character()`
#'
#' @param ... arguments to pass to `as.character()`
#'
#' @keywords internal
#'
#' @export
chr <- function(...) {
as.character(...)
}

#' Construct regex pattern for HCPCS codes
#'
#' @param x `<chr>` vector
#'
#' @examples
#' construct_regex(search_descriptions()$hcpcs_code)
#'
#' @returns `<chr>` vector
#'
#' @autoglobal
#'
#' @keywords internal
#'
#' @export
construct_regex <- function(x) {

# TODO: check for equal lengths

x <- collapse::funique(
collapse::na_rm(
gsub(" ", "", x)))

x <- stringr::str_split(x, "") |>
purrr::list_transpose() |>
purrr::map(collapse::funique) |>
purrr::map(pos_re) |>
purrr::list_c() |>
paste0(collapse = "")

paste0("^", x, "$")

}

#' Internal function for `construct_regex()`
#'
#' @param x `<chr>` vector
#'
#' @returns `<chr>` vector
#'
#' @autoglobal
#'
#' @noRd
pos_re <- function(x) {

sorted <- stringr::str_sort(x, numeric = TRUE)
alphabet <- purrr::list_c(strex::str_extract_non_numerics(sorted))
numbers <- purrr::list_c(strex::str_extract_numbers(sorted))

paste0("[",
fuimus::collapser(alphabet),
fuimus::collapser(numbers),
"]")

}

#' Apply {gt} Theme
#'
#' @param gt_object `<gt_tbl>` A [gt][gt::gt-package] table object
Expand Down
15 changes: 15 additions & 0 deletions man/chr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/construct_regex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/search_descriptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions vignettes/articles/columns.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ This file contains the Geographic Practice Cost Index (GPCI) component for each
```{r echo=FALSE, eval=TRUE}
search_gpcis() |>
mutate(
counties = NULL,
area = glue::glue("<small>{area}</small>")) |>
# counties = NULL,
locality_name = glue::glue("<small>{locality_name}</small>")) |>
distinct() |>
gt(rowname_col = "mac",
groupname_col = "state",
Expand All @@ -170,13 +170,15 @@ search_gpcis() |>
mac = "MAC",
state = "State",
locality = "",
area = "",
locality_name = "",
gpci_work = md("**GPCI**<i><sub>wk</sub></i>"),
gpci_pe = md("**GPCI**<i><sub>pe</sub></i>"),
gpci_mp = md("**GPCI**<i><sub>mp</sub></i>")) |>
gpci_mp = md("**GPCI**<i><sub>mp</sub></i>"),
gpci_gaf = md("**GPCI**<i><sub>gaf</sub></i>")
) |>
fmt_number(decimals = 2,
drop_trailing_zeros = TRUE) |>
fmt_markdown(columns = "area") |>
fmt_markdown(columns = "locality_name") |>
gt_theme_northstar(no_column_labels = FALSE) |>
tab_style(
style = cell_text(
Expand Down
76 changes: 70 additions & 6 deletions vignettes/northstar.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,95 @@ library(fuimus)
library(dplyr)
library(tidyr)
library(purrr)
library(collapse)
library(triebeard)
# library(pathologie)
library(gt)
```


* Calculate Patient Age -> Check ICD/HCPCS for Age Conflicts
* Calculate Provider Lag -> Days between DOS and DOR
* Calculate Balance -> Charges - (Payments + Adjustments)
* Calculate Coinsurance -> Charges - Allowed



## Load Example

```{r}
x <- get_example()
x
(x <- get_example() |>
dplyr::filter(!hcpcs %in% c("WCPAIN",
"MATERIALCTR",
"MATERIALOFF",
"SPBARIATRIC",
"LETTER")) |>
dplyr::mutate(
# coinsurance = charges - allowed,
balance = charges - (payments + adjustments),
.after = adjustments))
```

## Place of Service

```{r}
search_pos(pos_code = x$pos)
pos_trie <- triebeard::trie(
keys = search_pos()$pos_code,
values = chr(search_pos()$pos_type))
x_pos <- x |>
dplyr::mutate(
pos_type = triebeard::longest_match(pos_trie, pos),
.after = pos)
x_pos
```


## Categorize HCPCS

```{r}
search_descriptions(hcpcs_code = x$hcpcs)
hcpcs_unq <- collapse::funique(x$hcpcs[!x$hcpcs %in% not_hcpcs])
search_rbcs(hcpcs_code = hcpcs_unq) |>
dplyr::select(hcpcs_code:rbcs_family) |>
dplyr::left_join(
search_descriptions(hcpcs_code = hcpcs_unq,
hcpcs_desc_type = "Long") |>
dplyr::select(hcpcs_code, hcpcs_description),
by = dplyr::join_by(hcpcs_code))
```

## Define Modifiers

```{r}
search_rbcs(hcpcs_code = x$hcpcs)
mod_unq <- strsplit(toupper(x$mod[!is.na(x$mod)]), "-") |>
list_c() |>
funique()
search_modifiers(mod_code = mod_unq)
```



```{r}
x |>
select(
claim_id,
dos,
order,
hcpcs
) |>
group_by(claim_id) |>
filter(n() > 1) |>
ungroup()
```

```{r}
aocs <- search_aocs(hcpcs_code = hcpcs_unq) |>
tidyr::unnest(cols = aoc_complements)
aocs |>
filter(aoc_type == "Primary")
```

0 comments on commit 2cb4e3c

Please sign in to comment.