Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Jun 10, 2024
1 parent 95fccf4 commit c04dfa2
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 331 deletions.
8 changes: 3 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ Imports:
santoku,
vctrs,
wakefield,
withr (>= 3.0.0)
withr (>= 3.0.0),
ymd
Suggests:
covr,
forcats,
fuimus,
ggalt,
ggforce,
ggplot2,
ggrepel,
ggthemes,
gt,
gtExtras,
headliner,
ivs,
janitor,
knitr,
Expand All @@ -47,8 +46,7 @@ Suggests:
scales,
svglite,
testthat (>= 3.0.0),
tidyr,
waterfalls
tidyr
VignetteBuilder:
knitr
Remotes:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export(bin_aging)
export(dar_ex)
export(days_between)
export(load_ex)
export(mean_na)
export(mock_claims)
export(mock_parbx)
export(net_ex)
export(predict_net)
export(remove_quiet)
export(sorted_bars)
export(sum_na)
6 changes: 3 additions & 3 deletions R/aging.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @template returns-default
#'
#' @examples
#' generate_data(100) |>
#' mock_claims(100) |>
#' dplyr::mutate(
#' dar = dplyr::if_else(
#' !is.na(date_reconciliation),
Expand All @@ -23,7 +23,7 @@
#' balance = sum(balance, na.rm = TRUE),
#' .by = c(aging_bin))
#'
#' generate_data(10)[c(
#' mock_claims(10)[c(
#' "date_service",
#' "charges",
#' "payer")] |>
Expand Down Expand Up @@ -96,7 +96,7 @@ bin_aging <- function(df, ndays, bin_type = c("case", "chop")) {
#' @template returns-default
#'
#' @examples
#' generate_data(10)[c(
#' mock_claims(10)[c(
#' "date_service",
#' "charges",
#' "payer")] |>
Expand Down
17 changes: 4 additions & 13 deletions R/mock_generators.R → R/mock_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#'
#' @param rows `[integerish]` rows number of rows to generate; default is `100`
#'
#' @param payers `[character]` vector of payer names; default is `payer_names()`
#'
#' @param count_days `[logical]` add columns for days between events; default is `FALSE`
#'
#' @param ... `[dots]` additional arguments
Expand All @@ -16,23 +18,12 @@
#' @export
mock_claims <- function(rows = 100, payers = payer_names(), count_days = FALSE, ...) {

# payer_names <- c(
# "Medicare",
# "Medicaid",
# "Cigna",
# "Humana",
# "UHC",
# "Anthem",
# "BCBS",
# "Centene"
# )

# df$column <- sample(c("A", "B", "C"), nrow(relig_income), replace = TRUE)

rsmpl <- sample(1:rows, size = (75 * rows / 100))

df <- dplyr::tibble(
claimid = wakefield::id_factor(n = rows),
claimid = 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 @@ -85,7 +76,7 @@ mock_claims <- function(rows = 100, payers = payer_names(), count_days = FALSE,
#' @return A [tibble][tibble::tibble-package]
#'
#' @examples
#' # Every name generates 60 rows of data
#' # Every payer name generates 60 rows of data
#' mock_parbx()
#'
#' @autoglobal
Expand Down
45 changes: 45 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ load_ex <- function(name) {

}

#' Remove empty rows and columns
#'
#' @param df data frame
#'
#' @autoglobal
#'
#' @keywords internal
#'
#' @export
remove_quiet <- function(df) {

janitor::remove_empty(
df,
which = c("rows", "cols")
)
}

#' `mean()` with `NA` removal
#'
#' @param x numeric vector
#'
#' @autoglobal
#'
#' @keywords internal
#'
#' @export
mean_na <- function(x) {

mean(x, na.rm = TRUE)
}

#' `sum()` with `NA` removal
#'
#' @param x numeric vector
#'
#' @autoglobal
#'
#' @keywords internal
#'
#' @export
sum_na <- function(x) {

sum(x, na.rm = TRUE)
}

#' Sorted Bar Chart
#'
#' @param df data frame
Expand Down
121 changes: 45 additions & 76 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,8 @@ library(fuimus)

## The Lifecycle of a Claim

* <b>Provider Lag</b>: Date of Service - Date of Release
* <b>Billing Lag</b>: Date of Release - Date of Submission
* <b>Acceptance Lag</b>: Date of Submission - Date of Acceptance
* <b>Payment Lag</b>: Date of Acceptance - Date of Adjudication
* <b>Days in AR</b>: Date of Release - Date of Adjudication


<br>

```{r, echo=TRUE, warning=FALSE}
x <- mock_claims(15000)
x
(x <- mock_claims(15000))
```

<br>
Expand All @@ -88,8 +78,6 @@ long <- x |>
names_prefix = "date_",
values_to = "date") |>
mutate(
days = as.numeric(lead(date) - date),
days = lag(days, order_by = date),
date_type = fct_relevel(
date_type,
"service",
Expand All @@ -98,7 +86,10 @@ long <- x |>
"acceptance",
"adjudication",
"reconciliation"),
.by = claimid)
days = as.numeric(lead(date) - date),
days = lag(days, order_by = date),
.by = claimid) |>
arrange(claimid, date_type)
long
```
Expand All @@ -112,33 +103,46 @@ x <- long |>
names_glue = "{.value}_{date_type}",
values_from = c(date, days)
) |>
janitor::remove_empty("cols") |>
remove_quiet() |>
rowwise() |>
mutate(days_in_ar = as.numeric(diff(c(date_service, date_adjudication)))) |>
ungroup()
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
```


```{r echo=TRUE}
x |>
group_by(
year = get_year(date_service),
month = date_month_factor(date_service),
year = year(date_service),
month = month(date_service),
payer
) |>
) |>
summarise(
claims = n(),
balance = sum(balance, na.rm = TRUE),
avg_release = mean(days_release, na.rm = TRUE),
avg_submission = mean(days_submission, na.rm = TRUE),
avg_acceptance = mean(days_acceptance, na.rm = TRUE),
avg_adjudication = mean(days_adjudication, na.rm = TRUE),
avg_reconciliation = mean(days_reconciliation, na.rm = TRUE),
avg_dar = mean(days_in_ar, na.rm = TRUE),
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") |>
arrange(payer) |>
select(year, month, payer, claims, balance, avg_dar)
select(year, month, payer, claims, balance, avg_days_in_ar)
```

<br>
Expand All @@ -151,57 +155,22 @@ x |>
payer
) |>
summarise(
claims = n(),
balance = sum(balance, na.rm = TRUE),
avg_release = mean(days_release, na.rm = TRUE),
avg_submission = mean(days_submission, na.rm = TRUE),
avg_acceptance = mean(days_acceptance, na.rm = TRUE),
avg_adjudication = mean(days_adjudication, na.rm = TRUE),
avg_reconciliation = mean(days_reconciliation, na.rm = TRUE),
avg_dar = mean(days_in_ar, na.rm = TRUE),
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") |>
arrange(payer) |>
select(year, qtr, payer, claims, balance, avg_dar)
```


## Aging Calculation

```{r echo=TRUE}
x |>
mutate(
dar = if_else(
!is.na(date_reconciliation),
as.numeric((date_reconciliation - date_service)),
as.numeric((date_adjudication - date_service))
)
) |>
bin_aging(dar, "chop") |>
summarise(
n_claims = n(),
balance = sum(balance, na.rm = TRUE),
.by = c(aging_bin))
select(year, qtr, payer, claims, balance, avg_days_in_ar)
```

## Days in AR Calculation

```{r echo=TRUE}
x |>
mutate(
dar = if_else(
!is.na(date_reconciliation),
as.numeric((date_reconciliation - date_service)),
as.numeric((date_adjudication - date_service))
)
) |>
bin_aging(dar, "case") |>
summarise(
n_claims = n(),
balance = sum(balance, na.rm = TRUE),
.by = c(aging_bin, payer)) |>
arrange(aging_bin)
```

## Days in AR Monthly Calculation
> Monthly
```{r echo=TRUE}
tibble(
Expand All @@ -219,7 +188,7 @@ tibble(

<br>

## Days in AR Quarterly Calculation
> Quarterly
```{r echo=TRUE}
tibble(
Expand Down
4 changes: 2 additions & 2 deletions man/bin_aging.Rd

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

2 changes: 1 addition & 1 deletion man/days_between.Rd

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

15 changes: 15 additions & 0 deletions man/mean_na.Rd

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

Loading

0 comments on commit c04dfa2

Please sign in to comment.