Skip to contents

A glm_model fitted in this session keeps its training data, so stats::residuals() (type "deviance", "pearson", "response", "working" or "quantile"), stats::hatvalues(), stats::rstandard() (type "deviance" or "pearson") and stats::cooks.distance() work on it as on R's glm. glm_diagnostics() returns them all, with the working weights, as a data frame. prediction_se() is the standard error of each row's predicted mean (parameter error only, as predict(type = "response", se.fit = TRUE)); sum_prediction_error() the prediction error of the sum of the rows' means (a reserve: an origin's future cells), the parameter error by the delta method and the process error from the family (Taylor and McGuire, CAS Monograph 3, (5-11) to (5-13)). glm_gcv() is generalized cross-validation, n sum((y - mu)^2) / (n - p)^2, and log_likelihood_at() the training log-likelihood at a given dispersion. A model read with load_model() has no training data: only prediction_se() and sum_prediction_error() work on it.

Usage

glm_diagnostics(model)

prediction_se(model, newdata, offset = NULL)

sum_prediction_error(model, newdata, offset = NULL, weights = NULL)

glm_gcv(model)

log_likelihood_at(model, dispersion)

Arguments

model

A glm_model.

newdata

A data frame with the model's terms.

offset

Optional offset added to any offset() terms.

weights

Optional prior weights (for the process variance).

dispersion

The dispersion at which to evaluate.

Value

glm_diagnostics() a data frame with one row per training row; prediction_se() one value per row of newdata; sum_prediction_error() a list with total, parameter_variance, process_variance and rmsep; glm_gcv() and log_likelihood_at() a number.

Examples

d <- data.frame(claims = c(1, 2, 4, 2, 1, 3, 0, 5), region = c("N", "N", "S", "S", "W", "W", "N", "S"))
m <- glm_fit(claims ~ region, d, family = "poisson")
head(glm_diagnostics(m))
#>     fitted   response    pearson   deviance     working working_weights
#> 1 1.000000  0.0000000  0.0000000  0.0000000  0.00000000        1.000000
#> 2 1.000000  1.0000000  1.0000000  0.8789703  1.00000000        1.000000
#> 3 3.666667  0.3333333  0.1740777  0.1715353  0.09090909        3.666667
#> 4 3.666667 -1.6666667 -0.8703883 -0.9533048 -0.45454545        3.666667
#> 5 2.000000 -1.0000000 -0.7071068 -0.7833937 -0.50000000        2.000000
#> 6 2.000000  1.0000000  0.7071068  0.6578683  0.50000000        2.000000
#>    leverage standardized_pearson standardized_deviance cooks_distance
#> 1 0.3333333            0.0000000             0.0000000    0.000000000
#> 2 0.3333333            1.2247449             1.0765143    0.250000000
#> 3 0.3333333            0.2132007             0.2100869    0.007575758
#> 4 0.3333333           -1.0660036            -1.1675552    0.189393939
#> 5 0.5000000           -1.0000000            -1.1078859    0.333333333
#> 6 0.5000000            1.0000000             0.9303662    0.333333333
sum_prediction_error(m, data.frame(region = c("N", "S")))$rmsep
#> [1] 2.494438