Skip to content

Commit

Permalink
timestamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Dec 12, 2024
1 parent e2a2041 commit 590c4f9
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 103 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export(search_for)
export(single_line_string)
export(sorted_bars)
export(splitter)
export(summary_stats)
export(ttimestamp)
export(update_personal_packages)
export(years_df)
export(years_floor)
Expand Down
24 changes: 24 additions & 0 deletions R/dates.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
#' Timestamp
#'
#' @param time Sys.time() or a date-time object
#'
#' @returns A list()
#'
#' @examples
#' timestamp()
#'
#' date()
#'
#' ttimestamp()
#'
#' @autoglobal
#'
#' @export
ttimestamp <- function(time = Sys.time()) {
attr(time, "tzone") <- "EST"

list(
strftime(time, "%Y-%m-%d %H:%M:%S"),
strftime(time, "%a %b %e %H:%M:%S %Y"))
}

#' Calculate Number of Days Between Two Dates
#'
#' @note This calculation includes the end date in the sum (see example)
Expand Down
99 changes: 0 additions & 99 deletions R/experiments.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,102 +318,3 @@ is_valid_npi2 <- function(x) {

identical(test, x)
}

#' Generate Data for Testing
#'
#' @param year_seq sequence of years, e.g. `2010:2020`
#'
#' @returns A [tibble][tibble::tibble-package] containing the search results.
#'
#' @examples
#' provider_data(2010:2020)
#'
#' @autoglobal
#'
#' @family mock
#'
#' @export
provider_data <- function(year_seq) {

lng <- length(year_seq) * 2

vctrs::vec_rbind(
dplyr::tibble(
year = {{ year_seq }},
group = "A"),
dplyr::tibble(
year = {{ year_seq }},
group = "B")
) |>
dplyr::mutate(
net_payment = sample(
10000:20000, lng
)
)
}

#' Generate mock coding/billing data frame
#'
#' @param rows number of rows to generate; default is 10
#'
#' @param unnest a logical indicating whether to unnest the dates column;
#' default is `FALSE`
#'
#' @returns A [tibble][tibble::tibble-package]
#'
#' @examples
#' forager_data(rows = 5)
#'
#' @autoglobal
#'
#' @family mock
#'
#' @export
forager_data <- function(rows = 10, unnest = FALSE){


x <- dplyr::tibble(
claim_id = as.character(wakefield::id(n = rows)),
date_of_service = wakefield::date_stamp(
n = rows,
start = lubridate::today() - lubridate::dyears(2),
random = TRUE
),
payer = fixtuRes::set_vector(
rows,
set = c(
"Medicare",
"Medicaid",
"Cigna",
"Humana",
"UnitedHealth",
"Anthem",
"BCBS",
"Centene"
)
),
ins_class = fixtuRes::set_vector(rows, set = c("Primary", "Secondary")),
balance = as.double(wakefield::income(n = rows, digits = 2) / 300)
) |>
dplyr::mutate(
date_of_service = lubridate::as_date(date_of_service),
date_of_release = date_of_service + round(abs(stats::rnorm(
length(date_of_service), 11, 4
))),
date_of_submission = date_of_release + round(abs(stats::rnorm(
length(date_of_release), 2, 2
))),
date_of_acceptance = date_of_submission + round(abs(stats::rnorm(
length(date_of_submission), 3, 2
))),
date_of_adjudication = date_of_acceptance + round(abs(stats::rnorm(
length(date_of_acceptance), 30, 3
)))
) |>
tidyr::nest(dates = tidyr::contains("date"))

if (unnest)
x <- tidyr::unnest_wider(x, dates)

return(x)
}
99 changes: 99 additions & 0 deletions R/generate_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#' Generate Data for Testing
#'
#' @param year_seq sequence of years, e.g. `2010:2020`
#'
#' @returns A [tibble][tibble::tibble-package] containing the search results.
#'
#' @examples
#' provider_data(2010:2020)
#'
#' @autoglobal
#'
#' @family mock
#'
#' @export
provider_data <- function(year_seq) {

lng <- length(year_seq) * 2

vctrs::vec_rbind(
dplyr::tibble(
year = {{ year_seq }},
group = "A"),
dplyr::tibble(
year = {{ year_seq }},
group = "B")
) |>
dplyr::mutate(
net_payment = sample(
10000:20000, lng
)
)
}

