Model metrics
model_metrics.Rdfamily_deviance() is sum(w d(y, mu)) for a family; gini_index() the
Gini index of the ordered Lorenz curve (rows sorted by prediction,
exposure share against loss share); lift_table() cuts rows sorted by
predicted rate into bands of about equal exposure; crps_draws() the
continuous ranked probability score of draws for an outcome;
pinball_loss() the weighted mean pinball (quantile) loss of predicted
alpha quantiles, sum(w * u * (alpha - (u < 0))) / sum(w) with
u = y - pred, lowest in expectation at the true quantile.
Usage
family_deviance(family, y, mu, weights = NULL, theta = NULL, power = NULL)
gini_index(y, pred, exposure = NULL)
lift_table(y, pred, exposure = NULL, bands = 10)
crps_draws(draws, y)
pinball_loss(y, pred, alpha, weights = NULL)
log_score(
family,
y,
mu,
dispersion = 1,
weights = NULL,
theta = NULL,
power = NULL
)
pit_values(
family,
y,
mu,
dispersion = 1,
weights = NULL,
seed = 0,
theta = NULL,
power = NULL
)
quantile_residuals(
family,
y,
mu,
dispersion = 1,
weights = NULL,
seed = 0,
theta = NULL,
power = NULL
)
ks_uniform(values)
pit_from_draws(draws, y, u = 0.5)
pit_histogram(pit, bins = 10)
deviance_score(
family,
response,
theta = NULL,
power = NULL,
weight_column = NULL
)Arguments
- family
A family name, as in
glm_fit().- y
Outcomes.
- mu, pred
Predictions.
- weights, exposure
Optional weights or exposures.
- theta, power
Family parameters, as in
glm_fit().- bands
Number of lift bands.
- draws
Equally likely draws.
- alpha
Quantile level in
(0, 1).- dispersion
The family's dispersion.
- seed
Seed of the PIT's randomization.
- values
Values to compare with the uniform.
- u
The share of tied draws counted below
y.- pit
PIT values in
[0, 1].- bins
Number of histogram bins.
- response
The name of the response column.
- weight_column
The test data's weight column, or
NULL.
Value
A number; for pit_values(), quantile_residuals() and pit_from_draws() one value
per outcome; for pit_histogram() the count in each bin; for
deviance_score() a function score(model, test); for
lift_table() a data frame with columns exposure, expected and
actual.
Details
log_score() is the mean of -log f(y) under each row's predictive
distribution (the family with mean mu, dispersion and weight);
pit_values() the probability integral transform F(y), randomized
where the distribution has atoms (counts, a Tweedie's zero) and uniform
when the model is calibrated; quantile_residuals() its normal scores
qnorm(pit_values(...)), Dunn and Smyth's randomized quantile residuals,
standard normal under the model (as statmod::qresiduals; a fitted
glm_model's are residuals(m, type = "quantile", seed = 0));
ks_uniform() the Kolmogorov-Smirnov
distance of values from the uniform, about 1.36 / sqrt(n) or less 95%
of the time under uniformity. pit_from_draws() is the PIT of each y
under the empirical distribution of draws, ties randomized by u, and
pit_histogram() counts PIT values in bins equal-width bins of
[0, 1]. deviance_score() is a score for cross_validate(): the
family's mean deviance of the test data's response column,
sum(w d) / sum(w), with the weights from its weight_column (all 1
by default).
Examples
family_deviance("poisson", c(1, 0, 3), c(1, 0.5, 2))
#> [1] 1.432791
gini_index(c(0, 1), c(0.1, 0.9))
#> [1] 0.5
lift_table(c(0, 1, 2, 3), c(0.1, 0.9, 2.1, 2.9), bands = 2)
#> exposure expected actual
#> 1 2 1 1
#> 2 2 5 5
crps_draws(c(1, 2, 3), 2)
#> [1] 0.2222222
pinball_loss(c(1, 0), c(0, 1), 0.9)
#> [1] 0.5
log_score("poisson", 0, 1)
#> [1] 1
set.seed(1)
y <- rpois(500, 3)
ks_uniform(pit_values("poisson", y, rep(3, 500)))
#> [1] 0.0319566
pit_from_draws(c(1, 2, 2, 3), c(2, 5))
#> [1] 0.5 1.0
pit_histogram(c(0.05, 0.15, 0.95), bins = 10)
#> [1] 1 1 0 0 0 0 0 0 0 1