Skip to contents

High-level orchestration: takes a named list dfs of TPS open-data data.frames (one per CSI category) and returns both the per-year and per-ward CSI as a single rich-result object.

Usage

morie_tps_analyze_csi_from_dataframes(
  dfs,
  year_col = "OCC_YEAR",
  hood_col = "HOOD_158",
  variant = c("total", "violent")
)

Arguments

dfs

Named list of TPS data.frames. Keys outside of MORIE_TPS_CSI_CATEGORIES() are ignored.

year_col

Year column name (default "OCC_YEAR").

hood_col

Neighbourhood column name (default "HOOD_158").

variant

One of "total" or "violent" (default "total").

Value

A morie_tps_result named list carrying by_year and by_hood data.frames in payload.

Examples

set.seed(1)
df <- data.frame(OCC_YEAR = rep(2014:2023, each = 30),
                 OCC_MONTH = sample(month.name, 300, TRUE),
                 HOOD_158 = sample(sprintf("%03d", 1:20), 300, TRUE),
                 LAT_WGS84 = runif(300, 43.6, 43.8),
                 LONG_WGS84 = runif(300, -79.5, -79.2))
res <- try(morie_tps_analyze_csi_from_dataframes(list(Assault = df)))
if (!inherits(res, "try-error")) str(res, max.level = 1)
#> List of 7
#>  $ title         : chr "Toronto Crime Severity Index -- per-year + per-ward"
#>  $ call          : chr "morie_tps_analyze_csi_from_dataframes(dfs, variant=‘total’)"
#>  $ summary_lines :List of 6
#>  $ tables        : list()
#>  $ warnings      : chr(0) 
#>  $ interpretation: chr "Crime Severity Index weights each incident by the average sentence times the incarceration rate of its underlyi"| __truncated__
#>  $ payload       :List of 5
#>  - attr(*, "class")= chr [1:3] "morie_tps_result" "morie_rich_result" "list"