Skip to content

Commit

Permalink
replace by_x() calls with utilization(type = x)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Oct 24, 2023
1 parent 9f8d56e commit 9aff8cf
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 184 deletions.
43 changes: 24 additions & 19 deletions R/compare.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Compare Yearly Provider Data To State And National Averages
#' Compare Providers to State and National Benchmarks
#'
#' @description
#' + `compare_hcpcs()` allows the user to compare a provider's yearly HCPCS
Expand All @@ -12,34 +12,38 @@
#' + `compare_hcpcs()`
#' + `compare_conditions()`
#' @examplesIf interactive()
#' compare_hcpcs(by_service(year = 2018, npi = 1023076643))
#' compare_conditions(by_provider(year = 2018, npi = 1023076643))
#' compare_hcpcs(utilization(year = 2018, type = "service", npi = 1023076643))
#' compare_conditions(utilization(year = 2018, type = "provider", npi = 1023076643))
#'
#' compare_hcpcs(map_dfr(pop_years(), ~by_service(year = .x, npi = 1023076643)))
#' compare_conditions(map_dfr(pop_years(), ~by_provider(year = .x, npi = 1023076643)))
#' compare_hcpcs(map_dfr(
#' util_years(), ~utilization(year = .x, npi = 1023076643, type = "service")))
#'
#' compare_conditions(map_dfr(
#' util_years(), ~utilization(year = .x, npi = 1023076643, type = "provider")))
#' @name compare
NULL

