Skip to content

Commit

Permalink
refactor: aging_bins, avg_dar, generate_data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed May 22, 2024
1 parent e11cfd3 commit 697d62a
Show file tree
Hide file tree
Showing 18 changed files with 654 additions and 609 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Suggests:
covr,
tidyr,
svglite,
janitor,
ggplot2,
headliner,
forcats,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(avg_dar)
export(bin_aging)
export(dar_ex)
export(days_between)
export(generate_data)
export(load_ex)
export(net_ex)
Expand Down
156 changes: 91 additions & 65 deletions R/aging.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,115 @@
#'
#' @template args-df-default
#'
#' @template args-date-col
#' @param ndays `<dbl>` column of counts of days elapsed to bin by
#'
#' @param bin_type `<chr>` string specifying the bin type; one of "chop", "cut" or "ivs"
#'
#' @template returns-default
#'
#' @examples
#' binned <- bin_aging(
#' df = load_ex("aging_ex"),
#' date = dos
#' ) |>
#' dplyr::select(
#' dos:ins_class,
#' dar:aging_bin
#' )
#' generate_data(10)[c(
#' "date_srvc",
#' "charges",
#' "payer")] |>
#' days_between(date_srvc) |>
#' bin_aging(days_in_ar)
#'
#' head(binned)
#'
#' binned |>
#' load_ex("aging_ex") |>
#' dplyr::select(dos, charges, ins_name) |>
#' days_between(dos) |>
#' bin_aging(days_in_ar) |>
#' dplyr::arrange(aging_bin) |>
#' dplyr::summarise(n_claims = dplyr::n(),
#' balance = sum(charges),
#' .by = aging_bin) |>
#' dplyr::mutate(
#' tot_claims = sum(n_claims),
#' tot_balance = sum(balance),
#' pct_claims = n_claims / tot_claims,
#' pct_balance = balance / tot_balance) |>
#' print(n = 50)
#'
#' binned |>
#' dplyr::arrange(aging_bin, ins_name) |>
#' dplyr::summarise(
#' n_claims = dplyr::n(),
#' balance = sum(charges),
#' .by = c(aging_bin, ins_name)
#' ) |>
#' dplyr::mutate(
#' tot_claims = sum(n_claims),
#' tot_balance = sum(balance),
#' pct_claims = n_claims / tot_claims,
#' pct_balance = balance / tot_balance) |>
#' print(n = 50)
#'
#' binned |>
#' dplyr::arrange(ins_name, aging_bin) |>
#' dplyr::summarise(
#' n_claims = dplyr::n(),
#' balance = sum(charges),
#' .by = c(aging_bin, ins_name)
#' dplyr::group_by(
#' year = clock::get_year(dos),
#' month = clock::date_month_factor(dos),
#' ) |>
#' dplyr::mutate(
#' tot_claims = sum(n_claims),
#' tot_balance = sum(balance),
#' pct_claims = n_claims / tot_claims,
#' pct_balance = balance / tot_balance,
#' .by = ins_name) |>
#' print(n = 50)
#' janitor::tabyl(ins_name, aging_bin, year)
#'
#' @autoglobal
#'
#' @export
bin_aging <- function(df, date, bin_type = "chop") {
bin_aging <- function(df, ndays, bin_type = c("case", "chop")) {

df <- df |>
dplyr::mutate(
dar = clock::date_count_between(
{{ date }},
lubridate::today(),
"day"))
bin_type <- match.arg(bin_type)

if (bin_type == "chop") {

df <- df |>
dplyr::mutate(
aging_bin = santoku::chop_width(
dar,
30,
start = 0,
left = FALSE,
close_end = FALSE
df <- df |>
dplyr::mutate(
aging_bin = santoku::chop_width(
{{ ndays }},
30,
start = 0,
left = FALSE,
close_end = FALSE
)
)
}

if (bin_type == "case") {

df <- df |>
dplyr::mutate(
aging_bin = dplyr::case_when(
dplyr::between({{ ndays }}, 0, 30) ~ "0-30",
dplyr::between({{ ndays }}, 31, 60) ~ "31-60",
dplyr::between({{ ndays }}, 61, 90) ~ "61-90",
dplyr::between({{ ndays }}, 91, 120) ~ "91-120",
{{ ndays }} >= 121 ~ "121+"
),
aging_bin = suppressWarnings(
forcats::fct_relevel(
aging_bin,
c("0-30", "31-60", "61-90", "91-120", "121+"),
after = Inf
)
)
)
)
}
return(df)
}

#' Calculate Number of Days Between Two Dates
#'
#' @template args-df-default
#'
#' @param from `[character]` column of dates to calculate days between
#'
#' @param to `[character]` column of dates to calculate days between
#'
#' @template returns-default
#'
#' @examples
#' generate_data(10)[c(
#' "date_srvc",
#' "charges",
#' "payer")] |>
#' days_between(date_srvc)
#'
#' @autoglobal
#'
#' @export
days_between <- function(df, from, to = NULL) {

if (is.null(to)) {

df |>
dplyr::mutate(
days_in_ar = clock::date_count_between(
{{ from }},
clock::date_today(""),
"day")
)

} else {

df |>
dplyr::mutate(
days_in_ar = clock::date_count_between(
{{ from }},
{{ to }},
"day")
)
}
}
14 changes: 7 additions & 7 deletions R/dar.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#'
#' @template args-earb-col
#'
#' @param dart `<dbl>` Target Days in AR, default is `35` days
#' @param dart `[numeric]` Target Days in AR, default is `35` days
#'
#' @param period `<chr>` string specifying the calculation period; one of
#' @param by `[character]` string specifying the calculation period; one of
#' `"month"`, `"quarter"`, or `"year"`; defaults to `"month"`
#'
#' @template returns-default
Expand All @@ -21,14 +21,14 @@
#' gct = gross_charges,
#' earb = ending_ar,
#' dart = 35,
#' period = "month")
#' by = "month")
#'
#' avg_dar(df = dar_ex(),
#' date = date,
#' gct = gross_charges,
#' earb = ending_ar,
#' dart = 35,
#' period = "quarter")
#' by = "quarter")
#'
#' @autoglobal
#'
Expand All @@ -38,9 +38,9 @@ avg_dar <- function(df,
gct,
earb,
dart = 35,
period = c("month", "quarter")) {
by = c("month", "quarter")) {

period <- match.arg(period)
by <- match.arg(by)
datecol <- rlang::englue("{{ date }}")
earbcol <- rlang::englue("{{ earb }}")
gctcol <- rlang::englue("{{ gct }}")
Expand All @@ -63,7 +63,7 @@ avg_dar <- function(df,
ndip = lubridate::days_in_month({{ date }})
)

if (period == "quarter") {
if (by == "quarter") {

qtr_max_nmons <- df |>
dplyr::summarise(
Expand Down
19 changes: 15 additions & 4 deletions R/generated-globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ utils::globalVariables(c(
# <avg_dar>
"adc",
# <bin_aging>
"aging_bin",
# <generate_data>
"balance",
# <generate_data>
"charges",
# <avg_dar>
"dar",
# <generate_data>
"date_of_acceptance",
"date_accept",
# <generate_data>
"date_adjud",
# <generate_data>
"date_of_release",
"date_recon",
# <generate_data>
"date_of_service",
"date_rlse",
# <generate_data>
"date_of_submission",
"date_srvc",
# <generate_data>
"date_submit",
# <predict_net>
"earb_gt120",
# <predict_net>
Expand All @@ -29,6 +38,8 @@ utils::globalVariables(c(
"max_nmon",
# <avg_dar>
"month",
# <generate_data>
"NA_Date_",
# <avg_dar>
"ndip",
# <predict_net>
Expand Down
25 changes: 13 additions & 12 deletions R/predict_net.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ predict_net <- function(df, date, gct, earb, net, parb_120) {

df <- df |>
dplyr::mutate(
date = lubridate::ymd({{ date }}),
pct_paid = {{ net }} / {{ gct }},
parl_120 = 1 - {{ parb_120 }},
net_pred = ({{ gct }} * pct_paid) * parl_120,
date = lubridate::ymd({{ date }}),
pct_paid = {{ net }} / {{ gct }},
parl_120 = 1 - {{ parb_120 }},
net_pred = ({{ gct }} * pct_paid) * parl_120,
earb_gt120 = {{ earb }} * parb_120,
earb_lt120 = {{ earb }} * parl_120
)

pred <- dplyr::tibble(
date = df$date[nrow(df)] + months(1),
date = df$date[nrow(df)] + months(1),
net_pred = df$net_pred[nrow(df)]
)

Expand Down Expand Up @@ -75,11 +75,12 @@ predict_net <- function(df, date, gct, earb, net, parb_120) {
net_ex <- function() {

load_ex("monthly_raw") |>
dplyr::select(date,
gct = gross_charges,
earb = ending_ar,
net = net_payment,) |>
dplyr::mutate(date = lubridate::ymd(date),
parb_120 = rep(c(0.021, 0.047, 0.075), 4)
)
dplyr::select(
date,
gct = gross_charges,
earb = ending_ar,
net = net_payment,) |>
dplyr::mutate(
date = lubridate::ymd(date),
parb_120 = rep(c(0.021, 0.047, 0.075), 4))
}
Loading

0 comments on commit 697d62a

Please sign in to comment.