#' Generate mock coding/billing data frame
#'
#' @param rows number of rows to generate; default is 10
#'
#' @param unnest a logical indicating whether to unnest the dates column;
#' default is `FALSE`
#'
#' @returns A [tibble][tibble::tibble-package]
#'
#' @examples
#' forager_data(rows = 5)
#'
#' @autoglobal
#'
#' @family mock
#'
#' @export
forager_data <- function(rows = 10, unnest = FALSE){


x <- dplyr::tibble(
claim_id = as.character(wakefield::id(n = rows)),
date_of_service = wakefield::date_stamp(
n = rows,
start = lubridate::today() - lubridate::dyears(2),
random = TRUE
),
payer = fixtuRes::set_vector(
rows,
set = c(
"Medicare",
"Medicaid",
"Cigna",
"Humana",
"UnitedHealth",
"Anthem",
"BCBS",
"Centene"
)
),
ins_class = fixtuRes::set_vector(rows, set = c("Primary", "Secondary")),
balance = as.double(wakefield::income(n = rows, digits = 2) / 300)
) |>
dplyr::mutate(
date_of_service = lubridate::as_date(date_of_service),
date_of_release = date_of_service + round(abs(stats::rnorm(
length(date_of_service), 11, 4
))),
date_of_submission = date_of_release + round(abs(stats::rnorm(
length(date_of_release), 2, 2
))),
date_of_acceptance = date_of_submission + round(abs(stats::rnorm(
length(date_of_submission), 3, 2
))),
date_of_adjudication = date_of_acceptance + round(abs(stats::rnorm(
length(date_of_acceptance), 30, 3
)))
) |>
tidyr::nest(dates = tidyr::contains("date"))

if (unnest)
x <- tidyr::unnest_wider(x, dates)

return(x)
}
56 changes: 55 additions & 1 deletion R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ display_long <- function(data) {
#'
#' @param sep separator between combined row data, default is `"-"`
#'
#' @returns A `<data.frame>` or `<tibble>` with combined columns
#' @returns A `<tibble>` with combined columns
#'
#' @examples
#' x <- fuimus:::forager_data()[-5]
Expand Down Expand Up @@ -309,3 +309,57 @@ count_wide <- function(df, rows, cols) {
values_fill = 0
)
}

#' Summary statistics
#'
#' @param df data frame
#'
#' @param condition filter condition, i.e. `patient == "new"`
#'
#' @param group_vars variables to group by, i.e. `c(specialty, state, hcpcs, cost)`
#'
#' @param summary_vars variables to summarise, i.e. `c(min, max, mode, range)`
#'
#' @param arr column to arrange data by, i.e. `cost`
#'
#' @param digits Number of digits to round to, default is 3
#'
#' @returns A `<tibble>` with the summarized data
#'
#' @examples
#' x <- dplyr::tibble(
#' provider = sample(c("A", "B", "C"), size = 200, replace = TRUE),
#' city = sample(c("ATL", "NYC"), size = 200, replace = TRUE),
#' charges = sample(1000:2000, size = 200),
#' payment = sample(1000:2000, size = 200))
#'
#' summary_stats(
#' x,
#' condition = city == "ATL",
#' group_vars = provider,
#' summary_vars = c(charges, payment),
#' arr = provider)
#' @autoglobal
#'
#' @export
summary_stats <- function(df,
condition = NULL,
group_vars = NULL,
summary_vars = NULL,
arr = NULL,
digits = 3) {

df |>
dplyr::filter({{ condition }}) |>
dplyr::summarise(
dplyr::across({{ summary_vars }},
list(median = \(x) stats::median(x, na.rm = TRUE),
mean = \(x) mean(x, na.rm = TRUE),
sd = \(x) stats::sd(x, na.rm = TRUE)),
.names = "{.col}_{.fn}"),
n = dplyr::n(),
.by = ({{ group_vars }})) |>
dplyr::arrange(dplyr::desc({{ arr }})) |>
dplyr::mutate(dplyr::across(
dplyr::where(is.double), ~roundup(., digits = digits)))
}
2 changes: 1 addition & 1 deletion man/combine.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/forager_data.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/provider_data.Rd

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

48 changes: 48 additions & 0 deletions man/summary_stats.Rd

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

Loading

0 comments on commit 590c4f9

Please sign in to comment.