## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----install-cran, eval = FALSE-----------------------------------------------
# install.packages("deprivateR")

## ----install-gh, eval = FALSE-------------------------------------------------
# # install.packages("remotes")
# remotes::install_github("pfizer-opensource/deprivateR")

## ----api-key, eval = FALSE----------------------------------------------------
# tidycensus::census_api_key("YOUR_KEY_HERE", install = TRUE)

## ----load---------------------------------------------------------------------
library(deprivateR)

## ----sample-calc--------------------------------------------------------------
# load sample data for the Messer NDI
ndi_data <- dep_sample_data(index = "ndi_m")

# calculate the index
ndi_results <- dep_calc_index(
  ndi_data,
  geography = "county",
  index = "ndi_m",
  year = 2022,
  return_percentiles = TRUE
)

# view the results
ndi_results[, c("GEOID", "NAME", "NDI_M")]

## ----quantiles----------------------------------------------------------------
# split NDI into quartiles
ndi_results <- dep_quantiles(
  ndi_results,
  source_var = NDI_M,
  new_var = ndi_quartile,
  n = 4L,
  return = "label"
)

# view the distribution
table(ndi_results$ndi_quartile)

## ----map-breaks---------------------------------------------------------------
# calculate Fisher-Jenks breaks with 5 classes
ndi_results <- dep_map_breaks(
  ndi_results,
  var = "NDI_M",
  new_var = "map_class",
  classes = 5,
  style = "fisher"
)

# view the break labels
levels(ndi_results$map_class)

## ----manual-breaks------------------------------------------------------------
# define custom break points
my_breaks <- c(
  min(ndi_results$NDI_M, na.rm = TRUE),
  25, 50, 75,
  max(ndi_results$NDI_M, na.rm = TRUE)
)

# apply manual breaks
ndi_results <- dep_map_breaks(
  ndi_results,
  var = "NDI_M",
  new_var = "map_class_manual",
  breaks = my_breaks
)

levels(ndi_results$map_class_manual)

## ----get-index, eval = FALSE--------------------------------------------------
# # download and calculate SVI for Missouri tracts
# mo_svi <- dep_get_index(
# 
#   geography = "tract",
#   index = "svi20",
#   year = 2020,
#   state = "MO"
# )

## ----multi-index, eval = FALSE------------------------------------------------
# # calculate ADI and Gini together for Missouri counties
# mo_multi <- dep_get_index(
#   geography = "county",
#   index = c("adi", "gini"),
#   year = 2022,
#   state = "MO"
# )

## ----sf-output, eval = FALSE--------------------------------------------------
# # get SVI with geometry for mapping
# mo_svi_sf <- dep_get_index(
#   geography = "tract",
#   index = "svi20",
#   year = 2020,
#   state = "MO",
#   output = "sf"
# )
# 
# # plot with ggplot2
# library(ggplot2)
# ggplot(mo_svi_sf) +
#   geom_sf(aes(fill = SVI20), color = NA) +
#   scale_fill_viridis_c(direction = -1) +
#   theme_void() +
#   labs(title = "Social Vulnerability Index, Missouri Tracts (2020)")

## ----subscales, eval = FALSE--------------------------------------------------
# # keep SVI theme subscales and all component variables
# mo_detailed <- dep_get_index(
#   geography = "county",
#   index = "svi20",
#   year = 2020,
#   state = "MO",
#   keep_subscales = TRUE,
#   keep_components = TRUE
# )

## ----two-step, eval = FALSE---------------------------------------------------
# # step 1: build the variable list and download data
# library(tidycensus)
# 
# vars <- dep_build_varlist(
#   geography = "county",
#   index = "ndi_m",
#   year = 2022
# )
# 
# raw_data <- get_acs(
#   geography = "county",
#   variables = vars,
#   year = 2022,
#   state = "MO",
#   output = "wide"
# )
# 
# # step 2: calculate the index on your data
# results <- dep_calc_index(
#   raw_data,
#   geography = "county",
#   index = "ndi_m",
#   year = 2022
# )

