Skip to contents

Accepts either a long-format data.frame (columns year, category, count) or a nested list keyed [[year]][[category]] = count.

Usage

morie_tps_csi_per_year(
  counts_per_year,
  variant = c("total", "violent"),
  weights = NULL,
  population = NULL,
  per_capita_unit = 100000L,
  rebase_to_year = NULL,
  rebase_to_value = 100
)

Arguments

counts_per_year

Long data.frame or nested list (see above).

variant

One of "total" or "violent".

weights

Optional override vector of weights.

population

Optional named integer vector (year -> pop); defaults to MORIE_TPS_TORONTO_POPULATION_BY_YEAR().

per_capita_unit

Rate denominator (default 100000).

rebase_to_year

Optional anchor year for the index.

rebase_to_value

Index value at the anchor year (default 100).

Value

A data.frame with one row per year.

Details

Returns a data.frame indexed by year with columns:

  • raw_weighted_sum – sum_c w_c * n_(c,year)

  • total_count – sum_c n_(c,year)

  • population – Toronto population that year

  • csi_per_capita – raw_weighted_sum / population * per_capita_unit

  • simple_count_rate – total_count / population * per_capita_unit

When rebase_to_year is supplied, an additional csi_index column is added, anchored so that that year's value equals rebase_to_value.

Examples

set.seed(1)
counts <- data.frame(year = rep(2020:2023, 2),
                     category = rep(c("Assault", "Robbery"), each = 4),
                     count = rpois(8, 50))
res <- try(morie_tps_csi_per_year(counts))
if (!inherits(res, "try-error")) str(res, max.level = 1)
#> 'data.frame':	4 obs. of  6 variables:
#>  $ year             : int  2020 2021 2022 2023
#>  $ raw_weighted_sum : num  28722 38746 39779 38398
#>  $ total_count      : num  84 112 113 106
#>  $ population       : num  3005500 3017400 3046800 3080000
#>  $ csi_per_capita   : num  956 1284 1306 1247
#>  $ simple_count_rate: num  2.79 3.71 3.71 3.44