Description Usage Arguments Details Value Author(s) References See Also Examples
Fits generalized linear models using the same model specification as glm in the stats package, but with a modified default fitting method. The method provides greater stability for models that may fail to converge using glm.
1 2 3 4 |
formula |
as for |
family |
as for |
data |
as for |
weights |
as for |
subset |
as for |
na.action |
as for |
start |
as for |
etastart |
as for |
mustart |
as for |
offset |
as for |
control |
as for |
model |
as for |
method |
the method used in fitting the model. The default method |
x |
as for |
y |
as for |
singular.ok |
as for |
contrasts |
as for |
... |
as for |
glm2 is a modified version of glm in the stats package. It fits generalized linear models using the same model specification as glm. It is identical to glm except for minor modifications to change the default fitting method. The default method uses a stricter form of step-halving to force the deviance to decrease at each iteration and is implemented in glm.fit2. Like glm, user-supplied fitting functions can be used with glm2 by passing a function or a character string naming a function to the method argument. See Marschner (2011) for a discussion of the need for a modified fitting method.
The value returned by glm2 has exactly the same documentation as the value returned by glm, except for:
method |
the name of the fitter function used, which by default is |
glm2 uses the code from glm, whose authors are listed in the help documentation for the stats package. Modifications to this code were made by Ian Marschner.
Marschner, I.C. (2011) glm2: Fitting generalized linear models with convergence problems. The R Journal, Vol. 3/2, pp.12-15.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | library(glm2)
data(crabs)
data(heart)
#==========================================================
# EXAMPLE 1: logistic regression null model
# (behaviour of glm and glm2 for different starting values)
#==========================================================
y <- c(1,1,1,0)
# intercept estimate = log(0.75/0.25) = 1.098612
#--- identical behaviour ---#
fit1 <- glm(y ~ 1, family=binomial(link="logit"),
control=glm.control(trace=TRUE))
fit2 <- glm2(y ~ 1, family=binomial(link="logit"),
control=glm.control(trace=TRUE))
print.noquote(c(fit1$coef,fit2$coef))
#--- convergence via different paths ---#
fit1 <- glm(y ~ 1, family=binomial(link="logit"),start=-1.75,
control=glm.control(trace=TRUE))
fit2 <- glm2(y ~ 1, family=binomial(link="logit"),start=-1.75,
control=glm.control(trace=TRUE))
print.noquote(c(fit1$coef,fit2$coef))
#--- divergence of glm to infinite estimate ---#
fit1 <- glm(y ~ 1, family=binomial(link="logit"),start=-1.81)
fit2 <- glm2(y ~ 1, family=binomial(link="logit"),start=-1.81)
print.noquote(c(fit1$coef,fit2$coef))
#=======================================================================
# EXAMPLE 2: identity link Poisson (successful boundary convergence
# using 4 identical approaches in glm and glm2 with the method argument)
#=======================================================================
satellites <- crabs$Satellites
width.shifted <- crabs$Width - min(crabs$Width)
dark <- crabs$Dark
goodspine <- crabs$GoodSpine
fit1 <- glm(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4))
fit2 <- glm2(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4))
fit1.eq <- glm2(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4), method = "glm.fit")
fit2.eq <- glm(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4), method = "glm.fit2")
noquote(c("deviances: ",fit1$dev,fit2$dev,fit1.eq$dev,fit2.eq$dev))
noquote(c("converged: ",fit1$conv,fit2$conv,fit1.eq$conv,fit2.eq$conv))
noquote(c("boundary: ",fit1$bound,fit2$bound,fit1.eq$bound,fit2.eq$bound))
#===================================================================
# EXAMPLE 3: identity link Poisson (periodic non-convergence in glm)
#===================================================================
R1 <- crabs$Rep1
satellites <- crabs$Satellites[R1]
width.shifted <- crabs$Width[R1] - min(crabs$Width)
dark <- crabs$Dark[R1]
goodspine <- crabs$GoodSpine[R1]
fit1 <- glm(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4),
control = glm.control(trace=TRUE))
fit2 <- glm2(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4),
control = glm.control(trace=TRUE))
noquote(c("deviances: ",fit1$dev,fit2$dev))
noquote(c("converged: ",fit1$conv,fit2$conv))
#===============================================================
# EXAMPLE 4: log link binomial (periodic non-convergence in glm)
#===============================================================
patients <- heart$Patients
deaths <- heart$Deaths
agegroup <- heart$AgeGroup
severity <-heart$Severity
delay <- heart$Delay
region <- heart$Region
start.p <- sum(deaths)/sum(patients)
fit1 <- glm(cbind(deaths,patients-deaths) ~ factor(agegroup) + factor(severity)
+ factor(delay) + factor(region), family = binomial(link="log"),
start = c(log(start.p), rep(0,8)), control = glm.control(trace=TRUE,maxit=100))
fit2 <- glm2(cbind(deaths,patients-deaths) ~ factor(agegroup) + factor(severity)
+ factor(delay) + factor(region), family = binomial(link="log"),
start = c(log(start.p), rep(0,8)), control = glm.control(trace=TRUE))
noquote(c("deviances: ",fit1$dev,fit2$dev))
noquote(c("converged: ",fit1$conv,fit2$conv))
#====================================================================
# EXAMPLE 5: identity link Poisson (aperiodic non-convergence in glm)
#====================================================================
R2 <- crabs$Rep2
satellites <- crabs$Satellites[R2]
width.shifted <- crabs$Width[R2] - min(crabs$Width)
dark <- crabs$Dark[R2]
goodspine <- crabs$GoodSpine[R2]
fit1 <- glm(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4),
control = glm.control(trace=TRUE))
fit2 <- glm2(satellites ~ width.shifted + factor(dark) + factor(goodspine),
family = poisson(link="identity"), start = rep(1,4),
control = glm.control(trace=TRUE))
noquote(c("deviances: ",fit1$dev,fit2$dev))
noquote(c("converged: ",fit1$conv,fit2$conv))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.