GLMs for insurance rating: Goldburd, Khare, Tevet and Guller with prospicio
glm-ratemaking-goldburd.RmdRendered from docs/guides/glm-ratemaking-goldburd.qmd.
The code runs each time the site is built, so the output below is
current.
Mark Goldburd, Anand Khare, Dan Tevet and Dmitriy Guller wrote Generalized Linear Models for Insurance Rating (CAS Monograph 5, second edition, 2025 revision), the CAS text on building a classification rating plan with GLMs: the log link as a multiplicative plan, frequency, severity and Tweedie models, the measures that refine and choose between models, and extensions such as GAMs.
This guide follows that workflow in the prospicio R package
(glm_fit() and the model metrics, with the distribution
functions for the Tweedie and the F-test). The PDF does not contain the
insurers’ data, so the guide does not rerun the monograph’s coefficient
tables (Tables 4 to 12, Figure 9, Table 14) or its Gini of 56.1%. Where
the monograph prints arithmetic that a reader can redo, the tables show
the monograph’s value beside prospicio’s. The rest runs on a portfolio
that the repository ships.
This is part 1 (chapters 2 to 6.2: the rating plan, offsets and
weights, the choice of distribution, tests of significance). Part 2, glm-ratemaking-goldburd-validation.qmd,
covers continuous variables, interactions, residuals, validation and
regularization (chapters 5.4 to 7 and 10). Section numbers in
parentheses are the monograph’s.
This is the R twin of glm-ratemaking-goldburd.md,
with the same sections and numbers.
R/prospicio/tests/test-guides.R runs the chunks in order
and checks each against the text below it.
1. The data
validation/data/glm_policies.csv is a synthetic
portfolio of 600 policies, written by
validation/scripts/statsmodels_glm.py. It has a driver age
(18 to 80), a region (A to D, A the most populous and D the thinnest),
an exposure in years and a response for every family. A fixed seed draws
them from a known model, so the guide shows what a GLM recovers and what
it misses.
-
claimsis Poisson with meanexposure * exp(-1 - 0.01 (age - 40)) * r. The region factors arer = 1.0, 1.2, 0.8, 1.5for A to D. -
wavy_claimsis Poisson with a non-linear age effect,exposure * exp(0.5 + 0.8 sin(age / 8)). -
avg_sevis a gamma average ofn_sevclaims. -
pureis a compound Poisson-gamma pure premium.
The portfolio drew 155 claims where the true model expects 134 (1.8 standard deviations more), so fitted base rates run above the true one.
library(prospicio)
# The data file, found from the repository root (CI) or from docs/guides (Quarto).
find_data <- function(name, dir = getwd()) {
repeat {
path <- file.path(dir, "validation", "data", name)
if (file.exists(path)) return(path)
parent <- dirname(dir)
if (identical(parent, dir)) stop("validation/data/", name, " not found", call. = FALSE)
dir <- parent
}
}
d <- read.csv(find_data("glm_policies.csv"))
n <- nrow(d)
cat(n, "policies,", sprintf("%.1f", sum(d$exposure)), "years of exposure,", sum(d$claims), "claims\n")## 600 policies, 358.0 years of exposure, 155 claims
tab <- table(d$region)
cat("policies by region:", paste0(names(tab), " ", tab, collapse = ", "), "\n")## policies by region: A 243, B 163, C 120, D 74
true_r <- c(A = 1.0, B = 1.2, C = 0.8, D = 1.5)
true_mu <- d$exposure * exp(-1 - 0.01 * (d$age - 40)) * true_r[d$region]
cat(sprintf("claims expected under the true model: %.1f\n", sum(true_mu)))## claims expected under the true model: 134.0
2. The GLM as a multiplicative rating plan (sections 2.1 to 2.4)
A GLM draws the response from an exponential-family distribution with
mean mu and dispersion phi, with a link
function of the mean linear in the predictors. With the log link,
mu = exp(b0) * exp(b1 x1) * ...: a base rate times one
relativity for each rating variable. Premiums stay positive and a
surcharge is a percentage.
Section 2.1.3 uses a gamma severity model with intercept 5.8, a
driver-age slope of 0.1, a married coefficient of -0.15 and dispersion
0.3. The variance is phi * V(mu), and for the gamma
V(mu) = mu^2, so the coefficient of variation is
sqrt(phi) at any mean. gamma_from_mean_cv()
gives the variances of section 2.2:
library(gt)
# One row of a comparison with the book; `book` is NA where the book prints nothing.
row <- function(item, mine, book, decimals, signed = FALSE) {
data.frame(item = item, prospicio = mine, book = book, decimals = decimals, signed = signed)
}
book_table <- function(rows, title, subtitle = NULL, book = "monograph") {
rows <- do.call(rbind, rows)
tbl <- gt(setNames(rows[c("item", "prospicio", "book")], c("item", "prospicio", book)), rowname_col = "item") |>
tab_header(title, subtitle)
for (i in seq_len(nrow(rows))) {
tbl <- fmt_number(tbl, columns = c("prospicio", book), rows = i,
decimals = rows$decimals[i], force_sign = rows$signed[i])
}
sub_missing(tbl, missing_text = "")
}
b0 <- 5.8; b_age <- 0.1; b_married <- -0.15; phi <- 0.3
eta <- b0 + b_age * 25 + b_married * 1
factors <- c(round(exp(b0), 2), round(exp(b_age * 25), 3), round(exp(b_married), 3))
variance_m <- function(mean) variance(gamma_from_mean_cv(mean, sqrt(phi))) / 1e6
book_table(list(
row("linear predictor, 25, married", eta, 8.15, 2),
row("severity, 25, married", exp(eta), 3463.38, 2),
row("base rate", factors[1], 330.30, 2),
row("age factor, 25 years", factors[2], 12.182, 3),
row("married factor", factors[3], 0.861, 3),
row("product of the rounded factors", prod(factors), 3464.42, 2),
row("severity, 35, unmarried", exp(b0 + b_age * 35), 10938, 0),
row("variance at mean 3,464, millions", variance_m(3464), 3.6, 1),
row("variance at mean 10,938, millions", variance_m(10938), 35.9, 1)
), "Gamma severity model of section 2.1.3")| Gamma severity model of section 2.1.3 | ||
| prospicio | monograph | |
|---|---|---|
| linear predictor, 25, married | 8.15 | 8.15 |
| severity, 25, married | 3,463.38 | 3,463.38 |
| base rate | 330.30 | 330.30 |
| age factor, 25 years | 12.182 | 12.182 |
| married factor | 0.861 | 0.861 |
| product of the rounded factors | 3,464.42 | 3,464.42 |
| severity, 35, unmarried | 10,938 | 10,938 |
| variance at mean 3,464, millions | 3.6 | 3.6 |
| variance at mean 10,938, millions | 35.9 | 35.9 |
Both monograph figures are right. The value 3,463.38 is the exact mean. The value 3,464.42 is the product of factors rounded to the precision of a rating manual.
A categorical variable enters as one 0/1 column for each level except
a base level. Each coefficient is a log relativity to the base, and its
exponential is a factor (section 2.4.2: SUV 1.23, truck 0.57, van -0.30
against sedan). To move the base to van (Table 5), subtract the van
coefficient from every level. A logged continuous predictor gives a
power curve (section 2.4.1): a coefficient of 0.62 on log amount of
insurance makes $200,000 cost 2^0.62 times $100,000. A
logistic coefficient is a log odds ratio (section 2.8): 0.24 means 27%
higher odds.
vtype <- c(SUV = 1.23, truck = 0.57, van = -0.30)
factor_v <- exp(vtype)
rebased <- c(sedan = 0 - vtype[["van"]], SUV = vtype[["SUV"]] - vtype[["van"]], truck = vtype[["truck"]] - vtype[["van"]])
book_table(list(
row("factor, SUV", factor_v[["SUV"]], 3.421, 3),
row("factor, truck", factor_v[["truck"]], NA, 3),
row("factor, van", factor_v[["van"]], 0.741, 3),
row("change in %, SUV", 100 * (factor_v[["SUV"]] - 1), 242, 0, TRUE),
row("change in %, van", 100 * (factor_v[["van"]] - 1), -25.9, 1, TRUE),
row("van as base, sedan", rebased[["sedan"]], 0.30, 2),
row("van as base, SUV", rebased[["SUV"]], 1.53, 2),
row("van as base, truck", rebased[["truck"]], 0.88, 2),
row("AOI 200k against 100k", (200 / 100)^0.62, 1.54, 2),
row("odds change in %, coefficient 0.24", 100 * (exp(0.24) - 1), 27, 0)
), "Vehicle type, amount of insurance and odds")| Vehicle type, amount of insurance and odds | ||
| prospicio | monograph | |
|---|---|---|
| factor, SUV | 3.421 | 3.421 |
| factor, truck | 1.768 | |
| factor, van | 0.741 | 0.741 |
| change in %, SUV | +242 | +242 |
| change in %, van | −25.9 | −25.9 |
| van as base, sedan | 0.30 | 0.30 |
| van as base, SUV | 1.53 | 1.53 |
| van as base, truck | 0.87 | 0.88 |
| AOI 200k against 100k | 1.54 | 1.54 |
| odds change in %, coefficient 0.24 | 27 | 27 |
The rebased truck coefficient is 0.87 from the two-decimal coefficients of Table 4, and Table 5 prints 0.88. Unrounded estimates of 0.574 and -0.304 would give 0.878. This is rounding, not an error.
The base level changes the standard errors (section 2.4.3). A
coefficient measures a level against the base, so a thin base level
makes every comparison uncertain. Region D has 74 policies and region A
has 243. glm_fit() codes a factor with the first level as
the base, so factor(levels = ) sets the base. The claim
count model has the log of exposure as its offset (section 2.6,
next):
with_base <- function(base) {
dd <- d
dd$region <- factor(dd$region, levels = c(base, setdiff(c("A", "B", "C", "D"), base)))
dd
}
estimates <- lapply(c("A", "D"), function(base) {
fit <- glm_fit(claims ~ age + region + offset(log(exposure)), with_base(base), family = "poisson")
keep <- -(1:2)
data.frame(region = sub("^region", "", names(fit@coefficients)[keep]),
estimate = unname(fit@coefficients[keep]), se = unname(fit@std_errors[keep]))
})
by_base <- data.frame(region = c("A", "B", "C", "D"), check.names = FALSE)
for (k in 1:2) {
base <- c("A", "D")[k]
e <- estimates[[k]]
by_base[[paste("estimate", base)]] <- e$estimate[match(by_base$region, e$region)]
by_base[[paste("std. error", base)]] <- e$se[match(by_base$region, e$region)]
}
gt(by_base, rowname_col = "region") |>
tab_header("Region coefficients by base level", "Poisson claim counts on age and region, log exposure as offset") |>
tab_spanner("Base A", c("estimate A", "std. error A")) |>
tab_spanner("Base D", c("estimate D", "std. error D")) |>
fmt_number(columns = c("estimate A", "estimate D"), decimals = 3, force_sign = TRUE) |>
fmt_number(columns = c("std. error A", "std. error D"), decimals = 3) |>
sub_missing(missing_text = "")| Region coefficients by base level | ||||
| Poisson claim counts on age and region, log exposure as offset | ||||
|
Base A
|
Base D
|
|||
|---|---|---|---|---|
| estimate A | std. error A | estimate D | std. error D | |
| A | −0.204 | 0.245 | ||
| B | +0.078 | 0.192 | −0.126 | 0.253 |
| C | −0.258 | 0.249 | −0.462 | 0.299 |
| D | +0.204 | 0.245 | ||
The two fits are the same model. With D as the base, every standard error is larger, because every coefficient depends on the thinnest level. The true factors for B, C and D against A are 1.2, 0.8 and 1.5 (log relativities +0.18, -0.22 and +0.41), and with 155 claims the fit pins down none of them. A GLM gives the data full credibility and reports uncertainty only in the standard errors (section 2.10).
The figure shows each log relativity against base A with its 95% interval. The diamond is the true value:
library(ggplot2, exclude = "layer") # keeps prospicio's layer()
fit_a <- glm_fit(claims ~ age + region + offset(log(exposure)), with_base("A"), family = "poisson")
region_fit <- data.frame(region = paste("region", c("B", "C", "D")),
estimate = fit_a@coefficients[-(1:2)],
half = 1.96 * fit_a@std_errors[-(1:2)],
truth = log(true_r[c("B", "C", "D")]))
ggplot(region_fit, aes(region)) +
geom_hline(yintercept = 0, colour = "grey75") +
geom_pointrange(aes(y = estimate, ymin = estimate - half, ymax = estimate + half,
colour = "estimate with 95% interval"),
position = position_nudge(x = -0.06)) +
geom_point(aes(y = truth, colour = "true value"), shape = 18, size = 3,
position = position_nudge(x = 0.06)) +
labs(x = NULL, y = "log relativity against region A", colour = NULL,
title = "Region relativities: every interval covers the truth and zero") +
theme(legend.position = "top")3. Offsets and weights (sections 2.5 and 2.6)
An offset is a predictor with its coefficient fixed at 1: a part of
the rating plan that the GLM must respect but not re-estimate, such as
deductible factors or territorial base rates. On the log link the offset
is the log of the factor, and several offsets add. A weight divides the
variance: a row that averages w claims has 1/w
of the variance of one claim.
offsets <- Map(function(f, book) row(sprintf("offset, deductible factor %.2f", f), log(f), book, 4),
c(1.00, 0.95, 0.90), c(0, -0.0513, -0.1054))
book_table(c(offsets, list(row("log base loss cost 265", log(265), 5.5797, 4),
row("log loss cost, base 265, factor 0.90", log(265) + log(0.90), 5.4744, 4))),
"Offsets of section 2.5")| Offsets of section 2.5 | ||
| prospicio | monograph | |
|---|---|---|
| offset, deductible factor 1.00 | 0.0000 | 0.0000 |
| offset, deductible factor 0.95 | −0.0513 | −0.0513 |
| offset, deductible factor 0.90 | −0.1054 | −0.1054 |
| log base loss cost 265 | 5.5797 | 5.5797 |
| log loss cost, base 265, factor 0.90 | 5.4744 | 5.4744 |
Section 2.6: a Poisson model of the claim count with log exposure as the offset and a Poisson model of the frequency (claims per year) with exposure as the weight give the same coefficients and standard errors. prospicio fits the non-integer frequency as given:
counts <- glm_fit(claims ~ age + region + offset(log(exposure)), d, family = "poisson")
d$freq <- d$claims / d$exposure
rates <- glm_fit(freq ~ age + region, d, family = "poisson", weights = d$exposure)
cat("coefficients agree:", max(abs(counts@coefficients - rates@coefficients)) < 1e-9, "\n")## coefficients agree: TRUE
## standard errors agree: TRUE
gt(data.frame(term = names(counts@coefficients), `coef, offset` = unname(counts@coefficients),
`coef, weight` = unname(rates@coefficients), `se, offset` = unname(counts@std_errors),
`se, weight` = unname(rates@std_errors), check.names = FALSE), rowname_col = "term") |>
tab_header("Claim counts with an offset, frequency with a weight") |>
fmt_number(decimals = 4)| Claim counts with an offset, frequency with a weight | ||||
| coef, offset | coef, weight | se, offset | se, weight | |
|---|---|---|---|---|
| (Intercept) | −0.3636 | −0.3636 | 0.2394 | 0.2394 |
| age | −0.0105 | −0.0105 | 0.0045 | 0.0045 |
| regionB | 0.0778 | 0.0778 | 0.1921 | 0.1921 |
| regionC | −0.2581 | −0.2581 | 0.2490 | 0.2490 |
| regionD | 0.2036 | 0.2036 | 0.2448 | 0.2448 |
The age coefficient is close to the true value of -0.01. Footnote 5: the equivalence fails for the negative binomial (section 4).
A fixed external factor works the same way. If the filed region
factors are not under review, the offset is
log(exposure) + log(factor).
4. Choosing the distribution (sections 2.7 and 5.2)
Frequency. The Poisson fixes phi = 1.
The overdispersed Poisson estimates phi
(dispersion = "pearson"), which keeps the coefficients and
scales the standard errors by sqrt(phi). The negative
binomial adds a parameter k with variance
mu (1 + k mu). The "negative_binomial" family
takes theta = 1/k as an input. glm_nb_fit()
estimates theta with the coefficients by maximum likelihood
(as MASS::glm.nb does). The nb column was
drawn with theta = 2:
odp <- glm_fit(claims ~ age + region + offset(log(exposure)), d, family = "poisson", dispersion = "pearson")
cat(sprintf("overdispersed Poisson: phi %.3f, same coefficients: %s , age se %.5f -> %.5f\n", odp@dispersion,
max(abs(odp@coefficients - counts@coefficients)) < 1e-12, counts@std_errors[[2]], odp@std_errors[[2]]))## overdispersed Poisson: phi 0.944, same coefficients: TRUE , age se 0.00452 -> 0.00439
nb_fit <- glm_nb_fit(nb ~ age + region + offset(log(exposure)), d)
cat(sprintf("negative binomial: theta %.2f (se %.2f, drawn with 2), log-likelihood %.1f\n",
nb_fit$theta, nb_fit$se_theta, nb_fit$log_likelihood))## negative binomial: theta 1.40 (se 0.21, drawn with 2), log-likelihood -899.8
The Poisson data are not overdispersed (phi is just
under 1). The estimated theta is below the 2 that drew the
column: sampling variation in 600 policies. Because the variance
contains theta, a count model with an offset and a
frequency model with a weight estimate it differently (footnote 5).
Severity. The gamma (V = mu^2) is the
usual choice. The inverse Gaussian (V = mu^3) suits more
skewed data. Section 6.3.1 decides between them with deviance residuals,
which look roughly normal if the family is right. A right-skewed
histogram means the data are more skewed than the family assumes.
residuals() on a glm_fit() model returns the
deviance residuals, and glm_diagnostics() adds the other
residuals, leverages and Cook’s distances. The ig column
was drawn from an inverse Gaussian:
skewness <- function(x) {
m <- mean(x)
mean((x - m)^3) / mean((x - m)^2)^1.5
}
families <- do.call(rbind, lapply(c("gamma", "inverse_gaussian"), function(family) {
fit <- glm_fit(ig ~ age + region, d, family = family, link = "log")
data.frame(family = family, AIC = fit@aic, `residual skewness` = skewness(residuals(fit, type = "deviance")),
check.names = FALSE)
}))
gt(families, rowname_col = "family") |>
tab_header("Severity families on the inverse Gaussian column", "Log link, age and region") |>
fmt_number(columns = "AIC", decimals = 1) |>
fmt_number(columns = "residual skewness", decimals = 2, force_sign = TRUE)| Severity families on the inverse Gaussian column | ||
| Log link, age and region | ||
| AIC | residual skewness | |
|---|---|---|
| gamma | 8,240.2 | +0.52 |
| inverse_gaussian | 8,196.7 | +0.05 |
The gamma leaves right-skewed residuals. The inverse Gaussian gives nearly symmetric ones and the lower AIC, the pattern of Figures 16 and 17:
residual_sets <- do.call(rbind, lapply(c("gamma", "inverse_gaussian"), function(family) {
fit <- glm_fit(ig ~ age + region, d, family = family, link = "log")
r <- residuals(fit, type = "deviance")
data.frame(family = sprintf("%s: skewness %+.2f", family, skewness(r)),
residual = r / sqrt(mean(r^2)))
}))
ggplot(residual_sets, aes(residual)) +
geom_histogram(bins = 30) +
facet_wrap(~family) +
labs(x = "deviance residual (sd units)", y = "policies")A severity row that averages several claims carries the claim count
as its weight (section 2.5). A gamma fit weighted by n_sev
recovers the true region factors:
sev <- glm_fit(avg_sev ~ age + region, d, family = "gamma", link = "log", weights = d$n_sev)
cat(sprintf("weighted gamma dispersion %.3f\n", sev@dispersion))## weighted gamma dispersion 0.533
gt(data.frame(region = c("B", "C", "D"), true = unname(true_r[c("B", "C", "D")]),
fitted = unname(exp(sev@coefficients[3:5]))), rowname_col = "region") |>
tab_header("Severity relativities against region A", "Gamma, claim count as weight") |>
fmt_number(columns = c(true, fitted), decimals = 3)| Severity relativities against region A | ||
| Gamma, claim count as weight | ||
| true | fitted | |
|---|---|---|
| B | 1.200 | 1.268 |
| C | 0.800 | 0.853 |
| D | 1.500 | 1.474 |
Pure premium. The Tweedie with power
1 < p < 2 is a Poisson number of gamma losses, with a
point mass at zero. Section 2.7.3 gives its parameters from the Poisson
mean lambda, the gamma shape alpha and the
gamma scale theta:
- mean:
lambda alpha theta(equation 7), - power:
(alpha + 2) / (alpha + 1)(equation 8), - dispersion:
lambda^(1-p) (alpha theta)^(2-p) / (2 - p)(equation 9).
tweedie_from_poisson_gamma() does the conversion. The
pure column was drawn with gamma shape 2 and scale 500, so
equation 8 gives p = 4/3:
lam <- 0.4; alpha <- 2.0; scale <- 500.0
tw <- tweedie_from_poisson_gamma(lam, alpha, scale)
p <- (alpha + 2) / (alpha + 1)
book_table(list(
row("mean", tw@mean_param, lam * alpha * scale, 1),
row("power", tw@power, p, 4),
row("dispersion", tw@dispersion, lam^(1 - p) * (alpha * scale)^(2 - p) / (2 - p), 3)
), "Tweedie parameters from Poisson-gamma parameters", "Lambda 0.4, alpha 2, scale 500", book = "equations 7 to 9")| Tweedie parameters from Poisson-gamma parameters | ||
| Lambda 0.4, alpha 2, scale 500 | ||
| prospicio | equations 7 to 9 | |
|---|---|---|
| mean | 400.0 | 400.0 |
| power | 1.3333 | 1.3333 |
| dispersion | 203.581 | 203.581 |
The monograph picks p by estimating it, by comparing
candidates on log-likelihood or cross-validation, or by a customary 1.6
to 1.7. tweedie_profile() does the first: for each
p it maximizes the exact Tweedie log-likelihood over
phi (the Dunn and Smyth series density, with dispersion
phi / w for exposure weight w).
tweedie_glm_fit() estimates p directly, with a
95% profile-likelihood interval:
prof <- tweedie_profile(pure ~ age + region, d, powers = c(1.2, 1.3, 1.4, 1.5, 1.6, 1.7), weights = d$exposure)$profile
tw_fit <- tweedie_glm_fit(pure ~ age + region, d, weights = d$exposure)
cat(sprintf("estimated p %.3f (95%% interval %.3f to %.3f), phi %.1f\n", tw_fit$power, tw_fit$lower, tw_fit$upper, tw_fit$phi))## estimated p 1.314 (95% interval 1.264 to 1.371), phi 200.1
gt(data.frame(p = prof$power, loglik = prof$log_likelihood, phi = prof$dispersion), rowname_col = "p") |>
tab_header("Tweedie profile likelihood by power",
"Pure premium on age and region, exposure as weight") |>
cols_label(loglik = "log-likelihood", phi = "dispersion phi") |>
fmt_number(columns = c(loglik, phi), decimals = 1)| Tweedie profile likelihood by power | ||
| Pure premium on age and region, exposure as weight | ||
| log-likelihood | dispersion phi | |
|---|---|---|
| 1.2 | −1,430.1 | 329.0 |
| 1.3 | −1,417.0 | 212.8 |
| 1.4 | −1,421.0 | 137.0 |
| 1.5 | −1,433.4 | 89.9 |
| 1.6 | −1,452.3 | 61.1 |
| 1.7 | −1,479.2 | 44.2 |
The profile peaks near the true value of 4/3, inside the interval.
The monograph advises separate frequency and severity models where the
data allow (section 5.1.1), because the Tweedie’s constant
phi assumes that frequency and severity move together. Here
region moves frequency only, and a pure premium model cannot tell this
from a severity effect.
5. Variable significance and comparing models (sections 2.3 and 6.1 to 6.2)
Each coefficient has a standard error and a p-value
(@std_errors, @p_values), normal for a fixed
dispersion and Student’s t for an estimated one. To test a group of
parameters, such as all levels of a factor, the monograph uses the
F-test on nested models (section 6.2.1). The statistic is the drop in
unscaled deviance divided by the number of added parameters times the
dispersion of the larger model. It refers to an F distribution with that
many numerator degrees of freedom and n - p denominator
degrees of freedom.
The monograph’s example adds a five-level territory (4 parameters) to
a 6-parameter model on 972 rows. For two fitted models,
f_test() does the whole test (as
anova(small, big, test = "F") does). The monograph prints
only the deviances, so the code uses the F distribution function, a
regularized incomplete beta,
P(F <= f) = I(d1 f / (d1 f + d2); d1/2, d2/2), from
beta_distribution():
f_cdf <- function(f, d1, d2) cdf(beta_distribution(d1 / 2, d2 / 2), d1 * f / (d1 * f + d2))
f_stat <- (365.8 - 352.1) / (4 * 1.42)
cat(sprintf("F = %.3f (monograph 2.412); percentile on F(4, 962): %.1f%% (monograph 95.2%%)\n",
f_stat, 100 * f_cdf(f_stat, 4, 972 - 10)))## F = 2.412 (monograph 2.412); percentile on F(4, 962): 95.2% (monograph 95.2%)
The same test asks whether region belongs in the claim frequency
model. It uses the overdispersed Poisson, so that f_test()
estimates phi. Section 6.2.2 adds AIC
(-2 loglik + 2p, as @aic) and BIC
(-2 loglik + p ln n, as @bic):
small <- glm_fit(claims ~ age + offset(log(exposure)), d, family = "poisson", dispersion = "pearson")
big <- glm_fit(claims ~ age + region + offset(log(exposure)), d, family = "poisson", dispersion = "pearson")
test <- f_test(small, big)
cat(sprintf("region: F = %.3f on (%.0f, %.0f), percentile %.1f%%\n", test$statistic, test$df, test$df_resid,
100 * (1 - test$p_value)))## region: F = 0.970 on (3, 595), percentile 59.4%
models <- list("age" = claims ~ age + offset(log(exposure)), "age + region" = claims ~ age + region + offset(log(exposure)))
scores <- do.call(rbind, lapply(names(models), function(name) {
fit <- glm_fit(models[[name]], d, family = "poisson")
data.frame(model = name, deviance = fit@deviance, AIC = fit@aic, BIC = fit@bic)
}))
gt(scores, rowname_col = "model") |>
tab_header("Claim frequency models", "Poisson, log exposure as offset") |>
fmt_number(columns = c(deviance, AIC, BIC), decimals = 1)| Claim frequency models | |||
| Poisson, log exposure as offset | |||
| deviance | AIC | BIC | |
|---|---|---|---|
| age | 445.3 | 730.0 | 738.8 |
| age + region | 442.6 | 733.3 | 755.2 |
Region has an effect in the true model, but with 155 claims the test cannot see it (the 59th percentile is far short of 95%). Both criteria prefer the smaller model, BIC more strongly. As the monograph cautions, a high p-value is absence of evidence, not evidence of absence, and business knowledge also decides whether to keep a variable.
6. Where each step lives in prospicio
The monograph’s steps in prospicio, including steps that the guides only mention:
| Monograph step | In prospicio |
|---|---|
| Interactions, polynomials, hinge functions, bins and natural cubic splines as model terms (5.4, 5.6) | the formula (a:b, poly(),
pmax(), cut(), splines::ns()),
and Python’s Terms has the same terms |
| Residuals (deviance, Pearson, working, randomized quantile), hat values, Cook’s distance (6.3, 6.4) |
residuals(), hatvalues(),
rstandard() and cooks.distance() on a
glm_fit() model, and glm_diagnostics()
|
| F-test of nested models, BIC, VIF (2.9, 6.2) |
f_test(), likelihood_ratio_test(),
@bic and vif()
|
Negative binomial k estimated with the model
(2.7.2) |
glm_nb_fit() |
| Tweedie power by profile likelihood (2.7.3) |
tweedie_glm_fit() and
tweedie_profile()
|
| Double lift chart, loss ratio chart, ROC curve and AUROC (7.2, 7.3) |
double_lift_table(), loss_ratio_table(),
roc_curve(), confusion_matrix() and
normalized_gini()
|
A deviance scorer for cross_validate(), and the GAM’s
log-likelihood and AIC |
deviance_score(), and @log_likelihood and
@aic on a gam_fit() model |
| GLMMs, double GLMs (dispersion modelling), MARS (10.1, 10.2, 10.4) |
glmm_fit() (and bayes_glmm_fit()),
dglm_fit(), and mars_knots() with
mars_formula()
|
prospicio also has GAMs with P-spline, cyclic, monotone and tensor
smooths, and elastic nets for every family. It has sandwich standard
errors (robust_vcov()), predict_distribution()
for the full predictive distribution of a fitted GLM, and
save_model() for a fitted rating model.