Skip to content

Commit

Permalink
fun: bin_aging
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed May 9, 2024
1 parent 41552b2 commit 52ff396
Show file tree
Hide file tree
Showing 25 changed files with 401 additions and 367 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
^vignettes/articles$
^codecov\.yml$
^data-raw$
^man-roxygen$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.httr-oauth
.DS_Store
docs
inst/doc
11 changes: 10 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ Imports:
clock,
dplyr,
fixtuRes,
janitor,
fs,
lubridate,
pins,
rlang,
santoku,
tidyr,
wakefield
Suggests:
covr,
fuimus,
gt,
gtExtras,
knitr,
qs,
rmarkdown,
roxyglobals,
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Remotes:
github::andrewallenbruce/fuimus
Config/roxyglobals/filename: generated-globals.R
Config/roxyglobals/unique: TRUE
Config/testthat/edition: 3
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

export(age_days)
export(avg_dar)
export(bin_aging)
export(count_days)
export(dar_ex)
export(dar_month)
export(dar_qtr)
export(generate_data)
export(load_ex)
47 changes: 47 additions & 0 deletions R/aging.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Apply 30-Day Aging Bins
#'
#' @param df `<data.frame>` or `<tibble>` with a required date column
#'
#' @param date column of `<date>`s
#'
#' @returns a [tibble][tibble::tibble-package]
#'
#' @examples
#' binned <- bin_aging(load_ex(), dos)
#'
#' head(binned)
#'
#' binned |>
#' dplyr::arrange(aging_bin) |>
#' dplyr::summarise(
#' n_claims = dplyr::n(),
#' balance = sum(charges),
#' .by = aging_bin)
#'
#' binned |>
#' dplyr::arrange(aging_bin) |>
#' dplyr::summarise(
#' n_claims = dplyr::n(),
#' balance = sum(charges),
#' .by = c(aging_bin, ins_name))
#'
#' @autoglobal
#'
#' @export
bin_aging <- function(df, date) {

df |>
dplyr::mutate(
dar = clock::date_count_between(
{{ date }},
lubridate::today(),
"day"),
aging_bin = santoku::chop_width(
dar,
30,
start = 0,
left = FALSE,
close_end = FALSE
)
)
}
69 changes: 28 additions & 41 deletions R/dar.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#'
#' @examples
#' avg_dar(df = dar_ex(),
#' date = monthdate,
#' date = date,
#' gct = gross_charges,
#' earb = ending_ar,
#' dart = 35,
#' period = "month")
#'
#' avg_dar(df = dar_ex(),
#' date = monthdate,
#' date = date,
#' gct = gross_charges,
#' earb = ending_ar,
#' dart = 35,
Expand All @@ -50,8 +50,17 @@ avg_dar <- function(df,
df,
"{datecol}" := clock::as_date({{ date }}),
nmon = lubridate::month({{ date }}, label = FALSE),
mon = lubridate::month({{ date }}, label = TRUE, abbr = TRUE),
month = lubridate::month({{ date }}, label = TRUE, abbr = FALSE),
nqtr = lubridate::quarter({{ date }}),
yqtr = lubridate::quarter({{ date }}, with_year = TRUE),
dqtr = paste0(lubridate::quarter({{ date }}), "Q", format({{ date }}, "%y")),
year = lubridate::year({{ date }}),
ymon = as.numeric(format({{ date }}, "%Y.%m")),
myear = format({{ date }}, "%b %Y"),
nhalf = lubridate::semester({{ date }}),
yhalf = lubridate::semester({{ date }}, with_year = TRUE),
dhalf = paste0(lubridate::semester({{ date }}), "H", format({{ date }}, "%y")),
ndip = lubridate::days_in_month({{ date }})
)

Expand Down Expand Up @@ -91,15 +100,13 @@ avg_dar <- function(df,

# Days in Accounts Receivable
dar = {{ earb }} / adc,
dar_pass = dplyr::case_when(dar < {{ dart }} ~ TRUE, TRUE ~ FALSE),
dar_diff = dar - {{ dart }},

# Actual Ratio of Ending AR to Gross Charges
actual_ratio = {{ earb }} / {{ gct }},

# Ideal Ratio of Ending AR to Gross Charges
ideal_ratio = {{ dart }} / ndip,

# Actual - Ideal Ratio
diff_ratio = actual_ratio - ideal_ratio,
# Ratios: Ending AR to Gross Charges
ratio_actual = {{ earb }} / {{ gct }},
ratio_ideal = {{ dart }} / ndip,
ratio_diff = ratio_actual - ratio_ideal,

# Ending AR Target
"{{ earb }}_target" := ({{ gct }} * {{ dart }}) / ndip,
Expand All @@ -110,29 +117,9 @@ avg_dar <- function(df,
# Ending AR Percentage Decrease Needed
"{{ earb }}_dec_pct" := !!earb_dc_col / {{ earb }},

# <lgl> indicating whether DAR was under/over DARt
pass = dplyr::case_when(dar < {{ dart }} ~ TRUE, TRUE ~ FALSE))
# |>
# dplyr::select(
# dplyr::any_of(
# c("date",
# "month",
# "nmon",
# "nqtr",
# "ndip",
# "gct",
# "earb",
# "earb_trg",
# "earb_dc",
# "earb_pct",
# "adc",
# "dar",
# "pass",
# "actual",
# "ideal",
# "radiff")
# )
# )
earb_gct_diff = {{ earb }} - {{ gct }},

)
}

#' Days in AR Example Data
Expand All @@ -145,10 +132,10 @@ avg_dar <- function(df,
dar_ex <- function() {

dplyr::tibble(
monthdate = seq(
date = seq(
as.Date("2024-01-01"),
by = "month",
length.out = 10
length.out = 12
),

gross_charges = c(
Expand All @@ -161,9 +148,9 @@ dar_ex <- function() {
153991.95,
156975.52,
146878.12,
163799.44
# 151410.74,
# 169094.46
163799.44,
151410.74,
169094.46
),

ending_ar = c(
Expand All @@ -176,9 +163,9 @@ dar_ex <- function() {
182771.32,
169633.64,
179347.72,
178051.11
# 162757.49,
# 199849.32
178051.11,
162757.49,
199849.32
)
)
}
72 changes: 0 additions & 72 deletions R/dar_month.R

This file was deleted.

Loading

0 comments on commit 52ff396

Please sign in to comment.