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

## -----------------------------------------------------------------------------
library(insurancerating)

## -----------------------------------------------------------------------------
zip_experience <- factor_analysis(
  MTPL,
  risk_factors = "zip",
  claim_count = "nclaims",
  claim_amount = "amount",
  exposure = "exposure"
)

head(zip_experience)

## -----------------------------------------------------------------------------
claims_grid <- rating_grid(
  MTPL,
  group_by = c("zip", "bm"),
  exposure = "exposure",
  aggregate_cols = c("nclaims", "amount")
)

head(claims_grid)

## ----eval = FALSE-------------------------------------------------------------
# periods_reduced <- merge_date_ranges(
#   policy_periods,
#   period_start = "period_start",
#   period_end = "period_end",
#   group_by = c("policy_id", "coverage"),
#   aggregate_cols = "earned_exposure"
# )
# 
# grid <- rating_grid(
#   periods_reduced,
#   group_by = c("coverage", "region"),
#   exposure = "earned_exposure",
#   aggregate_cols = c("claim_count", "claim_amount")
# )

## ----eval = FALSE-------------------------------------------------------------
# thresholds <- assess_excess_threshold(
#   portfolio,
#   claim_amount = "claim_amount",
#   thresholds = c(50000, 100000, 150000),
#   exposure = "earned_exposure",
#   group = "sector",
#   claim_count = "claim_count"
# )

## ----eval = FALSE-------------------------------------------------------------
# large_loss_result <- redistribute_excess_loss(
#   portfolio,
#   claim_amount = "claim_amount",
#   threshold = 100000,
#   claim_count = "claim_count",
#   redistribution_weight = "earned_exposure",
#   risk_factor = "sector",
#   redistribution_method = "partial",
#   output = "excess_loading"
# )

## -----------------------------------------------------------------------------
age_effect <- risk_factor_gam(
  MTPL,
  risk_factor = "age_policyholder",
  claim_count = "nclaims",
  exposure = "exposure"
)

age_segments <- derive_tariff_segments(age_effect)
summary(age_segments)

## -----------------------------------------------------------------------------
portfolio <- MTPL
portfolio$zip <- factor(portfolio$zip)

frequency_model <- glm(
  nclaims ~ zip + offset(log(exposure)),
  family = poisson(),
  data = portfolio
)

portfolio$expected_claim_count <- predict(
  frequency_model,
  type = "response"
)
portfolio$claim_frequency <-
  portfolio$expected_claim_count / portfolio$exposure

## -----------------------------------------------------------------------------
rating_table(frequency_model, exposure = "exposure") |>
  add_portfolio_experience(
    data = portfolio,
    claim_count = "nclaims",
    exposure = "exposure",
    metric = "frequency"
  ) |>
  head()

## ----eval = FALSE-------------------------------------------------------------
# zip_restrictions <- data.frame(
#   zip = c("0", "3"),
#   relativity = c(0.95, 1.05)
# )
# 
# refined_model <- frequency_model |>
#   prepare_refinement(data = portfolio) |>
#   add_restriction(zip_restrictions) |>
#   refit()

## -----------------------------------------------------------------------------
check_overdispersion(frequency_model)

## ----eval = FALSE-------------------------------------------------------------
# grid_query <- rating_grid_db(
#   portfolio_db,
#   group_by = c("sector", "region"),
#   exposure = "earned_exposure",
#   aggregate_cols = c("claim_count", "claim_amount")
# )
# 
# grid <- dplyr::collect(grid_query)

