inst/doc/basic_demo.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = FALSE, # hide code
  message = FALSE, # hide messages
  warning = FALSE, # hide warnings
  fig.width = 12,
  fig.height = 7,
  out.width = "100%"
)

## -----------------------------------------------------------------------------
library(gsClusterDetect)
suppressWarnings(suppressMessages(library(data.table)))
data("example_count_data", package = "gsClusterDetect")
data("spline_01", package = "gsClusterDetect")
data("counties", package = "gsClusterDetect")

## ----echo = TRUE--------------------------------------------------------------
# use the built in example_count_data
cases <- data.table::as.data.table(example_count_data)

head(cases)

## ----echo=TRUE----------------------------------------------------------------
detect_date <- max(cases$date)

# get the test dates, given a detect date and test_length
get_test_dates(detect_date, test_length = 7)

# get the baseline dates, given a detect date, test and baseline length
get_baseline_dates(
  detect_date,
  test_length = 7,
  baseline_length = 90,
  guard = 0
) |> head()

## ----echo=TRUE----------------------------------------------------------------
zip_dm <- zip_distance_matrix("DC")
dim(zip_dm$distance_matrix)

county_dm <- county_distance_matrix("RI", source = "tigris")
dim(county_dm$distance_matrix)

## ----eval = FALSE, echo=TRUE--------------------------------------------------
# us_dm <- us_distance_matrix()

## ----echo=TRUE----------------------------------------------------------------
county_list <- create_dist_list(
  level = "county",
  st = "RI",
  threshold = 25
)
length(county_list)
head(county_list[[1]])

## ----echo=TRUE----------------------------------------------------------------
locs <- unique(cases$location)
dm <- outer(seq_along(locs), seq_along(locs), function(i, j) abs(i - j) * 5)
dimnames(dm) <- list(locs, locs)

## ----echo = TRUE--------------------------------------------------------------
clusters <- find_clusters(
  cases = cases,
  distance_matrix = dm,
  detect_date = detect_date,
  spline_lookup = "01",
  baseline_length = 90,
  max_test_window_days = 7,
  distance_limit = 15,
  baseline_adjustment = "add_test",
)

## ----plotting clusters, echo = TRUE-------------------------------------------
library(gsClusterDetect)
library(tigris)

options(tigris_use_cache = TRUE)

# Use example count data to find clusters
d <- example_count_data
dd <- d[, max(date)]
dm <- create_dist_list("county", st = "OH", threshold = 50, )
cl <- find_clusters(d, dm, dd)

# Get tigris based shape file
oh <- tigris::counties("OH", cb = TRUE, class = "sf")

# Pass these to the map_clusters() function
map_clusters(cl = cl, s = oh, s_id = "GEOID")

## ----echo=TRUE----------------------------------------------------------------
summary_tbl <- generate_summary_table(
  data = cases,
  end_date = detect_date,
  baseline_length = 90,
  test_length = 7
)
summary_tbl

## ----echo=TRUE----------------------------------------------------------------
heatmap_data <- generate_heatmap_data(
  data = cases,
  end_date = detect_date,
  baseline_length = 90,
  test_length = 7
)

p_heat <- generate_heatmap(heatmap_data, plot_type = "plotly")
class(p_heat)
p_heat

## ----echo = TRUE--------------------------------------------------------------
ts_data <- generate_time_series_data(
  data = cases,
  end_date = detect_date,
  baseline_length = 90,
  test_length = 7
)

p_ts <- generate_time_series_plot(ts_data, plot_type = "plotly")
class(p_ts)
p_ts

Try the gsClusterDetect package in your browser

Any scripts or data that you put into this service are public.

gsClusterDetect documentation built on July 27, 2026, 5:09 p.m.