Quarto Demo for DSAC/Eng CoP

Medicare Growth in Pennsylvania

Last Updated

February 10, 2026

TipSouped up version!

The differences here from the base version are that:

  1. additional yaml matter (e.g. dynamic date, dynamic subtitle, default turning off code rendering)
  2. code can now be viewed by the reader by clicking a link in the upper right hand corner
  3. Table of Contents added for easier navigation
  4. custom css formatting applied from an external css file in the repo
  5. params added to yaml that allow us to use those repeated parameters throughout the report and allows us to use this as parameterized report template, i.e. we can render this report for every state
Warning

This demo report relies on R, but you can use R, Python, Julia, and Observable.

This demo report renders a document, but Quarto has a number of output options including documents, presentations, dahboards, websites, books.

BACKGROUND

This is an important annual report where we compare growth in beneficiaries at the county level in Pennsylvania. Public data on monthly Medicare enrollments were pulled from Data.CMS.gov using the API.

Tip

We can show important code even if most are not shown by default, using echo: true in this code chunk. This chunk also includes code-fold: true to that it is accessible, but tucked away.

Code
# function to fetch data from data.cms.gov
fetch_data <- function(state, year, dataset_uuid,
                       baseurl = "https://data.cms.gov/data-api/v1") {

  endpoint <- stringr::str_glue("{baseurl}/dataset/{dataset_uuid}/data")

  cols <- paste(
    c("BENE_STATE_DESC", "BENE_STATE_ABRVTN", "BENE_COUNTY_DESC",
      "BENE_FIPS_CD", "BENE_GEO_LVL", "YEAR", "MONTH", "TOT_BENES"),
    collapse = ","
  )

  httr2::request(endpoint) |>
    httr2::req_url_query(
      `filter[BENE_STATE_ABRVTN]` = state,
      `filter[YEAR]` = year,
      `filter[BENE_GEO_LVL]` = "County",
      `filter[MONTH]` = "Year",
      column = cols
    ) |>
    httr2::req_throttle(rate = 2) |>  #max requests/sec
    httr2::req_retry(max_tries = 3) |>
    httr2::req_perform() |>
    httr2::resp_body_json()

}

The Medicare beneficiary dataset for this analysis pulled down just a subset of the rows and columns of the full data - 1,026 rows and 8 columns. The analysis is focusing in on only the total beneficiary counts (tot_benes) in Pennsylvania counties in 2020 to 2024.

Initial Munge

Once the data were loaded, we clean them for ease of use and to calculate the growth rate over this period in time. The following munging steps were performed1:

  • renamed columns for ease of use
  • filtered down the data to the particular state and focal years
  • subset the columns to only those that were needed for analysis

Access Spatial Data

We also brought in spatial data to map the growth rates across the state, leveraging the tigris package.

IMPORTANT ANALYSIS SECTION

Time to do some calculations!

Calculate Growth Rate

We calcualted growth rates to identify the counties with the largest growth during this period.

growth rate = (end value / start value) - 1

Join Data

Before we were able to visualize it, we needed to join the spatial data with the enrollment data using the county FIPS codes.

Tip

This is another example of showing an important code even if most are not shown by default, using echo: true in this code chunk.

df_viz <- left_join(state_counties_sf, df_gr, by = "county_fips")

VIZ

Okay, time for the fun stuff - data viz!

Top Counties

Here is a table (Table 1) of Pennsylvania’s largest counties in terms of growth rate over this period.

Table 1: Counties with the largest growth rates
County 2020 2024 Growth Rate
Pike County 14,565 16,534 13.5%
Butler County 43,738 49,092 12.2%
Sullivan County 1,879 2,107 12.1%
Chester County 97,478 109,107 11.9%
Northampton County 69,606 76,986 10.6%

State Map

We can visualize all Pennsylvania’s county growth rate data for comparison in a map (Figure 1).

Figure 1: County Growth Rates

PARAMETERIZE

Note

This qmd file now includes parameters (params) in the yaml. The parameters can be used through the documents (e.g. used to filter by state and years, dynamic text for titles/references).

The parameters can be changed manually and re-render, but we can pass the parameters through the render command in the terminal, a makefile, or another script to run this over multiple states or year and outputs.

From another Quarto or R script you could write the following code.

library(quarto)
library(purrr)

walk(
  c("Texas", "California", "New York"),
  ~ quarto_render(
    input = "demo_fancy.qmd",
    output_format = "pdf"
    execute_params = list(
      state = .x,
      start_year = 2020,
      end_year = 2024
    ),
    output_file = paste0("medicare_growth_", .x, ".pdf")
  )
)

All data are public and sourced from Data.CMS.gov

Disclaimer: The findings, interpretation, and conclusions expressed herein are those of the authors and do not necessarily reflect the views of Centers for Medicare and Medicaid Services. All errors remain our own.

Footnotes

  1. Note that some of the filtering and subseting occurred in the API call. Keeping these steps included here allow for transparency as well as the ability reproduce with the full dataset↩︎