Double GLMs: a model for the dispersion as well as the mean
dglm_fit.Rddglm_fit() fits a GLM for the mean, g(mu) = X beta + offset, with
variance phi V(mu) / w, and a gamma GLM with the log link for the
dispersion, log(phi) = Z gamma + doffset, each on its own formula, as
dglm::dglm() (Smyth's alternating scoring). The dispersion is
estimated by maximum likelihood by default, as dglm(), or by REML
(method = "reml").
dglm 1.8.6 weights its mean step by w V(mu) / phi and leaves the
offset out of mu.eta, which is right only for a canonical link without
an offset; this fit uses the scoring weights, so it agrees with dglm()
there and with glm() at a constant dispersion everywhere
(validation/scripts/r_dglm.R; docs/design/models.md, "Double GLMs").
Usage
dglm_fit(
formula,
dformula = ~1,
data,
family = "gaussian",
link = NULL,
power = NULL,
method = "ml",
offset = NULL,
weights = NULL,
doffset = NULL,
link_power = NULL
)
dglm_model(ptr, terms, xlevels, dterms, dxlevels)Arguments
- formula
The mean's formula, with the response.
- dformula
The dispersion's one-sided formula,
~ 1by default.- data
A data frame.
- family
"gaussian","poisson","gamma"or"tweedie".- link
The mean's link; by default the log link for the gamma, inverse Gaussian and Tweedie, the canonical link otherwise.
- power
The Tweedie power, in (1, 2).
- method
"ml"(the default) or"reml".- offset, weights
Optional offset of the mean (added to any
offset()term) and prior weights.- doffset
Optional offset of the log dispersion (added to any
offset()term ofdformula).- link_power
The power link's exponent.
Value
A dglm_model with coefficients, std_errors and
covariance (the mean's, summary(fit, dispersion = 1) in dglm),
dispersion_coefficients, dispersion_std_errors and
dispersion_covariance (on the log scale, dglm's
summary(fit$dispersion.fit, dispersion = 2)), fitted,
dispersion_fitted, m2loglik (dglm's, with its constants),
reml_criterion (m2loglik + log det(X' W X)), aic, method and
iterations. predict(m, newdata, type = "dispersion") gives the
dispersion of new rows, type = "response" (the default) their mean.
Examples
d <- data.frame(y = c(9, 11, 10, 10.5, 4, 18, 6, 15), urban = rep(c("no", "yes"), each = 4))
m <- dglm_fit(y ~ urban, ~ urban, d, family = "gamma", link = "log")
m@dispersion_coefficients
#> (Intercept) urbanyes
#> -5.208451 4.136286
predict(m, data.frame(urban = "yes"), type = "dispersion")
#> [1] 0.3422665