Skip to content

Commit 25352bb

Browse files
refactor/add mock data functions
1 parent 502808a commit 25352bb

12 files changed

+424
-461
lines changed

DESCRIPTION

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ Imports:
1717
collapse,
1818
dplyr,
1919
fixtuRes,
20+
forcats,
2021
fs,
2122
lubridate,
2223
pins,
2324
purrr,
2425
rlang,
2526
santoku,
27+
stringr,
28+
tidyr,
2629
vctrs,
2730
wakefield,
28-
withr (>= 3.0.0),
29-
ymd
31+
withr (>= 3.0.0)
3032
Suggests:
3133
covr,
32-
forcats,
3334
fuimus,
34-
ggforce,
3535
ggplot2,
3636
ggrepel,
3737
ggthemes,
@@ -46,7 +46,7 @@ Suggests:
4646
scales,
4747
svglite,
4848
testthat (>= 3.0.0),
49-
tidyr
49+
ymd
5050
VignetteBuilder:
5151
knitr
5252
Remotes:

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export(mock_claims)
1111
export(mock_parbx)
1212
export(net_ex)
1313
export(predict_net)
14+
export(prep_claims)
1415
export(remove_quiet)
1516
export(sorted_bars)
1617
export(sum_na)
18+
export(summarise_claims)