#' @param serv_tbl < *tbl_df* > // **required** [tibble][tibble::tibble-package] returned from [by_service()]
#' @param df < *tbl_df* > // **required** [tibble][tibble::tibble-package]
#' returned from `utilization(type = "service")`
#' @rdname compare
#' @autoglobal
#' @export
compare_hcpcs <- function(serv_tbl) {
compare_hcpcs <- function(df) {

if (!inherits(serv_tbl, "provider_by_serv")) {
if (!inherits(df, "utilization_service")) {
cli::cli_abort(c(
"{.var serv_tbl} must be of class {.cls 'provider_by_serv'}.",
"x" = "{.var serv_tbl} is of class {.cls {class(serv_tbl)}}."))
"{.var df} must be of class {.cls 'utilization_service'}.",
"x" = "{.var df} is of class {.cls {class(df)}}."))
}

x <- serv_tbl |>
x <- df |>
dplyr::select(year, state, hcpcs_code, pos) |>
dplyr::rowwise() |>
dplyr::mutate(state = list(by_geography(year, state, hcpcs_code, pos)),
national = list(by_geography(year, state = "National", hcpcs_code, pos)), .keep = "none")

results <- vctrs::vec_rbind(
dplyr::rename(serv_tbl,
dplyr::rename(df,
beneficiaries = tot_benes,
services = tot_srvcs) |>
hcpcs_cols(),
Expand Down Expand Up @@ -83,20 +87,21 @@ hcpcs_cols <- function(df) {
df |> dplyr::select(dplyr::any_of(cols))
}

#' @param prov_tbl < *tbl_df* > // **required** [tibble][tibble::tibble-package] returned from [by_provider()]
#' @param pivot < *boolean* > // __default:__ `TRUE` Pivot output
#' @param df < *tbl_df* > // **required** [tibble][tibble::tibble-package]
#' returned from `utilization(type = "provider")`
#' @param pivot < *boolean* > // __default:__ `FALSE` Pivot output
#' @rdname compare
#' @autoglobal
#' @export
compare_conditions <- function(prov_tbl, pivot = TRUE) {
compare_conditions <- function(df, pivot = FALSE) {

if (!inherits(prov_tbl, "provider_by_prov")) {
if (!inherits(df, "utilization_provider")) {
cli::cli_abort(c(
"{.var prov_tbl} must be of class {.cls 'provider_by_prov'}.",
"x" = "{.var prov_tbl} is of class {.cls {class(prov_tbl)}}."))
"{.var df} must be of class {.cls 'utilization_provider'}.",
"x" = "{.var df} is of class {.cls {class(df)}}."))
}

p <- dplyr::select(prov_tbl, year, conditions) |>
p <- dplyr::select(df, year, conditions) |>
tidyr::unnest(conditions) |>
dplyr::mutate(level = "Provider", .after = year) |>
dplyr::rename(
Expand Down
5 changes: 3 additions & 2 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ utils::globalVariables(c(
"level", # <compare_conditions>
"prevalence", # <compare_conditions>
"condition", # <compare_conditions>
"prov_tbl", # <compare_conditions>
"state", # <compare_conditions>
"sublevel", # <compare_conditions>
"state.abb", # <conditions>
Expand Down Expand Up @@ -251,8 +252,8 @@ utils::globalVariables(c(
"specialty", # <tidyup.provider>
"medical", # <tidyup.provider>
"drug", # <tidyup.provider>
"address", # <tidyup.services>
"specialty", # <tidyup.services>
"address", # <tidyup.service>
"specialty", # <tidyup.service>
"place_of_srvc", # <tidyup.geography>
"title", # <cms_update>
"modified", # <cms_update>
Expand Down
2 changes: 1 addition & 1 deletion R/ndc.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#' @examples
#' ndc_lookup("0002-1433-80")
#'
#' @examplesIf interactive()
#' medline("0002-1433-80")
#'
#' rxnorm("0002-1433-80")
#'
#' @param ndc < *character* > // **required** 10- to 11-digit National Drug Code
Expand Down
2 changes: 1 addition & 1 deletion R/quality_payment.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ quality_payment <- function(year,
top <- results |>
dplyr::select(-c(dplyr::contains("measure_"),
dplyr::contains("ind_"))) |>
dplyr::mutate(npi_id = dplyr:consecutive_id(npi), .after = npi)
dplyr::mutate(npi_id = dplyr::consecutive_id(npi), .after = npi)

measures <- dplyr::select(results, year, npi,
dplyr::contains("measure_")) |>
Expand Down
32 changes: 16 additions & 16 deletions R/utilization.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#' beneficiaries by physicians and other healthcare professionals; aggregated
#' by provider, service and geography.
#'
#' @section `"provider"`:
#' @section `type = "provider"`:
#'
#' The **Provider** dataset allows the user access to data such as
#' services and procedures performed; charges submitted and payment received;
#' and beneficiary demographic and health characteristics for providers
#' treating Original Medicare (fee-for-service) Part B beneficiaries,
#' aggregated by year.
#'
#' @section `"services"`:
#' @section `type = "service"`:
#'
#' The **Provider and Service** dataset is aggregated by:
#'
Expand All @@ -30,7 +30,7 @@
#' because separate fee schedules apply depending on whether the place
#' of service submitted on the claim is facility or non-facility.
#'
#' @section `"geography"`:
#' @section `type = "geography"`:
#'
#' The **Geography and Service** dataset contains information on utilization,
#' allowed amount, Medicare payment, and submitted charges organized nationally
Expand All @@ -46,17 +46,17 @@
#' @examplesIf interactive()
#' utilization(year = 2020, type = "provider", npi = 1003000423)
#'
#' utilization(year = 2019, type = "services", npi = 1003000126)
#' utilization(year = 2019, type = "service", npi = 1003000126)
#'
#' utilization(year = 2020, type = "geography", hcpcs_code = "0002A")
#'
#' # Use the years helper function to retrieve results for every year:
#' pop_years() |>
#' util_years() |>
#' map(\(x) utilization(year = x, type = "provider", npi = 1043477615)) |>
#' list_rbind()
#'
#' @param year < *integer* > // **required** Year data was reported, in `YYYY`
#' format. Run [pop_years()] to return a vector of the years currently available.
#' format. Run [util_years()] to return a vector of the years currently available.
#' @param type < *character* > // **required** dataset to query, `"provider"`, `"service"`, `"geography"`
#' @param npi < *integer* > 10-digit national provider identifier
#' @param first,last,organization < *character* > Individual/Organizational
Expand Down Expand Up @@ -125,7 +125,7 @@ utilization <- function(year,

rlang::check_required(year)
year <- as.character(year)
year <- rlang::arg_match(year, as.character(pop_years()))
year <- rlang::arg_match(year, as.character(util_years()))
type <- rlang::arg_match(type, c("provider", "service", "geography"))

if (type != "provider") {
Expand Down Expand Up @@ -198,7 +198,7 @@ utilization <- function(year,
"Rndrng_Prvdr_Geo_Cd", fips)

if (type == "provider") yr <- api_years("prv")
if (type == "services") yr <- api_years("srv")
if (type == "service") yr <- api_years("srv")
if (type == "geography") yr <- api_years("geo")

id <- dplyr::filter(yr, year == {{ year }}) |> dplyr::pull(distro)
Expand Down Expand Up @@ -243,7 +243,7 @@ utilization <- function(year,
if (tidy) {
results$year <- year
if (type == "provider") results <- tidyup.provider(results, nest = nest, detailed = detailed)
if (type == "services") results <- tidyup.services(results, rbcs = rbcs)
if (type == "service") results <- tidyup.service(results, rbcs = rbcs)
if (type == "geography") results <- tidyup.geography(results, rbcs = rbcs)
if (na.rm) results <- narm(results)
}
Expand Down Expand Up @@ -292,11 +292,11 @@ tidyup.provider <- function(results, nest, detailed) {
return(results)
}

#' @param results data frame from [utilization(type = "services")]
#' @param results data frame from [utilization(type = "service")]
#' @param rbcs < *boolean* > Add Restructured BETOS Classifications to HCPCS codes
#' @autoglobal
#' @noRd
tidyup.services <- function(results, rbcs) {
tidyup.service <- function(results, rbcs) {

results$level <- "Provider"

Expand All @@ -306,12 +306,12 @@ tidyup.services <- function(results, rbcs) {
dbl = "avg_",
yr = 'year') |>
combine(address, c('rndrng_prvdr_st1', 'rndrng_prvdr_st2')) |>
cols_util("services") |>
cols_util("service") |>
dplyr::mutate(specialty = correct_specialty(specialty))

if (rbcs) results <- rbcs_util(results)

class(results) <- c("utilization_services", class(results))
class(results) <- c("utilization_service", class(results))
return(results)
}

Expand All @@ -336,12 +336,12 @@ tidyup.geography <- function(results, rbcs) {
}

#' @param df data frame
#' @param type 'provider', 'services', 'geography' or 'rbcs'
#' @param type 'provider', 'service', 'geography' or 'rbcs'
#' @autoglobal
#' @noRd
cols_util <- function(df,
type = c("provider",
"services",
"service",
"geography",
"rbcs")) {

Expand All @@ -365,7 +365,7 @@ cols_util <- function(df,
"avg_std_pymt" = "avg_mdcr_stdzd_amt")
}

if (type == "services") {
if (type == "service") {

cols <- c('year',
'npi' = 'rndrng_npi',
Expand Down
24 changes: 13 additions & 11 deletions man/compare.Rd

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

4 changes: 2 additions & 2 deletions man/ndc_lookup.Rd

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

12 changes: 6 additions & 6 deletions man/utilization.Rd

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

Loading

0 comments on commit 9aff8cf

Please sign in to comment.