Demo: Data and Functions

library(SangerTools)
library(dplyr)
library(ggplot2)

This vignette is a single-page tour of SangerTools: every bundled dataset, every exported function, and a short end-to-end mini-case that chains them together. If you have just installed the package and want to confirm everything works, run this notebook top to bottom.

Bundled datasets

SangerTools ships three fabricated population health datasets so every example in the package is reproducible without external files.

PopHealthData

A small (1,000 rows) NHS-style population dataset with one row per patient.

data("PopHealthData", package = "SangerTools")
glimpse(PopHealthData)
#> Rows: 1,000
#> Columns: 8
#> $ Sex                <chr> "Male", "Female", "Male", "Male", "Male", "Female",…
#> $ Smoker             <dbl> 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ Diabetes           <dbl> 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, …
#> $ AgeBand            <chr> "Children and Young People (0-17)", "Working Age Ad…
#> $ IMD_Decile         <dbl> 9, 5, 3, 3, 4, 6, 3, 7, 6, 4, 9, 2, 1, 9, 2, 3, 4, …
#> $ Ethnicity          <chr> "White", "White", "White", "White", "White", "White…
#> $ Locality           <chr> "Cheltenham", "Gloucester City", "Cheltenham", "Glo…
#> $ PrimaryCareNetwork <chr> "Cheltenham Central", "Tewkesbury, Newent and Staun…
head(PopHealthData)
#> # A tibble: 6 × 8
#>   Sex   Smoker Diabetes AgeBand IMD_Decile Ethnicity Locality PrimaryCareNetwork
#>   <chr>  <dbl>    <dbl> <chr>        <dbl> <chr>     <chr>    <chr>             
#> 1 Male       0        0 Childr…          9 White     Chelten… Cheltenham Central
#> 2 Fema…      0        0 Workin…          5 White     Glouces… Tewkesbury, Newen…
#> 3 Male       0        0 Workin…          3 White     Chelten… Severn Health     
#> 4 Male       0        0 Workin…          3 White     Glouces… Cheltenham Central
#> 5 Male       0        1 Older …          4 White     Chelten… Cheltenham Periph…
#> 6 Fema…      0        1 Workin…          6 White     North C… Cheltenham Central

master_patient_index

A larger (10,000 rows) fabricated Master Patient Index inspired by Gloucestershire’s population. Includes a numeric Age column suitable for banding.

data("master_patient_index", package = "SangerTools")
glimpse(master_patient_index)
#> Rows: 10,000
#> Columns: 10
#> $ Sex                <chr> "Female", "Female", "Male", "Female", "Female", "Fe…
#> $ Smoker             <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, …
#> $ Diabetes           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, …
#> $ Dementia           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ Obesity            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, …
#> $ Age                <dbl> 17, 31, 34, 62, 37, 57, 25, 14, 30, 64, 45, 70, 11,…
#> $ IMD_Decile         <dbl> 7, 1, 3, 9, 4, 9, 2, 3, 3, 1, 4, 10, 3, 4, 6, 3, 4,…
#> $ Ethnicity          <chr> "White", "White", "White", "White", "White", "White…
#> $ Locality           <chr> "Gloucester City", "Stroud and Berkeley Vale", "Che…
#> $ PrimaryCareNetwork <chr> "Tewkesbury, Newent and Staunton and West Cheltenha…

uk_pop_standard

A 5-year age band weighting taken from ONS 2018 mid-year population estimates, ready to use as a standard population for direct standardisation.

data("uk_pop_standard", package = "SangerTools")
uk_pop_standard
#> # A tibble: 21 × 2
#>    UK_Population Ageband
#>            <dbl> <fct>  
#>  1       3914000 0-4    
#>  2       4139000 5-9    
#>  3       3859000 10-14  
#>  4       3669000 15-19  
#>  5       4185000 20-24  
#>  6       4527000 25-29  
#>  7       4463000 30-34  
#>  8       4372000 35-39  
#>  9       3993000 40-44  
#> 10       4507000 45-49  
#> # ℹ 11 more rows

Putting it together

A short end-to-end mini case: take the Master Patient Index, band ages, compute age-standardised diabetes prevalence by locality using the ONS UK standard population, and plot the result with the Sanger theme.

banded <- age_bandizer(master_patient_index, Age)

rates <- standardised_rates_df(
  df = banded,
  Split_by = Locality,
  Condition = Diabetes,
  Population_Standard = NULL,
  Granular = FALSE,
  Ageband
)

rates
#> # A tibble: 7 × 2
#>   Locality                       Standardised_Rate_1k
#>   <chr>                                         <dbl>
#> 1 The Forest of Dean                             57.0
#> 2 Gloucester City                                55.7
#> 3 Cheltenham                                     54.0
#> 4 Stroud and Berkeley Vale                       49.3
#> 5 North Cotswolds                                47.6
#> 6 South Cotswolds                                36.6
#> 7 Tewkesbury Newent and Staunton                 35.4

ggplot(rates, aes(x = reorder(Locality, Standardised_Rate_1k),
                  y = Standardised_Rate_1k,
                  fill = Locality)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(
    title    = "Age-standardised diabetes prevalence",
    subtitle = "Per 1,000 patients, fabricated Gloucestershire MPI",
    x = NULL,
    y = "Standardised rate per 1,000"
  ) +
  theme_sanger() +
  scale_fill_sanger()