Fit a generalized linear model with secure convergence.
glmnb.fit(X, y, dispersion, weights = NULL, offset = 0, coef.start = NULL,
start.method = "mean", tol = 1e-6, maxit = 50L, trace = FALSE)
design matrix, assumed to be of full column rank. Missing values not allowed.
numeric vector of responses. Negative or missing values not allowed.
numeric vector of dispersion parameters for the negative binomial distribution. If of length 1, then the same dispersion is assumed for all observations.
numeric vector of positive weights, defaults to all one.
offset vector for linear model
numeric vector of starting values for the regression coefficients
method used to find starting values, possible values are "mean"
or "log(y)"
small positive numeric value giving convergence tolerance
maximum number of iterations allowed
logical value. If TRUE
then output diagnostic information at each iteration.
List with the following components:
numeric vector of regression coefficients
numeric vector of fitted values
residual deviance
number of iterations used to convergence. If convergence was not achieved then iter
is set to maxit+1
.
This function implements a modified Fisher scoring algorithm for generalized linear models, analogous to the Levenberg-Marquardt algorithm for nonlinear least squares. The Levenberg-Marquardt modification checks for a reduction in the deviance at each step, and avoids the possibility of divergence. The result is a very secure algorithm that converges for almost all datasets.
glmnb.fit
is in principle equivalent to glm.fit(X,y,family=negative.binomial(link="log",theta=1/dispersion))
but with more secure convergence.
Here negative.binomial
is a function in the MASS package.
The dispersion
parameter is the same as 1/theta
for the MASS::negative.binomial
function or 1/size
for the stats::rnbinom
function.
dispersion=0
corresponds to the Poisson distribution.
Dunn, PK, and Smyth, GK (2018). Generalized linear models with examples in R. Springer, New York, NY. https://doi.org/10.1007/978-1-4419-0118-7
The glmFit
function in the edgeR package on Bioconductor is a high-performance version of glmnb.fit
for many y
vectors at once.
glm
is the standard glm fitting function in the stats package.
negative.binomial
in the MASS package defines a negative binomial family for use with glm
.
# NOT RUN {
y <- rnbinom(10, mu=1:10, size=5)
X <- cbind(1, 1:10)
fit <- glmnb.fit(X, y, dispersion=0.2, trace=TRUE)
# }
Run the code above in your browser using DataLab