Skip to contents

Fits by IRLS on the Rust core. The design matrix is R's own model.matrix(), so formulas, factors (treatment coding) and offset() terms work as in stats::glm(). Results match statsmodels' GLM (validation/scripts/statsmodels_glm.py).

Usage

glm_fit(
  formula,
  data,
  family = "poisson",
  link = NULL,
  offset = NULL,
  weights = NULL,
  dispersion = NULL,
  theta = NULL,
  power = NULL,
  link_power = NULL
)

Arguments

formula

A model formula; offset(...) terms are honoured.

data

A data frame.

family

"gaussian", "poisson", "gamma", "inverse_gaussian", "binomial" (response a proportion, weights the trials), "negative_binomial" (needs theta) or "tweedie" (needs power).

"identity", "log", "logit", "probit", "cloglog", "inverse", "inverse_squared" or "power" (needs link_power); NULL for the family's canonical link.

offset

Optional offset added to any offset() terms.

weights

Optional prior weights.

dispersion

NULL (1 for the Poisson, binomial and negative binomial; Pearson's estimate otherwise), "pearson" (with the Poisson, the over-dispersed Poisson, which accepts negative responses as long as the fitted means stay positive), "deviance" or a fixed number.

theta

Negative binomial theta (variance mu + mu^2 / theta).

power

Tweedie power in (1, 2).

Exponent of the power link.

Value

A glm_model with properties coefficients, std_errors, p_values, covariance, dispersion, deviance, null_deviance, log_likelihood, aic, df_resid, iterations and fitted. Use stats::coef(), stats::predict() and predict_distribution().

Examples

d <- data.frame(claims = c(1, 2, 4, 2, 1, 3), region = c("N", "N", "S", "S", "W", "W"),
                exposure = c(1, 2, 1, 1, 0.5, 1.5))
m <- glm_fit(claims ~ region + offset(log(exposure)), d, family = "poisson")
coef(m)
#>  (Intercept)      regionS      regionW 
#> 1.231682e-16 1.098612e+00 6.931472e-01 
predict(m, data.frame(region = "W", exposure = 2))
#> [1] 4