R/generated-globals.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,45 @@ utils::globalVariables(c(
99
"age",
1010
# <bin_aging>
1111
# <mock_parbx>
12+
# <prep_claims>
1213
"aging_bin",
1314
# <mock_claims>
15+
# <prep_claims>
16+
# <summarise_claims>
1417
"balance",
1518
# <mock_claims>
19+
# <prep_claims>
20+
# <summarise_claims>
1621
"charges",
22+
# <prep_claims>
23+
"claimid",
24+
# <prep_claims>
25+
# <summarise_claims>
26+
"dar",
1727
# <mock_claims>
28+
# <prep_claims>
1829
"date_acceptance",
1930
# <mock_claims>
31+
# <prep_claims>
2032
"date_adjudication",
2133
# <mock_claims>
34+
# <prep_claims>
2235
"date_reconciliation",
2336
# <mock_claims>
37+
# <prep_claims>
2438
"date_release",
2539
# <mock_claims>
40+
# <prep_claims>
2641
"date_service",
2742
# <mock_claims>
43+
# <prep_claims>
2844
"date_submission",
45+
# <prep_claims>
46+
"date_type",
47+
# <prep_claims>
48+
"dates",
49+
# <prep_claims>
50+
"days",
2951
# <predict_net>
3052
"earb_gt120",
3153
# <predict_net>
@@ -48,6 +70,8 @@ utils::globalVariables(c(
4870
"net_pred",
4971
# <predict_net>
5072
"parl_120",
73+
# <prep_claims>
74+
"payer",
5175
# <predict_net>
5276
"pct_paid",
5377
# <avg_dar>

R/mock_data.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mock_claims <- function(rows = 100, payers = payer_names(), count_days = FALSE,
2323
rsmpl <- sample(1:rows, size = (75 * rows / 100))
2424

2525
df <- dplyr::tibble(
26-
claimid = wakefield::id(n = rows),
26+
claimid = as.character(wakefield::id(n = rows)),
2727
payer = forcats::as_factor(fixtuRes::set_vector(size = rows, set = payers)),
2828
charges = as.double(wakefield::income(n = rows, digits = 2) / 300),
2929
age = as.double(wakefield::age(n = rows, x = 15:100)),
@@ -131,29 +131,29 @@ payer_names <- function() {
131131
"Medicare",
132132
"Medicaid",
133133
"Kaiser Permanente",
134-
"Elevance Health (Anthem)",
135-
"HCSC (Health Care Service Corp)",
134+
"Elevance (Anthem)",
135+
"HCSC",
136136
"UnitedHealth",
137137
"Centene",
138-
"CVS Health (Aetna)",
138+
"CVS Aetna",
139139
"Humana",
140-
"Cigna Health",
141-
"Molina Healthcare",
142-
"GuideWell (Florida Blue)",
140+
"Cigna",
141+
"Molina",
142+
"GuideWell",
143143
"Highmark",
144-
"BCBS Michigan",
145-
"University Health Care",
146-
"BCBS Wyoming",
147-
"Bright Healthcare of Texas",
148-
"Oscar Insurance Company",
149-
"Wellcare, Inc.",
150-
"Omaha Supplemental",
151-
"Athene Annuity and Life",
152-
"American General",
153-
"Massachusetts Mutual Life",
144+
"BCBS MI",
145+
"Univ. Healthcare",
146+
"BCBS WY",
147+
"Bright",
148+
"Oscar",
149+
"Wellcare",
150+
"Omaha",
151+
"Athene",
152+
"American",
153+
"Mass Mutual",
154154
"New York Life",
155-
"Lincoln National",
156-
"Equitable Financial",
157-
"Allianz Insurance"
155+
"Lincoln Nat'l",
156+
"Equitable",
157+
"Allianz"
158158
)
159159
}

R/summarise_claims.R

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#' Prep mock coding/billing data frame
2+
#'
3+
#' @param df `[data.frame]` data frame generated by `mock_claims()`
4+
#'
5+
#' @param ... `[dots]` additional arguments
6+
#'
7+
#' @returns A [tibble][tibble::tibble-package]
8+
#'
9+
#' @examples
10+
#' prep_claims(mock_claims(rows = 5))
11+
#'
12+
#' @autoglobal
13+
#'
14+
#' @export
15+
prep_claims <- function(df, ...) {
16+
17+
df |>
18+
tidyr::pivot_longer(
19+
cols = dplyr::starts_with("date"),
20+
names_to = "date_type",
21+
names_prefix = "date_",
22+
values_to = "date") |>
23+
dplyr::mutate(
24+
date_type = forcats::fct_relevel(
25+
date_type,
26+
"service",
27+
"release",
28+
"submission",
29+
"acceptance",
30+
"adjudication",
31+
"reconciliation"),
32+
days = as.numeric(dplyr::lead(date) - date),
33+
days = dplyr::lag(days, order_by = date),
34+
.by = claimid) |>
35+
dplyr::arrange(claimid, date_type) |>
36+
tidyr::pivot_wider(
37+
names_from = date_type,
38+
names_glue = "{.value}_{date_type}",
39+
values_from = c(date, days)
40+
) |>
41+
remove_quiet() |>
42+
dplyr::rowwise() |>
43+
dplyr::mutate(dar = dplyr::if_else(
44+
!is.na(date_reconciliation),
45+
as.numeric(date_reconciliation - date_service),
46+
as.numeric(date_adjudication - date_service)
47+
)) |>
48+
dplyr::ungroup() |>
49+
tidyr::nest(
50+
dates = c(
51+
date_release,
52+
date_submission,
53+
date_acceptance,
54+
date_adjudication,
55+
date_reconciliation)
56+
) |>
57+
bin_aging(dar) |>
58+
dplyr::select(
59+
claimid,
60+
payer,
61+
charges,
62+
balance,
63+
date_service,
64+
aging_bin,
65+
dar,
66+
dplyr::starts_with("days_"),
67+
dates
68+
) |>
69+
.add_class()
70+
}
71+
72+
#' Summarise mock coding/billing data frame
73+
#'
74+
#' @param df `[data.frame]` data frame generated from `mock_claims() |> prep_claims()`
75+
#'
76+
#' @param vars `[character]` variables to summarise, e.g. `c(days_service, days_reconciliation)`
77+
#'
78+
#' @returns A [tibble][tibble::tibble-package]
79+
#'
80+
#' @examples
81+
#' x <- mock_claims(rows = 500) |> prep_claims()
82+
#'
83+
#' summarise_claims(x)
84+
#'
85+
#' x |>
86+
#' dplyr::group_by(
87+
#' year = ymd::year(date_service),
88+
#' month = ymd::month(date_service)) |>
89+
#' summarise_claims()
90+
#'
91+
#' @autoglobal
92+
#'
93+
#' @export
94+
summarise_claims <- function(df, vars = c(dplyr::starts_with("days_"), dar)) {
95+
96+
df |>
97+
dplyr::summarise(
98+
n_claims = dplyr::n(),
99+
gross_charges = sum_na(charges),
100+
ending_ar = sum_na(balance),
101+
dplyr::across({{ vars }}, \(x) mean_na(x), .names = "mean_{.col}"),
102+
.groups = "drop"
103+
) |>
104+
dplyr::rename_with(
105+
~ stringr::str_replace(.x, "days_", ""),
106+
dplyr::starts_with("mean_")
107+
) |>
108+
.add_class()
109+
110+
}

README.Rmd

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pak::pak("andrewallenbruce/forager")
5757

5858
```{r setup, echo=TRUE, message=FALSE, warning=FALSE}
5959
library(tidyverse)
60-
library(clock)
60+
library(ymd)
6161
library(forager)
6262
library(fuimus)
6363
```
@@ -71,78 +71,28 @@ library(fuimus)
7171
<br>
7272

7373
```{r echo=TRUE}
74-
long <- x |>
75-
pivot_longer(
76-
cols = starts_with("date"),
77-
names_to = "date_type",
78-
names_prefix = "date_",
79-
values_to = "date") |>
80-
mutate(
81-
date_type = fct_relevel(
82-
date_type,
83-
"service",
84-
"release",
85-
"submission",
86-
"acceptance",
87-
"adjudication",
88-
"reconciliation"),
89-
days = as.numeric(lead(date) - date),
90-
days = lag(days, order_by = date),
91-
.by = claimid) |>
92-
arrange(claimid, date_type)
93-
94-
long
74+
(x <- prep_claims(x))
9575
```
9676

9777
<br>
9878

9979
```{r}
100-
x <- long |>
101-
pivot_wider(
102-
names_from = date_type,
103-
names_glue = "{.value}_{date_type}",
104-
values_from = c(date, days)
105-
) |>
106-
remove_quiet() |>
107-
rowwise() |>
108-
mutate(days_in_ar = if_else(
109-
!is.na(date_reconciliation),
110-
as.numeric((date_reconciliation - date_service)),
111-
as.numeric((date_adjudication - date_service))
112-
)) |>
113-
ungroup() |>
114-
nest(dates = c(
115-
date_release,
116-
date_submission,
117-
date_acceptance,
118-
date_adjudication,
119-
date_reconciliation
120-
)
121-
) |>
122-
bin_aging(days_in_ar)
123-
x
80+
summarise_claims(x) |>
81+
glimpse()
12482
```
12583

12684

85+
12786
```{r echo=TRUE}
12887
x |>
12988
group_by(
13089
year = year(date_service),
13190
month = month(date_service),
13291
payer
13392
) |>
134-
summarise(
135-
claims = n(),
136-
balance = sum_na(balance),
137-
avg_release = mean_na(days_release),
138-
avg_submission = mean_na(days_submission),
139-
avg_acceptance = mean_na(days_acceptance),
140-
avg_adjudication = mean_na(days_adjudication),
141-
avg_reconciliation = mean_na(days_reconciliation),
142-
avg_days_in_ar = mean_na(days_in_ar),
143-
.groups = "drop") |>
93+
summarise_claims() |>
14494
arrange(payer) |>
145-
select(year, month, payer, claims, balance, avg_days_in_ar)
95+
select(year, month, payer, n_claims, ending_ar, mean_dar)
14696
```
14797

14898
<br>
@@ -154,18 +104,9 @@ x |>
154104
qtr = quarter(date_service),
155105
payer
156106
) |>
157-
summarise(
158-
claims = n(),
159-
balance = sum_na(balance),
160-
avg_release = mean_na(days_release),
161-
avg_submission = mean_na(days_submission),
162-
avg_acceptance = mean_na(days_acceptance),
163-
avg_adjudication = mean_na(days_adjudication),
164-
avg_reconciliation = mean_na(days_reconciliation),
165-
avg_days_in_ar = mean_na(days_in_ar),
166-
.groups = "drop") |>
107+
summarise_claims() |>
167108
arrange(payer) |>
168-
select(year, qtr, payer, claims, balance, avg_days_in_ar)
109+
select(year, qtr, payer, n_claims, ending_ar, mean_dar)
169110
```
170111

171112
## Days in AR Calculation

0 commit comments

Comments
 (0)