set.seed(42)
driver_age <- rep(seq(20, 59), each = 4)
exposure <- rep(1, length(driver_age))
age_band <- cut(
driver_age,
breaks = c(18, 30, 40, 50, 60),
include.lowest = TRUE
)
expected_claims <- exp(
-1.7 + 0.018 * (driver_age - 20) + 0.0006 * (driver_age - 40)^2
)
portfolio <- data.frame(
claims = rpois(length(driver_age), exposure * expected_claims),
exposure = exposure,
driver_age = driver_age,
age_band = age_band
)
model <- glm(
claims ~ age_band + offset(log(exposure)),
family = poisson(),
data = portfolio
)
refinement <- prepare_refinement(model, data = portfolio) |>
add_smoothing(
model_variable = "age_band",
source_variable = "driver_age",
breaks = c(18, 30, 40, 50, 60),
weights = "exposure"
)
# Fit and inspect the initial smoothing specification.
initial_model <- refit(refinement)
# Edit the retained specification and fit it again.
explicit_refinement <- refinement |>
edit_smoothing(
model_variable = "age_band",
from = 30,
to = 50,
from_value = 1.00,
to_value = 1.10,
control_positions = c(40),
control_values = c(1.05)
)
explicit_model <- refit(explicit_refinement)
# Keep the current shape as the basis and raise the middle of this interval
# by up to 5 percent. The inherited transition remains continuous.
adjusted_refinement <- refinement |>
edit_smoothing(
model_variable = "age_band",
from = 30,
to = 50,
adjustment = 1.05
)
adjusted_model <- refit(adjusted_refinement)
# Keep the curve unchanged through age 40, then strengthen its remaining
# change by 10 percent while retaining continuity at age 40.
steeper_refinement <- refinement |>
edit_smoothing(
model_variable = "age_band",
from = 40,
slope_adjustment = 1.10
)
# A one-sided adjustment applies from age 40 to the end of the range.
upper_tail_refinement <- refinement |>
edit_smoothing(
model_variable = "age_band",
from = 40,
adjustment = 1.05,
transition = "linear"
)
Run the code above in your browser using DataLab