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

## -----------------------------------------------------------------------------
library(pksR)

## ----eval = FALSE-------------------------------------------------------------
# # Latest boundary, population weights: the released panel
# latest <- pks_get(years = 2005:2015)
# 
# # Boundary of 2011, after the reform in Mecklenburg-Vorpommern
# d2011 <- pks_get(years = 2005:2011, harmonize_to = 2011)
# 
# # Area weights instead of population weights
# area <- pks_get(years = 2005:2015, weights = "area")

## ----eval = FALSE-------------------------------------------------------------
# d <- pks_get(years = 2005:2012, offence = "total")
# w <- pks_compare_weights(d)   # downloads the same request with area weights
# w[abs(w$relative_difference) > 0.01, c("year", "ags", "region_name", "cases_population", "cases_area")]

## ----eval = FALSE-------------------------------------------------------------
# w$log_hz_population <- log(w$hz_population)
# w$log_hz_area <- log(w$hz_area)
# # e.g. the same two-way fixed effects model on log_hz_population and on log_hz_area

## ----eval = FALSE-------------------------------------------------------------
# published <- pks_get(years = 2005:2012, offence = "total", harmonize_to = NULL)
# table(published$year)          # the number of districts changes with the reforms
# harmonized <- pks_get(years = 2005:2012, offence = "total")
# table(harmonized$year)         # the same 400 districts every year

## ----eval = FALSE-------------------------------------------------------------
# # Districts that exist with the same code in every year of the period.
# # A district that absorbed another keeps its code (the Wartburgkreis in
# # 2021): drop such districts too, using the crosswalk of the release.
# stable <- Reduce(intersect, split(published$ags, published$year))
# balanced <- published[published$ags %in% stable, ]
# # Harmonized rows that rest on split weights
# sum(harmonized$estimated)

## -----------------------------------------------------------------------------
d <- pks_example()
r <- pks_rates(d, population = "corrected")
head(r[, c("year", "ags", "offence_key", "hz", "rate_population")])

## -----------------------------------------------------------------------------
d <- pks_example()
pop <- unique(data.frame(year = d$year, ags = d$ags, population = d$derived_population))
head(pks_rates(d, population = pop)[, c("year", "ags", "offence_key", "hz")])

