spaMM_glm.fit {spaMM} | R Documentation |
Fitting generalized linear models without initial-value or divergence headaches
Description
spaMM_glm.fit
is a stand-in replacement for glm.fit
, which can be called through glm
by using
glm(<>, method="spaMM_glm.fit")
. Input and output structure are exactly as for glm.fit
. It uses a Levenberg-Marquardt algorithm to prevent divergence of estimates. For models families such as Gamma()
(with default inverse link) where the linear predictor is constrained to be positive, if the rcdd package is installed, the function can automatically find valid starting values or else indicate that no parameter value is feasible. It also automatically provides good starting values in some cases where the base functions request them from the user (notably, for gaussian(log)
with some negative response).
spaMM_glm
is a convenient wrapper, calling glm
with default method glm.fit
, then calling method spaMM_glm.fit
, with possibly different initial values, if glm.fit
failed.
Usage
spaMM_glm.fit(x, y, weights = rep(1, nobs), start = NULL, etastart = NULL,
mustart = NULL, offset = rep(0, nobs), family = gaussian(),
control = list(maxit=200), intercept = TRUE, singular.ok = TRUE)
spaMM_glm(formula, family = gaussian, data, weights, subset,
na.action, start = NULL, etastart, mustart, offset,
control = list(...), model = TRUE, method = c("glm.fit","spaMM_glm.fit"),
x = FALSE, y = TRUE, singular.ok = TRUE, contrasts = NULL, strict=FALSE, ...)
Arguments
All arguments except strict
are common to these functions and their stats
package equivalents, glm
and glm.fit
. Most arguments operate as for the latter functions, whose documentation is repeated below. The control
argument may operate differently.
formula |
an object of class |
family |
a description of the error distribution and link
function to be used in the model. For |
data |
an optional data frame, list or environment (or object
coercible by |
weights |
an optional vector of ‘prior weights’ to be used
in the fitting process. Should be |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
na.action |
a function which indicates what should happen
when the data contain |
start |
starting values for the parameters in the linear predictor. |
etastart |
starting values for the linear predictor. |
mustart |
starting values for the vector of means. |
offset |
this can be used to specify an a priori known
component to be included in the linear predictor during fitting.
This should be |
control |
a list of parameters for controlling the fitting
process. This is passed to |
model |
a logical value indicating whether model frame should be included as a component of the returned value. |
method |
A 2-elements vector specifying first the method to be used by |
x , y |
For For |
singular.ok |
logical; if |
contrasts |
an optional list. See the |
intercept |
logical. Should an intercept be included in the null model? |
strict |
logical. Whether to perform a fit by |
... |
arguments to be used to form the default
|
Value
An object inheriting from class glm
. See glm
for details.
Note
The source and documentation is derived in large part from those of glm.fit
.
Examples
x <- c(8.752,20.27,24.71,32.88,27.27,19.09)
y <- c(5254,35.92,84.14,641.8,1.21,47.2)
# glm(.) fails:
(check_error <- try(glm(y~ x,data=data.frame(x,y),family=Gamma(log)), silent=TRUE))
if ( ! inherits(check_error,"try-error")) stop("glm(.) call unexpectedly succeeded")
spaMM_glm(y~ x,data=data.frame(x,y),family=Gamma(log))
## Gamma(inverse) examples
x <- c(43.6,46.5,21.7,18.6,17.3,16.7)
y <- c(2420,708,39.6,16.7,46.7,10.8)
# glm(.) fails (can't find starting value)
(check_error <- suppressWarnings(try(glm(y~ x,data=data.frame(x,y),family=Gamma()) , silent=TRUE)))
if ( ! inherits(check_error,"try-error")) stop("glm(.) call unexpectedly succeeded.")
if (requireNamespace("rcdd",quietly=TRUE)) {
spaMM_glm(y~ x,data=data.frame(x,y),family=Gamma())
}
## A simple exponential regression with some negative response values
set.seed(123)
x <- seq(50)
y <- exp( -0.1 * x) + rnorm(50, sd = 0.1)
glm(y~ x,data=data.frame(x,y),family=gaussian(log), method="spaMM_glm.fit")
# => without the 'method' argument, stats::gaussian(log)$initialize() is called
# and stops on negative response values.