R and S-Plus Categorical Data Analysis
R and S-Plus Categorical Data Analysis
1
Chapter 1: Introduction
Univariate binomial and multinomial inference
The function dbinom can generate binomial probabilities, for example, dbinom(6, 10,
0.5) gives the probability of 6 successes in 10 trials with “probability of success”
parameter π = 0.50. The function pbinom(6, 10, 0.5) would give the corresponding
cumulative probability of 6 or fewer successes.
The function [Link] gives the Pearson (score) test and score confidence inter-
val for a binomial proportion, for example, [Link](6, 10, p=.5, correct=FALSE),
where “correct=FALSE” turns off the continuity correction, which is the default. The
function [Link] gives a small-sample binomial test, for example [Link](8, 12,
p=0.5, alternative = c(”[Link]”)) gives a two-sided test of H0 : π = 0.50 with 8
successes in 12 trials.
The table function constructs contingency tables.
The function [Link] can perform the Pearson chi-squared test of goodness-of-fit
of a set of multinomial probabilities. For example, with 3 categories and hypothesized
values (0.4, 0.3, 0.3) and observed counts (12, 8, 10),
data: x
X-squared = 0.2222, df = 2, p-value = 0.8948
data: x
X-squared = 0.2222, df = NA, p-value = 0.8763
2
[Link]/web/packages/PropCIs/[Link]
The confidence interval based on the test using mid P -value is available with the
midPci function in the PropCIs package.
------------------------------------------------------------------------
> library(PropCIs)
Binomial tests and confidence intervals using the mid P -value are available with
the exactci package. Other available inferences with that package include the Blaker
exact confidence interval.
------------------------------------------------------------------------
> library(exactci)
Bayesian inference
----------------------------------------------------------------------
> qbeta(0.025, 1, 26); qbeta(0.975, 1, 26)
[1] 0.0009732879
[1] 0.1322746
3
See [Link]/ for utilities such as a quantile function for the
logit-normal distribution.
The hpd function in the TeachingDemos library can construct HPD intervals from
a posterior distribution. The package hdrcde is a more sophisticated package for such
methods. For the informative analysis of the vegetarians example at the end of Section
1.6.4:
library("TeachingDemos")
y <- 0; n <- 25
a1 <- 3.6; a2 <- 41.4
a <- a1 + y; b <- a2 + n
h <- hpd(qbeta, shape1=a, shape2=b)
The function [Link] also can perform the Pearson chi-squared test of independence
in a two-way contingency table. For example, for Table 3.2 of the text, using also the
stdres component for providing standardized residuals,
data: data
X-squared = 76.1483, df = 10, p-value = 2.843e-12
> [Link](data)$stdres
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.368577 -2.227511 -1.418621 -1.481383 -1.3349600 3.590075
4
[2,] -2.504627 -2.635335 -3.346628 1.832792 0.0169276 3.382637
[3,] 3.051857 4.724326 4.839597 -0.792912 1.0794638 -6.665195
Likewise, replacing the stdres component by expected would generate the expected
frequency estimates. As shown above, you can simulate the exact conditional distri-
bution to estimate the P -value whenever the chi-squared asymptotic approximation is
suspect.
Here is an example of using Mantel’s ordinal test for a two-way table with the M 2
statistic of Section 3.4 applied to the infant birth defects example:
---------------------------------------------------------------------------
> Malform <- matrix(c(17066, 14464, 788, 126, 37, 48, 38, 5, 1, 1), ncol=2)
> Malform
[,1] [,2]
[1,] 17066 48
[2,] 14464 38
[3,] 788 5
[4,] 126 1
[5,] 37 1
> library(vcdExtra)
> CMHtest(Malform, rscores=c(0, 0.5, 1.5, 4.0, 7.0))
Cochran-Mantel-Haenszel Statistics # test was proposed by Nathan Mantel
AltHypothesis Chisq Df Prob
cor Nonzero correlation 6.5699 1 0.010372
---------------------------------------------------------------------------
The function [Link] performs Fisher’s exact test. For example, for the tea
tasting data of Table 3.9 in the text,
data: tea
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.2117329 621.9337505
sample estimates:
odds ratio
6.408309
> [Link](tea,alternative="greater")
data: tea
p-value = 0.2429
alternative hypothesis: true odds ratio is greater than 1
5
The P -value is the sum of probabilities of tables with the given margins that have prob-
ability no greater than the observed table. The output also shows the conditional ML
estimate of the odds ratio (see Sec. 16.6.4) and a corresponding exact confidence inter-
val based on noncentral hypergeometric probabilities. Use [Link](tea,alternative=“greater”)
for the one-sided test.
For an I × J table called “table,” using
generates Monte Carlo simulation with B replicates to estimate the exact P -value
based on the exact conditional multiple hypergeometric distribution obtained by con-
ditioning on all row and column marginal totals (proposed by Agresti, Wackerly, and
Boyett, 1979).
For mid-P values, you can use the exact2x2 package:
---------------------------------------------------------------------------
> library(exact2x2)
> [Link](tea,midp=TRUE,[Link]=FALSE,alternative="greater")
p-value = 0.1286
alternative hypothesis: true odds ratio is greater than 1
> [Link](tea,midp=TRUE,[Link]=0.95)
p-value = 0.2571
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.3100451 311.4363036
---------------------------------------------------------------------------
This also shows a confidence interval based on inverting the exact conditional nonnull
hypergeometric distribution, but using the mid P -value.
For a 2×2 table, the function [Link] provides the Wald confidence interval for the
difference of proportions, where one uses the option correct=FALSE to suppress the
continuity correction.
For parameters comparing two binomial proportions such as the difference of pro-
portions, relative risk, and odds ratio, a good general-purpose method for constructing
confidence intervals is to invert the score test. Ralph Scherer at the Institute for Biom-
etry in Hannover, Germany, has prepared a package PropCIs on CRAN incorporating
many of these confidence interval functions for proportions and comparisons of propor-
tions. For example, Here, we illustrate for the example on aspirin and heart attacks
------------------------------------------------------------------------------
> library(PropCIs)
> riskscoreci(189, 11034, 104, 11037, 0.95) # score CI for relative risk
6
95 percent confidence interval:
1.433904 2.304713
> orscoreci(189, 11034, 104, 11037, 0.95) # score CI for odds ratio
95 percent confidence interval:
1.440802 2.329551
------------------------------------------------------------------------------
7
p1 = max(p1, 0)
p1 = min(p1,1)
se0 <- sqrt(p1*(1 - p1)/n[1] + p2*(1 - p2)/n[2])
if (!MN) z=(y[1]/n[1]-y[2]/n[2]-[Link])/se0 #Mee
else z=(y[1]/n[1]-y[2]/n[2]-[Link])/(se0*N/(N-1)) #Mietinnen and
Nurminen
if (se0 == 0){
z = 0
}
pvalue <- switch(alternative,
"[Link]" = 1 - pchisq(z^2, df=1),
"less" = pnorm(z),
"greater" = pnorm(z, [Link] = FALSE)
)
return(list([Link] = z, [Link] = pvalue, [Link] = p1, [Link] =
p2))
}
#returns lower and upper score bounds for diff of prop p1 - p2:
[Link] <- function(y,n,conflev=0.95, type="[Link]", MN=TRUE) {
if (type=="[Link]") c=qnorm(1-(1-conflev)/2)^2 else
c=qnorm(conflev)^2
delta1=(y[1]+1)/(n[1]+2) - (y[2]+1)/(n[2]+2) #starting point
if (any(type=="lower",type=="[Link]")) {
delta2=-1
while( abs(delta1-delta2)>10^(-6) ) {#Bisection for LB
delta=(delta1+delta2)/2
z=[Link](delta,y,n, MN = MN)$[Link]^2
if (z>c) delta2=delta else delta1=delta
}
}
[Link]=delta1
delta1=(y[1]+1)/(n[1]+2) - (y[2]+1)/(n[2]+2) #starting point
if (any(type=="upper",type=="[Link]")) {
delta2=1;
while( abs(delta1-delta2)>10^(-6) ) {#Bisection for UB
delta=(delta1+delta2)/2
z=[Link](delta,y,n, MN = MN)$[Link]^2
if (z>c) delta2=delta else delta1=delta
}
}
[Link]=delta1
return(switch(type,
"lower"=[Link],
"upper"=[Link],
"[Link]"=cbind([Link],[Link])
))
}
### Example:
8
> y <- c(10,5)
> n <- c(20,20)
> [Link](y,n)
[Link] [Link]
[1,] -0.05793719 0.5161737
> [Link](y,n, MN=FALSE)
[Link] [Link]
[1,] -0.05026568 0.5104669
> [Link]([Link]=0,y,n)
$[Link]
[1] 1.592168
$[Link]
[1] 0.1113469
$[Link]
[1] 0.375
$[Link]
[1] 0.375
> [Link]([Link]=0,y,n, MN=FALSE)
$[Link]
[1] 1.632993
$[Link]
[1] 0.1024704
$[Link]
[1] 0.375
$[Link]
[1] 0.375
Here is code to obtain the profile likelihood confidence interval for the odds ratio
for Table 3.1 on seat-belt use and traffic accidents (using the fact that the log odds
ratio is the parameter in a simple logistic model):
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -7.6361 0.2000 -38.17 <2e-16 ***
x 2.3827 0.2421 9.84 <2e-16 ***
-
> confint(fit)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) -8.055554 -7.268025
x 1.919634 2.873473
> exp(1.919634); exp(2.873473)
[1] 6.818462
[1] 17.69838
9
Fay (2010a) described an R package exact2x2 that constructs a small-sample confi-
dence interval for the odds ratio by inverting the test using the P -value (mentioned
in Section 16.6.1) that was suggested by Blaker (2000), which equals the minimum
one-tail probability plus an attainable probability in the other tail that is as close as
possible to, but not greater than, that one-tailed probability. See
[Link]/archive/2010-1/RJournal_2010-1_Fay.pdf
and
[Link]/web/packages/exact2x2/[Link]
For example, for a 2×2 table called data, the command exact2x2(data, tsmethod =
“blaker”) provides an exact test using Blaker’s P -value and the confidence interval
based on inverting that test.
You can construct a small-sample confidence interval for the odds ratio based on
the exact conditional test with mid P -value using the epitools package,
[Link]
For the data in the tea-tasting example, we have:
--------------------------------------------------------------------------
> [Link]("epitools")
> library(epitools)
> [Link](3, 1, 1, 3, or=1)
[Link] [Link]
1 0.1285714 0.2571429
> [Link](c(3,1,1,3), [Link]=0.95)
$[Link]
[1] 0.3100508 306.6338538
--------------------------------------------------------------------------
This is also available in the exactc2x2 package, although results differ slightly:
---------------------------------------------------------------------------
> library(exact2x2)
> [Link](tea,midp=TRUE,[Link]=0.95)
p-value = 0.2571
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.3100451 311.4363036
---------------------------------------------------------------------------
In a 2002 paper in the journal Biostatistics, Agresti and Min showed that for
independent binomial samples, one can obtain shorter exact confidence intervals for
the odds ratio using unconditional methods (inverting a score test) instead of the
conditional method. Their exact unconditional confidence interval for an odds ratio
is also available in the exact2x2 package, using the uncondExact2x2 function. For the
example in the Agresti and Min paper with sample sizes of 26 and success counts of 1
and 2:
--------------------------------------------------------------------------
> library(exact2x2)
> uncondExact2x2(1,26,2,26,parmtype="oddsratio",[Link]=0.95,
10
+ [Link]=TRUE,method="score",tsmethod="square")
Euijung Ryu, a former PhD student of mine who is now at Mayo Clinic, has
prepared R functions for various confidence intervals for the ordinal measure [P (Y 1 >
Y 2) + (1/2)P (Y 1 = Y 2)] that is useful for comparing two multinomial distributions
on an ordinal scale. See
[Link]/~aa/cda/R/stochastic/[Link]
for the functions, including the Wald confidence interval as well as score, pseudo-
score, and profile likelihood intervals that are computationally more complex and
require using Joe Lang’s [Link] function (see below). Also, Euijung has prepared an
R function for multiple comparisons of proportions with independent samples using
simultaneous confidence intervals for the difference of proportions or the odds ratio,
based on the Studentized-range inversion of score tests proposed by Agresti et al.
(2008). See
[Link]/~aa/cda/R/multcomp/[Link]
Joseph Lang’s [Link] function just mentioned is a general purpose and very pow-
erful function that can provide ML fitting of generalized loglinear models (Section
10.5.1) and other much more general “multinomial-Poisson homogeneous” models such
as covered in Lang (2004, 2005). These include models that can be specified in terms
of constraints of the form h(µ) = 0, such as the marginal homogeneity model and the
calf infection example in Section 1.5.6 of the text. For details, see
[Link]/~jblang/[Link]/[Link]
Joe has also prepared an R program, [Link], for computing (among other things)
score and likelihood-ratio-test-based (i.e., profile likelihood) intervals for contingency
table parameters. See
[Link]/~jblang/[Link]/[Link]
The vcd package can construct the ordinal measure of association gamma and its
standard error, as well as many other things for contingency tables, such as odds ratios,
mosaics plots, and CMH tests for stratified tables. See
[Link]
pdf
11
[Link]/bcwr
The latter is a website for the text Bayesian Computation with R by Albert. It shows
examples of some categorical data analyses, such as Bayesian inference for a 2×2 table,
a Bayesian test of independence in a contingency table, and probit regression.
Yongyi Min has prepared some R functions for Bayesian confidence intervals for
2×2 tables using independent beta priors for two binomial parameters, for the differ-
ence of proportions, odds ratio, and relative risk. See
[Link]/~aa/cda/R/bayes/[Link]
These are evaluated and compared to score confidence intervals in Agresti and Min
(2005). Here is an example for the difference of proportions and the odds ratio using
Jeffreys priors for a table with counts (3, 1) in row 1 and (1, 3) in row 2.
------------------------------------------------------------------------------
> diffCI(3, 4, 1, 4, 0.5, 0.5, 0.5, 0.5, 0.95)
[1] -0.1093044 0.8812513
Missing data
The ACD package can conduct some analyses of categorical data (e.g. loglinear models
by ML, functions of count data using weighted least squares) when data are missing.
See
[Link]/web/packages/ACD/[Link]
Call:
glm(formula = snoring ~ scores, family = binomial(link = logit))
12
Deviance Residuals:
1 2 3 4
-0.8346 1.2521 0.2758 -0.6845
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.86625 0.16621 -23.261 < 2e-16 ***
scores 0.39734 0.05001 7.945 1.94e-15 ***
---
Signif. codes: 0 1
For the identity link with data in the form of Bernoulli observations, use code such
as
The fitting procedure will not converge if at some stage of the fitting process, proba-
bility estimates fall outside the permissible (0, 1) range.
The profile likelihood confidence interval is available with the confint function in
R, which is applied to the model fit object. It is also available with the [Link]
function in the ProfileLikelihood library prepared by Leena Choi. See
[Link]/web/packages/ProfileLikelihood/[Link].
The glm function can be used to fit Poisson loglinear models and counts and for
rates. For negative binomial models, you can use the [Link] function in the MASS
library.
[Link]/R-manual/R-patched/library/MASS/html/[Link]
However, in the notation of Sec. 4.3.4, this function identifies the dispersion parameter
(which it calls “theta”) as k, not its reciprocal γ. Negative binomial regression can
13
also be handled by Thomas Yee’s VGAM package mentioned for Chapter 8 below and
by the negbin function in the aod package:
[Link]/web/packages/aod/[Link]
To illustrate R for models for counts, for the data in Sec. 4.3 on numbers of satellites
for a sample of horseshoe crabs (Note: The complete data set is in the Datasets link
[Link]/~aa/cda/[Link] at this website),
> library(MASS)
> [Link] <- [Link](satell ~ weight, link=log)
> summary([Link])
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8647 0.4048 -2.136 0.0327 *
weight2 0.7603 0.1578 4.817 1.45e-06 ***
---
Null deviance: 216.43 on 172 degrees of freedom
Residual deviance: 196.16 on 171 degrees of freedom
AIC: 754.64
Theta: 0.931
Std. Err.: 0.168
2 x log-likelihood: -748.644
The function [Link] has a type argument that can be used to request
standardized residuals. That is, you can type
> fit <- glm(... model formula, family, data, etc ...)
> rstandard(fit, type="pearson")
to get standardized Pearson residuals for a fitted GLM. Without the type argument,
rstandard(fit) returns the standardized deviance residuals.
The statmod library at CRAN contains a function [Link] that computes
score test statistics for adding explanatory variables to a GLM.
14
Statistical Models in S by J. M. Chambers and T. J. Hastie (Wadsworth, Belmont,
California, 1993, p. 227) showed the use of S-Plus in quasi-likelihood analyses using
the quasi and [Link] functions.
Following is an example of the analyses shown for the teratology data, including
the quasi-likelihood approach:
57 57 4 6 0
58 58 4 17 0
> rats$group <- [Link](rats$group)
> [Link] <- glm(y/n ~ group - 1, weights = n, data=rats, family=binomial)
> summary([Link])
15
variance="mu(1-mu)"),start=unique(predict([Link],type="response")))
> summary([Link]) # This shows another way to get the QL results
Coefficients:
Estimate Std. Error t value Pr(>|t|)
group1 0.75841 0.04007 18.929 <2e-16 ***
group2 0.10169 0.04710 2.159 0.0353 *
group3 0.03448 0.04055 0.850 0.3989
group4 0.04808 0.03551 1.354 0.1814
---
(Dispersion parameter for quasi family taken to be 2.864945)
Since logistic regression is a generalized linear model, it can be fitted with the glm
function, as mentioned above.
If y is a binary variable (i.e., ungrouped binomial data with each n = 1), the vector
of y values (0 and 1) can be entered as the response variable. Following is an example
with the horseshoe crab data as a data frame, declaring color to be a factor in order
to set up indicator variables for it (which, by default, choose the first category as the
baseline without its own indicator variable). (Note that the complete data set is in
the Datasets link [Link]/~aa/cda/[Link] at this website.)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.6947 0.8802 -4.198 2.70e-05 ***
weight 1.8151 0.3767 4.819 1.45e-06 ***
---
16
Residual Deviance: 195.7371 on 171 degrees of freedom
AIC: 199.74
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.2572 1.1985 -2.718 0.00657 **
weight 1.6928 0.3888 4.354 1.34e-05 ***
color2 0.1448 0.7365 0.197 0.84410
color3 -0.1861 0.7750 -0.240 0.81019
color4 -1.2694 0.8488 -1.495 0.13479
---
For grouped data, rather than defining the response as the set of success and failure
counts as was done in the Chapter 4 discussion above for the snoring data, one can
instead enter the response in the form y/n for y successes in n trials, entering the
number of trials as the weight. For example, again for the snoring data of Table 4.2,
Coefficients:
(Intercept) scores
-3.8662 0.3973
17
> fit <- glm(response ~ race + azt, family=binomial(link=logit))
> summary(fit)
Call:
glm(formula = response ~ race + azt, family = binomial(link = logit))
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.07357 0.26294 -4.083 4.45e-05 ***
race 0.05548 0.28861 0.192 0.84755
azt -0.71946 0.27898 -2.579 0.00991 **
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Hosmer-Lemeshow: One place I’ve seen a function for the Hosmer-Lemeshow test is
[Link]
html
The function rstandard can be used to request standardized residuals, after a
binary glm fit. For example, for the Berkeley admissions data shown on p. 63, the
model assuming no gender effect fits well except for the first department:
-------------------------------------------------------------------
> data <- [Link]("[Link]",header=TRUE)
> data
dept gender yes no
1 A male 512 313
2 A female 89 19
3 B male 353 207
4 B female 17 8
5 C male 120 205
6 C female 202 391
7 D male 138 279
8 D female 131 244
9 E male 53 138
10 E female 94 299
11 F male 22 351
12 F female 24 317
> attach(data)
> n <- yes + no
> fit <- glm(yes/n ~ factor(dept), weights=n, family=binomial)
> summary(fit)
Coefficients:
18
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.59346 0.06838 8.679 <2e-16 ***
factor(dept)B -0.05059 0.10968 -0.461 0.645
factor(dept)C -1.20915 0.09726 -12.432 <2e-16 ***
factor(dept)D -1.25833 0.10152 -12.395 <2e-16 ***
factor(dept)E -1.68296 0.11733 -14.343 <2e-16 ***
factor(dept)F -3.26911 0.16707 -19.567 <2e-16 ***
---
Null deviance: 877.056 on 11 degrees of freedom
Residual deviance: 21.736 on 6 degrees of freedom
AIC: 102.68
Big data: The R package glmnet can apparently fit logistic regression to data sets
with very large numbers of variables or observations, and as mentioned below can use
regularization methods such as the lasso:
[Link]/web/packages/glmnet/[Link]
ROC curves
ROC curves can be constructed with the ROCR library. For example, for a probit
model for the beetle mortality data of Section 7.1.4,
19
> library("ROCR") # to construct ROC curve
> pred <- prediction(fitted([Link]),y)
> perf <- performance(pred, "tpr", "fpr")
> plot(perf)
> performance(pred,"auc")
Slot "[Link]":
[[1]]
[1] 0.9010852 # area under ROC curve
Cochran–Mantel–Haenszel test
data: beitler
Mantel-Haenszel X-squared = 6.3841, df = 1, p-value = 0.01151
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
1.177590 3.869174
sample estimates:
common odds ratio
2.134549
R provides the exact conditional test (Sec. 7.3.5) and the conditional ML estimate of
the common odds ratio (Sec. 16.6.6). When I > 2 and/or J > 2, this function provides
the generalized test that treats X and Y as nominal scale (i.e., df = (I − 1)(J − 1),
given in equation (8.18) in the text).
Infinite estimates
Here is an example of the use of R for Table 6.11, in which center effect ML estimates
for centers 1 and 3 are actually −∞.
20
-------------------------------------------------------------------
> data <- [Link]("[Link]",header=TRUE)
> data
center treatment y n
1 1 1 0 5
2 1 0 0 9
3 2 1 1 13
4 2 0 0 10
5 3 1 0 7
6 3 0 0 5
7 4 1 6 9
8 4 0 2 8
9 5 1 5 14
10 5 0 2 14
> attach(data)
21
Other binary response models
For binary data, alternative links are possible. For example, continuing with the
horseshoe crab data from above,
For the complementary log-log link with the beetle data of Table 7.1, showing also
the construction of standardized residuals (which can also be obtained by requesting
rstandard([Link], type=”pearson”)) and profile likelihood confidence intervals,
22
5 1.811 0.8253968 0.75683967 1.2684541 1.4855799
6 1.837 0.8983051 0.91843509 -0.5649292 -0.7021989
7 1.861 0.9838710 0.98575181 -0.1249636 -0.1489834
8 1.884 1.0000000 0.99913561 0.2278334 0.2368981
> confint([Link])
2.5 % 97.5 %
(Intercept) -46.13984 -33.49923
dose 18.66945 25.68877
Bayesian fitting
Jim Albert in Bayesian Computation with R (Springer 2009, pp. 216-219) presented
an R function, [Link], for implementing his algorithm for fitting probit models
with a Bayesian approach.
Penalized likelihood
The Copas smoothing method can be implemented with the R function ksmooth, with
lambda=bandwidth. For example, for the kyphosis example of Sec. 7.4.3,
> x <- c(12, 15, 42, 52, 59, 73, 82, 91, 96, 105, 114, 120, 121, 128, 130,
139, 139, 157, 1, 1, 2, 8, 11, 18, 22, 31, 37, 61, 72, 81, 97,
112, 118, 127, 131, 140, 151, 159, 177, 206)
> y <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0)
> k1 <- ksmooth(x,y,"normal",bandwidth=25)
> k2 <- ksmooth(x,y,"normal",bandwidth=100)
> plot(x,y)
> lines(k1)
> lines(k2, lty=2)
The brglm function in the MASS library can implement bias reduction using the
Firth penalized likelihood approach for binary regression models, including models
with logit, probit, and complementary log-log links:
[Link]/web/packages/brglm/[Link]
The Firth penalized likelihood approach can also be done using the R package
logistf. For example, for Table 6.11 analyzed above in the “infinite estimates” subsec-
tion:
23
treatment 1.3678143 0.6436197 -3.125353 5.9101373 0.34269584
factor(center)1 -4.0036677 1.5193002 -8.998902 -1.6994870 17.89776479
factor(center)2 -3.6351503 1.0063781 -8.204822 -0.9953539 11.19907751
factor(center)3 -4.1707188 1.5811491 -9.187891 -1.6107831 14.20192563
factor(center)4 -0.8487087 0.6264638 -5.897048 4.2538020 0.03158963
factor(center)5 -1.8328467 0.6200202 -6.599538 2.9956561 0.00000000
p
treatment 5.582773e-01
factor(center)1 2.330947e-05
factor(center)2 8.183801e-04
factor(center)3 1.642024e-04
factor(center)4 8.589313e-01
factor(center)5 1.000000e+00
Covariance-Matrix:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.4142463 -0.2747484 -0.3377549 -0.3456519 -0.2304370 -0.2758511
[2,] -0.2747484 2.3082730 0.2240156 0.2292533 0.1528371 0.1829579
[3,] -0.3377549 0.2240156 1.0127969 0.2818266 0.1878864 0.2249146
[4,] -0.3456519 0.2292533 0.2818266 2.5000323 0.1922793 0.2301733
[5,] -0.2304370 0.1528371 0.1878864 0.1922793 0.3924569 0.1534505
[6,] -0.2758511 0.1829579 0.2249146 0.2301733 0.1534505 0.3844251
Lasso for binary and count models is available in the R packages glmnet and
glmpath:
[Link]/web/packages/glmnet/[Link]
[Link]/web/packages/glmpath/[Link]
The group lasso is available with the grplasso package:
[Link]/web/packages/grplasso/[Link]
> library("VGAM")
> [Link] <- vgam(y ~ s(weight), family=binomialff(link=logit), data=crabs)
> plot(weight, fitted([Link]))
GAMs can also be fitted with the gam function in the mgcv library:
24
[Link]/web/packages/mgcv/[Link]
Especially useful for modeling multinomial responses is the VGAM package and vglm
function developed by Thomas Yee at Auckland, New Zealand,
[Link]/~yee/VGAM
This package has functions that can also can fit a wide variety of models including
multinomial logit models for nominal responses and cumulative logit models, adjacent-
categories models, and continuation-ratio models for ordinal responses. For more
details, see “The VGAM package for categorical data analysis,” in Journal of Statistical
Software, vol. 32, pp. 1-34 (2010), [Link]/v32/i10. See also
[Link]/~yee/VGAM/doc/[Link]
for some basic examples of its multiple capabilities for modeling categorical data.
Following is an example of the use of vglm for fitting a baseline-category logit
model to the alligator food choice data in Table 8.1 of the textbook. The data file has
the five multinomial counts for the food choices identified as y1 through y5, with y1
being fish as in the text. The vglm function uses the final category as the baseline, so
to use fish as the baseline, in the model statement we identify the response categories
as (y2 , y3 , y4 , y5 , y1 ). By contrast, the multinom function in the nnet library picks
the first category of the response variable as the baseline. The following also shows
output using it. For both functions, a predictor identified as a factor in the model
statement has its first category as the baseline, so the lake estimates shown here differ
from those in the book, which used the last lake level as the baseline.
25
3 1 0 1 16 3 2 2 3
4 1 0 0 3 0 1 2 3
5 2 1 1 2 2 0 0 1
6 2 1 0 13 7 6 0 0
7 2 0 1 0 1 0 1 0
8 2 0 0 3 9 1 0 2
9 3 1 1 3 7 1 0 1
10 3 1 0 8 6 6 3 5
11 3 0 1 2 4 1 1 4
12 3 0 0 0 1 0 0 0
13 4 1 1 13 10 0 2 2
14 4 1 0 9 0 0 1 2
15 4 0 1 3 9 1 0 1
16 4 0 0 8 1 0 0 1
> library(VGAM)
> vglm(formula = cbind(y2,y3,y4,y5,y1) ~ size + factor(lake),
family=multinomial, data=alligators)
Coefficients:
(Intercept):1 (Intercept):2 (Intercept):3 (Intercept):4 size:1
-3.2073772 -2.0717560 -1.3979592 -1.0780754 1.4582046
size:2 size:3 size:4 factor(lake)2:1 factor(lake)2:2
-0.3512628 -0.6306597 0.3315503 2.5955779 1.2160953
factor(lake)2:3 factor(lake)2:4 factor(lake)3:1 factor(lake)3:2 factor(lake)3:3
-1.3483253 -0.8205431 2.7803434 1.6924767 0.3926492
factor(lake)3:4 factor(lake)4:1 factor(lake)4:2 factor(lake)4:3 factor(lake)4:4
0.6901725 1.6583586 -1.2427766 -0.6951176 -0.8261962
> library(nnet)
> fit2 <- multinom(cbind(y1,y2,y3,y4,y5) ~ size + factor(lake), data=alligators)
> summary(fit2)
Call:
multinom(formula = cbind(y1, y2, y3, y4, y5) ~ size + factor(lake),
data = alligators)
Coefficients:
(Intercept) size factor(lake)2 factor(lake)3 factor(lake)4
y2 -3.207394 1.4582267 2.5955898 2.7803506 1.6583514
y3 -2.071811 -0.3512070 1.2161555 1.6925186 -1.2426769
y4 -1.397976 -0.6306179 -1.3482294 0.3926516 -0.6951107
y5 -1.078137 0.3315861 -0.8204767 0.6902170 -0.8261528
Std. Errors:
(Intercept) size factor(lake)2 factor(lake)3 factor(lake)4
y2 0.6387317 0.3959455 0.6597077 0.6712222 0.6128757
y3 0.7067258 0.5800273 0.7860141 0.7804482 1.1854024
26
y4 0.6085176 0.6424744 1.1634848 0.7817677 0.7812585
y5 0.4709212 0.4482539 0.7296253 0.5596752 0.5575414
The vglm function in the VGAM library can also fit a wide variety of ordinal models.
Many examples of the use of vglm for various ordinal-response analyses are available
at the website for my book, Analysis of Ordinal Categorical Data (2nd ed., 2010),
[Link]/~aa/ordinal/[Link], and several of these are also shown below.
For example, for the cumulative logit model fitted to the happiness data of Table 8.5
of the textbook, entering each multinomial observation as a set of indicators that
indicates the response category, letting race = 0 for white and 1 for black, and letting
traumatic be the number of traumatic events,
Coefficients:
Value Std. Error t value
(Intercept):1 -0.51812 0.33819 -1.5320
(Intercept):2 3.40060 0.56481 6.0208
race -2.03612 0.69113 -2.9461
traumatic -0.40558 0.18086 -2.2425
27
> [Link] <- vglm(cbind(y1,y2,y3) ~ race + traumatic + race*traumatic,
family=cumulative(parallel=TRUE), data=happy)
> summary([Link])
Coefficients:
Value Std. Error t value
(Intercept):1 -0.43927 0.34469 -1.2744
(Intercept):2 3.52745 0.58737 6.0055
race -3.05662 1.20459 -2.5375
traumatic -0.46905 0.19195 -2.4436
race:traumatic 0.60850 0.60077 1.0129
The parallel=TRUE option requests the proportional odds version of the model with
the same effects for each cumulative logit. Then entering fitted(fit) would produce the
estimated probabilities for each category for each observation. Here, we also fitted the
model with an interaction term, which does not provide a significantly better fit.
To use vglm to fit the cumulative logit model not having the proportional odds
assumption, we take out the parallel=TRUE option. Then, we do a likelihood-ratio
test to see if it gives a better fit:
Coefficients:
Value Std. Error t value
(Intercept):1 -0.56605 0.36618 -1.545821
(Intercept):2 3.48370 0.75950 4.586850
race:1 -14.01877 322.84309 -0.043423
race:2 -1.84673 0.76276 -2.421095
traumatic:1 -0.34091 0.21245 -1.604644
traumatic:2 -0.48356 0.27524 -1.756845
> pchisq(deviance(fit)-deviance(fit2),df=[Link](fit)-[Link](fit2),[Link]=FALSE)
[1] 0.4936429
Note that the ML effect estimate of race for the first logit is actually −∞, reflecting
the lack of any black subjects in the first happiness category.
This function can also fit the partial proportional odds model. Here is an example
with cumulative logit link for the mental impairment data on p. 62 of the 2nd edition
of my book, Analysis of Ordinal Categorical Data (with proportional odds for the life
events effect):
28
> fit < -vglm(impair ~ ses + life, family=cumulative(parallel=FALSE~ses))
> summary(fit)
Coefficients:
Estimate Std. Error z value
(Intercept):1 -0.17660 0.69506 -0.25408
(Intercept):2 1.00567 0.66327 1.51623
(Intercept):3 2.39555 0.77894 3.07539
ses:1 0.98237 0.76430 1.28531
ses:2 1.54149 0.73732 2.09066
ses:3 0.73623 0.81213 0.90655
life -0.32413 0.12017 -2.69736
For the same data, to fit the cumulative probit model with common effects for
each probit, we use
Coefficients:
Value Std. Error t value
(Intercept):1 -0.34808 0.200147 -1.7391
(Intercept):2 1.91607 0.282872 6.7736
race -1.15712 0.378716 -3.0554
traumatic -0.22131 0.098973 -2.2361
> summary([Link])
Coefficients:
Value Std. Error t value
(Intercept):1 -0.49606 0.31805 -1.5597
(Intercept):2 3.02747 0.57392 5.2751
race -1.84230 0.64190 -2.8701
traumatic -0.35701 0.16396 -2.1775
29
Names of linear predictors: log(P[Y=1]/P[Y=2]), log(P[Y=2]/P[Y=3])
Residual Deviance: 148.1996 on 190 degrees of freedom
Log-likelihood: -74.09982 on 190 degrees of freedom
Number of Iterations: 5
To fit the continuation-ratio logit model to the same data, one direction for forming
the sequential logits yields the results:
> summary([Link])
Coefficients:
Value Std. Error t value
(Intercept):1 -0.45530 0.32975 -1.3808
(Intercept):2 3.34108 0.56309 5.9335
race -2.02555 0.67683 -2.9927
traumatic -0.38504 0.17368 -2.2170
The more common form of continuation-ratio logit is obtained by instead using RE-
VERSE=FALSE in the model-fitting statement.
For the proportional odds version of cumulative logit models, you can alternatively
use the polr function in the MASS library, with syntax shown next. However, the data
file then needs the response as a factor vector, so we first put the data from the above
examples in that form.
> library(MASS)
> response <- matrix(0,nrow=97,ncol=1)
> response <- ifelse(y1==1,1,0)
> response <- ifelse(y2==1,2,resp)
> response <- ifelse(y3==1,3,resp)
> y <- factor(response)
> polr(y ~ race + traumatic, data=happy)
Call:
polr(formula = y ~ race + traumatic, data=happy)
Coefficients:
race traumatic
2.0361187 0.4055724
30
Intercepts:
1|2 2|3
-0.5181118 3.4005955
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 6.29154 0.03667 171.558 < 2e-16 ***
A2 -1.78511 0.05976 -29.872 < 2e-16 ***
C2 -0.64931 0.04415 -14.707 < 2e-16 ***
M2 0.31542 0.04244 7.431 1.08e-13 ***
31
---
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 6.81387 0.03313 205.699 < 2e-16 ***
A2 -5.52827 0.45221 -12.225 < 2e-16 ***
C2 -3.01575 0.15162 -19.891 < 2e-16 ***
M2 -0.52486 0.05428 -9.669 < 2e-16 ***
A2:C2 2.05453 0.17406 11.803 < 2e-16 ***
A2:M2 2.98601 0.46468 6.426 1.31e-10 ***
C2:M2 2.84789 0.16384 17.382 < 2e-16 ***
---
> library(car)
> Anova([Link]) # likelihood-ratio tests for pairwise conditional associations
LR Chisq Df Pr(>Chisq)
A:C 187.38 1 < 2.2e-16
A:M 91.64 1 < 2.2e-16
C:M 497.00 1 < 2.2e-16
32
6 43 42.38317 0.09474777 0.6333249
7 2 1.38317 0.52447888 0.6333250
8 279 279.61683 -0.03688791 -0.6333249
By the results of Sec. 9.5, we get the same results for the association between
marijuana use and each of alcohol use and cigarette use if we treat the data as four
binomials (instead of eight Poissons) and model the logit for marijuana use in terms
of additive effects for alcohol use and cigarette use.
----------------------------------------------------------------------
> drugs2 <- [Link]("drugs_binomial.dat", header=TRUE)
> drugs2
A C M_yes M_no n
1 yes yes 911 538 1449
2 yes no 44 456 500
3 no yes 3 43 46
4 no no 2 279 281
> attach(drugs2)
> alc <- factor(A); cig <- factor(C)
> [Link] <- glm(M_yes/n ~ alc + cig, weights=n,
family=binomial(link=logit))
> summary([Link])
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.3090 0.4752 -11.172 < 2e-16
alcyes 2.9860 0.4647 6.426 1.31e-10
cigyes 2.8479 0.1638 17.382 < 2e-16
---
Null deviance: 843.82664 on 3 degrees of freedom
Residual deviance: 0.37399 on 1 degrees of freedom
----------------------------------------------------------------------
The loglin function in the MASS library can fit loglinear models using iterative
proportional fitting, reporting parameter estimates using constraints whereby they
sum to zero (rather than a baseline equaling 0). The loglm function allows the models
to be specified and fitted in a manner similar to using glm.
Association models
Following is an example for the linear-by-linear association model and the row effects
and columns effects models (with scores 1, 2, 4, 5) fitted to Table 10.3 on premarital
sex and teenage birth control.
33
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 4.10684 0.08951 45.881 < 2e-16 ***
premar2 -1.64596 0.13473 -12.216 < 2e-16 ***
premar3 -1.77002 0.16464 -10.751 < 2e-16 ***
premar4 -1.75369 0.23432 -7.484 7.20e-14 ***
birth2 -0.46411 0.11952 -3.883 0.000103 ***
birth3 -0.72452 0.16201 -4.472 7.74e-06 ***
birth4 -1.87966 0.24910 -7.546 4.50e-14 ***
uv 0.28584 0.02824 10.122 < 2e-16 ***
Null deviance: 431.078 on 15 degrees of freedom
Residual deviance: 11.534 on 8 degrees of freedom
AIC: 118.21
34
premar4 2.11167 0.18958 11.138 < 2e-16 ***
birth2 0.54590 0.11723 4.656 3.22e-06 ***
birth3 1.59262 0.14787 10.770 < 2e-16 ***
birth4 1.51018 0.16420 9.197 < 2e-16 ***
premar1:v 0.58454 0.05930 9.858 < 2e-16 ***
premar2:v 0.49554 0.07990 6.202 5.57e-10 ***
premar3:v 0.20315 0.06538 3.107 0.001890 **
premar4:v NA NA NA NA
Joseph Lang’s [Link] function can fit generalized loglinear models (Section 10.5.1)
and other much more general “multinomial-Poisson homogeneous” models such as
covered in Lang (2004, 2005):
[Link]/~jblang/[Link]/[Link]
The gnm add-on package for R, developed by David Firth and Heather Turner at the
Univ. of Warwick, can fit multiplicative models such as Goodman’s RC association
model for two-way contingency tables and Anderson’s stereotype model for ordinal
multinomial responses:
[Link]/fac/sci/statistics/staff/academic-research/firth/software
Thomas Yee’s VGAM package mentioned for Chapter 8 above can also fit Goodman’s
RC association model and Anderson’s stereotype model, as well as bivariate logistic
and probit models for bivariate binary responses.
Greenacre and Nenadic have developed the ca package for simple, multiple, and
joint correspondence analysis:
[Link]/advstats/[Link]
[Link]/web/packages/ca/[Link]
The ACD package can fit loglinear models when data are missing. See
[Link]/web/packages/ACD/[Link]
For the score CI due to Tango (1998) and the adjusted Wald CI proposed by Agresti
and Min (2005) that forms the ordinary Wald CI after adding 0.50 to each cell, see
[Link]/~aa/cda/R/matched/R2_matched/[Link]
The are also available in the PropCIs package.
35
--------------------------------------------------------------------------------
> library(PropCIs)
> [Link](54, 16, 433, 0.95); [Link](54, 136, 433, 0.95)
95 percent confidence interval:
-0.12651019 -0.05148492 95% score CI for difference of marginal probabilities
95 percent confidence interval:
0.1295873 0.2491655 95% Wald CI for difference of marginal probabilities
> [Link](54, 136, 433, 0.95)
95 percent confidence interval:
0.1288091 0.2482024 95% adjusted CI for difference of marginal probabilities
--------------------------------------------------------------------------------
McNemar test
The function [Link] can conduct McNemar’s test for matched pairs. For ex-
ample, for Table 11.1,
Bradley–Terry models
The Bradley–Terry model can be fitted using the glm function by treating it as a
generalized linear model. It can also be fitted using specialized functions, such as with
the brat function in Thomas Yee’s VGAM library mentioned above:
[Link]/Rdoc/library/VGAM/html/[Link]
or by Prof. David Firth as described at
[Link]/fac/sci/statistics/staff/academic-research/firth/software/
bradleyterry
[Link]/v12/i01
36
> abortion
gender response question case
1 1 1 1 1
2 1 1 2 1
3 1 1 3 1
4 1 1 1 2
5 1 1 2 2
6 1 1 3 2
7 1 1 1 3
8 1 1 2 3
9 1 1 3 3
...
5545 0 0 1 1849
5546 0 0 2 1849
5547 0 0 3 1849
5548 0 0 1 1850
5549 0 0 2 1850
5550 0 0 3 1850
> library(gee)
> summary([Link])
Model:
Link: Logit
Variance to Mean Relation: Binomial
Correlation Structure: Exchangeable
Coefficients:
Estimate Naive S.E. Naive z Robust S.E. Robust z
(Intercept) -0.125325730 0.06782579 -1.84775925 0.06758212 -1.85442135
gender 0.003437873 0.08790630 0.03910838 0.08784072 0.03913758
z1 0.149347107 0.02814374 5.30658404 0.02973865 5.02198729
z2 0.052017986 0.02815145 1.84779075 0.02704703 1.92324179
Working Correlation
[,1] [,2] [,3]
[1,] 1.0000000 0.8173308 0.8173308
[2,] 0.8173308 1.0000000 0.8173308
[3,] 0.8173308 0.8173308 1.0000000
37
> fit.gee2 <- gee(response ~ gender + z1 + z2, id=case, family=binomial,
+ corstr="independence", data=abortion)
> summary(fit.gee2)
Link: Logit
Variance to Mean Relation: Binomial
Correlation Structure: Independent
Coefficients:
Estimate Naive S.E. Naive z Robust S.E. Robust z
(Intercept) -0.125407576 0.05562131 -2.25466795 0.06758236 -1.85562596
gender 0.003582051 0.05415761 0.06614123 0.08784012 0.04077921
z1 0.149347113 0.06584875 2.26803253 0.02973865 5.02198759
z2 0.052017989 0.06586692 0.78974374 0.02704704 1.92324166
Working Correlation
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
From the geepack library, the function geeglm performs fitting of clustered data
using the GEE method. See
[Link]/v15/i02/paper
for details, including an example for a binary response. Possible working correla-
tion structures include independence, exchangeable, autoregressive (ar1), and unstruc-
tured. In addition to the sandwich covariance matrix (which is the default), when the
number of clusters is small one can find a jackknife estimator. Fitting statements have
the form:
The library repolr has a function repolr for GEE methods with ordinal responses:
[Link]/web/packages/repolr/[Link]
Here is an example for the insomnia data of Table 12.3, using the independence
working correlation structure (Thanks to Anestis Touloumis).
> insomnia<-[Link]("[Link]",header=TRUE)
> insomnia<-[Link](insomnia)
> insomnia
case treat occasion outcome
1 1 0 1
1 1 1 1
2 1 0 1
2 1 1 1
3 1 0 1
38
3 1 1 1
4 1 0 1
4 1 1 1
5 1 0 1
...
239 0 0 4
239 0 1 4
> library(repolr)
> fit <- repolr(formula = outcome ~ treat + occasion + treat * occasion,
+ subjects="case", data=insomnia, times=c(1,2), categories=4,
corstr = "independence")
> summary(fit$gee)
Coefficients:
Estimate Naive S.E. Naive z Robust S.E. Robust z
factor(cuts)1 -2.26708899 0.2027367 -11.1824294 0.2187606 -10.3633343
factor(cuts)2 -0.95146176 0.1784822 -5.3308499 0.1809172 -5.2591017
factor(cuts)3 0.35173977 0.1726860 2.0368745 0.1784232 1.9713794
treat 0.03361002 0.2368973 0.1418759 0.2384374 0.1409595
occasion 1.03807641 0.2375992 4.3690229 0.1675855 6.1943093
treat:occasion 0.70775891 0.3341759 2.1179234 0.2435197 2.9063728
Joseph Lang at the Univ. of Iowa has R and S-Plus functions such as [Link] for
ML fitting of marginal models (when the explanatory variables are categorical and
not numerous) through the generalized loglinear model (10.10). This uses the con-
straint approach with Lagrange multipliers. The function hmmm at CRAN developed
by R. Colombi, S. Giordano, M. Cazzaro, and J. Lang can fit hierarchical multino-
mial marginal models (Bergsma and Rudas 2002). The models can impose inequality
constraints on the parameters. For details, see
[Link]/web/packages/hmmm/[Link]
The function lmer (linear mixed effects in R) in the R package Matrix can be used to
fit generalized linear mixed models. See the Gelman and Hill (2007) text, such as Sec.
12.4. See also the lme4 package, described in
[Link]
These use adaptive Gauss–Hermite quadrature.
The function glmm in the repeated library can fit generalized linear mixed models
using Gauss–Hermite quadrature methods, for families including the binomial and
Poisson:
[Link]/Rdoc/library/repeated/html/[Link]
39
The package glmmAK can also fit them, with a Bayesian approach with priors for the
fixed effects parameters:
[Link]/web/packages/glmmAK/[Link]
The function glmmML in the glmmML package can fit GLMMs with random intercepts
by adaptive Gauss–Hermite quadrature. For instance, in the following code we use
it to analyze the opinions about abortion data analyzed in Sec. 13.3.2 with random
effects models, employing Gauss-Hermite quadrature with 75 quadrature points and a
starting value of 9 for the estimate of σ.
The function glmmPQL in the MASS library can fit GLMMs using penalized quasi-
likelihood. The R package MCMCglmm can fit them with Markov Chain Monte Carlo
methods:
[Link]/web/packages/MCMCglmm/vignettes/[Link]
For a text on GLMMs using R, see Multivariate Generalized Linear Mixed Models
by D. M. Berridge and R. Crouchley, published 2011 by CRC Press. The emphasis is
on multivariate models, using the Sabre software package in R.
40
Item response models
Dimitris Rizopoulos from Leuven, Belgium has prepared a package ltm for Item Re-
sponse Theory analyses. This package can fit the Rasch model, the two-parameter
logistic model, Birnbaum’s three-parameter model, the latent trait model with up to
two latent variables, and Samejima’s graded response model:
[Link]/biostat/software/[Link]#LatentIRT
Steve Buyske at Rutgers has prepared a library for fitting latent class models with the
EM algorithm:
[Link]/home/buyske/[Link]
The following shows the beta-binomial and quasi-likelihood analyses of the teratology
data presented in Sec. 14.3.4, continuing with the analyses shown above at the end of
the R discussion for Chapter 4. Beta-binomial modeling is an option with the vglm
function in the VGAM library (using Fisher scoring) and the betabin function in the
aod library. It seems that vglm in VGAM uses Fisher scoring and hence reports SE
values based on the expected information matrix, whereas betabin in aod uses the
observed information matrix. Quasi-likelihood with the beta-binomial type variance
is available with the quasibin function in the aod library. (In the following example,
the random part of the statement specifies the same overdispersion for each group).
For details about the aod package, see
[Link]/web/packages/aod/[Link]
Again, we borrow heavily from Laura Thompson’s excellent manual.
Coefficients:
Value Std. Error t value
(Intercept):1 1.3458 0.24412 5.5130
(Intercept):2 -1.1458 0.32408 -3.5355 # This is logit(rho)
group2 -3.1144 0.51818 -6.0103
group3 -3.8681 0.86285 -4.4830
group4 -3.9225 0.68351 -5.7387
41
> logit(-1.1458, inverse=T) # This is a function in VGAM
[1] 0.2412571 # The estimate of rho in beta-bin variance
Fixed-effect coefficients:
Estimate Std. Error z value Pr(> |z|)
(Intercept) 1.346e+00 2.481e-01 5.425e+00 5.799e-08
group2 -3.115e+00 5.020e-01 -6.205e+00 5.485e-10
group3 -3.869e+00 8.088e-01 -4.784e+00 1.722e-06
group4 -3.924e+00 6.682e-01 -5.872e+00 4.293e-09
Overdispersion coefficients:
Estimate Std. Error z value Pr(> z)
phi.(Intercept) 2.412e-01 6.036e-02 3.996e+00 3.222e-05
Fixed-effect coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.2124 0.2233 5.4294 < 1e-4
group2 -3.3696 0.5626 -5.9893 < 1e-4
group3 -4.5853 1.3028 -3.5197 4e-04
group4 -4.2502 0.8484 -5.0097 < 1e-4
Overdispersion parameter:
phi
0.1923
As shown above in the Chapter 4 description for R, the [Link] function in the MASS
library is a modification of the glm function to handle negative binomial regression
models:
[Link]/R-manual/R-patched/library/MASS/html/[Link]
The negbin function in the aod package can also handle negative binomial regres-
sion:
42
[Link]/web/packages/aod/[Link]
Thomas Yee’s VGAM package can also fit zero-inflated Poisson models and nega-
tive binomial models.
In the MASS library there is a lda function for linear discriminant analysis and a qda
function for quadratic discriminant analysis:
[Link]/R-manual/R-patched/library/MASS/html/[Link]
[Link]/R-manual/R-patched/library/MASS/html/[Link]
For example, for the horseshoe crab example in the text, you can use the code
Classification trees
> library(tree)
> attach(crabs)
> fit <- rpart(y ~ color + width, method="class")
> plot(fit)
> text(fit)
> printcp(fit)
Classification tree:
rpart(formula = y ~ color + width, method = "class")
n= 173
43
CP nsplit rel error xerror xstd
1 0.161290 0 1.00000 1.00000 0.101728
2 0.080645 1 0.83871 1.03226 0.102421
3 0.064516 2 0.75806 0.96774 0.100972
4 0.048387 3 0.69355 0.93548 0.100149
5 0.016129 4 0.64516 0.85484 0.097794
6 0.010000 6 0.61290 0.82258 0.096728
> plotcp(fit)
> summary(fit)
> plot(fit, uniform=TRUE,
main="Classification Tree for Crabs")
> pfit2 <- prune(fit, cp= 0.02)
> plot(pfit2, uniform=TRUE,
main="Pruned Classification Tree for Crabs")
plot(pfit2, uniform=TRUE,
+ main="Pruned Classification Tree for Crabs")
> text(pfit2, use.n=TRUE, all=TRUE, cex=.8)
> post(pfit2, file = "[Link]",
title = "Pruned Classification Tree for Crabs")
post(pfit2, file = "[Link]",
+ title = "Pruned Classification Tree for Crabs")
Cluster analysis
44
11 0 0 0 1 1 0 0 1
12 0 0 0 0 0 0 0 0
13 0 0 0 0 0 0 0 1
14 0 0 0 0 0 0 0 0
> distances <- dist(x,method="manhattan")
> states <- c("AZ", "CA", "CO", "FL", "IL", "MA", "MN",
"MO", "NM", "NY", "OH", "TX", "VA", "WY")
> democlust <- hclust(distances,"average")
> postscript(file="[Link]")
> plot(democlust, labels=states)
> [Link]()
45