Skip to content

Commit

Permalink
refactor/add mock data functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Jun 11, 2024
1 parent 502808a commit 25352bb
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 461 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ Imports:
collapse,
dplyr,
fixtuRes,
forcats,
fs,
lubridate,
pins,
purrr,
rlang,
santoku,
stringr,
tidyr,
vctrs,
wakefield,
withr (>= 3.0.0),
ymd
withr (>= 3.0.0)
Suggests:
covr,
forcats,
fuimus,
ggforce,
ggplot2,
ggrepel,
ggthemes,
Expand All @@ -46,7 +46,7 @@ Suggests:
scales,
svglite,
testthat (>= 3.0.0),
tidyr
ymd
VignetteBuilder:
knitr
Remotes:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export(mock_claims)
export(mock_parbx)
export(net_ex)
export(predict_net)
export(prep_claims)
export(remove_quiet)
export(sorted_bars)
export(sum_na)
export(summarise_claims)
24 changes: 24 additions & 0 deletions R/generated-globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,45 @@ utils::globalVariables(c(
"age",
# <bin_aging>
# <mock_parbx>
# <prep_claims>
"aging_bin",
# <mock_claims>
# <prep_claims>
# <summarise_claims>
"balance",
# <mock_claims>
# <prep_claims>
# <summarise_claims>
"charges",
# <prep_claims>
"claimid",
# <prep_claims>
# <summarise_claims>
"dar",
# <mock_claims>
# <prep_claims>
"date_acceptance",
# <mock_claims>
# <prep_claims>
"date_adjudication",
# <mock_claims>
# <prep_claims>
"date_reconciliation",
# <mock_claims>
# <prep_claims>
"date_release",
# <mock_claims>
# <prep_claims>
"date_service",
# <mock_claims>
# <prep_claims>
"date_submission",
# <prep_claims>
"date_type",
# <prep_claims>
"dates",
# <prep_claims>
"days",
# <predict_net>
"earb_gt120",
# <predict_net>
Expand All @@ -48,6 +70,8 @@ utils::globalVariables(c(
"net_pred",
# <predict_net>
"parl_120",
# <prep_claims>
"payer",
# <predict_net>
"pct_paid",
# <avg_dar>
Expand Down
40 changes: 20 additions & 20 deletions R/mock_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mock_claims <- function(rows = 100, payers = payer_names(), count_days = FALSE,
rsmpl <- sample(1:rows, size = (75 * rows / 100))

df <- dplyr::tibble(
claimid = wakefield::id(n = rows),
claimid = as.character(wakefield::id(n = rows)),
payer = forcats::as_factor(fixtuRes::set_vector(size = rows, set = payers)),
charges = as.double(wakefield::income(n = rows, digits = 2) / 300),
age = as.double(wakefield::age(n = rows, x = 15:100)),
Expand Down Expand Up @@ -131,29 +131,29 @@ payer_names <- function() {
"Medicare",
"Medicaid",
"Kaiser Permanente",
"Elevance Health (Anthem)",
"HCSC (Health Care Service Corp)",
"Elevance (Anthem)",
"HCSC",
"UnitedHealth",
"Centene",
"CVS Health (Aetna)",
"CVS Aetna",
"Humana",
"Cigna Health",
"Molina Healthcare",
"GuideWell (Florida Blue)",
"Cigna",
"Molina",
"GuideWell",
"Highmark",
"BCBS Michigan",
"University Health Care",
"BCBS Wyoming",
"Bright Healthcare of Texas",
"Oscar Insurance Company",
"Wellcare, Inc.",
"Omaha Supplemental",
"Athene Annuity and Life",
"American General",
"Massachusetts Mutual Life",
"BCBS MI",
"Univ. Healthcare",
"BCBS WY",
"Bright",
"Oscar",
"Wellcare",
"Omaha",
"Athene",
"American",
"Mass Mutual",
"New York Life",
"Lincoln National",
"Equitable Financial",
"Allianz Insurance"
"Lincoln Nat'l",
"Equitable",
"Allianz"
)
}
110 changes: 110 additions & 0 deletions R/summarise_claims.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#' Prep mock coding/billing data frame
#'
#' @param df `[data.frame]` data frame generated by `mock_claims()`
#'
#' @param ... `[dots]` additional arguments
#'
#' @returns A [tibble][tibble::tibble-package]
#'
#' @examples
#' prep_claims(mock_claims(rows = 5))
#'
#' @autoglobal
#'
#' @export
prep_claims <- function(df, ...) {

df |>
tidyr::pivot_longer(
cols = dplyr::starts_with("date"),
names_to = "date_type",
names_prefix = "date_",
values_to = "date") |>
dplyr::mutate(
date_type = forcats::fct_relevel(
date_type,
"service",
"release",
"submission",
"acceptance",
"adjudication",
"reconciliation"),
days = as.numeric(dplyr::lead(date) - date),
days = dplyr::lag(days, order_by = date),
.by = claimid) |>
dplyr::arrange(claimid, date_type) |>
tidyr::pivot_wider(
names_from = date_type,
names_glue = "{.value}_{date_type}",
values_from = c(date, days)
) |>
remove_quiet() |>
dplyr::rowwise() |>
dplyr::mutate(dar = dplyr::if_else(
!is.na(date_reconciliation),
as.numeric(date_reconciliation - date_service),
as.numeric(date_adjudication - date_service)
)) |>
dplyr::ungroup() |>
tidyr::nest(
dates = c(
date_release,
date_submission,
date_acceptance,
date_adjudication,
date_reconciliation)
) |>
bin_aging(dar) |>
dplyr::select(
claimid,
payer,
charges,
balance,
date_service,
aging_bin,
dar,
dplyr::starts_with("days_"),
dates
) |>
.add_class()
}

#' Summarise mock coding/billing data frame
#'
#' @param df `[data.frame]` data frame generated from `mock_claims() |> prep_claims()`
#'
#' @param vars `[character]` variables to summarise, e.g. `c(days_service, days_reconciliation)`
#'
#' @returns A [tibble][tibble::tibble-package]
#'
#' @examples
#' x <- mock_claims(rows = 500) |> prep_claims()
#'
#' summarise_claims(x)
#'
#' x |>
#' dplyr::group_by(
#' year = ymd::year(date_service),
#' month = ymd::month(date_service)) |>
#' summarise_claims()
#'
#' @autoglobal
#'
#' @export
summarise_claims <- function(df, vars = c(dplyr::starts_with("days_"), dar)) {

df |>
dplyr::summarise(
n_claims = dplyr::n(),
gross_charges = sum_na(charges),
ending_ar = sum_na(balance),
dplyr::across({{ vars }}, \(x) mean_na(x), .names = "mean_{.col}"),
.groups = "drop"
) |>
dplyr::rename_with(
~ stringr::str_replace(.x, "days_", ""),
dplyr::starts_with("mean_")
) |>
.add_class()

}
77 changes: 9 additions & 68 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pak::pak("andrewallenbruce/forager")

```{r setup, echo=TRUE, message=FALSE, warning=FALSE}
library(tidyverse)
library(clock)
library(ymd)
library(forager)
library(fuimus)
```
Expand All @@ -71,78 +71,28 @@ library(fuimus)
<br>

```{r echo=TRUE}
long <- x |>
pivot_longer(
cols = starts_with("date"),
names_to = "date_type",
names_prefix = "date_",
values_to = "date") |>
mutate(
date_type = fct_relevel(
date_type,
"service",
"release",
"submission",
"acceptance",
"adjudication",
"reconciliation"),
days = as.numeric(lead(date) - date),
days = lag(days, order_by = date),
.by = claimid) |>
arrange(claimid, date_type)
long
(x <- prep_claims(x))
```

<br>

```{r}
x <- long |>
pivot_wider(
names_from = date_type,
names_glue = "{.value}_{date_type}",
values_from = c(date, days)
) |>
remove_quiet() |>
rowwise() |>
mutate(days_in_ar = if_else(
!is.na(date_reconciliation),
as.numeric((date_reconciliation - date_service)),
as.numeric((date_adjudication - date_service))
)) |>
ungroup() |>
nest(dates = c(
date_release,
date_submission,
date_acceptance,
date_adjudication,
date_reconciliation
)
) |>
bin_aging(days_in_ar)
x
summarise_claims(x) |>
glimpse()
```



```{r echo=TRUE}
x |>
group_by(
year = year(date_service),
month = month(date_service),
payer
) |>
summarise(
claims = n(),
balance = sum_na(balance),
avg_release = mean_na(days_release),
avg_submission = mean_na(days_submission),
avg_acceptance = mean_na(days_acceptance),
avg_adjudication = mean_na(days_adjudication),
avg_reconciliation = mean_na(days_reconciliation),
avg_days_in_ar = mean_na(days_in_ar),
.groups = "drop") |>
summarise_claims() |>
arrange(payer) |>
select(year, month, payer, claims, balance, avg_days_in_ar)
select(year, month, payer, n_claims, ending_ar, mean_dar)
```

<br>
Expand All @@ -154,18 +104,9 @@ x |>
qtr = quarter(date_service),
payer
) |>
summarise(
claims = n(),
balance = sum_na(balance),
avg_release = mean_na(days_release),
avg_submission = mean_na(days_submission),
avg_acceptance = mean_na(days_acceptance),
avg_adjudication = mean_na(days_adjudication),
avg_reconciliation = mean_na(days_reconciliation),
avg_days_in_ar = mean_na(days_in_ar),
.groups = "drop") |>
summarise_claims() |>
arrange(payer) |>
select(year, qtr, payer, claims, balance, avg_days_in_ar)
select(year, qtr, payer, n_claims, ending_ar, mean_dar)
```

## Days in AR Calculation
Expand Down
Loading

0 comments on commit 25352bb

Please sign in to comment.