Skip to contents

Reads the SPI over the months in window from a single spi_expected() fit on the full history. Observed counts and expected counts are summed over the window inside each posterior draw, divided, and centred on the national observed-to-expected ratio of the same draw, so SPI = 1 is the national level. With parents, a parent unit such as a province is the sum of its districts inside each draw, so the two levels agree on how much was expected. The summation and centring are those spi_index() uses.

This differs from spi_index(). spi_index() refits the model once per assessment year on the months before that year, with the year's own counts masked, so each expected count is predicted from earlier reporting only. spi_window_summary() refits nothing: it reads any window, of any length, from one fit whose expected counts were estimated with the window's own counts included. Use it to update a rolling window every month from a fit that can be cached.

Usage

spi_window_summary(
  fit,
  window,
  id_col = "adm2_guid",
  parents = NULL,
  person_time = c("window", "observed")
)

Arguments

fit

An spi_expected() result with keep_draws = TRUE, or any list with a draws matrix and a data tibble (columns id_col, month, count, pop, one row per column of draws).

window

Date. First-of-month dates to read over, usually spi_window().

id_col

Character. District identifier column in fit$data. Default: "adm2_guid".

parents

Optional two-column data frame mapping id_col to a parent unit. The result is then one row per parent. Default: NULL.

person_time

Character. How pop_u15 is formed. The numerator is the sum, over the unit's rows in the window, of each district's annual population in fit$data$pop. With "window" (default) the denominator is the number of months in window; with "observed" it is the number of window months present in the fit. The two agree when every window month is in the fit.

Value

Tibble with one row per unit: the unit column, observed, pop_u15 (annual denominator), months (window months in the fit), expected_total (posterior median expected count), spi_median, spi_mean, spi_q05, spi_q10, spi_q90, spi_q95 and national_oe (summed observed over the median summed expected count).