10.07.2015 Views

S-PLUS (and R) - Department of Statistics

S-PLUS (and R) - Department of Statistics

S-PLUS (and R) - Department of Statistics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

S-<strong>PLUS</strong> (<strong>and</strong> R) Manual to AccompanyAgresti’s Categorical Data Analysis (2002)2 nd editionLaura A. Thompson, 2006 ©


Table <strong>of</strong> ContentsIntroduction <strong>and</strong> Changes from First Edition......................................1A. Obtaining the R S<strong>of</strong>tware for Windows.............................................................................1B. Libraries in S-<strong>PLUS</strong> <strong>and</strong> Packages in R...........................................................................1C. Setting contrast types using Options()..............................................................................3D. Credit for functions...............................................................................................................3E. Editing functions ...................................................................................................................3F. A note about using Splus Menus .......................................................................................4G. Notice <strong>of</strong> errors.....................................................................................................................4H. Introductions to the S Language........................................................................................4I. References..............................................................................................................................4J. Acknowledgements...............................................................................................................5Chapter 1: Distributions <strong>and</strong> Inference for CategoricalData: ...........................................................................................................................................6A. Summary <strong>of</strong> Chapter 1, Agresti.........................................................................................6B. Categorical Distributions in S-<strong>PLUS</strong> <strong>and</strong> R.....................................................................6C. Proportion <strong>of</strong> Vegetarians (Statistical Inference for Binomial Parameters) ...............8D. The Mid-P-Value ...............................................................................................................11E. Pearson’s Chi-Squared Statistic .....................................................................................11F. Likelihood Ratio Chi-Squared Statistic...........................................................................12G. Maximum Likelihood Estimation.....................................................................................12Chapter 2: Describing Contingency Tables.......................................16A. Summary <strong>of</strong> Chapter 2, Agresti........................................................................................16B. Comparing two proportions...............................................................................................18C. Partial Association in Stratified 2 x 2 Tables..................................................................19D. Conditional Odds Ratios ...................................................................................................23E. Summary Measures <strong>of</strong> Assocation: Ordinal Trends.....................................................24Chapter 3: Inference for Contingency Tables .................................28A. Summary <strong>of</strong> Chapter 3, Agresti........................................................................................28B. Confidence Intervals for Association Parameters .........................................................29C. Testing Independence in Two-way Contingency Tables .............................................35D. Following Up Chi-Squared Tests.....................................................................................37E. Two-Way Tables with Ordered Classification................................................................39F. Small Sample Tests <strong>of</strong> Independence ............................................................................41G. Small-Sample Confidence Intervals For 2x2 Tables....................................................44Chapter 4: Generalized Linear Models ..................................................50i


A. Summary <strong>of</strong> Chapter 4, Agresti........................................................................................50B. Generalized Linear Models for Binary Data ...................................................................51C. Generalized Linear Models for Count Data ...................................................................56D. Overdispersion in Poisson Generalized Linear Models ...............................................61E. Negative Binomial GLIMs..................................................................................................63F. Residuals for GLIMs...........................................................................................................65G. Quasi-Likelihood <strong>and</strong> GLIMs............................................................................................67H. Generalized Additive Models (GAMs).............................................................................68Chapter 5 : Logistic Regression....................................................................72A. Summary <strong>of</strong> Chapter 5, Agresti.......................................................................................72B. Logistic Regression for Horseshoe Crab Data .............................................................73C. Goodness-<strong>of</strong>-fit for Logistic Regression for Ungrouped Data....................................77D. Logit Models with Categorical Predictors......................................................................78E. Multiple Logistic Regression............................................................................................82F. Extended Example (Problem 5.17).................................................................................88Chapter 6 – Building <strong>and</strong> Applying Logistic RegressionModels ....................................................................................................................................92A. Summary <strong>of</strong> Chapter 6, Agresti........................................................................................92B. Model Selection ..................................................................................................................93C. Using Causal Hypotheses to Guide Model Fitting ........................................................94D. Logistic Regression Diagnostics......................................................................................96E. Inference about Conditional Associations in 2 x 2 x K Tables ..................................102F. Estimation/Testing <strong>of</strong> Common Odds Ratio .................................................................105G. Using Models to Improve Inferential Power.................................................................106H. Sample Size <strong>and</strong> Power Considerations......................................................................107I. Probit <strong>and</strong> Complementary Log-Log Models................................................................109J. Conditional Logistic Regression <strong>and</strong> Exact Distributions ...........................................111K. Bias-reduced Logistic Regression .................................................................................116Chapter 7 –Logit Models for Multinomial Responses............117A. Summary <strong>of</strong> Chapter 7, Agresti......................................................................................117B. Nominal Responses: Baseline-Category Logit Models..............................................118C. Cumulative Logit Models.................................................................................................121D. Cumulative Link Models..................................................................................................125E. Adjacent-Categories Logit Models.................................................................................127F. Continuation-Ratio Logit Models....................................................................................128G. Mean Response Models .................................................................................................134H. Generalized Cochran-Mantel Haenszel Statistic for Ordinal Categories ...............139Chapter 8 –Loglinear Models for Contingency Tables .........141A. Summary <strong>of</strong> Chapter 8, Agresti......................................................................................141ii


B. Loglinear Models for Three-way Tables .......................................................................142C. Inference for Loglinear Models ......................................................................................145D. Loglinear Models for Higher Dimensions ....................................................................147E. Loglinear-Logit Model Connection.................................................................................150F. Contingency Table St<strong>and</strong>ardization...............................................................................151Chapter 9 –Building <strong>and</strong> Extending Loglinear Models .........152A. Summary <strong>of</strong> Chapter 9, Agresti......................................................................................152B. Model Selection <strong>and</strong> Comparison.................................................................................153C. Diagnostics for Checking Models .................................................................................155D. Modeling Ordinal Assocations ......................................................................................156E. Assocation Models ...........................................................................................................158F. Association Models, Correlation Models, <strong>and</strong> Correspondence Analysis ...............164G. Poisson Regression for Rates .......................................................................................170H. Modeling Survival Times.................................................................................................172I. Empty Cells <strong>and</strong> Sparseness...........................................................................................174Chapter 10 – Models for Matched Pairs .............................................176A. Summary <strong>of</strong> Chapter 10, Agresti ...................................................................................176B. Comparing Dependent Proportions...............................................................................177C. Conditional Logistic Regression for Binary Matched Pairs........................................178D. Marginal Models for Square Contingency Tables......................................................181E. Symmetry, Quasi-symmetry, <strong>and</strong> Quasi-independence ...........................................186F. Square Tables with Ordered Categories .....................................................................189G. Measuring Agreement Between Observers ...............................................................192H. Kappa Measure <strong>of</strong> Agreement......................................................................................195I. Bradley-Terry Model for Paired Preferences................................................................196J. Bradley-Terry Model with Order Effect.........................................................................199K. Marginal <strong>and</strong> Quasi-symmetry Models for Matched Sets.........................................200Chapter 11 –Analyzing Repeated Categorical ResponseData.........................................................................................................................................203A. Summary <strong>of</strong> Chapter 11, Agresti ...................................................................................203B. Comparing Marginal Distributions: Multiple Responses ............................................203C. Marginal Modeling: Maximum Likelihood Approach...................................................205D. Marginal Modeling: Maximum Likelihood Approach. Modeling a RepeatedMultinomial Response ..........................................................................................................211E. Marginal Modeling: GEE Approach. Modeling a Repeated Multinomial Response215F. Marginal Modeling: GEE Approach. Modeling a Repeated Multinomial OrdinalResponse................................................................................................................................219G. Markov Chains: Transitional Modeling .........................................................................221iii


Chapter 12 – R<strong>and</strong>om Effects: Generalized Linear MixedModels for Categorical Responses..........................................................226A. Summary <strong>of</strong> Chapter 12, Agresti ...................................................................................226B. Logit GLIMM for Binary Matched Pairs.........................................................................227C. Examples <strong>of</strong> R<strong>and</strong>om Effects Models for Binary Data...............................................230D. R<strong>and</strong>om Effects Models for Multinomial Data .............................................................243E. Multivariate R<strong>and</strong>om Effects Models for Binary Data.................................................245iv


1Introduction <strong>and</strong> Changes from First EditionThis manual accompanies Agresti’s Categorical Data Analysis (2002). It provides assistance indoing the statistical methods illustrated there, using S-<strong>PLUS</strong> <strong>and</strong> the R language. Although I have usedthe Windows versions <strong>of</strong> these two s<strong>of</strong>twares, I suspect there are few changes in order to use the code inother ports. I have included examples <strong>of</strong> almost all <strong>of</strong> the major (<strong>and</strong> some minor) analyses introduced byAgresti. The manual chapters parallel those from Agresti so that, for example, in Chapter 2 I discussusing the s<strong>of</strong>tware to conduct analyses from Agresti’s Chapter 2. In most cases I use the data providedin the text. There are only one or two occasions where I use data from the problems sections in Agresti.Discussion <strong>of</strong> results <strong>of</strong> analyses is brief since the discussion appears in the text. That said, one <strong>of</strong> theimprovements in the current manual over the previous version <strong>of</strong> the manual (Thompson, 1999) is that itis more self-contained. In addition, I include a summary <strong>of</strong> the corresponding chapter from Agresti at thebeginning <strong>of</strong> each chapter in the manual. However, it will still be helpful to refer to the text to underst<strong>and</strong>completely the analyses in this manual.In the manual, I frequently employ functions that come from user-contributed libraries (packages)<strong>of</strong> S-<strong>PLUS</strong> (or R). In the text, I note when a particular library or package is used. These libraries are notautomatically included with the s<strong>of</strong>tware, but can be easily downloaded from the internet, especially forthe newest version <strong>of</strong> R for Windows. I mention in the next section how to get these libraries <strong>and</strong> how toinstall them for use. Many times, the library or package has its own help manual or help section. I willdemonstrate how to access these from inside the s<strong>of</strong>tware.I used S-<strong>PLUS</strong> 6.1 for Windows <strong>and</strong> R versions 1.8.1, 1.9, <strong>and</strong> 2.0 for Windows for the analyses.However, S-<strong>PLUS</strong> for Windows versions as far back as 3.0 will do many <strong>of</strong> the analyses (but not all).This is not so easily said for R, as user-contributed packages frequently apply to the newer versions <strong>of</strong> R(e.g., at least 1.3.0). Many <strong>of</strong> the analyses can be applied to either S-<strong>PLUS</strong> or R. Some need smallchanges in order to apply to both s<strong>of</strong>twares; these changes I have provided where necessary. In general,when I refer to both <strong>of</strong> the s<strong>of</strong>twares, I will use the “generic” name, S. Also, if I do not indicate that I amusing either S-<strong>PLUS</strong> or R for a particular comm<strong>and</strong>, that means it will work in both s<strong>of</strong>twares.To separate input to R or S-<strong>PLUS</strong> <strong>and</strong> output from other text in this manual, I have put normaltext in Arial font <strong>and</strong> comm<strong>and</strong>s <strong>and</strong> output in courier font. The input comm<strong>and</strong>s are in bold font,whereas the output is not. Also, all assignments will use the “


2yags2 (V. Carey) - (used for chapter 11)Most <strong>of</strong> these libraries can be obtained in .zip form from URL http://lib.stat.cmu.edu/DOS/S/Swin orhttp://lib.stat.cmu.edu/DOS/S. Currently, the URL http://www.stats.ox.ac.uk/pub/MASS4/Winlibs/ containsmany ports to S-<strong>PLUS</strong> 6.0 for Windows. To install a library, first download it to any folder on yourcomputer. Next, “unzip” the file using an “unzipping” program. This will extract all the files into a newfolder in the directory into which you downloaded the zip file. Move the entire folder to the librarydirectory under your S-<strong>PLUS</strong> directory (e.g., c:/program files/Insightful/splus61/library).To load a library, you can either pull down the File menu in S-<strong>PLUS</strong> <strong>and</strong> select Load Library or type one<strong>of</strong> the following in a script or comm<strong>and</strong> windowlibrary(“libraryname”,first=T)library(libraryname)# loads libraryname into first database positionTo use the library’s help manual from inside S-<strong>PLUS</strong> or R type in a script or comm<strong>and</strong> windowhelp(library=“libraryname”)The R packages used in this manual that do not come with the s<strong>of</strong>tware are (note: I still need toadd libraries I will use in chapters 11-15)MASS – (VR bundle, Venables <strong>and</strong> Ripley)rmutil (J. Lindsey) – (used with gnlm) http://alpha.luc.ac.be/~lucp0753/rcode.htmlgnlm (J. Lindsey) – http://alpha.luc.ac.be/~lucp0753/rcode.htmlrepeated (J. Lindsey) – http://alpha.luc.ac.be/~lucp0753/rcode.htmlSuppDists (B. Wheeler) – (used in chapter 1)combinant (V. Carey) – (used in chapters 1, 3)methods – (used in chapter 3)Bhat (E. Luebeck)– (used throughout)mgcv (S. Wood) – (used for fitting GAM models)modreg (B. Ripley) – (used for fitting GAM models)gee <strong>and</strong> geepack (J. Yan) – (used in chapter 11)yags (V. Carey) – (used in chapter 11)gllm – (used for generalized log linear models)GlmmGibbs (Myles <strong>and</strong> Clayton) – (used for generalized linear mixed models, chap. 12)glmmML (G. Broström) – (used for generalized linear mixed models, chapter 12)CoCoAn (S. Dray) – (used for correspondence analysis)e1071 (A. Weingessel) – (used for latent class analysis)vcd (M. Friendly)– (used in chapters 2, 3, 5, 9 <strong>and</strong> 10)brlr (D. Firth) – (used in chapter 6)BradleyTerry (D. Firth) – (used in chapter 10)ordinal (P. Lindsey) – (used in chapter 7) http://euridice.tue.nl/~plindsey/rlibs.htmldesign (F. Harrell) – (used throughout) http://hesweb1.med.virginia.edu/biostatHmisc (F. Harrell) – (used throughout)VGAM (T. Yee) – (used in chapters 7 <strong>and</strong> 9)http://www.stat.auckl<strong>and</strong>.ac.nz/~yee/VGAM/download.shtmlmph (J. Lang) – (used in chapters 7, 10)http://www.stat.uiowa.edu/~jblang/mph.fitting/mph.fit.documentation.htm#availabilityexactLoglinTest (B. Caffo) – (used in chapters 9, 10)http://www.biostat.jhsph.edu/~bcaffo/downloads.htmR packages can be installed from Windows using the install.packages function. This function can becalled from the console or from the pull-down “Packages” menu. A package is loaded in the same way


3as for S-<strong>PLUS</strong>. As well, the comm<strong>and</strong> help(package=pkg) can used to invoke the help menu forpackage pkg.To detach a library or package, named library.name or pkg, respectively, one can issue the followingcomm<strong>and</strong>s, among others.detach(“library.name”)detach(“package:pkg”)# S-<strong>PLUS</strong># RC. Setting contrast types using Options()The options function can be used to set the type <strong>of</strong> contrasts used in estimating models. The defaultfor S-<strong>PLUS</strong> 6.1 is Helmert contrasts for factors <strong>and</strong> polynomial contrasts for ordered factors. The defaultfor R 1.6 is treatment contrasts for factors <strong>and</strong> polynomial contrasts for ordered factors. I use treatmentcontrasts for factors for most <strong>of</strong> the analysis so that they match Agresti’s estimates. However, there aretimes when I use sum-to-zero contrasts (contr.sum). The type <strong>of</strong> contrasts I use is usually indicated by acall to options prior to fitting the model, if necessary. If the call to options has been omitted, pleaseassume I am using treatment contrasts for factors.One can find out exactly what the contrasts are in a glm-type fit by using the functions model.matrix<strong>and</strong> contrasts. Issuing the com<strong>and</strong> contrasts(model.matrix(fit)) gives the contrasts.D. Credit for functionsThe author <strong>of</strong> a function is named if the function was not written by me. Whenever I use functions that donot come with S, I will mention the S-<strong>PLUS</strong> library or R package that includes them. I also give a URL, ifapplicable.E. Editing functionsIn several places in the text, I mention creating functions or editing existing functions that come witheither S-<strong>PLUS</strong> or R. There are many ways to edit or create a function. Some <strong>of</strong> them are specific to yourplatform. However, one procedure that works on all platforms from the comm<strong>and</strong> line is a call to fix. Forexample, to edit the method function update.default, <strong>and</strong> call the new version update.crosstabs, typethe following at the comm<strong>and</strong> line (R or S-<strong>PLUS</strong>)update.crosstabs


4F. A note about using S-<strong>PLUS</strong> MenusMany <strong>of</strong> the more common methods I will illustrate can be accomplished via the S-<strong>PLUS</strong> menus. If youwant to know what code corresponds to a particular menu comm<strong>and</strong>, issue the menu comm<strong>and</strong> <strong>and</strong> callup the History window (using the Window menu). All comm<strong>and</strong>s you have issued from menus will bethere in (gui) code form which can then be used in a comm<strong>and</strong> window or script.G. Notice <strong>of</strong> errorsAll code has been tested, but there are undoubtedly still errors. Please notify me <strong>of</strong> any errors in themanual or <strong>of</strong> easier ways to perform tasks. My email address is [email protected]. Introductions to the S LanguageThis manual assumes some working knowledge <strong>of</strong> the S language. There is not space to also describeit. Fortunately, there are many tutorials available for learning S. Some <strong>of</strong> them are listed in your User’sGuides that come with S-<strong>PLUS</strong>. Others are listed on the CRAN website for R (see Section A above).I. ReferencesAgresti, A. (2002). Categorical Data Analysis 2 nd edition. Wiley.Bishop, C. (1995). Neural Networks for Pattern Recognition. Cambridge University PressChambers, J. (1998). Programming with Data. Springer-Verlag.Chambers, J. <strong>and</strong> Hastie, T. (1992). Statistical Models in S. Chapman & Hall.Ewens, W. <strong>and</strong> Grant. G. (2001) Statistical Methods in Bioinformatics. Springer-Verlag.Gentleman, R. <strong>and</strong> Ilhaka, R. (2000). “Lexical Scope <strong>and</strong> Statistical Computing.” Journal <strong>of</strong>Computational <strong>and</strong> Graphical <strong>Statistics</strong>, 9, 491-508.Green, P. <strong>and</strong> Silverman, B. (1994). Nonparametric Regression <strong>and</strong> Generalized Linear Models,Chapman & Hall.Fletcher, R. (1987). Practical Methods <strong>of</strong> Optimization. Wiley.Harrell, F. (1998). Predicting Outcomes: Applied Survival Analysis <strong>and</strong> Logistic Regression. Unpublishedmanuscript. Now available as Regression Modeling Strategies : With Applications to LinearModels, Logistic Regression, <strong>and</strong> Survival Analysis. Springer (2001).Liao, L. <strong>and</strong> Rosen, O. (2001). “Fast <strong>and</strong> Stable Algorithms for Computing <strong>and</strong> Sampling From theNoncentral Hypergeometric Distribution." The American Statistician, 55, 366-369.Lloyd, C. (1999). Statistical Analysis <strong>of</strong> Categorical Data, Wiley.McCullagh, P. <strong>and</strong> Nelder, J. (1989). Generalized Linear Models, 2 nd ed., Chapman & Hall.Ripley B. (2002). On-line complements to Modern Applied <strong>Statistics</strong> with S<strong>PLUS</strong>(http://www.stats.ox.ac.uk/pub/MASS4).Ross, S. (1997). Introduction to Probability Models. Addison-Wesley.


5Selvin, S. (1998). Modern Applied Biostatistical Methods Using S<strong>PLUS</strong>. Oxford University Press.Sprent, P. (1997). Applied Nonparametric Statistical Methods. Chapman & Hall.Thompson, L. (1999) S-<strong>PLUS</strong> Manual to Accompany Agresti’s (1990) Catagorical Data Analysis.(http://math.cl.uh.edu/~thompsonla/5537/Splusdiscrete.PDF).Venables, W. <strong>and</strong> Ripley, B. (2000). S programming. Springer-Verlag.Venables, W. <strong>and</strong> Ripley, B. (2002). Modern Applied <strong>Statistics</strong> with S. Springer-Verlag.Wickens, T. (1989). Multiway Contingency Tables Analysis for the Social Sciences. LEA.J. AcknowledgementsThanks to Gregory Rodd <strong>and</strong> Frederico Zanqueta Poleto for reviewing the manuscript <strong>and</strong> finding manyerrors that I overlooked. Remaining errors are the sole responsibility <strong>of</strong> the author.


6Chapter 1: Distributions <strong>and</strong> Inference forCategorical DataA. Summary <strong>of</strong> Chapter 1, AgrestiIn Chapter 1, Agresti introduces categorical data, including its types, distributions <strong>and</strong> statisticalinference. Categorical variables measured on a nominal scale take values that do not have a naturalordering, whereas categorical variables measured on an ordinal scale take values that do have anordering, but not necessarily a numerical ordering. Categorical variables are sometimes called discretevariables because they only take on a discrete or countable number <strong>of</strong> values.Agresti discusses three main distributions for categorical variables: binomial, multinomial, <strong>and</strong>Poisson. The binomial distribution describes the distribution <strong>of</strong> the sum <strong>of</strong> a fixed number <strong>of</strong> independentBernoulli trials (i.e., binary outcomes), where the probability <strong>of</strong> success is fixed across trials. Themultinomial distribution extends the binomial distribution to h<strong>and</strong>le trials with possibly more than twooutcomes. The Poisson distribution describes the distribution <strong>of</strong> the number <strong>of</strong> events occurring in aspecified length <strong>of</strong> time or space, where the intervals <strong>of</strong> time or space are independent with respect to theoccurrence <strong>of</strong> an event. Formal assumptions related to the Poisson distribution (which derives from thehomogeneous Poisson process) can be found in any text on stochastic processes (see, for example,Ross, 1997). A nice explanation, however, can also be found in Ewens <strong>and</strong> Grant (2001).When observations modeled as coming from a binomial or Poisson distribution are much morevariable than that predicted by the respective theoretical distributions, the use <strong>of</strong> these distributions canlead to poor predictions because the excess variability is not considered. The prescence <strong>of</strong> this excessvariability is called overdispersion. There are options for dealing with overdispersion when it is suspectedin a data set that otherwise could be reasonably modeled using a conventional distribution like thebinomial or Poisson. One option is to incorporate r<strong>and</strong>om effects into the model (see Agresti, Chapter12). Another option is to use a distribution with a greater variance than that dictated by the binomial orPoisson (e.g., the beta-binomial distribution is a mixture <strong>of</strong> binomials with different probabilities <strong>of</strong>success).Given a particular probability distribution to describe the data, the likelihood function is theprobability <strong>of</strong> the data as a function <strong>of</strong> the parameters. The maximum likelihood estimate (MLE) is thevalue <strong>of</strong> the parameter that maximizes this function. Thus, the MLE is the value for the set <strong>of</strong> parametersthat give the observed data the highest probability <strong>of</strong> occurrence. Inference for categorical data dealswith the MLE <strong>and</strong> tests derived from maximum likelihood. Tests <strong>of</strong> the null hypothesis <strong>of</strong> zero-valuedparameters can be carried out via the Wald Test, Likelihood Ratio Test, <strong>and</strong> the Score Test. These testsare based on different aspects <strong>of</strong> the likelihood function, but are asymptotically equivalent. However, insmaller samples they yield different answers <strong>and</strong> are not equally reliable.Agresti also discusses confidence intervals derived from “inverting” the above three tests. Asmentioned on p. 13 <strong>of</strong> Agresti, “a 95% confidence interval for β is the set <strong>of</strong> β0for which the test <strong>of</strong> H 0 :β = β0has a P-value exceeding 0.05.” That is, it describes the set <strong>of</strong> β0values for which we would“keep” the null hypothesis (assuming a significance level <strong>of</strong> 0.05).The chapter concludes with illustration <strong>of</strong> statistical inference for binomial <strong>and</strong> multinomialparameters.B. Discrete Probability Distributions in S-<strong>PLUS</strong> <strong>and</strong> RS-<strong>PLUS</strong> <strong>and</strong> R come with support for many built-in probability distributions, <strong>and</strong> support for manyspecialized distributions can be downloaded from statlib or from the CRAN website (see IntroductionSection). Each distribution has functions for obtaining cumulative probabilities, density values, quantiles,<strong>and</strong> realizations <strong>of</strong> r<strong>and</strong>om variates. For example, in both S-<strong>PLUS</strong> <strong>and</strong> Rdbinom(x, size, prob) computes the density <strong>of</strong> the indicated binomial distribution at xpbinom(x, size, prob) computes the cumulative density <strong>of</strong> the indicated binomial distribution


8C. Proportion <strong>of</strong> Vegetarians (Statistical Inference for Binomial Parameters)An example <strong>of</strong> the use <strong>of</strong> the S-<strong>PLUS</strong> distribution functions comes from computing the asymptotic<strong>and</strong> exact confidence intervals on the proportion <strong>of</strong> vegetarians (p. 16). In a questionnaire given to anintroductory statistics class (n = 25), zero students said they were vegetarians. Assuming that thenumber responding yes is distributed binomially with success probability π , what is a 95% confidenceinterval for π , given that a point estimate, ˆ π , is 0? Agresti cites three approximate methods <strong>and</strong> twoexact methods. The approximate methods are given first.1. Approximate Confidence Intervals on π1) Inverting the Wald Test (AKA Normal Approximation)ˆ πwhich is computed quite easily in S-<strong>PLUS</strong> “by h<strong>and</strong>”.zα /2ˆ π (1 − ˆ π )n± (1.1)phat


9library(Hmisc, T)binconf(x=0, n=25, alpha=.05, method="wilson")PointEst Lower Upper0 1.202937e-017 0.13319233) A 95% likelihood-ratio (LR) confidence interval is the set <strong>of</strong> π0for which the likelihood ratio test has ap-value exceeding the significance level, α . The expression for the LR statistic is simple enough so thatwe can find the upper confidence bound analytically. However, we could have obtained it using uniroot(both R <strong>and</strong> S-<strong>PLUS</strong>) or using nlmin (S-<strong>PLUS</strong> only). These functions can be used to solve an equation,i.e., find its zeroes. uniroot is only for univariate functions. nlmin can also be used to solve a system<strong>of</strong> equations. A function doing the same in R would be function nlm or function nls (for nonlinear leastsquares) in library nls.Using uniroot we set up the LR statistic as a function, giving as first argument the parameter for whichwe want the confidence interval. The remaining arguments are the observed successes, the total, <strong>and</strong> thesignificance level. uniroot takes an argument called interval that specifies where we want to searchfor the root. There cannot be a singularity at either <strong>of</strong> these values. Thus, we cannot use 0 <strong>and</strong> 1.LR


10[1] T$conv.type:[1] "x convergence"2. Exact Confidence Intervals on πThere are several functions available for computing exact confidence intervals in R <strong>and</strong> S-<strong>PLUS</strong>. In R,the function binom.test computes a Clopper-Pearson interval. But, the same function in S-<strong>PLUS</strong>doesn’t give confidence intervals, at least not automatically.binom.test(x=0, n=25, conf.level=.95)# RExact binomial testdata: 0 <strong>and</strong> 25number <strong>of</strong> successes = 0, number <strong>of</strong> trials = 25, p-value = 5.96e-08alternative hypothesis: true probability <strong>of</strong> success is not equal to 0.595 percent confidence interval:0.0000000 0.1371852sample estimates:probability <strong>of</strong> success0The function binconf in the Hmisc library also gives Clopper-Pearson intervals via the use <strong>of</strong> the“exact” method.library(Hmisc, T)binconf(x = 0, n = 25, alpha = .05, method = "exact") # S<strong>PLUS</strong>PointEst Lower Upper0 0 0.1371852In addition, several individuals have written their own functions for computing Clopper-Pearson as well asother types <strong>of</strong> approximate intervals besides the normal approximation interval. A search <strong>of</strong> “exactbinomial confidence interval” on the “S-news” search page (http://lib.stat.cmu.edu/cgi-bin/iform?SNEWS)gave several user-made functions.The improved confidence interval described in Blaker (2000, cited in Agresti) can be computed in S-<strong>PLUS</strong>using the following function, which is a slight modification <strong>of</strong> the function appearing in the Appendix <strong>of</strong> theoriginal reference.acceptbin


11}lower


12The S-<strong>PLUS</strong> function chisq.test is used for situations where one has a 2x2 cross-tabulation(preferably computed using the function table) or two factor vectors, <strong>and</strong> wants to test for independence<strong>of</strong> the row <strong>and</strong> column variables.F. Likelihood Ratio Chi-Squared StatisticThe likelihood ratio chi-squared statistic is easily computed “by h<strong>and</strong>” for multinomial frequencies. Usingvectorized calculations, G 2 is for Mendel’s dataobs


13the φ1parameter using the scale argument so that when p[1] is subtracted from p[3], we should alwaysget a positive value. I have also started φ1at a value much larger than λ1.obj.function


14[1] 5.191676e-007$iterations:[1] 22We get the same result. One could supply the Hessian using the function deriv3. The Mass librarysupplies a function called vcov.nlminb to extract the inverse <strong>of</strong> observed information (or a finitedifference approximation if the Hessian is not supplied) from an nlminb object.In R, we can use the function nlm, which is similar in that it minimizes an objective function <strong>of</strong> severalvariables, allows boundary constraints, <strong>and</strong> uses a quasi-Newton optimizer. However, it does not allowbox constraints on the parameters.obj.res


15}attr(obj.res(la1, la2, pi1, y1, y2, y3, y4), "gradient")optim(par=c(11, 62, 2*(11+4)), fn=obj.function, gr=obj.gr, lower=c(0,0,0),method="L-BFGS-B", control=list(parscale=c(1,1,10)), y=c(11, 62, 4, 7))$par[1] 14.35235 57.89246 19.57130$value[1] -216.6862$countsfunction gradient25 25$convergence[1] 0$message[1] "CONVERGENCE: REL_REDUCTION_OF_F


16Chapter 2: Describing Contingency TablesA. Summary <strong>of</strong> Chapter 2, AgrestiChapter two in Agresti introduces two-way I × J contingency tables. If both the row <strong>and</strong> column<strong>of</strong> a table denote r<strong>and</strong>om variables, then the probabilities { π ij} define the joint distribution <strong>of</strong> the twovariables. The marginal distributions are denoted by { πi +} for the row variable <strong>and</strong> { π + j} for the columnvariable. For a fixed value i <strong>of</strong> the row variable, the column variable has the conditional distribution{ π ,..., π } . The conditional distribution is especially important if the row variable is fixed by design1| i J|i<strong>and</strong> not free to vary for each observation.With diagnostic tests for a disease, the sensitivity <strong>of</strong> the test is the conditional probability that thediagnostic test is positive given that subject has the disease. The specificity <strong>of</strong> the test is the conditionalprobability that the test is negative given that the subject does not have the disease. In a 2x2 table withrows indicating disease status (yes, no) <strong>and</strong> columns indicating test result (positive, negative), thesensitivity is π1|1, <strong>and</strong> the specificity is π2|2.Row <strong>and</strong> column variables are independent if the conditional distribution <strong>of</strong> the column variablegiven the row variable is the same as the marginal distribution <strong>of</strong> the column variable (<strong>and</strong> vice versa).That is, πji |= π + jfor i = 1,…, I, <strong>and</strong> πij |= πi +j = 1,…, J. Equivalently, if all joint probabilities equal theproduct <strong>of</strong> their marginal probabilities: πij= πi +π + j, for all i <strong>and</strong> j. Thus, when the two variables areindependent, knowledge <strong>of</strong> the value <strong>of</strong> the row variable does not change the distribution <strong>of</strong> the columnvariable, <strong>and</strong> vice versa.When the row variable is an explanatory variable <strong>and</strong> the column is a response variable, thenthere is no joint distribution, <strong>and</strong> independence is referred to as homogeneity <strong>of</strong> the conditionaldistributions <strong>of</strong> the column variable given a value for the row variable.Y differ depending on how sampling was done. IfThe distributions <strong>of</strong> the cell counts { } ijobservations are to be collected over a certain period <strong>of</strong> time <strong>and</strong> cross-classified into one <strong>of</strong> the I × Jcategories, then a Poisson sampling model might be used where cell counts are treated as independentPoisson r<strong>and</strong>om variables with parameters { µ ij}. If the total sample size <strong>of</strong> observations is fixed inadvance (e.g., in a cross-sectional study), then a multinomial sampling model might be used where cell. If the row totalscounts are treated as multinomial r<strong>and</strong>om variables with index n <strong>and</strong> probabilities { π ij}are fixed in advance, perhaps as fixed-size r<strong>and</strong>om samples drawn from specific populations that are tobe compared, as in prospective studies, then a product multinomial sampling model may apply where foreach i, the counts { Yji}| have a multinomial distribution with index ni<strong>and</strong> probabilities πji |j = 1,…, J . Ifboth row <strong>and</strong> column totals are fixed by design, then a hypergeometric sampling distribution applies forthe cell counts.However, there are times when certain sampling models are assumed, but sampling was actuallydone differently. For example, when the row variable is an explanatory variable, product multinomialsampling model may be used even though the row totals were not actually fixed in advance. Also, thePoisson model is used even when the total sample size is fixed in advance (see Chapter 8 <strong>of</strong> Agresti).Section 2.2 discusses comparing two proportions from two samples, including the difference <strong>of</strong>proportions, relative risk, <strong>and</strong> odds ratio. The relative risk compares the proportions <strong>of</strong> the two groups ina ratio. If the rows <strong>of</strong> a 2x2 table represent groups <strong>and</strong> the columns represent a binary response, thenthe relative risk <strong>of</strong> a positive response is the ratio π 1|1π 1|2. A relative risk <strong>of</strong> 1.0 corresponds toindependence. A relative risk <strong>of</strong> C means that π 1|1= Cπ1|2. The odds ratio is the ratio <strong>of</strong> odds <strong>of</strong> apositive response by group


17π1|1 (1 −π1|1 ) π11π22θ = =(2.1)π1|2 (1 −π1|2 ) π12π21θ = , the row <strong>and</strong> column variables are independent. Values <strong>of</strong> θ farther from 1.0 in a givenθ = , the oddsWhen 1direction represent stronger association. For example, as on p. 45 <strong>of</strong> Agresti, when 0.25<strong>of</strong> “success” in group 1 (row 1) are 0.25 times the odds in group 2 (row 2), or equivalently, the odds <strong>of</strong>success in group 2 are 4 times the odds in group 1. The odds ratio can be used with a joint distribution <strong>of</strong>the row <strong>and</strong> column variables too. Indeed, it can be used with prospective (rows totals fixed),retrospective (column totals fixed), <strong>and</strong> cross-sectional designs. Finally, if the rows <strong>and</strong> columns areinterchanged, the value <strong>of</strong> the odds ratio does not change. The sample odds ratio uses the observedsample counts, nij.Odds ratios can be computed for retrospective sampling designs (case-control studies). Relativerisk cannot be computed because the outcome variable is fixed by design. However, if the probability <strong>of</strong>the outcome is close to zero for both groups, then the odds ratio can be used to approximate relative riskusing the formula on p. 47 <strong>of</strong> Agresti.In observational studies, confounding variables can be controlled with stratification orconditioning. The association between two variables X <strong>and</strong> Y given that another measured variable Ztakes the value z is called a conditional association. The 2 x 2 table resulting from cross-classifying allobservations with Z = z by their X <strong>and</strong> Y values is called a partial table. If Z is ignored, the X-Y table iscalled a marginal table. Simpson’s Paradox is the result that a marginal association can have a differentdirection than a conditional association. For example, in the death penalty example on p. 49-51, ignoringvictim’s race, the death penalty (Y) is more likely for whites than for blacks (X). However, conditioning onvictim’s race (either black or white), the death penalty is more likely for blacks than for whites. Theparadox in this case can be explained by the strong association between victim’s race (ignored in themarginal association) <strong>and</strong> defendant’s race <strong>and</strong> that between victim’s race <strong>and</strong> the death penalty. Thedeath penalty was more likely when the victims were white (regardless <strong>of</strong> defendant race). Also, whiteswere more likely to kill whites than any other victim/defendant race combination in the sample. So, thereare a lot <strong>of</strong> whites receiving the death penalty in the sample. On the other h<strong>and</strong>, blacks were more likelyto kill blacks. Thus, there are fewer blacks receiving the death penalty in the sample. But, if we look atonly white victims, there are relatively more blacks receiving the death penalty than whites. The same istrue for black victims. An unmodeled association between victim’s <strong>and</strong> defendant’s race hides thisconclusion.Does Simpson’s Paradox imply that we should distrust all contingency table analyses? After all,there are undoubtedly unmeasured variables that could be potential conditioning variables in allcontingency tables. Could these variables change the direction <strong>of</strong> marginal associations? Page 51 inAgresti paraphrases J. Cornfield’s result “that with a very strong XY association [marginal association], avery strong association must exist between the confounding variable Z <strong>and</strong> both X <strong>and</strong> Y in order for theeffect to disappear or change …”.For I x J x K tables (where X has I levels, Y has J levels, <strong>and</strong> Z has K levels), if X <strong>and</strong> Y areindependent in partial table k, then X <strong>and</strong> Y are conditionally independent given that Z takes on value k. IfX <strong>and</strong> Y are independent at all levels <strong>of</strong> Z, then X <strong>and</strong> Y are conditionally independent given Z. Or, X <strong>and</strong>Y only depend on each other through Z. Once variability in Z is removed, by fixing it, X <strong>and</strong> Y are nolonger related statistically. Conditional independence does not imply marginal independence. For 2 x 2 xK tables, X <strong>and</strong> Y are conditionally independent given Z if the odds ratio between X <strong>and</strong> Y equals 1 ateach category <strong>of</strong> Z. For the general case <strong>of</strong> I x J x K tables, independence is equivalent to all( I −1)( J −1)local odds ratios equaling 1.0.An analogy to no three-way interaction in ANOVA is homogeneous association. A 2 x 2 x K tablehas homogeneous XY association if the conditional odds ratios comparing two categories <strong>of</strong> X to twocategories <strong>of</strong> Y are the same at each level <strong>of</strong> Z. When interaction exists, the conditional odds ratio forany pair <strong>of</strong> variables (say X <strong>and</strong> Y) changes across categories <strong>of</strong> the third (say Z), wherein the thirdvariable is called an effect modifier because the effect <strong>of</strong> X on Y (the response) changes depending onthe level <strong>of</strong> Z. For the general case <strong>of</strong> I x J x K tables, homogeneous XY association means that anyconditional odds ratio formed using two categories <strong>of</strong> X <strong>and</strong> two categories <strong>of</strong> Y is the same at eachcategory <strong>of</strong> Z.


18The chapter concludes with discussion <strong>of</strong> summary measures <strong>of</strong> association for nominal <strong>and</strong>ordinal data. The measures described include Kendall <strong>and</strong> Stuart’s measure <strong>of</strong> proportional reduction invariance from the marginal distribution <strong>of</strong> the response to the conditional distributions given the value <strong>of</strong>an explanatory vector, Theil’s uncertainty coefficient, the proportional reduction in entropy (or uncertainty)in response given explanatory variables, <strong>and</strong> measures for ordinal association such as concordance <strong>and</strong>Gamma.B. Comparing two proportionsThe Aspirin <strong>and</strong> Heart Attacks example is used to illustrate several ways to compare proportions betweentwo groups. The two groups are aspirin-takers <strong>and</strong> placebo-takers. Fatal <strong>and</strong> non-fatal heart attacks arenot distinguished. The data are in Table 2.1 on p. 37 <strong>of</strong> Agresti. Setting up the data is easy:x


19temp$estimate[1]-temp$estimate[2][1] -0.007706024Other useful quantities are easily computed. Here, I calculate the relative risk <strong>and</strong> odds ratio using theobject temp above, as well as the original data vectors:Relative risk:temp$estimate[2]/temp$estimate[1][1] 1.817802Odds ratio (simple enough not to need temp):x[2]*(n[1]-x[1])/(x[1]*(n[2]-x[2]))[1] 1.832054C. Partial Association in Stratified 2 x 2 TablesThe Death Penalty example is given on p. 48 <strong>of</strong> Agresti to illustrate partial tables <strong>and</strong> conditional oddsratios. The effect <strong>of</strong> the defendant’s race (X) on the death penalty verdict (Y) is studied, treating thevictim’s race (Z) as a control variate. The 2 x 2 x 2 table can be created using the function crosstabs inS-<strong>PLUS</strong> (xtabs in R). The function print.crosstabs can also be called directly with output fromcrosstabs to control what is actually printed after the call.vic.race


20victim=blackdefendant|death|yes |no |RowTotl|-------+-------+-------+-------+white | 0 | 16 |16 ||0 |1 |0.1 ||0 |0.1 | ||0 |0.024 | |-------+-------+-------+-------+black | 4 |139 |143 ||0.028 |0.97 |0.9 ||1 |0.9 | ||0.0059 |0.21 | |-------+-------+-------+-------+ColTotl|4 |155 |159 ||0.025 |0.97 | |-------+-------+-------+-------+Test for independence <strong>of</strong> all factorsChi^2 = 419.5589 d.f.= 4 (p=0)Yates' correction not usedSome expected values are less than 5, don't trust stated p-valueThe differences in R are enough so that the above is worth repeating in R. Below is the same result(including set up) from R’s xtabs. Note that the fac.design has been replaced by exp<strong>and</strong>.grid.The output is also much more terse by default.vic.race


21Getting the marginals can be done by re-calling the function without victim, or one can use the updatefunction to update the call by subtracting victim from the formula. update is a generic function forwhich methods are written for different classes <strong>of</strong> objects. There is no method for class crosstabs, so acall to update via the set <strong>of</strong> comm<strong>and</strong>s:temp


22|0.079 |0.64 | |-------+-------+-------+-------+black | 15 |176 |191 ||0.079 |0.92 |0.28 ||0.22 |0.29 | ||0.022 |0.26 | |-------+-------+-------+-------+ColTotl|68 |606 |674 ||0.1 |0.9 | |-------+-------+-------+-------+Test for independence <strong>of</strong> all factorsChi^2 = 1.468519 d.f.= 1 (p=0.2255796)Yates' correction not usedwhich is the bottom portion <strong>of</strong> Table 2.6 in Agresti.In R, we also create an update method for xtabs, which is a function that we will call update.xtabs.This will be the same as update.default, with the following substitutions:Change the linecall


23prop. defendant = white0.0 0.2 0.4 0.6 0.8 1.0whiteblackwhiteblackyesdeathnoIn the body <strong>of</strong> the plot are the segments for white <strong>and</strong> black victims. The sizes <strong>of</strong> the circles connectedby segments represent the relative count at that particular cell <strong>of</strong> the table. The dotted segmentrepresents the marginal table (not conditioning on race <strong>of</strong> victim). It shows that the proportion <strong>of</strong> whitedefendants is higher for the death penalty group than for those who did not get the death penalty.From conditional associations, however, we see that a white victim is much more likely to have a whitedefendant than a black defendant (due to the height <strong>of</strong> the “white” segment), <strong>and</strong> a black victim is muchmore likely to have a black defendant. Those who receive the death penalty are more frequently thosewho had white victims than those who had black victims (see the circles for the “yes” column).D. Conditional Odds RatiosAs the objects returned by xtabs in R <strong>and</strong> crosstabs in S-<strong>PLUS</strong> are already arrays, we can just useapply on the 2D slices representing the conditional tables.apply(temp,3,function(x) x[1,1]*x[2,2]/(x[2,1]*x[1,2]))white black0.4306105 0.0000000


24The R package vcd has a function oddsratio, which computes conditional odds ratios for 2x2x… tables,along with asymptotic confidence intervals. If we use it on the death penalty table to get odds ratioswithin victim’s race, we getsummary(oddsratio(temp, log=F, stratum=3))Odds Ratio lwr uprwhite 0.4208843 0.20498745 0.8641678black 0.9393939 0.04838904 18.2367947Note that the odds ratio for black victims is noticeably larger than that computed using apply. Thefunction oddsratio adds 0.5 to each count prior to computing the odds ratios. The estimate near 1.0implies that for black victims, the odds <strong>of</strong> getting the death penalty are equal for white <strong>and</strong> blackdefendants.A plot method is also available for oddsratio (plot.oddsratio), which may be quite useful with a largetable.E. Summary Measures <strong>of</strong> Assocation: Ordinal TrendsAn example to illustrate ordinal measures <strong>of</strong> association comes from the income <strong>and</strong> job satisfaction datain Table 2.8 (p. 57, Agresti). The respondents are black males in the U.S. Four ordinal classificationsconstitute the response categories for both variables (see the table). We might postulate that as incomeincreases, reported job satisfaction increases as well, <strong>and</strong> vice versa. Measures analogous to correlationmeasure the degree <strong>of</strong> monotonic relationship between the two variables. Agresti uses a concordancemeasure to quantify the association.A pair <strong>of</strong> individuals in the sample is concordant if the individual who is ranked higher (among the twoindividuals) on one variable, say X, also ranks higher on the second variable, say Y. The pair isdiscordant if the individual ranking higher on X ranks lower on Y. The pair is tied if the individuals havethe same classification on X <strong>and</strong>/or Y. If the number <strong>of</strong> concordant pairs, say C, exceeds the number <strong>of</strong>discordant pairs, say D, then a positive monotonic relationship is supported. A statistic that is based onthis difference is Goodman <strong>and</strong> Kruskal’s Gamma. It is the estimated difference between the probability<strong>of</strong> concordance <strong>and</strong> the probability <strong>of</strong> discordance, given that a pair is untied. Gamma, γ , is estimatedas( C − D)ˆ γ =(2.2)( C + D)Gamma measures monotonic association between two variables, with range −1≤≤ 1. Positive <strong>and</strong>negative associations have the corresponding sign changes in ˆ γ . Perfect monotonicity in the sample( ˆ γ = 1 ) occurs when there are either no discordant pairs (D = 0: ˆ γ = 1 ) or there are no concordantpairs (C = 0: ˆ γ =− 1 ).One can create a cross-classified table in S-<strong>PLUS</strong> out <strong>of</strong> Table 2.8 using the following comm<strong>and</strong>s (usextabs in R instead <strong>of</strong> crosstabs):income


25Call:crosstabs(formula = count ~ income + jobsat, data = table.2.8)96 cases in table+----------+|N ||N/RowTotal||N/ColTotal||N/Total |+----------+income |jobsat|VD |LD |MS |VS |RowTotl|-------+-------+-------+-------+-------+-------+40000 | 0 | 1 | 9 |11 |21 ||0 |0.048 |0.43 |0.52 |0.22 ||0 |0.077 |0.21 |0.31 | ||0 |0.01 |0.094 |0.11 | |-------+-------+-------+-------+-------+-------+ColTotl|4 |13 |43 |36 |96 ||0.042 |0.14 |0.45 |0.38 | |-------+-------+-------+-------+-------+-------+Test for independence <strong>of</strong> all factorsChi^2 = 5.965515 d.f.= 9 (p=0.7433647)Yates' correction not usedSome expected values are less than 5, don't trust stated p-valueHere is a function for computing Goodman <strong>and</strong> Kruskal’s gamma. There is a different version <strong>of</strong> thisfunction in Chapter 3 <strong>of</strong> this manual (called Gamma2.f). This version uses the computations from problem3.27 in Agresti, <strong>and</strong> also computes the st<strong>and</strong>ard error <strong>of</strong> Gamma. It is faster than Gamma.f.Gamma.f


26We can use this on table 2.8 by just inputting the result <strong>of</strong> the crosstabs call above:Gamma.f(temp)$gamma:[1] 0.2211009$C:[1] 1331$D:[1] 849Selvin (1998) computes the number <strong>of</strong> concordant <strong>and</strong> discordant pairs using the outer function alongwith ifelse statements (Selvin, p. 339). However, the procedure is memory intensive. The functionabove takes between 0.33 <strong>and</strong> 0.60 CPU seconds on a Pentium 4, 1.4 GHz, with 512 MB RAM.Other measures <strong>of</strong> association can be computed immediately from the chi-square value output fromchisq.test (e.g., phi, Cramer’s V, Cramer’s C). See Selvin p. 336ff for more details. The R packagevcd has a function assoc.stats that computes these association measures along with the Pearson <strong>and</strong>LR chi-square tests. For example, on the job satisfaction data (where we used xtabs instead <strong>of</strong>crosstabs),summary(assoc.stats(temp))jobsatincome VD LD MS VS40000 0 1 9 11X^2 df P(> X^2)Likelihood Ratio 6.7641 9 0.66167Pearson 5.9655 9 0.74336Phi-Coefficient : 0.249Contingency Coeff.: 0.242Cramer's V : 0.144A nice method for an object <strong>of</strong> class crosstabs is the “[“ method. This allows us to select smaller tablesin the following way. Suppose I want to print Table 2.8 with the last category <strong>of</strong> the income variableeliminated. This istemp[1:3,1:4]Call:crosstabs(formula = count ~ income + jobsat, data = table.2.8)75 cases in table+----------+|N ||N/RowTotal||N/ColTotal||N/Total |+----------+income |jobsat|VD |LD |MS |VS |RowTotl|-------+-------+-------+-------+-------+-------+


|0.25 |0.23 |0.23 |0.17 | ||0.01 |0.031 |0.1 |0.062 | |-------+-------+-------+-------+-------+-------+15000-2| 2 | 3 |10 | 7 |22 ||0.091 |0.14 |0.45 |0.32 |0.29 ||0.5 |0.23 |0.23 |0.19 | ||0.021 |0.031 |0.1 |0.073 | |-------+-------+-------+-------+-------+-------+25000-4| 1 | 6 |14 |12 |33 ||0.03 |0.18 |0.42 |0.36 |0.44 ||0.25 |0.46 |0.33 |0.33 | ||0.01 |0.062 |0.15 |0.12 | |-------+-------+-------+-------+-------+-------+ColTotl|4 |12 |34 |25 |75 ||0.053 |0.16 |0.45 |0.33 | |-------+-------+-------+-------+-------+-------+Test for independence <strong>of</strong> all factorsChi^2 = 1.432754 d.f.= 6 (p=0.9638357)Yates' correction not usedSome expected values are less than 5, don't trust stated p-value27


28Chapter 3: Inference for Contingency TablesA. Summary <strong>of</strong> Chapter 3, AgrestiThis chapter discusses interval estimation <strong>and</strong> testing for two-way contingency tables, bothunordered <strong>and</strong> ordered. Confidence intervals for association parameters like the odds ratio, difference inproportions, <strong>and</strong> relative risk for 2x2 tables can be computed using large-sample normality (Wolf’sprocedure). Score <strong>and</strong> pr<strong>of</strong>ile likelihood confidence intervals are better alternatives, especially for smallersample sizes.For an I x J contingency table, the hypothesis that the row <strong>and</strong> column variables are independentmay be tested using a chi-square test (either likelihood ratio statistic or Pearson’s chi-squared statistic).Although the two statistics are asymptotically equivalent <strong>and</strong> asymptotically chi-squared, Pearson’s chisquaredstatistic tends to be better for smaller counts. If the row <strong>and</strong> column variables are ordered, thena trend may be used as the alternative hypothesis. Chi-squared tests can be followed up by residualanalysis <strong>and</strong> chi-squared tests on partitioned tables to indicate exactly where association lies. Rules forpartitioning likelihood ratio chi-squared tests appear on p. 84 <strong>of</strong> Agresti.If independence <strong>of</strong> the two classification factors <strong>of</strong> an I x J contingency table approximatelydescribes the table, then the MLEs <strong>of</strong> the cell probabilities under the hypothesis <strong>of</strong> independence canhave lower MSE than can the MLEs under the alternative (i.e., the sample proportions), except for whenthe sample size is very large. This is because the variance <strong>of</strong> the MLEs under independence is smallerbecause they are based on fewer parameters. However, they are biased. The bias can dominate thevariance when the sample size increases, thereby causing the independence MLEs to lose theiradvantage over the sample proportions. Agresti gives details on p. 85-86.If row <strong>and</strong> column variables are ordered, then a test <strong>of</strong> independence that has a trend alternativehas greater power to detect a true association (if the association is a trend <strong>of</strong> that type) than would ageneral-purpose chi-squared test. One reason for this is due to the fewer degrees <strong>of</strong> freedom for thetrend statistics versus the chi-squared statistics, as there are fewer parameters to estimate for the trendstatistics.There is a disadvantage in using trend tests with ordinal level variables because one has tochoose the scores for the levels <strong>of</strong> each <strong>of</strong> the variables. An inappropriate choice can downplay anassociation, making the test less sensitive to it. Agresti shows that the popular choice to use categorymidranks as scores can affect sensitivity <strong>of</strong> test statistics if the cell sizes are quite disparate. Asuggestion is to use equally-spaced scores unless there is an inherent numerical order in the levelsalready.When either the row or column variable has two categories, then the tests based on linear ormonotone trend that are discussed in Section 3.4 <strong>of</strong> Agresti reduce to certain well-known nonparametrictests. See p. 90 in Agresti. However, some care is needed when using midranks for the categories <strong>of</strong>the response variable (reducing to Wilcoxon Mann-WhitneyTest), as there may be very many ties (seeSprent, 1997).With a small total sample size, the exact tests <strong>of</strong> Section 3.5 are preferred over analogous largesampletests. Fisher’s Exact Test is for testing independence <strong>of</strong> row <strong>and</strong> column variables in a 2x2 table.The same ideas apply for an I x J table, though the computing time is increased. The test assumes fixedrow <strong>and</strong> column totals, but as these are sufficient statistics for the row <strong>and</strong> column probabilities, whichdetermine the null distribution, the test can be applied even if row <strong>and</strong> column totals are not fixed bydesign. If marginal totals were not fixed by design, an alternative is to use the “unconditional” test <strong>of</strong>Section 3.5.5, which fixes row totals only <strong>and</strong> assumes independent binomial row samples. Anunconditional test may have more power than Fisher’s Exact Test because the null distribution is lessdiscrete, allowing more values for the p-value to assume. Thus, one is not forced to be as conservativeas with a conditional test.Small-sample “exact” confidence intervals for the odds ratio for a 2x2 table can be computedusing Cornfield’s tail method. However, if the data are highly discrete, then Agresti suggets anadjustment to the interval using the mid-p-value.


29B. Confidence Intervals for Association ParametersWald confidence intervals, score intervals, <strong>and</strong> pr<strong>of</strong>ile likelihood intervals are illustrated for theAspirin <strong>and</strong> Myocardial Infarction example on p. 72 in Agresti. We briefly describe each type <strong>of</strong> interval.1. Wald Confidence IntervalsSuppose the statistic T n is used to estimate an unknown parameter θ = ET ( n). Wald confidenceintervals are based on asymptotic normality <strong>of</strong> the st<strong>and</strong>ardized statistic, ( T ) n −θse( T). Inverting this statisticgives the 100(1− α )% confidence interval on θ , Tn± zα/2σ ( Tn), where z α /2is the 100(1 − α /2) thquantile <strong>of</strong> the st<strong>and</strong>ard normal distribution, <strong>and</strong> σ ( ) is the st<strong>and</strong>ard deviation <strong>of</strong> T n . An estimate <strong>of</strong>T nσ ( ) is given by the delta method (see Section 3.1.5 in Agresti), with sample statistics inserted whereparameters are required (e.g., replace cell probabilities with sample proportions). For the odds ratio (θ ),difference in proportions ( π1 − π2), <strong>and</strong> relative risk ( π1 π2) in a 2x2 table (assuming independentbinomial distributions for each <strong>of</strong> the two samples), the st<strong>and</strong>ard errors estimated using this method are:T n−1 −1 −1 −1( ) 1/211 12 21 22ˆ(log σ ˆ θ) = n + n + n + n(3.1)<strong>and</strong>ˆ1(1 ˆ1) ˆ2(1 ˆ2)ˆ ˆ ˆ⎡π −π π −π⎤σπ (1− π2)=⎢−⎣ n1 n ⎥2 ⎦ˆ σ ( log ˆ π ˆ π ) = ⎡(1 − ˆ π ) ˆ π n + (1 − ˆ π ) ˆ π1/2−1 −1 −1 −11 2 1 1 1 2 2 21/2(3.2)⎣ n ⎤⎦ (3.3)A Wald confidence interval on either the log odds ratio or the log ratio is more stable than oneconstructed for the respective ratios, themselves. Take the antilog <strong>of</strong> the interval endpoints to get aconfidence interval on the ratio.We can apply these formulae to the Aspirin <strong>and</strong> Myocardial Infarction data easily using arithmeticoperations in S. However, sometimes a function is useful. The function Wald.ci below computesasymptotic confidence intervals for the proportion difference, odds ratio, <strong>and</strong> relative risk.To set up the data, we first do so as a data frame, then use the design.table function (S-<strong>PLUS</strong>) tochange it to an array.Drug


30We apply the function Wald.ci below to get the confidence intervals.Wald.ci


31So, the death rate for the placebo group ranges from 0.85 to 2.85 times that for the aspirin group (oddsratio confidence interval). As the interval for the odds ratio covers 1.0 <strong>and</strong> somewhat below 1.0, there isstill a small chance that aspirin has slight negative effects. The intervals on the relative risk <strong>and</strong>difference <strong>of</strong> proportions give similar conclusions.Note that the relative risk estimate is very close to that <strong>of</strong> the odds ratio. This is not surprising becausethe sample proportion <strong>of</strong> deaths from myocardial infarction were small in both groups. The relationshipbetween the odds ratio <strong>and</strong> relative risk (see p. 47 <strong>of</strong> Agresti) indicates that the two will be similar in thiscircumstance.The same code works in R with minor changes. R does not have the design.table function (althoughwith the change <strong>of</strong> one line, you can source it, as well as factor.names into R. Contact me for the onelinechange). However, a glance at the function design.table in S-<strong>PLUS</strong> shows that the “workhorse” isthe tapply function. So, just change the last line as follows:Drug


32minimizer <strong>of</strong> L( 1, 2| y1, y2, n1, n2)that the gradients <strong>of</strong> the two functions L <strong>and</strong> c are related as follows:π π (see for example Fletcher, 1987). It can be shown that this implies∇ L( π , π | y , y , n, n ) = λ∇ c( π , π )(3.5)1 2 1 2 1 2 1 2implying that to find a local minimizer (note that we will negate L later to find a maximizer) we must solve(3.5) <strong>and</strong> also the equation c( π1, π2)= 0. Thus, we must solve a system <strong>of</strong> three equations in threeunknowns ( π1, π2, λ ) , then discard the solution for the constant λ (called the Lagrange multiplier).The function f below is the left-h<strong>and</strong> side <strong>of</strong> the system <strong>of</strong> equations we want to solve. This is a functionthat returns a three-dimensional S vector whose first two values represent the gradient <strong>of</strong> (the negative<strong>of</strong>) the Lagrangian, with respect to ( π1, π2), with values for y i <strong>and</strong> n i given by the values in Table 3.1(Agresti). The third value in the vector is c( π1, π2).f


33The same idea can be carried out with R, using, for example, the optim function. Then, the followingcomm<strong>and</strong>s produce a score confidence interval. This time, the function g (here called gfun) is directlyused instead <strong>of</strong> just existing within solveNonlinear. Also, we control the step sizes (ndeps) for thecomputation <strong>of</strong> the finite difference gradient to be much smaller than the default (see also, the R packageBhat with function dfp to possibly get around this). And, we use a set <strong>of</strong> Deltas much narrower in orderto get better accuracy without needing much more computation time.gfun


34However, we still must type in the log likelihood as a function <strong>of</strong> θ , π1 +, <strong>and</strong> π + 1. One can find thenecessary transformations very easily using a computer algebra system like Mathematica (2001)..muchmore readily than using S!. These are then copied almost “word” for “word” into the following function,nlogf:library(Bhat)# neg. log-likelihood <strong>of</strong> "new" multinomial modelnlogf


35trying upper bound ------------------------starting at: 0.9856728initial guess: 0.5029411 0.03382424 1.153263begin Newton-Raphson search for pr<strong>of</strong>ile lkh conf. bounds:eps value for stop criterium: 0.001nmax : 10CONVERGENCE: 6 iterationschisquare value is: 3.841459confidence bound <strong>of</strong> theta is 0.8617511log derivatives: 2.122385e-06 6.523067e-06label estimate log deriv log curv1 p1p 0.502946 2.12238e-06 339.3352 pp1 0.0336989 6.52307e-06 41.85023 theta 0.861751 6.44456 10.8561[1] 0.8617511 2.8960895Thus, the pr<strong>of</strong>ile likelihood based confidence interval for the odds ratio is: (0.862, 2.896), fairly close tothe Wald interval.C. Testing Independence in Two-way Contingency TablesFor multinomial sampling with probabilities { π ij} in an I x J contingency table, the null hypothesis <strong>of</strong>statistical independence <strong>of</strong> the row <strong>and</strong> column variables is H : 0πij = πi +πj +for all i <strong>and</strong> j. Pearson’schi-squared statistic can be used to test this hypothesis. The expected frequencies in an I x Jcontingency table under H 0 are µij= nπij = nπi +πj +, with MLEs given by ˆijµ = n i +n j +n. Then, thechi-squared statistic, X 2 , is given on p. 78 <strong>of</strong> Agresti.The score test uses Pearson’s chi-squared statistics. But, as mentioned in Agresti, the likelihood ratiotest uses a different “chi-squared statistic” (they are both called chi-squared statistics because they bothhave asymptotic chi-squared distribution under the null hypothesis <strong>of</strong> independence). The likelihood ratiochi-squared statistic is given on p. 79 <strong>of</strong> Agresti. The degrees <strong>of</strong> freedom for each asymptotic chisquareddistribution is (I − 1)(J − 1). The limiting distributions apply when the total sample size is large<strong>and</strong> the number <strong>of</strong> cells is fixed. Agresti p.79-80 gives further details.Agresti uses data on religious fundamentalism <strong>and</strong> degree <strong>of</strong> education to illustrate the two different chisquaredtests <strong>of</strong> independence. The data are in Table 3.2 (p.80).First, I set up the data in S-<strong>PLUS</strong>:religion.counts


36Pearson's chi-square test without Yates' continuity correctiondata: design.table(table.3.2)X-square = 69.1568, df = 4, p-value = 0To obtain the expected frequencies, we take advantage <strong>of</strong> the outer function, as well as the functionsrowSums <strong>and</strong> colSums:expected.freqs


37the rules <strong>of</strong> thumb required for the asymptotic chi-squared distribution to be appropriate, <strong>and</strong> the table istoo large to use some <strong>of</strong> the exact tests <strong>of</strong>fered. To request p-value computation via Monte Carlosimulation, set the argument simulate.p.value (or sim) to TRUE. The argument B controls the number<strong>of</strong> replicates in the simulation.chisq.test(table.3.2.array, sim=T, B=2000)Pearson's Chi-squared test with simulated p-value (based on 2000replicates)data: table.3.2.arrayX-squared = 69.1568, df = NA, p-value = < 2.2e-16Here, the p-value is similarly very low, rejecting the hypothesis <strong>of</strong> independence <strong>of</strong> the two variables.For the likelihood ratio test statistic, one can easily use the expected frequencies obtained above <strong>and</strong> theformula for the statistic on p. 79 <strong>of</strong> Agresti. For example,2*sum(table.3.2.array*log(table.3.2.array/expected.freqs)) # R: Use res$expected instead<strong>of</strong> expected.freqs[1] 69.81162Alternatively, jumping ahead a bit, we can use the glm function for generalized linear models (seeChapter 4) <strong>and</strong> the Poisson distribution (which is the distribution <strong>of</strong> the counts in the table when the totalnumber <strong>of</strong> cases is not fixed – see p. 132 in Agresti) to get the likelihood ratio chi-squared statistic. Forexample, in either S-<strong>PLUS</strong> or R, we getfit.glm


38Note that the sum <strong>of</strong> the squared Pearson residuals equals the Pearson chi-squared statistic:sum(resid.pear^2)[1] 69.11429To get the st<strong>and</strong>ardized residuals, just modify resid.pear according to the formula on p. 81 <strong>of</strong> Agresti.ni


39update(fit, subset=(School=="Eclectic" | School=="Medical") & (Origin=="Biogenic" |Origin=="Environmental"))$deviance[1] 0.2941939Thus, we do not reject the hypothesis <strong>of</strong> independence for Subtable 1. Subtables 2 through 4 require abit more than just case selection via an update call. Subtable 2 compares the Eclectic <strong>and</strong> Medicalschools on whether the origin is a combination or not. So, we use the aggregate function to sumcolumns.First, I add a column called select that indicates the new categories. Then, I aggregate the counts intable.3.3 by School <strong>and</strong> select, <strong>and</strong> sum these counts. This gives the new table, which is supplied asthe data argument to glm. Finally, to restrict the analysis to just the Eclectic <strong>and</strong> Medical schools, I usethe subset argument.table.3.3$select


40Then, we repeat the income <strong>and</strong> jobsat values in table.2.8 count times. We can do this in at least twoways. One way is to use apply. The other is to just rep the row labels count times. Both methods arefairly comparable in efficiency, but method 2 requires a subsequent change <strong>of</strong> class <strong>of</strong> the columns tonumeric. Then, we apply cor, which returns an entire matrix, from which we select the 2,1 element.res


41psi


42consistent with the alternative hypothesis than the one corresponding to the observed table, plus theprobability corresponding to the observed table. One possible two-sided p-value is double this result,provided the answer does not exceed 1. Other possibilities are given in Agresti p. 93. Some <strong>of</strong> these canbe implemented using dhyper (see below).To demonstrate Fisher’s Exact Test, Agresti uses the data from Table 3.8 (p. 92). The functionfisher.test in S-<strong>PLUS</strong> gives a two-sided test; the version in R gives either a one- or two-sided test.The function can be used for I x J tables as well, with I, J ≤ 10, but the total sample size in the tablecannot exceed 200. (Interesting, the “Tea Tasting” example is given in the help page for fisher.test inR). In S-<strong>PLUS</strong> we get(test


4395 <strong>of</strong> Agresti, X 2 2 3 3= 6 <strong>and</strong> we need the supremum <strong>of</strong> PX ( ≥ 6) = 2 π (1 − π ) . In S, we can use generaloptimization functions for this. The function optim exists for both R <strong>and</strong> S-<strong>PLUS</strong> implementations (in S-<strong>PLUS</strong> it resides in the MASS library).library(MASS) # S-<strong>PLUS</strong> only(res


44num


45Liao <strong>and</strong> Rosen (2001) give an R function for probability calculations from a non-central hypergeometricdistribution. Their function can be used to obtain the confidence interval above. The syntax (usingAgresti’s notation) is hypergeometric( n1 +, n+ 1, n, θ ). This returns a set <strong>of</strong> functions such as thecumulative distribution function (CDF), the probability mass function, <strong>and</strong> moment functions. Theconfidence interval can be found by trial <strong>and</strong> error via the CDF, changing the odds ratio each time. Or,one can use optim. We can determine both endpoints with the same function by summing the squareddifferences <strong>of</strong> the two p-values from 0.025.f


46$countsfunction gradient58 46$convergence[1] 0res


47if(mode>ll){r1


48}MC(mean, list(prob=prob, shift=shift, ll=ll, uu=uu))MC(var, list(prob=prob, shift=shift, ll=ll, uu=uu))MC(sample.low.to.high, list(prob=prob, shift=shift, ll=ll, uu=uu) )MC(sample.high.to.low, list(prob=prob, shift=shift, ll=ll, uu=uu) )MC(r, list(prob=prob, shift=shift, ll=ll, uu=uu) )}return(mean, var, d, p, r);Thus, the small-sample confidence interval on the odds ratio, <strong>and</strong> its adjustment using the mid-p-valuecan be obtained using the same comm<strong>and</strong>s as above:f


The small-sample confidence interval on the difference <strong>of</strong> proportions, mentioned in Section 3.6.4 <strong>of</strong>Agresti, can be computed using the methodology from Section 3.F.2 <strong>of</strong> this manual.49


50Chapter 4: Generalized Linear ModelsA. Summary <strong>of</strong> Chapter 4, AgrestiChapter 4 in Agresti deals with generalized linear models (GLIMs). GLIMs extend the generallinear model by allowing nonnormal response distributions <strong>and</strong> allowing a nonlinear mean function.There are three components <strong>of</strong> a GLIM, which are detailed on p. 116-117 <strong>of</strong> Agresti. Briefly, the r<strong>and</strong>omcomponent consists <strong>of</strong> independent observations from a distribution in the natural exponential family.The pdf for this family is given in equation (4.1) <strong>of</strong> Agresti. Special discrete r<strong>and</strong>om variable cases arethe Poisson <strong>and</strong> binomial distributions. The systematic component relates a vector, η = ( η1,... η ) Tn, to aset <strong>of</strong> explanatory variables through a linear model: η = Xβ . η is called the “linear predictor”. The linkfunction links the r<strong>and</strong>om <strong>and</strong> systematic components. It describes the relationship between the mean <strong>of</strong>the response, µi, <strong>and</strong> the linear predictor, ηi= g( µi). When we specify a link function, g, we are sayingthat the systematic effects are additive on the scale given by the link function.A fourth component is sometimes specified explicitly. This is the variance function, which is afunction relating the variance to the mean (see Section 4.4 in Agresti). It is proportional to the variance <strong>of</strong>the response distribution, with proportionality constant the inverse <strong>of</strong> a parameter called the dispersionparameter. (If we use a particular r<strong>and</strong>om component, we automatically accept its variance function.However, there are methods where we can use a particular variance function that we believe describesthe r<strong>and</strong>om phenomenon, but then refrain from “choosing” a distribution for the r<strong>and</strong>om component.These methods use what are called “quasi-likelihood functions”.)Typical cases <strong>of</strong> GLIMs are the binomial logit model (Bernoulli response with log odds linkfunction) <strong>and</strong> the Poisson loglinear model (Poisson response with log link). Other cases are given inTable 4.1 in Agresti. For binomial <strong>and</strong> Poisson models, the dispersion parameter is fixed at 1.GLIMs are fit by solving the set <strong>of</strong> likelihood equations. This leads to maximum likelihoodestimates <strong>of</strong> the coefficients <strong>of</strong> the linear predictor. As the likelihood equations are usually nonlinear inthe coefficients β <strong>of</strong> the linear predictor, the solutions are found iteratively. Iterative Reweighted LeastSquares (IRLS) is the iterative method commonly used to fit GLIMs (it is used in S-<strong>PLUS</strong>). It uses Fisherscoring, which is based on the Newton-Raphson method, which achieves second-order convergence <strong>of</strong>the estimates. The difference between the two algorithms lies in the use <strong>of</strong> the observed informationmatrix for Newton-Raphson <strong>and</strong> the expected information matrix for Fisher scoring (see p. 145-146,Agresti). For canonical link models, these are the same. Fisher scoring will produce the estimatedasymptotic covariance matrix as a by-product, but it need not have second-order convergence. Plus, theobserved information matrix may be easier to calculate for complex models. The name IRLS comes fromthe iterative use <strong>of</strong> weighted least squares estimation, where the weights <strong>and</strong> responses (linearized forms<strong>of</strong> the link function evaluated at the observed data) change at each iteration. It is explained on p. 147 <strong>of</strong>Agresti that IRLS <strong>and</strong> Fisher scoring are the same thing.Model deviance is the LR statistic for testing the null hypothesis that the model holds against thegeneral alternative <strong>of</strong> a saturated model. It is twice the difference between the saturated log likelihood<strong>and</strong> the log likelihood maximized under the restrictions <strong>of</strong> the model being tested. In certain cases, thisquantity has an asymptotic chi-squared distribution. If the dispersion parameter is not fixed at 1, thentwice the difference between the saturated log likelihood <strong>and</strong> the restricted log likelihood is equal to thedeviance scaled by the dispersion parameter (hence called the scaled deviance). Model deviance for atwo-way contingency table is equivalent to the likelihood ratio chi-squared statistic. The deviance has anapproximate chi-squared distribution for large Poisson expected values <strong>and</strong> large binomial sample sizesper covariate combination. Thus, the model deviance for Bernoulli data (0/1, instead <strong>of</strong> counts out <strong>of</strong> atotal) is not asymptotically chi-squared.One can compare two nested models (i.e., one model is a subset <strong>of</strong> the other) using thedifference between their deviance values. The deviance for the larger model (more parameters toestimate) will be smaller. The comparison proceeds by first assuming that the larger model holds <strong>and</strong>testing to see if the smaller model is not statistically significantly worse in deviance. The difference indeviance is then a LRT <strong>and</strong> has an asymptotic chi-squared null distribution. For binomial or Bernoulli


51data, the difference in deviance is the same, unlike their respective model deviances. Thus, the chisquaredapproximation holds for both. In general, the use <strong>of</strong> the chi-squared approximation is muchmore reliable for differences <strong>of</strong> deviances than model deviances themselves (see also, McCullagh <strong>and</strong>Nelder, 1989). Model comparison is examined in detail in later chapters. St<strong>and</strong>ardized Pearson <strong>and</strong>deviance residuals are additional ways to assess the fit <strong>of</strong> a model.When we want to fit a GLIM such as the Poisson loglinear model to a data set, but the observedvariance <strong>of</strong> the data is greater than that allowed by the Poisson model, we may have a case for fitting anoverdispersed version <strong>of</strong> the model. If overdispersion is the result <strong>of</strong> subject heterogeneity, wheresubjects within each covariate combination still differ greatly (perhaps because we didn’t measureenough covariates), then a r<strong>and</strong>om effects version <strong>of</strong> the model (e.g., r<strong>and</strong>om effects Poisson regression,r<strong>and</strong>om effects logistic regression) may be appropriate. Another alternative is to fit a model with ar<strong>and</strong>om component that allows for a greater variance than the ordinary Poisson or binomial. Someexamples are the negative binomial (for r<strong>and</strong>om count data) <strong>and</strong> the beta-binomial (for counts out <strong>of</strong> atotal).A third alternative is to use quasi-likelihood estimation. In quasi-likelihood estimation, we takeadvantage <strong>of</strong> the fact that the likelihood equations for GLIMS depend on the assumed responsedistribution only through its mean <strong>and</strong> variance (which may be a function <strong>of</strong> the mean). Distributions inthe natural exponential family are characterized by the relationship between the mean <strong>and</strong> the variance.Quasi-likelihood estimation is determined by this relationship. Thus, if we wanted to assume that thevariance <strong>of</strong> a r<strong>and</strong>om count was some specified function <strong>of</strong> its mean, but not equal to it, we could usequasi-likelihood estimation to estimate the coefficients <strong>of</strong> the linear predictor.Generalized Additive Models (GAMs) further generalize GLIMs by replacing the linear predictorwith smooth functions <strong>of</strong> the predictors (one for each predictor). A commonly used smooth function is thecubic spline. Each smooth function is assigned a degrees <strong>of</strong> freedom, which determines how rough thefunction will be. The GLIM is the special case <strong>of</strong> each smooth function being a linear function. GAMs arefit via penalized maximum likelihood estimation in S-<strong>PLUS</strong> (Chambers <strong>and</strong> Hastie, 1992). GAMs have anadvantage over procedures like lowess because they recognize the form <strong>of</strong> the response variable <strong>and</strong>only give predictions within its bounds, which is not true <strong>of</strong> lowess (although, one can use a lowessfunction in a GAM). In general, the advantage <strong>of</strong> GAMs over GLIMS is that the form <strong>of</strong> the predictorsdoes not need to satisfy a particular functional relationship, like linear, logarithmic, etc. Finally, GAMsmay be used in an exploratory sense by determining a parametric function to use for a predictor based onits fitted smooth function.B. Generalized Linear Models for Binary DataSuppose the response is binary, taking one <strong>of</strong> two possible outcomes. Then, three special cases <strong>of</strong> theGLIM use an identity link (linear probability model), a logit link (logistic regression model), <strong>and</strong> an inversenormal CDF link (probit regression model). I briefly remind you what these models are, then fit some datato them using functions in S-<strong>PLUS</strong> <strong>and</strong> R.For a binary response, the regression modelπ ( x)= α + βxis called a linear probability model because it purports a linear relationship between the probability <strong>of</strong>positive response <strong>and</strong> the explanatory variables. Although it has a simple interpretation, the linearprobability model has a structural defect in that the predicted probability <strong>of</strong> positive response can exceed1 or be less than 0, due to the link function being the identity.The regression modelexp( α + βx)π ( x)=1 + exp( α + βx)


52is called a logistic regression model. This model corresponds to a binomial GLIM with log odds link (i.e.,g( π ( x)) = log( π( x) (1 −π( x )))).The regression modelπ ( x) = Φ ( α + βx)for a normal(0, 1) cdf, Φ , is called a probit regression model. It is a binomial GLIM with link function−Φ 1 ( π ( x )).The decision to use each <strong>of</strong> these link functions can depend on the expected rate <strong>of</strong> increase <strong>of</strong> π ( x ) asa function <strong>of</strong> x or can depend on a comparison <strong>of</strong> appropriate goodness-<strong>of</strong>-fit measures (note thatotherwise identical models with different link functions are not nested models).To illustrate a logit, probit, <strong>and</strong> linear probability model, Agresti uses Table 4.2 (Snoring <strong>and</strong> heartdisease, p. 121). The response variable is whether the subject had heart disease. The explanatoryvariable is the subject’s spouse’s report <strong>of</strong> the level <strong>of</strong> snoring (never, occasionally, nearly every night,every night). These levels are changed into scores (0, 2, 4, 5). The levels <strong>of</strong> snoring are treated asindependent binomial samples.To fit the three models using iterative reweighted least squares (IRLS), we can use the function glm, withfamily=”binomial” (for the logit <strong>and</strong> probit links, at least). However, glm doesn’t have a Newton-Raphson method or any other type <strong>of</strong> optimization method. Thus, for more general maximum likelihoodestimation, we might use the function optim (both S-<strong>PLUS</strong> <strong>and</strong> R). Conveniently, Venables <strong>and</strong> Ripley(2002) wrote a function for maximum likelihood estimation for a binomial logistic regression. We canmake minor changes to fit the linear probability model <strong>and</strong> the probit regression model.To set up the data, type,n


53}cat("\nResidual Deviance:", format(fit$value), "\n")cat("\nConvergence message:", fit$convergence, "\n")invisible(fit)The function is written for binomial logistic regression, but is easily modified for probit regression <strong>and</strong>linear probability regression.Thus, to fit a linear probability model, we change the functions fmin <strong>and</strong> gmin within logitreg to readfmin


54Residual Deviance: 836.7943Convergence message: 0The warnings given with the linear probability model are somewhat expected, as the probability can beless than 0 or greater than 1. Also, note that we cannot use the default starting values.Approximate st<strong>and</strong>ard errors for the two parameter estimates can be obtained using the inverse <strong>of</strong> theobserved information matrix.sqrt(diag(solve(logit.fit$hessian)))[1] 0.11753115 0.03536285sqrt(diag(solve(lpm.fit$hessian)))[1] 0.002426329 0.001976457sqrt(diag(solve(probit.fit$hessian)))[1] 0.04981505 0.01670894We can obtain fitted probabilities for all three link functions.eta


55Predicted Probability0.00 0.05 0.10 0.15 0.20LogisticProbitLinear0 1 2 3 4 5Level <strong>of</strong> SnoringEstimation with IRLSIf you were wondering what estimates <strong>and</strong> st<strong>and</strong>ard errors IRLS would give, you can fit all three <strong>of</strong> theabove models using the glm function, which is described in more detail in the next section. For example,to fit the binomial with logit link using IRLS, typesnoring


56Notice also that, in the formula, we use the proportions for each category as the response, plus we addthe weights argument, which is the total number in each category.prop


57plot(y=plot.table.4.3$Sa,x=plot.table.4.3$W,xlab="Width (cm)",ylab="Number <strong>of</strong> Satellites", bty="L", axes=F, type="n")axis(2, at=1:15)axis(1, at=seq(20,34,2))text(y=plot.table.4.3$Sa,x=plot.table.4.3$W,labels=plot.table.4.3$x)It is probably possible to do the above using table instead <strong>of</strong> aggregate.Figure 4.4 plots the mean number <strong>of</strong> satellites for the mean width <strong>of</strong> eight width categories created fromthe W variable. In addition, a fitted smooth from the result <strong>of</strong> a generalized additive model (GAM) fit tothese data is superimposed. Generalized additive models (as extensions to GLIMs) are discussed brieflyin Section 4.8 in Agresti, <strong>and</strong> in the corresponding section <strong>of</strong> this manual.In the following code, first I cut the W variable into categories, then use aggregate again to get means <strong>of</strong>Sa <strong>and</strong> <strong>of</strong> W by the categories. Then, the points are plotted.table.4.3$W.fac


58To see exactly what the smoothed term in width in the gam call represents, we can plot (mean) width bythe smoothed term. The se=T argument adds plus <strong>and</strong> minus two pointwise st<strong>and</strong>ard deviations asdashed lines to the plot.plot.gam(res, se=T)s(plot.x, df = 5)-1.0 -0.5 0.0 0.524 26 28 30plot.xThe smoothed term does not deviate too much from linearity, <strong>and</strong> Figure 4.4 shows a linear trend relatingthe mean number <strong>of</strong> satellites to width. Agresti fits Poisson GLIMs with both log <strong>and</strong> identity links to thedata.We will use IRLS <strong>and</strong> glm to fit the Poisson models. For non-canonical links (e.g., identity), the estimatesmay differ slightly from Agresti’s. A Poisson loglinear model is fit usinglog.fit


59I’ve given the output from a call to summary (in S-<strong>PLUS</strong>), which summarizes model fitting by givingcoefficient estimates, their approximate SEs, residual deviance, <strong>and</strong> a summary <strong>of</strong> deviance residuals.The dispersion parameter by default is set to 1 (we will change that later). The correlation between thecoefficient estimates is quite high (almost perfectly negative). This can be reduced by using the me<strong>and</strong>eviation in width instead <strong>of</strong> width in the model. The null deviance is the deviance value (p. 119, Agresti)for a model with only an intercept. The residual deviance is the deviance value for a model with both anintercept <strong>and</strong> width. The reduction in deviance is a test <strong>of</strong> the width coefficient. That islog.fit$null.deviance-log.fit$deviance[1] 64.91309is the LR statistic for testing the significance <strong>of</strong> the Width variable in the model. Compared to a chisquareddistribution with 1 degree <strong>of</strong> freedom, the p-value <strong>of</strong> this test is quite low, rejecting the nullhypothesis <strong>of</strong> a zero-valued coefficient on width. We can get similar information from the Wald test givenby the t-value next to the coefficient estimate (z-value in R version). However, the LRT is usuallyconsidered more reliable (see Agresti, <strong>and</strong> also Lloyd 1999).The summary result for any glm.object in S-<strong>PLUS</strong> has the following attributes that can be extracted:attributes(summary(log.fit)) # S-<strong>PLUS</strong>$names:[1] "call" "terms" "coefficients" "dispersion" "df" "deviance.resid"[7] "cov.unscaled" "correlation" "deviance" "null.deviance" "iter" "nas"[13] "na.action"$class:[1] "summary.glm"The same summary call in R has a few additional components, notably AIC.attributes(summary(log.fit)) # R$names[1] "call" "terms" "family" "deviance"[5] "aic" "contrasts" "df.residual" "null.deviance"[9] "df.null" "iter" "deviance.resid" "aic"[13] "coefficients" "dispersion" "df" "cov.unscaled"[17] "cov.scaled"As an example, to extract the estimated coefficients, along with their st<strong>and</strong>ard errors, type:summary(log.fit)$coefficientsValue Std. Error t value(Intercept) -3.3047572 0.54222774 -6.094777W 0.1640451 0.01996492 8.216665Thus, the fitted model islog ˆ µ = − 3.305 + 0.164 x(4.2)The glm.object itself has the following components. It “inherits” all the attributes <strong>of</strong> lm.objectsattributes(log.fit) # S-<strong>PLUS</strong> (R has quite a few more)$names:[1] "coefficients" "residuals" "fitted.values" "effects" "R"[6] "rank" "assign" "df.residual" "weights" "family"[11] "linear.predictors" "deviance" "null.deviance" "call" "iter"[16] "y" "contrasts" "terms" "formula" "control"$class:[1] "glm" "lm"


60For example, the fitted response values (expected values) at each <strong>of</strong> the width values in the model canbe obtained by extracting the fitted.values.log.fit$fitted.valuesThe same answer can be obtained by the call fitted(log.fit). The functions, fitted, resid,coef are shortened versions to extract fitted values, residuals, <strong>and</strong> coefficients, respectively, from glmobjects.Using the predict method for glm, we can get the fitted response value for any width value we input.For example, the expected number <strong>of</strong> satellites at a width <strong>of</strong> 26.3 ispredict.glm(log.fit, type="response", newdata=data.frame(W=26.3))[1] 2.744581Agresti also fits a Poisson model with identity link to the Horseshoe Crab data. This is fit in S-<strong>PLUS</strong> usingid.fit


61guiModify( "CommentDate", Name = "GSD2$1",xPosition = "0.542857",yPosition = "3.52967", Angle = "90")# make arrows <strong>and</strong> textind


62If we examine Table 4.4 (p. 129 in Agresti) <strong>of</strong> the sample mean <strong>and</strong> variance <strong>of</strong> the number <strong>of</strong> satellitesper Width category, we see that the variance exceeds the mean in all cases. A Poisson model postulatesthat for each Width, the variance <strong>and</strong> mean <strong>of</strong> the number <strong>of</strong> satellites is the same. Thus, we haveevidence <strong>of</strong> overdispersion, where the “ordinary” Poisson model is not correct. Incidentally, the functiontapply (or by) can be used to get the sample means <strong>and</strong> variances:tapply(table.4.3$Sa, table.4.3$W.fac, function(x) c(mean=mean(x), variance=var(x)))# by(table.4.3$Sa, table.4.3$W.fac, function(x) c(mean=mean(x), variance=var(x)))The general density for the r<strong>and</strong>om component <strong>of</strong> a GLIM has an unspecified dispersion parameter that isrelated to the scale <strong>of</strong> the density. This density is given in equation (4.14) in Agresti (2002). Forexample, with a Gaussian r<strong>and</strong>om component, the dispersion parameter is the variance. For a binomialor Poisson r<strong>and</strong>om component, the dispersion is fixed at 1, so we have not needed to deal with it yet.However, if we believe that the mean response is like a Poisson mean, but the variance is proportional tothe Poisson mean (with proportionality constant the dispersion parameter), then we can leave thedispersion parameter unspecified <strong>and</strong> estimate it along with the mean parameter. In this case, we nolonger have a Poisson distribution as our r<strong>and</strong>om component. Plus, the estimate <strong>of</strong> the dispersionparameter must be used in st<strong>and</strong>ard error <strong>and</strong> deviance calculations. The general form <strong>of</strong> the scaleddeviance is given in equation (4.30) in Agresti.If the actual Poisson means (i.e., per Width value) are large enough (or, in the case <strong>of</strong> binomial data, thesample sizes at each covariate condition are large enough), then a simple way to detect overdispersion isto compare the residual deviance with the residual df, which will be equal in the asymptotic sense justmentioned. These two quantities are quite disparate for both link models. So, we decide to estimate thedispersion parameter instead <strong>of</strong> leaving it fixed at 1. We can do this in S-<strong>PLUS</strong> using a dispersion=0argument to summary.glm. In R, we must use the quasipoisson family first. (This makes sense after thediscussion <strong>of</strong> quasi-likelihood).summary.glm(log.fit, dispersion=0)$dispersion # S-<strong>PLUS</strong>Poisson3.181952# Rlog.fit.over


63The usual deviance must be divided by the dispersion estimate to get the scaled deviance used in F-testsfor model comparison (see Venables <strong>and</strong> Ripley, 2002). An anova method is available for glm objectsthat does these tests provided argument test=”F” is supplied. anova.glm will print the F-test that usesthe dispersion estimate from the larger model. Thus, if we wanted to compare the null model (with q = 1parameters) to log.fit (with p = 2) using the F-test (in Agresti’s notation)we would doF( D( y; µ0) − D( y; µ1))=( p−q)ˆ φ(4.4)null.fit


64mean <strong>and</strong> dispersion parameter. The pdf <strong>of</strong> the negative binomial is given in equation (4.12) <strong>of</strong> Agresti,with2EY ( ) = µ <strong>and</strong> var( Y)= µ + µ kwhere k is the (positive) dispersion parameter. The smaller the dispersion parameter, the larger thevariance as compared to the mean. But, with growing dispersion parameter, the variance converges tothe mean, <strong>and</strong> the r<strong>and</strong>om quantity converges to the Poisson distribution. Actually, negative binomial isthe distribution <strong>of</strong> a Poisson count with gamma-distributed rate parameter (see Section 13.4 in Agresti).There are several methods for going about fitting a negative binomial GLIM in S-<strong>PLUS</strong> <strong>and</strong> R. If thedispersion parameter is known, then the MASS library has a negative.binomial family function to usewith glm. For example, to fit a negative binomial GLIM to the Horseshoe Crab data, where the dispersionparameter (called theta in the function) is fixed to be 1.0, we can uselibrary(MASS)glm(Sa~W, family=negative.binomial(theta=1.0,link="identity"),data=table.4.3,start=predict(log.fit, type="link")) # for R use start=coef(log.fit)Call:glm(formula = Sa ~ W, family = negative.binomial(theta = 1, link = "identity"), data =table.4.3, start = predict(log.fit, type = "link"))Coefficients:(Intercept)W-11.62843 0.5537723Degrees <strong>of</strong> Freedom: 173 Total; 171 ResidualResidual Deviance: 202.8936A simpler version <strong>of</strong> negative.binomial (called neg.bin, with only the log link) is also available fromMASS, as well as a function called glm.nb. The function glm.nb allows one to estimate theta usingmaximum likelihood estimation. The output is similar to that <strong>of</strong> glm <strong>and</strong> has a summary method.library(MASS)nb.fit


65Theta: 0.932Std. Err.: 0.1682 x log-likelihood: -747.929The coefficient estimates are slightly different that those obtained by Agresti (p. 131).LRTs are also available for comparing nested negative binomial GLIMs (via the anova method).However, as theta must be held constant between the two models being compared, one must firstconvert the glm.nb object to a glm object, which will fix theta if the model is then update’d. Theconversion is done using the glm.convert function.To use custom GLIMs, one must create a family. The function make.family can be used to make a newglm family. You must specify the name <strong>of</strong> the family, the link functions allowed for it (e.g., logit, log,cloglog) <strong>and</strong> their derivatives, inverses, <strong>and</strong> initialization expressions (used to initialize the linearpredictor for Fisher scoring), <strong>and</strong> the variance <strong>and</strong> deviance functions.For example, in the function listing for negative.binomial, you can see where the links <strong>and</strong> varianceslists are defined <strong>and</strong> where the function make.family is used. The reason why the link functions are notactually typed out in function form is because all the links already appear in other glm families in S-<strong>PLUS</strong>.Their information is stored in the matrix glm.links. negative.binomial accesses the appropriate linkfunction using glm.links[,link].Beta-binomial regression models can be fit using gnlr in library gnlm for R.F. Residuals for GLIMsTo demonstrate how to obtain the residuals in Agresti’s Section 4.5.5, I use the Horseshoe Crab data fit.For example, the Pearson <strong>and</strong> deviance residuals are obtained fromresid(log.fit, type="deviance")pear.res


66Pearson Residuals-2 0 2 4 6St<strong>and</strong>ardized Pearson Residuals-2 0 2 4 60 50 100 150observation0 50 100 150observationIssuing the comm<strong>and</strong> plot(log.fit) may also be useful:par(mfrow=c(1,2))old.par


67Std. deviance resid.0.0 0.5 1.0 1.5Scale-Location plot15149 56Cook's distance0.00 0.02 0.04 0.06Cook's distance plot115561490.5 1.0 1.5 2.00 50 100 150Predicted valuesObs. numberG. Quasi-Likelihood <strong>and</strong> GLIMsQuasi-likelihood estimation assumes only a mean-variance relationship rather than a completedistribution for the response variable. However, many times the relationship specified determines aparticular distribution. This is because distributions in the natural exponential family are characterized bythe relationship between the mean <strong>and</strong> the variance function. Thus, using the R function quasipoissonwith log link (<strong>and</strong> constant variance) gives the same estimates as using the Poisson family with log linkbecause the Poisson is a distribution in the natural exponential family. The reason the estimates are thesame is because the estimating equations used for quasi-likelihood estimation are the same as thelikelihood equations in the case <strong>of</strong> a specified distribution with that mean <strong>and</strong> variance relationship.In general then, quasi-likelihood estimates (QLEs) are not MLEs, but in some cases where we areassuming a particular distribution, but with a magnified variance (e.g., when the dispersion parametermultiplies the variance), the point estimates will be identical because the dispersion parameter drops out<strong>of</strong> the estimating equations.Agresti uses data from a teratology experiment to illustrate overdispersion <strong>and</strong> the use <strong>of</strong> quasi-likelihoodestimation. Rats in 58 litters were on iron-deficient diets <strong>and</strong> given one <strong>of</strong> four treatments (groups 1-4).The rats were made pregnant <strong>and</strong> killed after three weeks. The number <strong>of</strong> dead fetuses out <strong>of</strong> the totallitter size is the response variable. (The data set is available on Agresti’s CDA website). I copied the datainto a text file called teratology.ssc. It is then read into S-<strong>PLUS</strong>/R bytable.4.5


68The MLEs <strong>of</strong> the probabilities are then(pred


69where the s jare smooth functions <strong>of</strong> the predictors. When the s jare linear, the GAM becomes a GLIM.The functions s jare fit using scatterplot smoothers. One example <strong>of</strong> a scatterplot smoother is locallyweighted least squares regression (lowess). Another example is a smoothing spline. A common functionfor the s jis a restricted cubic spline. A cubic spline in a predictor variable is a piecewise cubic polynomialrepresentation <strong>of</strong> the predictor, where the pieces are determined by “knot” locations placed over therange <strong>of</strong> the predictor variable. The restriction occurs by forcing the resulting piecewise curve to havevanishing second derivatives at the boundaries. Then the number <strong>of</strong> degrees <strong>of</strong> freedom for each originalpredictor variable is equal to the number <strong>of</strong> knots minus 1. The more degrees <strong>of</strong> freedom allotted to apredictor variable, the more complex the relationship between the predictor <strong>and</strong> the response.A Fisher scoring-type outer loop (to update the working responses) with a backfitting inner loop (toupdate the smooth functions) can be used to find the estimates <strong>of</strong> the s j(see Chambers <strong>and</strong> Hastie,1992, p. 300ff). When we assume that the s jin a GAM are polynomial smoothing splines (like the cubicsplines above), then the algorithm corresponds to penalized maximum likelihood estimation, where thepenalty is for roughness <strong>of</strong> the s j(Green <strong>and</strong> Silverman, 1994).The function gam in S-<strong>PLUS</strong> can be used to fit GAMs. If the functions are restricted to be smoothingsplines (denoted by s in the gam formula) then gam uses a basis function algorithm, with a basis <strong>of</strong> cubicB-splines, to find the smooth functions (see Green <strong>and</strong> Silverman, 1994, p. 44ff). The number <strong>of</strong> knotscovering the range <strong>of</strong> the predictor values is chosen by default to be the number <strong>of</strong> unique data points (ifthese are less than 50), <strong>and</strong> otherwise “a suitable fine grid <strong>of</strong> knots is chosen”. The smoothing parameterused in the penalized least squares estimation to find the basis function representation <strong>of</strong> the smoothfunctions can be specified by the user or determined via generalized cross-validation (see Green <strong>and</strong>Silverman, 1994, p. 35). Equivalently, the number <strong>of</strong> degrees <strong>of</strong> freedom for a smooth function can bespecified.The function gam in S-<strong>PLUS</strong> “inherits” a lot <strong>of</strong> functionality from glm <strong>and</strong> lm. But, the output is moregraphical because the fitted smooth functions usually must be graphed to interpret the model (Chambers<strong>and</strong> Hastie, 1992).Horseshoe Crab Data – Bernoulli responseAgresti fits a GAM to the binary response <strong>of</strong> whether a female crab has at least one satellite, using alogit link <strong>and</strong> the width <strong>of</strong> carapace as a predictor. We fit a GAM using gam in S-<strong>PLUS</strong> <strong>and</strong> R, with 3degrees <strong>of</strong> freedom for width. Then, we plot the fitted smooth function.First, I get the binary response <strong>and</strong> the number <strong>of</strong> observations at each data point.table.4.3$Sa.bin0,1,0)plot.table.4.3


70Title = "Width, `x` (cm)") # S-<strong>PLUS</strong> only (GSD2 is the name <strong>of</strong> the graphsheet.Substitute the name <strong>of</strong> your graphsheet in place <strong>of</strong> it, if GSD2 is not it)Fit the GAM <strong>and</strong> plot the fit.res


71plot.gam(res, se=T)s(W, df = 3)-5 0 5 1022 24 26 28 30 32 34Wpar(old.par)# change back to old pty setting


72Chapter 5: Logistic RegressionA. Summary <strong>of</strong> Chapter 5, AgrestiThis chapter treats logistic regression in more detail than did Chapter 4. It begins with univariatelogistic regression. The interpretation <strong>of</strong> the coefficient, β , in the univariate logistic regression (equation5.1 in Agresti) is discussed initially. The direction <strong>of</strong> this coefficient indicates the direction <strong>of</strong> the effect <strong>of</strong>the variable x on the probability <strong>of</strong> a positive response. The magnitude <strong>of</strong> β is usually interpreted interms <strong>of</strong> the odds <strong>of</strong> a positive response. The odds change multiplicatively by exp( β ) for each one-unitincrease in x. The expression exp( β ) is actually an odds ratio: the odds (<strong>of</strong> positive response) atX = x + 1 to the odds at X = x . Prior to fitting a logistic regression model to data, one should checkthe assumption <strong>of</strong> a logistic relationship between the response <strong>and</strong> explanatory variables. A simple wayto do this is to use the linear relationship between the logit <strong>and</strong> the explanatory variable. The values <strong>of</strong>the explanatory variable can be plotted against the sample logits (p. 168, Agresti) at those values. Theplot should look roughly linear for a logistic model to be appropriate. If there are not enough responsedata at each unique x value (<strong>and</strong> categorizing x values is undesirable), then the technique <strong>of</strong> the lastsection in Chapter 4 can be used (i.e., GAM). There, we saw that a sigmoidal (or S-shaped) trendappeared in the plot <strong>of</strong> the response by predictor (Figure 4.7, Agresti).Logistic regression can be used with retrospective studies to estimate odds ratios. The fit <strong>of</strong> alogistic regression model to retrospective response data, given an explanatory variable whose values arenot known in advance, yields a coefficient estimate whose exponent is the same estimated odds ratio asif the response variable had been prospective.A logistic regression model is fit via maximum likelihood estimation. In practice, this can beachieved via IRLS, as mentioned in the previous chapter. That is, the MLE is the limit <strong>of</strong> a sequence <strong>of</strong>weighted least squares estimates, where the weight matrix changes at each iteration (see Section 5.5 inAgresti).Inference for the maximum likelihood estimators is asymptotic. Confidence intervals can be Waldconfidence intervals, LR confidence intervals or score confidence intervals. The Wald, LR, <strong>and</strong> scoretests can be used to test hypotheses. The LRT is preferred, as it uses both the null maximized likelihoodvalue as well as the alternative maximized likelihood value (providing more information than the othertests), instead <strong>of</strong> just one <strong>of</strong> these values. When comparing two unsaturated fitted models, the differencebetween their individual LRT statistics in comparison with the saturated model has an approximate chisquarednull distribution, <strong>and</strong> this approximation is better than the chi-squared approximation by eachLRT statistic alone.Overall chi-squared goodness-<strong>of</strong>-fit tests for a logistic regression model can only be done forcategorical predictors, <strong>and</strong> for continuous predictors only if they are categorized. This is because thenumber <strong>of</strong> unique predictor combinations grows with increasing sample size when the predictors remaincontinuous.When a logit model includes categorical predictors (factors), there is a parameter for eachcategory <strong>of</strong> each predictor. However, one <strong>of</strong> those parameters per predictor is redundant. Setting theparameter for the last category equal to zero, <strong>and</strong> changing the definition <strong>of</strong> the remaining parameters tothat <strong>of</strong> deviation from this last parameter, will eliminate redundancy <strong>of</strong> parameters in the logit model. But,the interpretation <strong>of</strong> each parameter is modified. Another way to eliminate redundancy is to set “sum-tozeroconstraints”, where the sum <strong>of</strong> the parameters for a particular factor is constrained to equal zero.However, any constraints for the category parameters does not affect the meaning <strong>of</strong> estimates <strong>of</strong> theodds ratios between two categories or <strong>of</strong> the joint probabilities.Multiple logistic regression is the direct extension <strong>of</strong> univariate logistic regression. The multiplelogistic regression model is given in equations (5.8) <strong>and</strong> (5.9) in Agresti. In that representation, thequantity exp( β i) for the ith covariate represents the multiplicative effect on the odds <strong>of</strong> a 1-unit increasein that covariate, at fixed levels <strong>of</strong> the other covariates, provided there are no interactions between the ithcovariate <strong>and</strong> other covariates. With all categorical predictors, the model is called a logit model. A


73representation <strong>of</strong> a logit model with two predictors is given on p. 184 <strong>of</strong> Agresti. A logit model that doesnot contain an interaction between two categorical predictors assumes that the odds ratio(s) between one<strong>of</strong> the predictors <strong>and</strong> the binary dependent variable is(are) the same at or given each level <strong>of</strong> the otherpredictor. (In the case <strong>of</strong> a binary predictor, there is just one odds ratio between that predictor <strong>and</strong> thedependent variable). Then, the predictor <strong>and</strong> the dependent variable are said to be conditionallyindependent. In terms <strong>of</strong> the model representation, this means that the parameters corresponding to thecategories <strong>of</strong> that predictor are all equal (see p. 184 in Agresti). A formal test <strong>of</strong> homogeneous oddsratios can be carried out using the chi-squared goodness-<strong>of</strong>-fit statistic G 2 .It is possible for additivity <strong>of</strong> predictors (i.e., no interactions) to hold on the logit scale, but notother link scales or vice versa.B. Logistic Regression for Horseshoe Crab DataOne interpretation <strong>of</strong> the horseshoe crab data <strong>of</strong> Table 4.3 in Agresti has a binary response witha positive response being that the female crab has at least one satellite. So, a logistic regression isplausible for describing the relationship between width <strong>of</strong> carapace <strong>and</strong> probability <strong>of</strong> at least onesatellite. If the widths are grouped into eight categories (Table 4.4, p. 129 Agresti), then a plot <strong>of</strong> themeans <strong>of</strong> the width categories by the proportion <strong>of</strong> female crabs within each category having satellites isin Figure 5.2 <strong>of</strong> Agresti, with a logistic regression fit superimposed.We plotted these proportions in Section I <strong>of</strong> Chapter 4, where we superimposed a GAM with logitlink <strong>and</strong> binary response. Now, we will superimpose a logistic regression function.Here is the logit fit, using glm.table.4.3$Sa.bin0,1,0) # change number <strong>of</strong> satellites to binaryresponse(crab.fit.logit


74# plot points <strong>and</strong> regression curve (note the ordering <strong>of</strong> the widths first)lines(y=prop, x=plot.x, pch=16, type="p")ind


75The Wald test is shown in the t value column under Coefficients (in R, this is a z value). To get theLRT, we need the log likelihood value at the estimate <strong>and</strong> at the null value 0. For this we can use thedeviance values.crab.fit.logit$null.deviance-crab.fit.logit$deviance[1] 31.30586A pr<strong>of</strong>ile likelihood ratio confidence interval can be found easily in R using package Bhat. We first definethe negative log likelihood, then use plkhci in the same way as in Chapter 2.library(Bhat)# neg. log-likelihood <strong>of</strong> logistic model with width includednlogf


76I have since found that the MASS library has a function confint that computes pr<strong>of</strong>ile-likelihoodconfidence intervals for the coefficients from glm objects. It is much simpler to use than plkhci for logitmodels. Below, I use it on the logit model fit to the crab datalibrary(MASS)confint(crab.fit.logit)Waiting for pr<strong>of</strong>iling to be done...2.5 % 97.5 %(Intercept) -17.8104562 -7.4577421W 0.3084012 0.7090312A plot <strong>of</strong> the predicted probabilities along with pointwise confidence intervals can be obtained usingoutput from the predict function, which gives the st<strong>and</strong>ard errors <strong>of</strong> the predictions.crab.predict


77The above plot is from R. Note that it extends on the left-h<strong>and</strong> side to only a width <strong>of</strong> 21.0 cm. However,Figure 5.3 in Agresti extends to a width <strong>of</strong> 20.0 cm. As 21.0 cm is the lowest width in the data set, inorder to predict the probability <strong>of</strong> a satellite at a width <strong>of</strong> 20.0 cm using the function predict we need touse the newdata argument. For example,predict(crab.fit.logit, type="response", se=T, newdata=data.frame(W=seq(20,32,1)))gives predictions <strong>and</strong> st<strong>and</strong>ard errors at widths from 20 to 32 cm, by cm.C. Goodness-<strong>of</strong>-fit for Logistic Regression for Ungrouped DataWith categorical predictors (grouped data), one may use a chi-squared goodness-<strong>of</strong>-fit statistic where theexpected number <strong>of</strong> positive <strong>and</strong> negative responses per predictor value (or predictor combination, withmore than one predictor) are obtained from the fitted model. When the predictors are continuous(ungrouped data), they must be categorized prior to using the test. In that case, one may assign themidpoint <strong>of</strong> the category to the observations in that category in order to compute the expected number <strong>of</strong>positives.In Agresti’s Table 5.2, the expected number <strong>of</strong> positives in each category (Fitted Yes) is obtained bysumming the predicted probabilities for each observation that falls within that category. Then, theobserved number <strong>of</strong> Yes’s are compared to these expected numbers in a chi-square test. RobertoBertolusso has sent me code to compute the values within Table 5.2, <strong>and</strong> also compute the goodness-<strong>of</strong>fitstatistics. I present his code (somewhat modified) below. The original code appears in the code filesfor this document.First, we create a table with the successes <strong>and</strong> failures per width categorycont.table


78The Pearson chi-squared statistic is then easily computed(x.squared = sum((observed - fitted.all)^2/fitted.all))[1] 5.320099df


79To constrain the first category parameter to be zero, useoptions(contrasts=c("contr.treatment", "contr.poly"))Thus, to fit a logit model to the data in Table 5.3 on maternal alcohol consumption <strong>and</strong> child’s congenitalmalformations, we use glm with options set according to the constraint used.Alcohol


803 -5.060060 0.0063051704 -4.836282 0.0078740165 -3.610918 0.026315789The sample proportions tend to increase with alcohol consumption.A model that specifies independence between alcohol consumption <strong>and</strong> congenital malformations is fit by(Table.5.3.logit3


81(Intercept)scores -0.436482# chi-squared statisticsum(residuals(Table.5.3.LL, type="pearson")^2)[1] 2.050051# LR statisticTable.5.3.LL$null.deviance - Table.5.3.LL$deviance[1] 4.253277with fitted logits <strong>and</strong> proportionscbind(logit = predict(Table.5.3.LL), fitted.prop = predict(Table.5.3.LL, type ="response"))logit fitted.prop1 -5.960460 0.0025720922 -5.802180 0.0030118633 -5.485620 0.0041288464 -4.694219 0.0090650795 -3.744539 0.023100295Pr<strong>of</strong>ile likelihood confidence intervals can be obtained using the plkhci function in the R package Bhat,which is illustrated in subsection B <strong>of</strong> this chapter, or more easily using the confint function from libraryMASS.library(MASS)confint(Table.5.3.LL)Waiting for pr<strong>of</strong>iling to be done...2.5 % 97.5 %(Intercept) -6.19303606 -5.7396909scores 0.01865425 0.5236161A logit model with an ordered categorical predictor can also be fit using orthogonal polynomial contrasts.However, by default, S-<strong>PLUS</strong> assumes the levels <strong>of</strong> the ordered factor are equally spaced. Forillustrative purposes, you could useAlcoholO


821 - pchisq(z2, df = 1)[1] 0.01037159which suggests strong evidence <strong>of</strong> a positive slope.E. Multiple Logistic Regression1. Multiple Logit Model – AIDS Symptoms DataAgresti introduces multiple logistic regression with a data set that has two categorical predictors. Thus,the model is a logit model. Table 5.5 in Agresti cross-classifies 338 veterans infected with the AIDS viruson the two predictors Race (black, white) <strong>and</strong> (immediate) AZT Use (yes, no), <strong>and</strong> the dependent variablewhether AIDS symptoms were present (yes, no). The model that is fit is the “main effects” modelAZT racelogit PY ( = 1) = α + β + β[ ]In S, we will represent the predictors as factors with two levels each. This ensures that we have thecorrect level specifications.table.5.5


83Analysis <strong>of</strong> Deviance TableModel 1: cbind(Yes, No) ~ AZTModel 2: cbind(Yes, No) ~ AZT + RaceResid. Df Resid. Dev Df Deviance P(>|Chi|)1 2 1.420612 1 1.38353 1 0.03708 0.84730Thus, the reduction in deviance (0.03708) is not significantly greater than chance, <strong>and</strong> we conclude thatRace probably does not belong in the model.As demonstrated above in Subsection D, one can easily change the type <strong>of</strong> constraint imposed on theparameters in order to estimate them uniquely. Use the options statement above in either R or S-<strong>PLUS</strong>.The estimated probabilities for each <strong>of</strong> the four predictor combinations is given by the function predict,<strong>and</strong> st<strong>and</strong>ard errors are given by setting the argument se to TRUE.res


84Now, add the sample proportions to the plot.attach(table.5.5)propAIDS


85table.4.3$C.fac


86lines(seq(18,34,1),res2) # add colors 2-4lines(seq(18,34,1),res3)lines(seq(18,34,1),res4)# add arrows <strong>and</strong> textarrows(x0=29, res1[25-17],x1=25, y1=res1[25-17], length=.09)text(x=29.1, y=res1[25-17], "Color 1", adj=c(0,0))arrows(x0=23, res2[26-17],x1=26, y1=res2[26-17], length=.09)text(x=21.1, y=res2[26-17], "Color 2", adj=c(0,0))arrows(x0=28.9, res3[24-17],x1=24, y1=res3[24-17], length=.09)text(x=29, y=res3[24-17], "Color 3", adj=c(0,0))arrows(x0=25.9, res4[23-17],x1=23, y1=res4[23-17], length=.09)text(x=26, y=res4[23-17], "Color 4", adj=c(0,0))Predicted Probability0.2 0.4 0.6 0.8 1.0Color 2Color 1Color 3Color 418 20 22 24 25 26 28 30 32 34Width, x(cm)As mentioned previously, there are some differences in the use <strong>of</strong> arrows <strong>and</strong> text across R <strong>and</strong> S-<strong>PLUS</strong>. Here is the code to plot Figure 5.5 in S-<strong>PLUS</strong>.plot(seq(18,34,1),res1,type="l",bty="L",ylab="Predicted Probability",axes=F)axis(2, at=seq(0,1,.2))axis(1, at=seq(18,34,2))guiModify( "XAxisTitle", Name = "GSD2$1$Axis2dX1$XAxisTitle", Title= "Width,`x`(cm)")lines(seq(18,34,1),res2)lines(seq(18,34,1),res3)lines(seq(18,34,1),res4)arrows(x1=29, y1=res1[25-17],x2=25, y2=res1[25-17],size=.25,open=T)text(x=29.1, y=res1[25-17], "Color 1", adj=0)


87arrows(x1=23, y1=res2[26-17],x2=26, y2=res2[26-17], size=.25,open=T)text(x=21.1, y=res2[26-17], "Color 2", adj=0)arrows(x1=28.9, y1=res3[24-17],x2=24, y2=res3[24-17], size=.25,open=T)text(x=29, y=res3[24-17], "Color 3", adj=0)arrows(x1=25.9, y1=res4[23-17],x2=23, y2=res4[23-17], size=.25,open=T)text(x=26, y=res4[23-17], "Color 4", adj=0)To test whether the width effect changes at each color, we can test the significance <strong>of</strong> a color by widthinteraction by fitting a new model with the addition <strong>of</strong> this interaction <strong>and</strong> comparing the model deviancewith that <strong>of</strong> the previous fit.crab.fit.logist.ia


88Judging by the p-value, we can go with the simpler (fewer parameters) quantitative color model.Other scores can be created by using logical operators. For example, the set <strong>of</strong> binary scores {1, 1, 1, 0}are created bytable.4.3$C.bin


89type -0.5980609duration -0.9190218 0.5496310type:duration 0.5481108 -0.9137683 -0.5964068The high negative correlation between duration <strong>and</strong> the intercept can probably be reduced byst<strong>and</strong>ardizing duration:sorethroat.lg


90# R: legend(x=100, y=.6, legend=list("Tracheal","Laryngeal"), lty=1:2)prob0.0 0.2 0.4 0.6 0.8 1.0TLTT LL LLTLTLLT TLT T LL T L T LTrachealLaryngealT20 40 60 80 100 120durationWe can test for a type difference at a particular duration, say at 60 minutes, which is about 0.5 st<strong>and</strong>arddeviations based on the mean <strong>and</strong> st<strong>and</strong>ard deviation <strong>of</strong> duration. The I( ) function is used so that(scale(duration)-0.5) is interpreted as is, meaning as a number, here.sorethroat.lgA


91One result we can obtain from setting different intercepts is a prediction <strong>of</strong> the duration at which prob(sorethroat upon awakening) = .25, .5, .75 for tracheal tubes/laryngeal mask. We can use the functiondose.p from the MASS library. The second argument to the function (i.e., c(1, 3) or c(2, 3) ) refers to thecoefficients specifying the common slope <strong>and</strong> separate intercept. For tracheal tubes, they are thetype.factrach coefficient <strong>and</strong> scale(duration) coefficient. The third argument refers to theprobability points (.25, .5, .75).For tracheal tubes, we have the following predictions:library(MASS)dose.p(sorethroat.lgB, c(1, 3), (1:3)/4)Dose SEp = 0.25: -0.61499711 0.3808615p = 0.50: -0.04219353 0.3567465p = 0.75: 0.53061006 0.4543964And, for laryngeal mask, we have:dose.p(sorethroat.lgB, c(2, 3), (1:3)/4)Dose SEp = 0.25: -1.4799537 0.5190731p = 0.50: -0.9071502 0.3720825p = 0.75: -0.3343466 0.3231864Of course, the predicted durations are in st<strong>and</strong>ard deviation units. The probability <strong>of</strong> sore throat ispredicted to be higher at lower durations (“doses”) when one is using the laryngeal mask.To print residual plots for this model, use the plot.glm function, which is called via plot with firstargument a glm object.par(mfrow=c(2,2))plot(sorethroat.lgB,ask=T)


92Chapter 6 – Building <strong>and</strong> Applying Logistic RegressionModelsA. Summary <strong>of</strong> Chapter 6, AgrestiThis chapter discusses logistic regression further, emphasizing practical issues related to modelchoice <strong>and</strong> model assessment. In Section 6.6, other link functions besides logit link are discussed forbinary data.We would like to fit a model that is rich enough to describe the data, but does not overfit the data.We also must be aware <strong>of</strong> issues such as multicollinearity among predictors. Multicollinearity may causerelated predictors to appear nonsignificant marginally. Stepwise procedures (forward selection, backwardelimination, or both) can be used to select predictors for an exploratory analysis. Forward selection addsterms sequentially until further terms do not improve the fit (based on a criterion such as Akaike’sInformation Criterion, AIC). Backward elimination starts with a model containing many terms <strong>and</strong>sequentially removes terms that do not add “significantly” to the fit. Once the final model is obtained, p-values must be interpreted cautiously because they are usually based on knowing the model form prior tolooking at the data (whereas, with stepwise selection we use the data to choose the model). Bootstrapadjustments may be needed for hypothesis tests <strong>and</strong> st<strong>and</strong>ard errors. In certain cases, conceptuallyimportant predictors should be included in a model, even though they may not be statistically significant.Model building can take advantage <strong>of</strong> causal diagrams that dictate conditional independencerelations among a set <strong>of</strong> variables. In this way, the causal diagram guides what models should be fit.Agresti gives an example from a British data set on extra-marital sex.Section 6.2 expounds on diagnostics for logistic regression analysis. These include residuals(Pearson <strong>and</strong> deviance), influence diagnostics or case-deletion diagnostics (e.g., Dfbetas, <strong>and</strong>confidence interval diagnostic that measures the change in a joint confidence region on a set <strong>of</strong>parameters after deleting each observation), <strong>and</strong> measures <strong>of</strong> predictive power (e.g. R-squared likemeasures, ROC curves).Section 6.3 deals with conditional inference from 2 x 2 x K tables. Testing conditional independence<strong>of</strong> a binary response Y <strong>and</strong> a binary predictor X conditional on the level <strong>of</strong> a third variable Z can be doneusing a test <strong>of</strong> the appropriate parameter <strong>of</strong> a logit model or using the Cochran-Mantel-Haenszel (CMH)Test (The CMH test is a score statistic alternative for the LR test <strong>of</strong> the logit model parameters). The logitmodel tests differ depending on whether the association between the predictor <strong>and</strong> response is assumedthe same at each level <strong>of</strong> the third variable (i.e., no XZ interaction) or it is assumed to differ.The CMH test conditions on both response Y <strong>and</strong> predictor X totals within each level <strong>of</strong> Z. Then, thefirst cell count in each table has a hypergeometric distribution (independent <strong>of</strong> the other tables). TheCMH statistic compares the sum <strong>of</strong> the first cell counts across the K tables to its expected value <strong>of</strong>conditional independence within strata. The asymptotic distribution <strong>of</strong> the statistic is chi-squared.Section 6.4 discusses the use <strong>of</strong> parsimonious models to improve inferential power <strong>and</strong> estimation.One example <strong>of</strong> this concept is illustrated by showing the improved power in testing for an association inthe presence <strong>of</strong> a logit model when a predictor has ordinal levels. In this case, the use <strong>of</strong> numericalscores in place <strong>of</strong> the nominal levels <strong>of</strong> the ordinal predictor results in fewer parameters in the model <strong>and</strong>ultimately results in better power <strong>and</strong> smaller asymptotic variability <strong>of</strong> the cell probability estimates.Section 6.5 discusses power <strong>and</strong> sample size calculations for the two-sample binomial test, test <strong>of</strong>nonzero coefficient in logistic regression <strong>and</strong> in multiple logistic regression, <strong>and</strong> chi-squared test incontingency tables. The formulas provide rough indications <strong>of</strong> power <strong>and</strong>/or sample size, based onassumptions about the distribution <strong>of</strong> predictors <strong>and</strong> <strong>of</strong> the model probabilities expected.When samples are small compared to the number <strong>of</strong> parameters in a logistic regression model,conditional inference may be used. With conditional inference, inference for a parameter conditions onsufficient statistics for remaining parameters, thereby eliminating them. What remains is a conditionallikelihood that only depends on the parameter <strong>of</strong> interest. In many respects, a conditional likelihood canbe used like an ordinary likelihood, giving conditional MLEs <strong>and</strong> asymptotic st<strong>and</strong>ard errors. Exactinference for parameters uses the conditional distributions <strong>of</strong> their sufficient statistics. Conditional


93inference is also used for sparse tables (with many zeroes) <strong>and</strong> tables that display “separation” where thesuccess cases all correspond to one level <strong>of</strong> the risk factor.B. Model Selection for Horseshoe Crab DataBackward elimination is done for the Horseshoe Crab data <strong>of</strong> Table 4.3 in order to select aparsimonious logistic regression model that predicts the probability <strong>of</strong> a female crab having a satellite.We begin by putting all the variables from Table 4.3 into a model. Agresti uses two dummy variables forthe variable spline condition, which we create by forming factors on the two variables.options(contrasts=c("contr.treatment", "contr.poly"))table.4.3$C.fac


94To perform stepwise selection <strong>of</strong> predictor variables for various types <strong>of</strong> fitted models (including glmobjects), one can use the function step with a lower <strong>and</strong> upper model specified. The criterion is AIC.Only the final model is printed unless trace=T is specified. An example <strong>of</strong> a call to the glm method isstep.glm(fit, scope=list(lower = formula(fit), upper = ~ .^2 ), scale=1, trace=T,direction="both")The above call specifies both forward <strong>and</strong> backward stepwise selection <strong>of</strong> terms (direction="both").The scope <strong>of</strong> the selection has a lower bound or starting model as “fit”. The upper bound modelincludes all two-way interactions. The component “anova” gives a summary <strong>of</strong> the trace path.To illustrate stepwise procedures, we perform backward elimination on a model fitted to the horseshoecrab data. This model includes up to a three-way interaction among Color, Width, <strong>and</strong> Spine Condition.We fit this model in S-<strong>PLUS</strong> or R usingcrab.fit.logist.stuffed


95extramarital sex, <strong>and</strong> marital status (divorced, still married) is given in Table 6.3 (p. 217, Agresti). Thedata come from a British survey <strong>of</strong> a sample <strong>of</strong> men <strong>and</strong> women who had petitioned for divorce, <strong>and</strong> asimilar number <strong>of</strong> married people.The causal diagram indicates a conditional independence relation: M <strong>and</strong> G are conditionallyindependent given E <strong>and</strong> P. Thus, if we broke the arrows connecting E to M <strong>and</strong> P to M, there would beno path between G <strong>and</strong> M. A logit model with M as response, then, might have E <strong>and</strong> P as explanatoryvariables, but not G. This model <strong>and</strong> the remaining in Table 6.4 (p. 218, Agresti) are fitted below using S.First, I enter the data (see note at end <strong>of</strong> this section)table.6.3


962 E + P + E:P 1032 1331.266 +E:P 1 12.914043 E + P + G + E:P 1031 1326.718 +G 1 4.54768Adding E:P to a model with E <strong>and</strong> P reduces the deviance by about 13 points (=18.2 – 5.2). Furtheradding G reduces deviance by 4.5 points ( = 5.2 – 0.7). Thus, we see how Table 6.4 is obtained.I have since found this data set available in the R package vcd. Thus, if you have loaded the vcd library,just issue the comm<strong>and</strong>: data(PreSex), to have the PreSex data array available. The functionas.data.frame() can be used to transform it to a data frame.D. Logistic Regression DiagnosticsThis section gives more details on diagnostics for logistic regression. After illustrating each set <strong>of</strong>procedures, I use the two data sets in Subsections 6.2.2 <strong>and</strong> 6.2.3 in Agresti to demonstrate their use inS.1. Pearson, Deviance <strong>and</strong> St<strong>and</strong>ardized ResidualsWe already illustrated Pearson residuals in Chapter 3, Section D.1 <strong>and</strong> Chapter 4, Section F. The sum <strong>of</strong>the squared Pearson residuals is equal to the Pearson chi-squared statistic. For a logistic regressionmodel, with responses as counts out <strong>of</strong> totals, n i, i=1,…,N, the fitted response value at the ithcombination <strong>of</strong> the covariates is nπ ˆi i. So, the Pearson <strong>and</strong> deviance residuals use deviations <strong>of</strong> theobserved responses from these fitted values. Pearson residuals are divided by an estimate <strong>of</strong> thest<strong>and</strong>ard deviation <strong>of</strong> an observed response, <strong>and</strong> the st<strong>and</strong>ardized version is further divided by thesquare root <strong>of</strong> the 1 − the ith estimated leverage value (ith diagonal <strong>of</strong> estimated “hat” matrix). Thisst<strong>and</strong>ardized residual is approximately distributed st<strong>and</strong>ard normal when the model holds. Thus,absolute values <strong>of</strong> greater than about three provide evidence <strong>of</strong> lack <strong>of</strong> fit.Deviance residuals were illustrated for S in Chapter 4, Section F. These are the signed squareroots <strong>of</strong> the components <strong>of</strong> the LR statistic. St<strong>and</strong>ardized deviance residuals are approximatelydistributed st<strong>and</strong>ard normal.These residuals can be plotted against fitted linear predictors to detect lack <strong>of</strong> fit, but as Agrestisays, they have limited use. When n i= 1, individual residuals can be either uninteresting (Pearson) oruninformative (deviance).The heart disease data in Agresti (Table 6.5, p. 221) classifies blood pressure (BP) for a sample <strong>of</strong> maleresidents aged 40-59, into one <strong>of</strong> 8 categories. Then, a binary indicator response is whether each m<strong>and</strong>eveloped coronary heart disease (CHD) during a six-year follow-up period. An independence model isfit initially. This model has BP independent <strong>of</strong> CHD.BP


97pear.std.indep


98dev.indep


99table.6.7


1003. Summarizing Predictive Power: R <strong>and</strong> R-squared MeasuresThe function lrm from Harrell’s Design library (available for both R <strong>and</strong> S-<strong>PLUS</strong>) computes measures <strong>of</strong>predictive ability <strong>of</strong> a logistic regression model, including R <strong>and</strong> R-squared-like measures. Thesemeasures can also be validated using resampling (function validate.lrm). This function calculatesbias-corrected estimates <strong>of</strong> predictive ability. Bias is incurred when the data used to fit a model is alsoused to assess its predictive ability on a potential new data set. Model selection, assessment, <strong>and</strong> tuning<strong>of</strong> a model fit are usually not taken into account when one computes a measure <strong>of</strong> predictive power.These activities can bias the computed measure upward. Thus, bias-correction is in order.To illustrate the variety <strong>of</strong> measures computed by these functions, I use lrm to fit the linear logit model tothe blood pressure data. lrm requires binary responses instead <strong>of</strong> count response, so I form table.6.5out <strong>of</strong> objects defined previously. Note that I set the arguments x <strong>and</strong> y to TRUE so that we can usevalidate.lrm later on. First, when loading the libraries, set first=T so that certain functions fromDesign don’t get confused with built-in ones from S-<strong>PLUS</strong> (done automatically in R).library(Hmisc, first=T) # first=T not needed in Rlibrary(Design, first=T) # R: loads Hmisc upon loading Design#BP


101R1− exp( −( L −LM)/ n)=1−exp( −L/ n)2 0N0where L 0is the deviance (-2 times log likelihood) under the null model, <strong>and</strong> L Mis the deviance under theunconstrained model, <strong>and</strong> n is the number <strong>of</strong> responses. All three <strong>of</strong> these will differ when using grouped2versus ungrouped data, <strong>and</strong> the R-squared values will differ. Consider the value <strong>of</strong> RNfor the glm fit,resLL


102I only show the two indexes <strong>of</strong> predictive accuracy, Dxy (Somer’s D) <strong>and</strong> R2, but several others areoutput as well. The index.corrected column is the row index corrected for bias due to over-optimism(i.e., index.orig – optimism). The optimism column is computed as the difference between theaverage <strong>of</strong> the indexes calculated from the training samples <strong>and</strong> the average calculated from the testsamples. Here, there is very little over-optimism, so the corrected estimates are very close to the originalindexes.validate.lrm(res.lrm, method = "boot", B = 100)# excerptindex.orig training test optimism index.corrected nDxy 0.272819936 0.270357318 0.27281993603 -0.0024626183 0.27528255435 100R2 0.045469052 0.047315513 0.04546905186 0.0018464612 0.04362259074 100..........snipHarrell has written much about the topic <strong>of</strong> assessing predictive ability from linear, logistic, <strong>and</strong> survivalmodels. The above information came from a preprint (Harrell, 1998) <strong>of</strong> his now-published Springer book.In addition, on the website for one <strong>of</strong> my courses at UHCL (http://math.cl.uh.edu/~thompsonla/5537), Ihave html documents <strong>and</strong> S-<strong>PLUS</strong> scripts illustrating many <strong>of</strong> the lrm model assessments on GLIMs,with annotation.After using Design <strong>and</strong> Hmisc, we should detach them.detach("Design") # R: detach("package:Hmisc")detach("Hmisc") # R: detach("package:Design")E. Inference about Conditional Associations in 2 x 2 x K TablesIn this section, I show how to test for conditional independence between response Y <strong>and</strong> predictor X in 2x 2 x K tables using S, where K is the number <strong>of</strong> levels <strong>of</strong> a stratification variable. Agresti illustrates a testusing an appropriate logit model <strong>and</strong> the Cochran-Mantel-Haeszel Test. Table 6.9 (p. 230, Agresti) givesresults <strong>of</strong> a clinical trial with eight centers. The study compared two cream preparations, an active drug<strong>and</strong> a control, on their success in treating an infection. This data set is available on Agresti’s text website(I have changed the name, <strong>and</strong> changed the extension to “ssc”). I read it in using scan in order to readthe numeric levels (1 <strong>and</strong> 2) for the response <strong>and</strong> treatment as characters. Then, I make it a data frame<strong>and</strong> change the levels to something more meaningful.table.6.9


103}if (l == 2 && dim(x) != c(2, 2))stop("Not a 2 x 2 - table.")if (!is.null(stratum) && dim(x)[-stratum] != c(2, 2))stop("Need strata <strong>of</strong> 2 x 2 - tables.")lor


104Thus, we reject the null hypothesis <strong>of</strong> conditional independence between Treatment <strong>and</strong> Response givenCenter. (The common odds ratio result is discussed later). We can also use the logit model test. This isa test that the coefficient corresponding to Treatment in a logit model is zero, given the model includesCenter (see equation (6.4) in Agresti). If we use glm, we need to make a few modifications to the dataframe. Below, I add the total to each Center/Treatment combination.n


105Exact conditional test <strong>of</strong> independence in 2 x 2 x k tablesdata: table.6.9.arrayS = 55, p-value = 0.01336alternative hypothesis: true common odds ratio is not equal to 195 percent confidence interval:1.137370 4.079523sample estimates:common odds ratio2.130304If we assume that the log odds ratios have different directions across strata, we can test conditionalindependence using a logit model fit. The test is a likelihood-ratio test <strong>of</strong> model (6.5) in Agresti (only aCenter effect) compared with a saturated model (Center, Treatment <strong>and</strong> Center:Treatment). With thistest, we also reject the null hypothesis.res2


106Treatment 1 6.66882 7 9.74632 0.009811426Alternatively, the Woolf test is available in the R package vcd, under woolf.test. The null hypothesis isno three-way (XYZ) interaction, or homogeneous odds ratios across Centers. The function can also beused within S-<strong>PLUS</strong> with no modification.library(vcd)woolf.test(table.6.9.array)Woolf-test on Homogeneity <strong>of</strong> Odds Ratios (no 3-Way assoc.)data: table.6.9.arrayX-squared = 5.818, df = 7, p-value = 0.5612G. Using Models to Improve Inferential PowerAs Agresti says, ordinal test statistics in categorical data analysis usually refer to narrower, more relevantalternatives than do nominal test statistics. They also usually have more power when an approximatelinear trend actually exists between the response <strong>and</strong> a predictor variable. A linear trend model (e.g.,linear logit model) has fewer parameters to estimate than does a nominal model with a separateparameter for each level <strong>of</strong> the ordinal variable. The LR or Pearson goodness-<strong>of</strong>-fit statistic used to testthe linear trend parameter <strong>of</strong> a linear logit model has only 1 degree <strong>of</strong> freedom associated with it,whereas the test <strong>of</strong> the corresponding nominal model against an independence model has degrees <strong>of</strong>freedom equal to one less than the number <strong>of</strong> levels <strong>of</strong> the variable. Thus, the observed value <strong>of</strong> the teststatistic in the former case does not have to be as large (compared to the chi-squared distribution) as thatin the second case in order to reject the independence model over the linear logit model .Agresti uses an example on the treatment <strong>of</strong> leprosy by sulfones <strong>and</strong> streptomycin drugs to illustrate thedifference in power to detect a suspected association (Table 6.11, p. 239). The degree <strong>of</strong> infiltrationmeasures the amount <strong>of</strong> skin damage (High, Low). The response, the amount <strong>of</strong> clinical change incondition after 48 weeks, is ordinal with five categories (Worse, …, Marked Improvement). The responsescores are {-1, 0, 1, 2, 3}. Agresti compares the mean change for the two infiltration levels, <strong>and</strong> notesthat this analysis is identical to a trend test treating degree <strong>of</strong> infiltration as the response <strong>and</strong> clinicalchange as the predictor. Thus, a linear logit model has the logit <strong>of</strong> the probability <strong>of</strong> high filtration linearlyrelated to the change in clinical condition.In S, first we set up the data, then fit a model with a separate parameter for each change category.table.6.11


107NULL 4 7.277707change 4 7.277707 0 0.000000 0.1219205With p = 0.12, this test does not reject an independence model. Now, we fit a linear logit model usingscores c(-1, 0, 1, 2, 3), <strong>and</strong> test it against an independence model.resLL.leprosy


108bpower(n=200, p1=.6, p2=.7, alpha=0.05)Power0.3158429where we assume the 0.10 difference is divided as p1 = 0.6 <strong>and</strong> p2 = 0.7, <strong>and</strong> n is the sum <strong>of</strong> n1 <strong>and</strong> n2(with n1 = n2). bpower only computes power for a two-sided alternative.With n1 = 20 <strong>and</strong> n2 = 25, we get slightly lower powerbpower(n1=20,n2=25,p1=.6,p2=.7)Power0.1084922binomial.sample.size also computes sample size needed to achieve a given power. The functionbsamsize in package Hmisc does as well. Again, the latter is closer to Agresti’s formula on p. 242, whichhe says is an underestimate <strong>of</strong> the sample size.To compute sample size required to achieve 90% power with p1 = 0.6 <strong>and</strong> p2 = 0.7, <strong>and</strong> an 0.05-leveltest, we need close to 500 subjects for each group.binomial.sample.size(p=.6, p2=.7, alpha=.05,alternative="two.sided", power=.9)p1 p2 delta alpha power n1 n2 prop.n21 0.6 0.7 0.1 0.05 0.9 497 497 1bsamsize(power=.9, p1=.6, p2=.7, alpha=0.05)n1 n2476.0072 476.0072Sample size calculations for logistic regression can easily be programmed into a simple S function,following the formulas on p. 242 in Agresti.Power for a chi-squared test can be computed using the following function in S-<strong>PLUS</strong>:chipower.f


109As a first example, I use Table 6.13 in Agresti. This table <strong>and</strong> its “true” joint probabilities from thephysician are input below. Then I fit two models, one with the st<strong>and</strong>ard <strong>and</strong> new therapies independentlyinfluencing the response, <strong>and</strong> one model with st<strong>and</strong>ard therapy alone influencing the response. We willcompute the power for detecting an independent effect <strong>of</strong> the new therapy on response, over <strong>and</strong> abovethat from the st<strong>and</strong>ard therapy.table.6.13


110As described in Section 6.6 in Agresti, the idea behind probit models is that there is a latent tolerancevalue underlying each binary response. The tolerance is subject-specific. When a linear combination <strong>of</strong>the predictor variables is high enough to exceed an individual’s tolerance, the binary response becomes 1<strong>and</strong> remains there. Otherwise, it remains at zero. The tolerance is a continuous r<strong>and</strong>om variable. Whenit has a normal distribution, we get a probit model. Probit models are used in toxicological experimentswhere the predictor is dosage. So, when the dosage exceeds a threshold (tolerance), the response isdeath.In GLIM terminology, the probit link function is the inverse CDF <strong>of</strong> the st<strong>and</strong>ard normal distribution(equation 6.11 in Agresti). Agresti gives some differences between probit link <strong>and</strong> logit link for binomialregression. Fitting the probit model is fitting a GLIM, so no new estimation procedures are introduced.Estimates from Newton-Raphson <strong>and</strong> Fisher scoring will have slightly different estimated st<strong>and</strong>ard errorsbecause observed <strong>and</strong> expected information differ in the probit model. The probit is not the canonical linkfor a binomial model.Agresti fits a probit model to the beetle mortality data in Table 6.14.table.6.14


111log.dose n y fitted.probit fitted.cloglog1 1.691 59 6 3.4 5.72 1.724 60 13 10.7 11.33 1.755 62 18 23.4 20.94 1.784 56 28 33.8 30.35 1.811 63 52 49.6 47.76 1.837 59 53 53.4 54.27 1.861 62 61 59.7 61.18 1.884 60 60 59.2 59.9plot(table.6.14$log.dose,table.6.14$y/table.6.14$n, pch=16, xlab="Log dosage",ylab="Proportion Killed", bty="L", axes=F)axis(1, at=seq(1.7, 1.9, .05))axis(2, at=seq(0,1,.2))lines(table.6.14$log.dose, fitted(res.probit), lty=2)lines(table.6.14$log.dose, fitted(res.cloglog), lty=1)key(x=1.8, y=.4, text=list(c("Complementary log-log", "Probit")),lines=list(type=c("l", "l"), lty=c(1,2)), border=F,text.width.multiplier=.8)# R: legend(x=1.8,y=.4,legend=c("Complementary log-log", "Probit"),lty=c(1,2),cex=.85, text.width=1)1.00.8Proportion Killed0.60.4Complementary log-logProbit0.21.70 1.75 1.80 1.85Log dosageJ. Conditional Logistic Regression <strong>and</strong> Exact Distributions1. Small-sample Conditonal Inference for 2x2 Contingency tablesSmall-sample conditional tests for 2 x 2 tables use Fisher’s Exact test. See the discussion in Chapter 3.2. Small-sample Conditonal Inference for Linear Logit ModelsSmall-sample conditional inference for the linear logit model is an exact conditional trend test, <strong>and</strong> theconditional distribution <strong>of</strong> the cell counts under the null hypothesis <strong>of</strong> β = 0 is multiple hypergeometric.Agresti applies an exact test to Table 5.3 on maternal alcohol consumption <strong>and</strong> infant malformation. This


112data set has a lot <strong>of</strong> cases, but the table is sparse in that there are few successes or cases. Thus,ordinary maxmimum likelihood inference (with its asymptotic basis) breaks down.To do conditional logistic regression in S, one can use the coxph function (or the clogit function in R,which is just a wrapper for coxph). However, with more than about 500 cases (in my experience), thefunction hangs “forever”. Thus, we turn to approximations to conditional logistic regression for largesamples. There are S functions available for doing approximate conditional logistic regression on ascalar parameter <strong>of</strong> interest. One is cond.glm (normal approximation) from Aless<strong>and</strong>ra Brazzale (seehttp://www.ladseb.pd.cnr.it/~brazzale/lib.html#ins). Another is cox.test (for a normal approximation)<strong>and</strong> cpvalue.saddle <strong>and</strong> cl.saddle (for a saddle-point approximation) from Chris Lloyd (Lloyd, 1999).These latter functions are apparently available on the Wiley website (http://www.wiley.com), but I havenot been able to locate them yet. I contacted Pr<strong>of</strong>essor Lloyd, via the Wiley website, for copies.First, I show how to use coxph <strong>and</strong> clogit for an exact conditional test. However, the problem is muchtoo large to use this method. So, I do not give results. The response value must be binary (“cases”indicate successes). Here I create a data frame with case as the response, <strong>and</strong> alcohol as a numericexplanatory variable.# recall from chapter 5malformed


113conditional estimate <strong>of</strong> the coefficient <strong>and</strong> its st<strong>and</strong>ard error. A summary function gives hypothesis testsabout this coefficient.# R: library(cond)(fit


114Modifications within R would be similar.To use cl.saddle on table.5.3, which is quite large, we type the following. I choose to compute B=20points <strong>of</strong> the conditional log-likelihood, within the range –1 to 1 <strong>of</strong> the parameter value. The termargument specifies the parameter <strong>of</strong> interest.attach(table.5.3)(approx.points


115mantelhaen.test(xtabs(count~month+race, data=table.6.15), exact=T, alternative=”less”)Exact conditional test <strong>of</strong> independence in 2 x 2 x k tablesdata: xtabs(count ~ race + promote + month, data = table.6.15)S = 0, p-value = 0.02566alternative hypothesis: true common odds ratio is less than 195 percent confidence interval:0.0000000 0.7795136sample estimates:common odds ratio0The result using S-<strong>PLUS</strong> <strong>and</strong> the correct=F argument ismantelhaen.test(design.table(table.6.15), correct=F)Mantel-Haenszel chi-square test without continuity correctiondata: design.table(table.6.15)Mantel-Haenszel chi-square = 4.5906, df = 1, p-value = 0.0321A 95% two-sided confidence interval on the odds ratio is printed automatically.# R onlymantelhaen.test(xtabs(count~race+promote+month, data=table.6.15), exact=T, alternative= "two.sided")Exact conditional test <strong>of</strong> independence in 2 x 2 x k tablesdata: xtabs(count ~ race + promote + month, data = table.6.15)S = 0, p-value = 0.05625alternative hypothesis: true common odds ratio is not equal to 195 percent confidence interval:0.000000 1.009031sample estimates:common odds ratio0A final example is given where the table displays “separation”. That is, the cases all correspond to onelevel <strong>of</strong> the risk factor. In these situations, as Agresti says, maximum likelihood estimation gives infiniteestimates. Thus, exact inference is needed. Since the number <strong>of</strong> observations is quite large, we can’tuse coxph. Even if we take advantage <strong>of</strong> the fact that we really have a 2x2 table in Cephalexin <strong>and</strong>Diarrhea (as mentioned by Agresti, bottom <strong>of</strong> p. 256), we still have over 1,000 observations. However,with the 2x2 table, we can use Fisher’s Exact Test. Note that S-<strong>PLUS</strong> won’t h<strong>and</strong>le this data set becauseit is too large (> 200 counts total). However, R h<strong>and</strong>les it.table.6.16


116odds ratioInfNote that we get the confidence interval as well.K. Bias-reduced Logistic RegressionIn cases <strong>of</strong> quasi or complete separation, a different kind <strong>of</strong> estimation can be done instead <strong>of</strong> ordinarymaximization. Bias-reduced logistic regression (Firth, 1993, as cited in R help file for package brlr)maximizes a penalized likelihood with penalty function, Jeffreys invariant prior. This leads to less biasedestimates that are always finite. According to Firth, the bias reduction (toward zero) can be quitenoticeable for problems that display separation. Here, we fit the main-effects only logistic regressionmodel from subsection J to the diarrhea data, using glm (ML) <strong>and</strong> brlr (penalized ML). Note thedifference in the coefficient estimate for Cephalexin, as well as its smaller st<strong>and</strong>ard error from thepenalized estimation. The large magnitude <strong>of</strong> the Ceph estimate <strong>and</strong> its corresponding large st<strong>and</strong>arderror from the glm fit indicate an infinite maximizer.library(brlr)fit


117Chapter 7 – Logit Models for Multinomial ResponsesA. Summary <strong>of</strong> Chapter 7, AgrestiIn Chapter 7, Agresti discusses logit models for multinomial responses, both nominal responses<strong>and</strong> ordered responses. Multinomial logit models for nomial responses use a separate binary logit modelfor each pair <strong>of</strong> response categories (some <strong>of</strong> which are redundant), <strong>and</strong> are fit using maximum likelihoodestimation subject to the individual response probabilities simultaneously satisfying their separate logitmodels.There are several types <strong>of</strong> models for ordinal response categories. Section 7.2 discussescumulative logit models, which use the logits <strong>of</strong> the cumulative probabilities PY ( ≤ j| x ) =π ( ) ... ( )1x + + π jx , j = 1,…,J. So, the cumulative logit is the log <strong>of</strong> the odds <strong>of</strong> responding in category upto j. A cumulative logit model models all J – 1 cumulative logits simulataneously. A popular cumulativelogit model is a proportional odds model. This model assumes the J – 1 logits have different intercepts,increasing in j, but otherwise the coefficients are identical. Thus, the J – 1 response curves have thesame shape, but are shifted horizontally from one another. The term proportional odds applies becausethe log <strong>of</strong> the odds ratio <strong>of</strong> cumulative probabilities (comparing the conditions in one covariate vector toanother) is proportional to the distance between the covariate vector. The same proportionality constantapplies to each logit. Proportional odds models are fit using maximum likelihood estimation.A latent variable interpretation <strong>of</strong> the proportional odds model assumes an underlying(unobserved) continuous response variable that has a logistic distribution. When this response fallsbetween the (j – 1)th <strong>and</strong> jth cutpoints on the response scale, the observed discrete response value is j.Effects <strong>of</strong> explanatory variables are invariant to the choice <strong>of</strong> cutpoints for the response so that thecoefficients are identical across logits.Section 7.3 discusses cumulative link models for ordinal response categories. Here, we use alink function that relates the cumulative probabilities to a linear predictor involving the covariates. Theuse <strong>of</strong> the logit link is the cumulative logit model. The probit link gives the cumulative probit model.These models assume that the ordinal response categories have an underlying continuous distributionthat is related to the particular link function. They also assume that the distributions <strong>of</strong> each covariatesetting are stochastically ordered, so that the cumulative probabilities given one covariate setting arealways at least as great or at least as less as the cumulative probabilities <strong>of</strong> another covariate setting. Ifthis is not so, then one covariate setting may differ in dispersion <strong>of</strong> responses than another covariatesetting.Other models for ordinal responses are discussed in Section 7.4. These include adjacentcategorieslogit models, continuation-ratio logit models, <strong>and</strong> mean response models. The adjacentcategorieslogits are the J – 1 logits formed by the log odds <strong>of</strong> the probability <strong>of</strong> response falling incategory j given that the response falls within either category j or j + 1, j = 1,…,J – 1. Adjacent-categorieslogit models differ from cumulative logit models in that effects <strong>of</strong> explanatory variables refer to individualresponse categories <strong>and</strong> not cumulative groups. They also do not assume an underlying latentcontinuous response.Continuation-ratio logits, as given in Agresti, are the logits <strong>of</strong> the conditional probabilities that theresponse falls in the jth category given that it falls at least in the jth category for j = 1,…,J – 1. The fulllikelihood is the product <strong>of</strong> multinomial mass functions, which can each be represented as a product <strong>of</strong>binomial mass functions, leading to an easy way to estimate these models.Finally, mean response models for ordered responses are linear regression models for ordinalresponses, represented by numerical scores. The linear probability model is a special case. They canbe fit using maximum likelihood estimation, where the sampling model is product multinomial.To test conditional independence in I x J x K tables, where one factor Z is conditioned, one canuse multinomial models such as cumulative logit models or baseline-category logit models depending onwhether the response Y is ordinal or nominal, respectively. The test <strong>of</strong> homogenous XY associationacross levels <strong>of</strong> Z is then a test <strong>of</strong> the inclusion <strong>of</strong> a term or terms involving X.Generalized Cochran-Mantel Haenszel (CMH) tests can be used to test conditionalindependence for I x J x K tables with possibly ordered categories. With both X <strong>and</strong> Y ordinal, the


118generalization is Mantel’s M 2 statistic. Some versions <strong>of</strong> the generalized CMH statistic correspond toscore tests for various multinomial logit models.Discrete-choice multinomial logit models use a different set <strong>of</strong> values for a categoricalexplanatory variable for different response choices. Conditional on the choice set (e.g., choosing whichtype <strong>of</strong> transportation one prefers), the probability <strong>of</strong> selecting option j is a logistic function <strong>of</strong> theexplanatory variables. The model can also incorporate explanatory variables that are characteristic <strong>of</strong> theperson doing the choosing. Fitting these models using maximum likelihood can be done using the coxphfunction in R or S-<strong>PLUS</strong>, as the likelihood has the same form as the Cox partial likelihood. This can beseen from equation (7.22) in Agresti.B. Nominal Responses: Baseline-Category Logit ModelsWhen responses are nominal, for a given covariate pattern, x, the vector <strong>of</strong> counts at the Jresponse categories is assumed multinomial with probability vector { π1( x),..., π J( x )} . The J – 1 uniquelogits pair each <strong>of</strong> J – 1 response categories with a baseline category (say, the Jth category). As shownon p. 268 <strong>of</strong> Agresti, from these J – 1 logits that compare each category with a baseline, we can estimatelogits comparing any two categories.Agresti uses the Alligator Food Choice data set to fit a multinomial logit model. The measuredresponse is the category <strong>of</strong> primary food choice <strong>of</strong> 219 alligators caught in four Florida lakes (fish,invertebrate, reptile, bird, other). The explanatory variables are all categorical: L = lake <strong>of</strong> capture, G =gender, S = size ( ≤ 2.3 m, > 2.3 m). The data can be entered into S using the following comm<strong>and</strong>s. Thelevels argument codes the first level indicated as the baseline category.food.labs


119deviance(fit4)-deviance(fitS)deviance(fit5)-deviance(fitS)deviance(fit0)-deviance(fitS)Collapsing over gender gives# options(contrasts=c("contr.treatment","contr.poly"))fitS


120hancock 2.3 9.1 0.4 1.1 2.3 3.1oklawaha 2.3 12.8 7.0 5.5 0.8 1.9trafford 2.3 8.6 5.6 5.9 3.1 5.8george 2.3 14.5 3.1 0.5 1.8 2.2Each <strong>of</strong> the 4 binary logit models has a set <strong>of</strong> estimated effects or coefficients. These can be extractedusing the summary function, as here. Based on the way the factor levels were given in the definition <strong>of</strong>table.7.1, the category fish is the baseline category, <strong>and</strong> each logit model compares the odds <strong>of</strong>selecting another food to the odds <strong>of</strong> selecting fish.library(MASS) # needed for vcov functionsummary(fit3, cor = F)Coefficients:(Intercept) size lakehancock lakeoklawaha laketraffordinvert -1.549021 1.4581457 -1.6581178 0.937237973 1.122002rep -3.314512 -0.3512702 1.2428408 2.458913302 2.935262bird -2.093358 -0.6306329 0.6954256 -0.652622721 1.088098other -1.904343 0.3315514 0.8263115 0.005792737 1.516461Std. Errors:(Intercept) size lakehancock lakeoklawaha laketraffordinvert 0.4249185 0.3959418 0.6128465 0.4719035 0.4905122rep 1.0530583 0.5800207 1.1854035 1.1181005 1.1163849bird 0.6622971 0.6424863 0.7813123 1.2020025 0.8417085other 0.5258313 0.4482504 0.5575446 0.7765655 0.6214372To estimate response probabilities using values on the explanatory variables that (together) did notappear as a data record, you can use predict, with newdata, a data frame containing the labeledexplanatory values. In S-<strong>PLUS</strong> (but not R), when we have categorical variables entered as factors in theoriginal fit, we have to specify the prediction values as being levels <strong>of</strong> those factors. This can be done byusing factor along with the original levels. So, to estimate the probability that a large alligator in LakeHancock has invertebrates as the primary food choice, we use in S-<strong>PLUS</strong># S-<strong>PLUS</strong>: options(contrasts=c("contr.treatment", "contr.poly"))predict(fit3, type="probs", newdata=data.frame(size=factor(">2.3", levels=c(">2.3","2.3", lake="hancock"))fish invert rep bird other0.57018414 0.02307664 0.07182898 0.14089666 0.19401358Obviously, the specification in R is how we would want to do it!To get the estimated response probabilities for all combinations <strong>of</strong> levels <strong>of</strong> the predictors, use a call toexp<strong>and</strong>.grid as the newdata. For example,predictions


121cbind(exp<strong>and</strong>.grid(size = size.labs, lake = lake.labs), predictions)size lake fish invert rep bird other1 2.3 hancock 0.5701841 0.02307664 0.07182898 0.140896663 0.194013583 2.3 oklawaha 0.4584248 0.24864188 0.19484366 0.029424140 0.068665475 2.3 trafford 0.2957470 0.19296047 0.20240167 0.108228505 0.200662307 2.3 george 0.6574619 0.13968168 0.02389991 0.081046954 0.09790956The same multinomial model can be fit using lcr in R package ordinal from P. Lindsey. However, thatfunction is much more complicated to use than multinom because it fits more flexible models. We willsee it in the next sections for ordinal responses.Another function that is as easy to use as multinom is vglm from R package VGAM (<strong>and</strong> soon to bepackage for S-<strong>PLUS</strong> 6.X, although it exists for the Unix versions). The comm<strong>and</strong>s for fitting the maineffects model above islibrary(vgam)fit.vglm


122Now, we fit the main effects modellibrary(MASS)fit.polr


123measurements <strong>and</strong> time-varying covariates. To use the function, we have to set up the response <strong>and</strong>covariate objects, then put them within a repeated object. These additional functions come from thermutil library, which is loaded with ordinal.library(ordinal)First, we must have a numeric response, as well as numeric covariates. So, we use the mentalC variable<strong>of</strong> table.7.5. It also must begin at 0, not 1. (Factor covariates must be transformed to dummy coding oranother numeric coding scheme. One can do this using the function wr, in the rmutil library.)# set up response vectory


124Call:nordr(w, dist = "prop", mu = ~ses + life, pmu = c(-0.5, 1, -0.3),pintercept = c(1, 2))proportional odds model-Log likelihood 49.54895AIC 54.54895Iterations 17Location coefficientsLocation function:~ses + lifeestimate s.e.(Intercept) -0.2817 0.6423ses[.i] 1.1111 0.6109life[.i] -0.3189 0.1210Intercept contrastsestimate s.e.b[2] 1.495 0.3898b[3] 2.491 0.5012Correlation matrix1 2 3 4 51 1.0000 -0.4449 -0.5924 -0.2557 -0.22292 -0.4449 1.0000 -0.1859 0.1885 0.23633 -0.5924 -0.1859 1.0000 -0.2312 -0.30034 -0.2557 0.1885 -0.2312 1.0000 0.71685 -0.2229 0.2363 -0.3003 0.7168 1.0000detach("table.7.5")To obtain a score test, we extract the likelihood attribute from the fitted object fit.lcr, <strong>and</strong> compare it tothe likelihood from fitting a model with separate sets <strong>of</strong> coefficients per response. This latter model canbe conveniently fitted using lcr as well.fit


125P(Y > 2)0.0 0.2 0.4 0.6 0.8 1.0SES=0SES=10 1 2 3 4 5 6 7 8 9Life Events IndexWe see that the probability <strong>of</strong> moderate or impaired mental health, P(Y > 2), is predicted to be greaterwith a higher life events index, <strong>and</strong> furthermore that a low SES has a higher predicted probability thanhigher SES.Fitting interaction models is straightforward. Different link functions other than the logit link can be usedin the function lcr <strong>and</strong> the function vglm, which is from the R package VGAM (coming soon to S-<strong>PLUS</strong> 6.x,but available for S-<strong>PLUS</strong> under L/Unix). This is illustrated in the next section. The function vglm can als<strong>of</strong>it partial proportional odds models mentioned on page 282 <strong>of</strong> Agresti. This is done by setting the zeroargument <strong>of</strong> the family function, cumulative. See the help file for details.D. Ordinal Responses: Cumulative Link ModelsTo illustrate the use <strong>of</strong> different link functions within an ordinal categorical model, Agresti uses a life tableby race <strong>and</strong> sex (Table 7.7). The percentages within the four populations indicate that the underlyingcontinuous cdf <strong>of</strong> life length increases slowly at low to moderate ages, then increases sharply at olderages. This pattern suggests a complementary log-log link function.Several different link functions can be used within a proportional odds model. The function lcr in the Rpackage ordinal fits a cumulative complementary log-log link model (as well as many other linkfunctions).First, we set up the data. I created a numeric response, LifeC, as well as a categorical one. lcrrequires a numerical response.table.7.7


126library(ordinal)y


127over 65 73.0 55.4 84.9 73.7The function vglm will support dispersion effects, but it requires some work. In theory, it is possible towrite your own family function to do this, by modifying the cumulative family.E. Ordinal Responses: Adjacent-Categories Logit ModelsAdjacent-categories logit models fit J – 1 logits involving the log odds <strong>of</strong> the probability that a responsefalls in category j given that it falls in either category j or j + 1, j = 1,…,J. These logits are modeled aslinear functions <strong>of</strong> explanatory variables asπj( x)log = αj+ β ′ x, j = 1,..., J −1(7.1)π +( x)j 1Library functions exist for directly fitting these models in R. However, as Agresti shows on p. 287, theycan also be fit using the equivalent baseline-category logit model. Thus, they can be fit in S-<strong>PLUS</strong> aswell.Agresti uses the Job Satisfaction Example to illustrate adjacent-categories logit models. The responsevariable is job satisfaction in categories (Very Dissatisfied, Little Satisfied, Moderately Satisfied, VerySatisfied). The explanatory variables are gender (1 = females) <strong>and</strong> income (< $5,000, $5,000 – $15,000,$15,000 – $25,000, > $25,000). Scores <strong>of</strong> 1 to 4 are used for income. The data set is available onAgresti’s web site. Here we read it into R/S-<strong>PLUS</strong> <strong>and</strong> create an ordered factor out <strong>of</strong> the response.table.7.8


128fit.vglm


129y


130The "Model L.R." given above is supposed to be the model likelihood ratio chisquare according to Harrell(1998). However, if you examine what glm gives, you can see that model L.R. is actually equal to –2LogLH(model with intercept + x). The d.f. above gives the number <strong>of</strong> d.f. used with the addition <strong>of</strong> x inthe model (i.e., 1). What Agresti gives (p. 291) is the model residual deviance. That is, he gives –2LogLH(model with intercept only) – (–2LogLH(model with intercept + x). His d.f. correspond to theresulting d.f. when going from an intercept model (df = 4) to a model with x (df = 3). These are the df <strong>and</strong>LR statistic given directly by glm when modeling a linear logit, as shown later.To fit the j = 2 model:fit=malformed * x 0.010986 0.0013020 8.44 0Thus, when y>=malformed (j = 2), the linear logit is –5.70 + .017x. When y=all (j = 1), the linear logit is –3.247 + .0064x. The less desirable outcome is more likely as the concentration increases.Notice that the values for model L.R. in the individual model sum to the model L.R. for the interactionmodel above. However, the d.f. do not add.1. Odds RatiosTo get selected odds ratios for the j = 2 model, first issue the datadist comm<strong>and</strong> <strong>and</strong> reissue the lrmcall, as follows:


131x.u


132fit$null.deviance[1] 259.1073fit$null.deviance-fit$deviance[1] 253.3298# this is what lrm gave us as model L.R.summary(fit)Null Deviance: 259.1073 on 4 degrees <strong>of</strong> freedomResidual Deviance: 5.777478 on 3 degrees <strong>of</strong> freedomThe difference <strong>of</strong> the above sets <strong>of</strong> values gives Null Deviance-Residual Deviance = 253.3298 <strong>and</strong> df=1.These are the Model L.R. <strong>and</strong> Model df reported by lrm.For the j = 2 model, take the second <strong>and</strong> third columns <strong>of</strong> table.7.9a, <strong>and</strong> use the second column asthe success:n2


133(Intercept1) -2.404463 0.1078119Concen 0.007184 0.0003578Correlation matrix1 2 31 1.0000 0.6162 -0.79632 0.6162 1.0000 -0.77383 -0.7963 -0.7738 1.0000Note the different estimated concentraton effect.4. Using vglm from library VGAMUsing vglm for continuation-ratio models is similar to using it for adjacent-categories logit models. We justchange the family function to sratio (or cratio). First, I illustrate sratio, as this matches Agresti’sdefinition <strong>of</strong> continuation-ratio logit. Both sratio <strong>and</strong> cratio have an argument, reverse, which fits thecorresponding logits in reverse order (see the help files).For the data, we use the contingency table format (table.7.9a), <strong>and</strong> cbind the response columns.x=2])Dispersion Parameter for sratio family: 1Residual Deviance: 11.83839 on 6 degrees <strong>of</strong> freedomLog-likelihood: -730.3872 on 6 degrees <strong>of</strong> freedomNumber <strong>of</strong> Iterations: 4Note that the Residual Deviance reported is the sum <strong>of</strong> the individual Residual Deviances (the likelihoodrati<strong>of</strong>it statistics) reported by glm.Now, the family function cratio fits the logits, logit(P(y > j | y ≥ j), which is not the same as logit(P(y = j | y≥ j). In fact it is the logit <strong>of</strong> the complement, under the conditioning. Here is that fit.x


134Number <strong>of</strong> linear predictors: 2Names <strong>of</strong> linear predictors: logit(P[Y>1|Y>=1]), logit(P[Y>2|Y>=2])Dispersion Parameter for cratio family: 1Residual Deviance: 11.83839 on 6 degrees <strong>of</strong> freedomLog-likelihood: -730.3872 on 6 degrees <strong>of</strong> freedomNumber <strong>of</strong> Iterations: 4It is not surprising to see that the estimates are all negated, as we are fitting the logits <strong>of</strong> thecomplementary probabilities.As a side note, as mentioned in Harrell (1998) as well as Agresti (Chapter 7 notes), the continuation-ratiomodel is a discrete version <strong>of</strong> the Cox proportional hazards model. Thus, one could probably fit thesemodels using either coxph or cph, which is in the Design library. It is left to the reader to make theconnection.G. Ordinal Responses: Mean Response ModelsA mean response model for ordinal responses is a linear regression model with ordinal responsesrepresented by numerical scores. As in ordinary linear regression with continuous response, theconditional mean is assumed linearly related to the explanatory variables. The response distribution isassumed product multinomial (i.e., independent multinomial at each fixed set <strong>of</strong> covariates).This type <strong>of</strong> model can be fit using something like the lpmreg function <strong>of</strong> Chapter 4.Also, using the Poisson/multinomial connection, we might try glm with a poisson(identity) family. Or,we might try aov, as we are fitting a linear regression model. The functions glm <strong>and</strong> aov give similarcoefficient estimates. However, they are not exactly the same as those assuming the productmultinomialmodel. Also, the st<strong>and</strong>ard errors are wrong.fit.aov


135Multinomial-Poisson Homogeneous ModelA better alternative for fitting mean response models using ML estimation is to use J. Lang’s mph.fitfunction for R, which fits Multinomial-Poisson Homogenous models. For the mean response model, weassume product-multinomial sampling, given the totals from the eight populations.To use the function mph.fit, we need Table 7.8 in the follow form, as a 8 x 4 matrix <strong>of</strong> counts.table.7.8a


136fit


1372 0 0 3 5 13 0 0 0 7 34 0 0 1 9 6Next, we need the sample size for each population (income x gender combination), <strong>and</strong> the sampleproportions for each population.n


138F ( π ) = α + 0⋅ β + 1 ⋅ β =ν ( π ,..., π )T1 g x1(1,1) 4(1,1)TF ( π ) = α + β + 4 ⋅ β =ν ( π ,..., π )8 g x1(2,4) 4(2,4)representing the 2 x 4 combinations <strong>of</strong> Gender <strong>and</strong> Income. The 4 x 1 vector ν contains the scores forthe 4 job satisfaction categories.Now, note that the matrixQ ⎡ ⎤ on p. 602 contains the 1 x 32 row vectors∂ Fk( π)= ⎣ ∂πji| ⎦∂ Fk( π)∂π= (01,...,0 ( k −1)4, ν1,..., ν4,0,...,0 32),ji |<strong>and</strong> the design matrix is..1 gender income1 1 11 1 21 1 31 1 41 0 11 0 21 0 31 0 4All <strong>of</strong> the above is input into S via the following comm<strong>and</strong>s. For the F( ) functions, we use the sampleproportions instead <strong>of</strong> the probabilities, π .js


139sqrt(diag(Covb))[1] 0.2311374 0.1369473 0.0681758I compute a Wald statistic for entire model using the formula on p. 603 in Agresti.as.numeric(t(Fp-X%*%b)%*%InvVF%*%(Fp-X%*%b))5.242789H. Generalized Cochran-Mantel Haenszel Statistic for Ordinal CategoriesThe mantelhaen.test function in the R package ctest, which comes with R, can h<strong>and</strong>le I x J x K tables,but does not take special advantage <strong>of</strong> ordinal categories (The same built-in function in S-<strong>PLUS</strong> can onlyh<strong>and</strong>le 2 x 2 x 2 tables, but the statistic itself is easily calculable using formula that is alreadyprogrammed into the R function. A calculation is available in the S-<strong>PLUS</strong> scripts for this manual). To testfor conditional independence <strong>of</strong> job satisfaction <strong>and</strong> income given gender, treating job satisfaction <strong>and</strong>income with scores {1, 3, 4, 5} <strong>and</strong> {3, 10, 20, 35}, respectively, we need to use equation (7.21) in Agresti.This statistic has an approximate chi-squared distribution with 1 df.With nominal categories for job satisfaction <strong>and</strong> income, mantelhaen.test calculates (7.20) in Agrestiusing input from xtabs.mantelhaen.test(xtabs(freq~jobsatf+income+gender, data=table.7.8))Cochran-Mantel-Haenszel testdata: xtabs(freq ~ jobsatf + income + gender, data = table.7.8)Cochran-Mantel-Haenszel M^2 = 12.5314, df = 9, p-value = 0.185But, this gives a different answer than that given by SAS in Table 7.12 <strong>of</strong> Agresti. I don’t know why, as itis clear from the R code that it is computing (7.20).The test <strong>of</strong> nonzero correlation uses the ordinal scores for both row <strong>and</strong> column variables <strong>and</strong> computes(7.21). This computation is easy in either R or S-<strong>PLUS</strong>.First, we add the scores to the data frametable.7.8$jobsatS


140(sum(rowsums*u^2) - (sum(rowsums*u)^2)/n)*(sum(colsums*v^2) -(sum(colsums*v)^2)/n)/(n-1)}, u=c(1,3,4,5), v=c(3,10,20,35))The statistic is(sum(Tk-ETk)^2)/sum(varTk)[1] 6.156301with p-value1-pchisq((sum(Tk-ETk)^2)/sum(varTk),df=1)[1] 0.01309447The row mean scores differ association treats rows as nominal <strong>and</strong> columns as ordinal. The test statisticcan be computed in R or S-<strong>PLUS</strong> using the formula in the notes for Chapter 7 (p. 302, Agresti).However, the matrices in this formula do not have the appropriate dimensions for multiplication. So, onemay have to check the original paper. In any case, the mantelhaen.test function can be modified easilyto incorporate the B matrix given on p. 302, to get the new statistic.


141Chapter 8 –Loglinear Models for Contingency TablesA. Summary <strong>of</strong> Chapter 8, AgrestiLoglinear models are used for modeling cell counts in a contingency table. As Agresti notes, thesemodels are usually used when we have a multivariate response, not a univariate response, as the modeltreats all classification factors as responses. With a univariate response, methods such as logit modelsor multinomial logit models are better alternatives, <strong>and</strong> there is a correspondence between logit modelswith categorical explanatory variables <strong>and</strong> loglinear models (Section 8.5, Agresti). Loglinear modelsmodel the expected cell frequencies as log linear combinations <strong>of</strong> effects (model parameters) due to eachclassification factor by itself <strong>and</strong> possibly due to interactions among classification factors. Certaincontrasts involving parameters (such as differences between parameters for a given factor) areinterpreted as log odds <strong>of</strong> making one response on that variable, relative to another response. Contrastsinvolving interaction parameters can have interpretations in terms <strong>of</strong> log odds ratios. Interactionparameters by themselves are most useful in an association interpretation. That is, a three-factorinteraction parameter is zero if there is no three-factor association. For identifiability <strong>of</strong> all parameters,arbitrary constraints are made. This makes the individual parameter estimates not unique acrossconstraints, but estimated contrasts that encode log odds ratios are the same across constraints.A generalized linear model interpretation <strong>of</strong> loglinear models treats the N = IJ cell counts <strong>of</strong> an I x Jtable as independent observations from a Poisson r<strong>and</strong>om component with corresponding means equalto the expected cell counts. The same model can have a multinomial interpretation with two categoricalresponses <strong>and</strong> N total observations. These statements also apply to three-way tables.In a three-way table with response variables X, Y, <strong>and</strong> Z, several types <strong>of</strong> potential independence canbe present. Mutual independence <strong>of</strong> all three variables results in all interaction parameters in the loglinearmodel being zero. In a multinomial interpretation, this means all joint cell probabilities equal the products<strong>of</strong> the corresponding marginal probabilities. Joint independence <strong>of</strong> one variable, Y, <strong>and</strong> the combinedclassifications <strong>of</strong> the other two (X <strong>and</strong> Z) results in a loglinear model with only one possible nonzerointeraction parameter, that between X <strong>and</strong> Z. Finally, variables X <strong>and</strong> Y are conditionally independent <strong>of</strong>variable Z if independence holds within each partial table conditional on a given value <strong>of</strong> Z. This wouldresult in a loglinear model with two possible nonzero interaction parameters: that describing associationbetween X <strong>and</strong> Z <strong>and</strong> that describing association between Y <strong>and</strong> Z. Relationships among the types <strong>of</strong>independence appear in Table 8.1. in Agresti.A loglinear model for no three-factor interaction in a three-way table is called a homogenousassociation model. This means that the conditional odds ratios between any two variables are identical ateach category <strong>of</strong> the third variable. Its parameters have interpretations in terms <strong>of</strong> conditional oddsratios. Of theIP2× JP2possible odds ratios, there are (I – 1)(J – 1) nonredundant odds ratios describingthe association between variables X <strong>and</strong> Y, at each <strong>of</strong> K levels <strong>of</strong> a third conditioning variable.Conditional independence <strong>of</strong> X <strong>and</strong> Y (i.e., no three-factor interaction) means that all <strong>of</strong> the odds ratiosare equal to 1.0. The logs <strong>of</strong> these odd ratios are functions <strong>of</strong> the parameters <strong>of</strong> the homogeneousassociation model, as shown in equation (8.14) in Agresti, <strong>and</strong> the functions do not depend on the level <strong>of</strong>the conditioning variable.Higher dimensional tables have straightforward extensions from the three-way table.Chi-squared goodness-<strong>of</strong>-fit tests can be used to compare nested models. This usually means usingthe likelihood ratio chi-squared statistic, which has an asymptotic chi-squared distribution when theexpected frequencies are large (with fixed number <strong>of</strong> cells). The degrees <strong>of</strong> freedom equal the differencein dimension between the null <strong>and</strong> alternative hypothesis.Loglinear models can be fit using one <strong>of</strong> two methods for doing maximum likelihood estimation,Newton-Raphson or Iterative proportional fitting (IPF). Newton-Raphson is an iterative procedure thatsolves a weighted least squares equation at each iteration. At each iteration <strong>of</strong> the IPF algorithm, thefitted values satisfy the model <strong>and</strong> eventually converge to match the sufficient statistics (leading toMLEs). However, IPF does not automatically produce the estimated covariance matrix <strong>of</strong> the parametersas a byproduct. ML parameter estimates have asymptotic normal distributions <strong>and</strong> asymptotic st<strong>and</strong>arderrors estimated by the inverse <strong>of</strong> the Information matrix <strong>of</strong> the log likelihood.


142A generalized loglinear model generalizes mean modeling for loglinear models such as PoissonGLIMs. Specifically, the link function relating the response mean to the linear predictor is not strictlylogarithmic <strong>and</strong> not necessarily invertible, <strong>and</strong> takes the formC(log Aµ ) = Xβfor matrices C <strong>and</strong> A, which are not necessarily invertible. With A <strong>and</strong> C identity matrices, we getordinary loglinear models. For example, the logit model for a three-way table that postulatesindependence <strong>of</strong> the response <strong>and</strong> the two explanatory variables, each with two levels (i.e., the logitmodel only has an intercept) has A equal to an 8 x 8 identity matrix so that = ( µ , µ ,..., µ ) <strong>and</strong>⎡1 0 −1 0 0 0 0 0 ⎤⎢0 1 0 −1 0 0 0 0⎥C = ⎢⎥⎢ 0 0 0 0 1 0 − 1 0 ⎥⎢⎥⎣0 0 0 0 0 1 0 −1⎦µ111 112 222With X a vector <strong>of</strong> four ones, <strong>and</strong> β = α , we get four row logits (two per each level <strong>of</strong> the secondexplanatory variable) that are all equal.B. Loglinear Models for Three-way TablesWith a three-way table, there are three response variables for a loglinear model. We can test the fit<strong>of</strong> a saturated model (three-way interaction) against a homogeneous association model (all pairwiseassociations) or that <strong>of</strong> homogeneous association versus conditional independence <strong>of</strong> two variablesgiven a third. Agresti uses a data set on alcohol, cigarette, <strong>and</strong> marijuana use to fit these various models.We can set up the data astable.8.3


143data.frame(table.8.3[,-4], ACM=c(aperm(fitted(fitACM))),AC.AM.CM=c(aperm(fitted(fitAC.AM.CM))), AM.CM=c(aperm(fitted(fitAM.CM))),AC.M=c(aperm(fitted(fitAC.M))), A.C.M=c(aperm(fitted(fitA.C.M))))marijuana cigarette alcohol ACM AC.AM.CM AM.CM AC.M A.C.M1 Yes Yes Yes 911 910.383057 909.2395630 611.17749 539.982542 No Yes Yes 538 538.616089 438.8404236 837.82251 740.226073 Yes No Yes 44 44.616840 45.7604179 210.89632 282.091254 No No Yes 456 455.385590 555.1595459 289.10370 386.700075 Yes Yes No 3 3.616919 4.7604165 19.40246 90.597396 No Yes No 43 42.383881 142.1595764 26.59754 124.193927 Yes No No 2 1.383159 0.2395833 118.52373 47.328818 No No No 279 279.614380 179.8404236 162.47627 64.87991Because we set param = T, we can get estimates <strong>of</strong> the model parameters (the lambdas), usingfit$param. These can be used to get estimates <strong>of</strong> conditonal odds ratios, as per equation (8.14) inAgresti (see Thompson (1999, p. 27)). However, it may be simpler to just compute the odds ratios fromthe array <strong>of</strong> fitted values. To do this, we use the apply function on the array. For example, for thehomogeneous association model (AC, AM, CM), which has fitted counts close to the observed counts, wehave the following conditional odds ratios:fit.array


144[1,] 1 3[2,] 2 4, , 2[,1] [,2][1,] 1 3[2,] 2 4sum.array(junk)[,1] [,2][1,] 2 4[2,] 6 8Now, we compute the marginal odds ratios. The current dimensions <strong>of</strong> fit.array are A,C,M.odds.ratio(sum.array(fit.array)) # AC (sum over M, so 3 needs to be listed first)[1] 17.70244odds.ratio(sum.array(fit.array, perm=c(1,2,3))) # CM (sum over A, so 1 is first)[1] 25.13620odds.ratio(sum.array(fit.array, perm=c(2,1,3))) # AM (sum over C, so 2 is first)[1] 61.87182As loglm is a front-end to loglin, there is no need to show the results from loglin. loglin requiresinput <strong>of</strong> a table in the form <strong>of</strong> an array <strong>and</strong> also has an argument margin, where the model is specifiedusing a list <strong>of</strong> numeric vectors. As an example, to fit the homogeneous association model, we dologlin(fitted(fitACM), margin=list(c(1,2), c(2,3), c(1,3)), param=T,fit=T)$lrt:[1] 0.3738868$pearson:[1] 0.4011037$df:[1] 1$margin:$margin[[1]]:[1] "alcohol" "cigarette"$margin[[2]]:[1] "cigarette" "marijuana"$margin[[3]]:[1] "alcohol" "marijuana"$fit:, , YesYes NoYes 910.382996 44.617058No 3.616851 1.383149, , NoYes NoYes 538.61639 455.3836No 42.38408 279.6159


145The margin argument gives the associations allowed for the model. Here, we choose all pairwiseassociations. lrt <strong>and</strong> pearson give the associated goodness-<strong>of</strong>-fit statistics, <strong>and</strong> fit gives the fittedcounts.The same model can be fit using glm with a poisson family. Here, the fit algorithm is Newton-Raphson.options(contrasts=c("contr.treatment","contr.poly")) # dummy coding for factors(fit.glm X^2)Likelihood Ratio 0.3742223 1 0.5407117Pearson 0.4011002 1 0.5265216Comparison <strong>of</strong> nested models can be done using the anova method. For example,anova(fitAC.M, fitAC.AM.CM, fitAM.CM, fitA.C.M)LR tests for hierarchical log-linear modelsModel 1:count ~ alcohol + cigarette + marijuanaModel 2:count ~ alcohol + cigarette + marijuana + alcohol:cigaretteModel 3:count ~ alcohol + cigarette + marijuana + alcohol:marijuana + cigarette:marijuanaModel 4:count ~ alcohol + cigarette + marijuana + alcohol:cigarette + alcohol:marijuana +cigarette:marijuanaDeviance df Delta(Dev) Delta(df) P(> Delta(Dev)


146Model 1 1286.0200195 4Model 2 843.8267822 3 442.1932373 1 0.00000Model 3 187.7544556 2 656.0723267 1 0.00000Model 4 0.3742223 1 187.3802338 1 0.00000Saturated 0.0000000 0 0.3742223 1 0.54071gives the likelihood ratio tests comparing hierarchical loglinear models given in the list <strong>of</strong> arguments.Each item in the Delta(Dev) column compares Deviances between the current row <strong>and</strong> the previousrow. So, the test <strong>of</strong> conditional independence between A <strong>and</strong> C, which compares models AM.CM <strong>and</strong>AM.CM.AC is 187.75 – 0.37 = 187.38. According to the output, the only model that fits well among thefour is the homogeneous association model (Model 4). It’s deviance is close enough to the deviance forthe saturated model (zero) to give a nonsignificant p-value.The parameter estimates returned by loglin <strong>and</strong> loglm (fit$param) come from a parameterization suchthat the constant component describes the overall mean, each single factor sums to zero, each tw<strong>of</strong>actor parameter sums to zero both by rows <strong>and</strong> columns, etc. Thus, the parameterization is not suchthat the parameters in the last row <strong>and</strong> the last column are zero, <strong>and</strong> will not match those from SAS on p.325 in Agresti. To obtain parameter estimates <strong>and</strong> st<strong>and</strong>ard errors from loglm matching those from SAS,we can use plug-in calculations, as mentioned by Agresti. That is, take the fitted expected counts fromloglm output <strong>and</strong> plug them into the formula (8.25) on p. 339 in Agresti. (This is illustrated below)However, we can get the parameter estimates <strong>and</strong> st<strong>and</strong>ard errors directly from the glm fit. In order thatour estimates match the SAS results given by Agresti on p. 325, we must first issue the comm<strong>and</strong>options(contrasts= c("contr.treatment", "contr.poly")), <strong>and</strong> then ensure that our factor levelsmatch those <strong>of</strong> SAS. An inspection <strong>of</strong> the model.matrix from fit.glm shows the dummy coding usedby contr.treatmentmodel.matrix(fit.glm)(Intercept) marijuana cigarette alcohol marijuana:cigarette marijuana:alcohol cigarette:alcohol1 1 0 0 0 0 0 02 1 1 0 0 0 0 03 1 0 1 0 0 0 04 1 1 1 0 1 0 05 1 0 0 1 0 0 06 1 1 0 1 0 1 07 1 0 1 1 0 0 18 1 1 1 1 1 1 1By the manner in which table.8.3 was set up, we see that the coding is 0 = Yes <strong>and</strong> 1 = No. We want 1= Yes <strong>and</strong> 0 = No. Fortunately, we don’t have to redefine the factors in table.8.3. We just need to usethe contrasts argument <strong>of</strong> glm, as follows.fit.glm2


147marijuana:alcohol 2.986014 0.46454749 6.427791cigarette:alcohol 2.054534 0.17406289 11.803401(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 2851.461 on 7 degrees <strong>of</strong> freedomResidual Deviance: 0.3739859 on 1 degrees <strong>of</strong> freedomNumber <strong>of</strong> Fisher Scoring Iterations: 3For loglm, we compute equation (8.25) using the model.matrix function to get X. Note that we still needto modify the 0/1 coding using contrasts argument to match the coding in Agresti.options(contrasts=c("contr.treatment","contr.poly"))contrasts# ensure we have treatmentX


148fitG.I.L.S


149locationbelt Urban RuralNo 10358.93 6150.193Yes 10959.23 6697.643, , gender = Female, injury = Yeslocationbelt Urban RuralNo 1009.7857 964.3376Yes 713.3783 797.4979, , gender = Male, injury = Yeslocationbelt Urban RuralNo 834.0684 1056.8071Yes 389.7681 508.3566We can examine the three-way interaction by seeing how the conditional odds ratios <strong>of</strong> any two variables<strong>of</strong> the trio (gender, location, belt use) change at the levels <strong>of</strong> the third variable. But, they are the sameregardless <strong>of</strong> injury status. (Output is given from R)fit.array


150Yes 0.5799411 0.5799412apply(fit.array,c(1,3),odds.ratio)# IL (same for each combination <strong>of</strong> GS)Female MaleNo 2.134127 2.134127Yes 2.134127 2.134127apply(fit.array,c(2,3),odds.ratio)# IS (same for each combination <strong>of</strong> GL)Female MaleUrban 0.4417123 0.4417123Rural 0.4417122 0.4417123Computing the dissimilarity matrix to check goodness-<strong>of</strong>-fit <strong>of</strong> this model to the data is simple in S.Fitted.values


151these remaining coefficients is a consequence <strong>of</strong> having only two levels per variable <strong>and</strong> also <strong>of</strong> usingtreatment contrasts. To see that this is so, read subsection 8.5.2 in Agresti, <strong>and</strong> recall that we set the lastcoefficient in each row <strong>and</strong> column <strong>of</strong> a pairwise interaction to zero for constraints.The fact that there is a correspondence between estimates from the two models, which assume twodifferent sampling distributions for responses, follows from the correspondence <strong>of</strong> the likelihoods withrespect to the parameters in question.F. Contingency Table St<strong>and</strong>ardizationTable st<strong>and</strong>ardization is useful for comparing tables with different marginal totals or matching sampletable data to st<strong>and</strong>ardized marginal distributions. Next, we show how to “rake” Table 8.15 by forcing allrow <strong>and</strong> column totals to equal 100. This is done using the tip on p. 346 <strong>of</strong> Agresti. We make the log <strong>of</strong>the observed count an <strong>of</strong>fset in a glm <strong>and</strong> create a pseudo response that satisfies the model <strong>and</strong> themarginal totals.First I input the data, coding factors with levels in the order most natural.table.8.15


152Chapter 9 –Building <strong>and</strong> Extending Loglinear ModelsA. Summary <strong>of</strong> Chapter 9, AgrestiThis chapter extends loglinear models to ordinal variables <strong>and</strong> correlated variables, <strong>and</strong> also dealswith special issues like collapsibility <strong>and</strong> empty cells.With higher order tables, the analysis is easier if we can collapse over some dimensions (variables).Collapsibility describes the conditions under which a variable can be collapse over (i.e., ignored). Forthree-way tables with variables X, Y, <strong>and</strong> Z, we can collapse over Z while keeping the XY association thesame for the marginal table as well as the conditional table (on levels <strong>of</strong> Z) if either Z <strong>and</strong> X areconditionally independent given Y or Z <strong>and</strong> Y are conditionally independent given X. For four-way tableswith variables W, X, Y, <strong>and</strong> Z, if we collapse over Y, <strong>and</strong> association terms involving XY <strong>and</strong> WY are zero,then the partial WX odds ratios, conditioning on Y, are the same as the marginal WX odds ratios aftercollapsing. The general principle describing these examples is on p. 360 <strong>of</strong> Agresti.Loglinear models treat all variables as responses. However, sometimes certain marginal totals arefixed by sampling design. If the corresponding loglinear terms are not included in the model, then thefitted marginal totals will not necessarily match the observed totals for these combinations. This isbecause the likelihood will not match the observed total to the fitted total in the likelihood equation(equation 8.22 in Agresti). In sum, the highest interaction term containing all explanatory variables shouldbe included in a loglinear model.Nested loglinear models can be compared using the likelihood ratio statistic in equation (9.3) inAgresti, or using the Pearson statistic in equation (9.4). These have asymptotic chi-squared distributions,<strong>and</strong> are used to test the significance <strong>of</strong> an additional term or variable in the model. To check the fit <strong>of</strong> agiven model, one can examine cell residuals.Just as with logit models, if there are ordinal responses, then greater power results from fittingspecific ordinal trends than either ignoring ordinality or fitting general association terms. The lattersituation may not even be possible without fitting a saturated model. The linear-by-linear association (L xL) model assigns ordered row scores to rows <strong>and</strong> ordered column scores to columns, <strong>and</strong> includes, inaddition to main effect terms for the rows <strong>and</strong> columns, an interaction term in the row <strong>and</strong> column scores.The interaction term has a single unknown parameter which represents positive or negative linearassociation, depending on the sign <strong>of</strong> the parameter. Odds ratios are a function <strong>of</strong> the distance betweencorresponding rows <strong>and</strong> columns <strong>and</strong> the magnitude <strong>of</strong> the interaction parameter.L x L models are best fit when the observed counts show a linear trend in the rows at each column<strong>and</strong> a linear trend in the columns for each row. The model is fit using ML estimation, <strong>and</strong> the correlationbetween rows <strong>and</strong> columns is the same in both the observed <strong>and</strong> fitted counts.Association models generalize loglinear models for ordinal responses to multi-way tables <strong>and</strong> tomixed ordinal <strong>and</strong> nominal responses. The row effects model <strong>and</strong> column effects model have either rowsor columns as nominal responses, respectively, <strong>and</strong> the other as ordinal. The nominal variable hasparameter scores instead <strong>of</strong> fixed scores. Ordinal responses within multi-way tables are straightforwardgeneralizations <strong>of</strong> their two-way table analogs.Other types <strong>of</strong> models that emphasize the estimation <strong>of</strong> association are Multiplicative Row <strong>and</strong>Column effects models (RC Models), correlation models, <strong>and</strong> correspondence analysis. The RC modelmodifies the L x L model by replacing the scores with parameters. Correlation models are similar to RCmodels. In these models, each cell probability is augmented with an association term, dependent on row<strong>and</strong> column scores. Correlation models can have either fixed or parameter scores. Correspondenceanalysis is mostly a graphical technique to represent associations in contingency tables.The Poisson loglinear model can also be used to model the rate <strong>of</strong> occurrence <strong>of</strong> an event over anexposure time or space, dependent on covariates. The exposure is treated as an <strong>of</strong>fset. Also, due to theconnection between ML estimation using a Poisson likelihood for numbers <strong>of</strong> events <strong>and</strong> a negativeexponential likelihood for survival time, one can model survival times using a Poisson loglinear model.Sparse tables have many empty cells where there are no observations. A sampling zero occurs in acell when the data do not contain observations corresponding to that cell. A structural zero occurs whenit is impossible for the data to have an observation for the cell. For unsaturated models, MLEs existwhen all cell counts are positive, but do not exist when some counts in the sufficient marginal tables are


153zero. They also may not exist when all sufficient counts are positive, but at least one cell is zero.However, even if a point estimate is infinite, one endpoint <strong>of</strong> a likelihood-ratio confidence interval isusually finite. Adding a constant like 0.5 to each cell to prevent sampling zeroes for fitting an unsaturatedmodel is not recommended, especially for large numbers <strong>of</strong> cells, because it influences estimates <strong>and</strong>test statistics conservatively.As an alternative to large-sample goodness-<strong>of</strong>-fit tests (which may not apply when a table is sparse),one can use exact tests or Monte Carlo approximations to exact tests.B. Model Selection <strong>and</strong> ComparisonAgresti uses the Student Survey example to illustrate model selection <strong>and</strong> comparison. We will use abackwards stepwise procedure to help in comparing models by comparing likelihood ratio chi-squarestatistics.We start by inputing the datatable.9.1


154Step: AIC= 45.78count ~ cigarette + alcohol + marijuana + sex + race + cigarette:alcohol +cigarette:marijuana + cigarette:sex + alcohol:marijuana +alcohol:sex + alcohol:race + marijuana:sex + marijuana:race + sex:raceDf Deviance AIC- cigarette:sex 1 16.73504 44.73504 NA 15.78347 45.78347- marijuana:race 1 18.95695 46.95695- alcohol:sex 1 19.18046 47.18046- alcohol:race 1 20.33869 48.33869- marijuana:sex 1 25.56924 53.56924- alcohol:marijuana 1 107.79480 135.79480- cigarette:alcohol 1 201.21688 229.21688- cigarette:marijuana 1 513.50057 541.50057Step: AIC= 44.74count ~ cigarette + alcohol + marijuana + sex + race + cigarette:alcohol +cigarette:marijuana + alcohol:marijuana + alcohol:sex +alcohol:race + marijuana:sex + marijuana:race + sex:raceDf Deviance AIC NA 16.73504 44.73504- marijuana:race 1 19.90859 45.90859- alcohol:race 1 21.29033 47.29033- alcohol:sex 1 22.02148 48.02148- marijuana:sex 1 25.81125 51.81125- alcohol:marijuana 1 108.77579 134.77579- cigarette:alcohol 1 204.11536 230.11536- cigarette:marijuana 1 513.73033 539.73033Call: glm(formula = count ~ cigarette + alcohol + marijuana + sex + race +cigarette:alcohol + cigarette:marijuana + alcohol:marijuana +alcohol:sex + alcohol:race + marijuana:sex + marijuana:race + sex:race, family =poisson, data = table.9.1)Coefficients:Value Std. Error t value(Intercept) 2.2651798 0.12467553 18.168600cigarette -0.2822714 0.05491143 -5.140485alcohol -1.4163287 0.12121797 -11.684149marijuana 1.2671910 0.12443051 10.183925sex 0.1090531 0.04581267 2.380413race -1.1940934 0.05467500 -21.839842cigarette:alcohol 0.5136335 0.04351564 11.803423cigarette:marijuana 0.7119723 0.04095963 17.382295alcohol:marijuana 0.7486558 0.11619789 6.442938Value Std. Error t valuealcohol:sex 0.07321130 0.03190131 2.2949310alcohol:race 0.11284083 0.05166169 2.1842266marijuana:sex -0.06805431 0.02261757 -3.0089132marijuana:race 0.07958278 0.04495553 1.7702554sex:race 0.03582621 0.03976789 0.9008829(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 4818.051 on 31 degrees <strong>of</strong> freedomResidual Deviance: 16.73504 on 18 degrees <strong>of</strong> freedomThe output is lengthy, but it indicates the eliminated coefficient (which I’ve bolded) at each step. Notethat we don’t quite get to Agresti’s Model 6, but stop at Model 5. Model 6 would correspond to theremoval:Df DevianceAIC


155- marijuana:race 1 19.90859 45.90859We now rename the result <strong>of</strong> the stepwise search:fit.AC.AM.CM.AG.AR.GM.GR


15624 No No No female other 12 14.31255445 -0.8848276025 Yes Yes Yes male other 30 35.77995571 -1.7412161926 No Yes Yes male other 1 1.75353441 -0.5885741827 Yes No Yes male other 1 0.27593320 1.4173616628 No No Yes male other 0 0.10552401 -0.3285553129 Yes Yes No male other 19 18.51435109 0.1486450130 No Yes No male other 18 15.65328710 0.7438181231 Yes No No male other 8 2.88487421 3.2662966732 No No No male other 17 19.03254028 -0.74988607D. Modeling Ordinal AssociationsWhen both responses in a loglinear model are ordinal, a linear-by-linear association model can be usedto fit a linear trend in ordered row <strong>and</strong> column scores. These scores may be assumed to represent anunderlying continuous distribution. For an I x J table, the loglinear model adds an interaction term in thescores, as followsX Ylog µ = λ + λ + λ + βuv , i= 1,..., I; j = 1,..., Jij i j i jThe parameter, β , describes the linear association. It is positive or negative with the sign <strong>of</strong> β . Oddsratios depend on β , as well as the score distance between the corresponding rows <strong>and</strong> columns. When{ u = i}<strong>and</strong> { v = j}, the local odds ratios for adjacent rows <strong>and</strong> columns are uniform, <strong>and</strong> equalijexp( β ) . This is called the uniform association model.Agresti fits a uniform association (UA) model to data from the 1991 General Social Survey on opinionsabout premarital sex <strong>and</strong> birth control for teenagers. The four categories for opinions about premaritalsex range from “Always wrong” to “Not wrong at all”. The four categories for opinions about teenage birthcontrol sex range from “Strongly disagree” to “Strongly agree”. The scores used are {1, 2, 3, 4} for bothrows <strong>and</strong> columns, initially.The data are available from Agresti’s web site. I have copied the data into a text file called sex.txt. Wefirst read in the data, <strong>and</strong> give the columns names.table.9.3


157options(contrasts=c("contr.treatment","contr.poly"))(fit.ua


158Degrees <strong>of</strong> Freedom: 16 Total; 9 ResidualResidual Deviance: 127.6529A LR test <strong>of</strong> the null hypothesis <strong>of</strong> independence (i.e., H : 00β = ) has df =1:anova(fit.ind, fit.ua, test = "Chi") # output from S-<strong>PLUS</strong>Analysis <strong>of</strong> Deviance TableResponse: countTerms Resid. Df Resid. Dev Test Df Deviance Pr(Chi)1 premar + birth 9 127.65292 premar + birth + u1:v1 8 11.5337 +u1:v1 1 116.1192 0E. Association ModelsAssociation models generalize loglinear models to ordinal responses, <strong>and</strong> include the linear-by-linearassociation models. The focus <strong>of</strong> these models is on estimating association. Row <strong>and</strong> column effectsmodels have rows or columns, respectively, as nominal responses, <strong>and</strong> model an ordinal effect at each <strong>of</strong>the rows or columns. Multi-way tables with some ordinal responses generalize the two-way tables,adding three or higher way interactions.1. Row <strong>and</strong> Column Effects ModelsThe row effect model treats rows (X) as nominal <strong>and</strong> columns (Y) as ordinal. Each row has a parameterassociated with it, called the row effect. The model for the log expected count in cell ij isX Ylog µ = λ+ λ + λ + µvij i j i jwhere the µiare the unknown row effects (i = 1,…, I), <strong>and</strong> the vjare known column scores. Thus, themodel has I – 1 more parameters than an independence model. An independence model here wouldimply that all row effects are equal. That is, the linear effect <strong>of</strong> the column variable on the log expectedcount is the same within each row. The column effects model is defined similarly.For the row effects model, the distance between adjacent-categories logits is the same across rows.However, the level <strong>of</strong> the logit will differ between rows i <strong>and</strong> k by µi− µk. Thus, this model is called theparallel odds model.The Political Ideology Example (Table 9.5 in Agresti) uses a table that cross-classifies Party Affiliation(Democrat, independent, Republican) <strong>and</strong> Political Ideology (Liberal, Moderate, Conservative) for asample <strong>of</strong> voters in a Wisconsin primary.A row effects model is fit to Table 9.5, with rows as Party Affiliation. When we construct the table, thevariable c.Ideo are the scores (1, 2, 3) for Political Ideology.table.9.5


159I coded the levels <strong>of</strong> the factor variables so that the Republican <strong>and</strong> Conservative levels had zero-valuedcoefficients, for identifiability. This is done to match the SAS results in Table 9.6 in Agresti.To fit a row effects model:options(contrasts = c("contr.treatment", "contr.poly"))(fit.RE


160An extension <strong>of</strong> association models to multi-way tables permits higher-order interaction models that aremore parsimonious than their corresponding nominal models. In particular, the three-way interactionmodel for a three-way table is not saturated. For a three-way table, with X <strong>and</strong> Y ordinal, with scores{ u i} <strong>and</strong> { v j} respectively, we model heterogeneous linear-by-linear XY association aslog µ = λ+ λ + λ + λ + β uv + λ + λX Y Z XZ YZijk i j k k i j ik jkIn this model, the log XY odds ratios are uniform within each level <strong>of</strong> Z, but differ across levels <strong>of</strong> Z. Ifβk= β for all k, then the log odds ratios the same across k as well. The model is then called thehomogeneous linear-by-linear association model.The data in Table 9.7 in Agresti are used to fit these models. The table cross-classifies smoking status(S), breathing test results (B) <strong>and</strong> age (A) for workers in industrial plants.The data are read in, <strong>and</strong> the levels are set so that the last category is zero. I also define scores for allthree variables. These are denoted with a “c” prefix.table.9.7


161A homogeneous L x L model is fit usingfit.homo


162Age55-59Wheeze -1.4479318 0.1233793 -11.735617Age60-64Wheeze -1.6556074 0.1337443 -12.378905c.Breath:c.Wheeze 3.676197 0.1998956 18.39058(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 25889.47 on 35 degrees <strong>of</strong> freedomResidual Deviance: 6.801743 on 7 degrees <strong>of</strong> freedomThus, the estimated BW local odds ratio at level k <strong>of</strong> Age is 3.676 – 0.131k.3. Conditional Tests for Ordinal Models in Multi-way TablesFor small samples, the asymptotic chi-squared approximations to goodness-<strong>of</strong>-fit tests, such as thelikelihood-ratio test, do not hold. Instead, theoretically, the p-value for a test <strong>of</strong> the goodness <strong>of</strong> fit <strong>of</strong> amodel can be computed from a conditional distribution <strong>of</strong> the test statistic, by conditioning on estimates <strong>of</strong>certain nuisance parameters. Conditioning on these sufficient statistics leaves the conditional distributionfree <strong>of</strong> these unknown parameters. So, the p-value is the probability <strong>of</strong> the test statistic exceeding itsobserved value, conditional on the sufficient statistic taking on its observed value.The functions in exactLoglinTest give Monte Carlo estimates <strong>of</strong> conditional p-values for tests <strong>of</strong>Poisson log-linear models. The function mcexact conditions on all sufficient statistics for parameters in agiven model, <strong>and</strong> simulates multi-way tables from the conditional distribution satisfying the observedvalues <strong>of</strong> the sufficient statistics. The function approximates the conditional distribution, which is ageneralized hypergeometric by default, by sampling from it via either importance sampling (method =“bab”) or MCMC (method = “cab”). The details <strong>of</strong> the procedures can found in the citations that arelisted in the documentation.The default test statistics used are the LR test statistic <strong>of</strong> a model against the saturated model (modeldeviance) <strong>and</strong> the Pearson chi-squared statistic.For example, to fit model (9.12) in Agresti using a conditional test, we use mcexact with importancesampling.library(exactLoglinTest)set.seed(1)fit.mc


163pvalue 0.45862023 0.45123625mcse 0.01003939 0.00998465“Number <strong>of</strong> iterations” gives the number <strong>of</strong> simulations actually obtained. “T degrees <strong>of</strong> freedom” is atuning parameter, <strong>and</strong> df is the model df. “Proportion <strong>of</strong> valid tables” here is just proportion <strong>of</strong> simulationsdone, out <strong>of</strong> maxiter.In the second half <strong>of</strong> the output, we have the estimated p-values. We can compare these to their largesampleversionspchisq(fit.mc$dobs, df=7, lower=F)[1] 0.4498103 0.4491093They are close, which is expected because <strong>of</strong> the large sample size.However, before we “believe” these estimates, we might examine the trace plots <strong>and</strong> the autocorrelationfunction <strong>of</strong> the iterations, as well as the logs <strong>of</strong> the importance weights (if we use importance sampling).We saved these results by specifying savechain=T. They are saved as the chain attribute <strong>of</strong> fit.mc.layout(matrix(c(1,1,2,3),2,2, byrow=T))plot(fit.mc$chain[,1], type="l", xlab="iteration", ylab="deviance")acf(fit.mc$chain[,1])library(MASS)truehist(fit.mc$chain[,3], xlab="log importance weight")


164deviance0 200 400 600 800 10000 2000 4000 6000 8000 10000iterationSeries fit.mc$chain[, 1]ACF0.0 0.2 0.4 0.6 0.8 1.00.00 0.05 0.10 0.150 10 20 30 40Lag-109100 -108900 -108700log importance weightThe trace plot shows good mixing (“r<strong>and</strong>omness”), <strong>and</strong> the acf shows low autocorrelations. Also, many <strong>of</strong>the logs <strong>of</strong> the importance weights are similar.To continue the sampling, we can use the update method for bab (update.bab). However, based on thediagnostic plots, <strong>and</strong> the low Monte Carlo st<strong>and</strong>ard errors (in summary output) we probably have enoughiterations. Large, sparse tables may benefit most from these methods, <strong>and</strong> we will examine them later inthis chapter.F. Association Models, Correlation Models, <strong>and</strong> Correspondence Analysis1. Multiplicative Row <strong>and</strong> Column Effects ModelThe row effects model <strong>and</strong> the column effects model (as well as the L x L model) are special cases <strong>of</strong> theRow-<strong>and</strong>-column-effects model (Goodman’s RC model), which has parameter scores for both rows <strong>and</strong>columns. The model multiplies the two sets <strong>of</strong> parameters in the model equation,


165X Ylog µ = λ + λ + λ + βµνij i j i jwhere µi<strong>and</strong> νjare parameter scores. So, instead <strong>of</strong> having to fix arbitrary scores, we can estimateparameter scores. Agresti notes that finding MLEs is not always easy because the likelihood is notalways concave. An iterative modeling fitting algorithm is suggested by Goodman, where one alternatesbetween fitting row effects <strong>and</strong> column effects models, using the estimated scores from each in turn. Inthis way, one can ideally get MLEs <strong>of</strong> the row <strong>and</strong> column scores. Actually, if corner-point constraints areused for the parameter scores, then one gets the differences between each score <strong>and</strong> the last score (ifthe last parameter score is set to zero for identifiability). Also, one set <strong>of</strong> either the row or columnparameter score estimates is multiplied by the estimate for β . Thus, the method does not appear to beuseful as a way to estimate parameter scores.In the R package VGAM, there is a function grc, which is a front end for the function rrvglm (reducedrankvector generalized linear models), also in the same package. This function will fit an RC model, butnot estimate the scores as per Agresti’s (9.13) representation <strong>of</strong> the RC model. Instead, it estimates amatrix <strong>of</strong> “interaction terms”, δij, i = 1,…, I – 1; j = 1,…, J – 1, where δij= aci j= β ( µi−µ I)( νj− νJ), inthe language <strong>of</strong> model (9.13) in Agresti. Separating δ ijinto these three components appears to beimpossible. However, we can still fit the model, <strong>and</strong> compare its deviance to that <strong>of</strong> an L x L model. Wewill see later that correspondence analysis will give us MLEs <strong>of</strong> the row <strong>and</strong> column scores.When I prepare the data for grc, I use “reverse” levels for the factors so that the last category <strong>of</strong> therespective factor is zeroed out instead <strong>of</strong> the first category.table.9.9


166In the output, the Row <strong>and</strong> Column coefficients refer to the λ Xi<strong>and</strong> λ Y j’s. Because we set the lastcategory to zero, Row2 refers to SES E <strong>and</strong> Row6 refers to SES A. Also, Col2 refers to Moderate, <strong>and</strong>Col4 refers to Well. The coefficients labeled A through E refer to the first five cjvalues (the last is 0). Acall to summary (i.e., summary(fit.rc)) gives some more information, as well as st<strong>and</strong>ard errors.To get the matrix <strong>of</strong> δ ij’s, we first compute a biplot (without plotting it). The use <strong>of</strong> biplot reflects thefact that the cjvalues are involved in a latent variable interpretation in the rrvglm context. However, weonly use biplot here to get the interaction term estimates.res


167(fit.LL


168First canonical correlation(s): 0.1615119MH scores:well mild moderate impaired-1.611765 -0.1822571 0.08506316 1.470761SES scores:A B C D E F-1.127146 -1.151843 -0.3717666 0.07545902 1.018916 1.775564Compare these estimates with those on p. 381 in Agresti.We could fit a saturated model (i.e., M = 3) by using nf = 3 or by using functions for correspondenceanalysis, described next.3. Correspondence AnalysisCorrespondence analysis is a graphical method for describing associations among categorical variables.The rows <strong>and</strong> columns <strong>of</strong> a contingency table are represented by points on a graph. The greater themagnitude <strong>of</strong> the projections <strong>of</strong> the points onto an axis <strong>of</strong> the graph, the greater the association describedby that axis. Using the same notation as for equation (9.1) above, correspondence analysis uses theadjusted scoresx = λ µ , y = λνik k ik jk k jkThen, the graph <strong>of</strong> the first two dimensions plots ( xi1, xi2) for each row, <strong>and</strong> ( yj1, yj 2) for each column.There are many functions for doing correspondence analysis in S-<strong>PLUS</strong> <strong>and</strong> R. I will try to illustratedifferent aspects <strong>of</strong> each.The library multiv in both S-<strong>PLUS</strong> <strong>and</strong> R has function ca for doing correspondence analysis <strong>and</strong> functionplaxes to help with creating a plot like that in Figure 9.4 <strong>of</strong> Agresti. It needs an array or table, sotable.9.9b.array


169evals are the squared correlations, the λkabove. The second <strong>and</strong> third are almost zero, showing lesserimportance <strong>of</strong> the second <strong>and</strong> third dimensions in explaining variability. rproj <strong>and</strong> cproj are the row <strong>and</strong>column scores on each <strong>of</strong> the three dimensions.To plot the scores, use# plot <strong>of</strong> first <strong>and</strong> second factorsplot(fit.ca$rproj[,1], fit.ca$rproj[,2],type="n",ylim=c(-.1,.1),xlim=c(-.3,.3),xlab="",ylab="",axes=F)text(fit.ca$rproj[,1], fit.ca$rproj[,2], labels=dimnames(table.9.9b.array)$SES)text(fit.ca$cproj[,1], fit.ca$cproj[,2], labels=dimnames(table.9.9b.array)$MH)# Place additional axes through x=0 <strong>and</strong> y=0:my.plaxes(fit.ca$rproj[,1], fit.ca$rproj[,2],size=.15)# R: my.plaxes.f(fit.ca$rproj[,1], fit.ca$rproj[,2],Length=.15)where my.plaxes is a modification <strong>of</strong> plaxes# S-<strong>PLUS</strong>my.plaxes


170fit.corresp$cscore %*% diag(fit.corresp$cor)[,1] [,2] [,3]A -0.18204759 0.01965179 0.027712258B -0.18603645 0.01028853 -0.026939901C -0.06004474 0.02157969 -0.010480869D 0.01218753 -0.04121676 0.009748570E 0.16456713 -0.04381688 -0.008190311F 0.28677477 0.06237172 0.003614761Plotting is done via plot(fit.corresp)The R package CoCoAn will do (constrained) correspondence analysis via the function CAIV. The calltakes an array (table) <strong>and</strong> here would belibrary(CoCoAn)fit.CAIV


171Coefficients:Value Std. Error t value(Intercept) -6.3120972 0.5064590 -12.463196Valve -0.3298665 0.4381267 -0.752902Age 1.2209479 0.5136586 2.376964(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 10.84053 on 3 degrees <strong>of</strong> freedomResidual Deviance: 3.222511 on 1 degrees <strong>of</strong> freedomNumber <strong>of</strong> Fisher Scoring Iterations: 4Thus, the estimated rate for the older age group (coded 1, by the ordering <strong>of</strong> the levels) is exp(1.221) =3.4 times that for the younger group. The LR confidence intervals arelibrary(MASS)exp(confint(fit.rate))2.5 % 97.5 %(Intercept) 0.0005868836 0.004396417Valve 0.2988564722 1.709356104Age 1.3239556759 10.392076295The chi-squared statistic issum(resid(fit.rate, type = "pearson")^2)[1] 3.113503which is not a significantly poor fit at the .05 level.Fitted values are obtain byattach(table.9.11)mhat


172summary(fit.id, cor=F)Coefficients:Value Std. Error t valueI(codes(Valve) * Exposure) -0.001936109 0.001315309 -1.4719796I(rev(codes(Age)) * Exposure) 0.003965548 0.001440062 2.7537340Exposure 0.000479430 0.003249681 0.1475314(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: Inf on 4 degrees <strong>of</strong> freedomResidual Deviance: 1.093067 on 1 degrees <strong>of</strong> freedomNumber <strong>of</strong> Fisher Scoring Iterations: 4H. Modeling Survival TimesAgresti notes the connection between ML estimation using a Poisson likelihood <strong>and</strong> a survival-timelikelihood using an exponential hazard function. So, we can use methods for fitting loglinear models to fitthese types <strong>of</strong> survival models.The r<strong>and</strong>om variable is the time to some event. For the ith subject, this is t i. If the ith subject does notexperience the event during the study time, then their recorded observation is censored. Let wi= 0 if theith observation is censored, <strong>and</strong> 1 otherwise. Then, if the exponential hazard is modeled asht ( ; x) = λ exp( β ′ x)for covariates in x, then the survival-time log likelihood as a function <strong>of</strong> β is identical to the log likelihoodfor independent Poisson variates w iwith expected values µi= thti(i; xi). Thus, we fit the Poissonloglinear model log( µiti)= log λ + β ′ xito responses { wi}.In the Lung Cancer Survival example, Agresti uses a piecewise constant hazard rate instead <strong>of</strong> aconstant hazard rate. The hazard is constant in two-month intervals <strong>of</strong> follow-up time. The data containcounts <strong>of</strong> deaths within each follow-up interval, by factors Histology (I, II, III) <strong>and</strong> Stage <strong>of</strong> disease (1, 2,3). When we read in the data set, the variable Deaths is the number <strong>of</strong> deaths at each cell <strong>of</strong> the table, orthe sum <strong>of</strong> the { w } for the ith histology, jth stage, <strong>and</strong> kth time interval.ijkAs usual, we reverse the levels <strong>of</strong> factors so that the glm coding sets the last category to zero instead <strong>of</strong>the first.table.9.13


173fit.surv


174I. Empty Cells <strong>and</strong> SparsenessWhen some cells in a table are empty, ordinary ML estimation may give infinite estimates <strong>of</strong> modelparameters, or very large estimates with large st<strong>and</strong>ard errors. Also, goodness-<strong>of</strong>-fit statistics may nothave asymptotic chi-squared distributions. Alternative tests include exact small-sample tests <strong>and</strong> MonteCarlo approximations to exact tests.For example, for the pharmaceutical clinical trials example, with five centers, two centers have nosuccesses for either the treatment or placebo groups. A logit model with factors Treatment <strong>and</strong> Center,with no intercept gives nonsense estimates for the Center 1 <strong>and</strong> 3 effects because the Center-Responsemarginal table has zeroes.table.9.16


175However, we can use the survival library to get an exact likelihood ratio test <strong>of</strong> the treatment effect.Recall that to use clogit (R) or coxph (S-<strong>PLUS</strong>) for conditional logistic regression, the data must be in0’s <strong>and</strong> 1’s (i.e., success, failure) instead <strong>of</strong> counts out <strong>of</strong> totals. I modify the data first, <strong>and</strong> rename thetable table.9.16a. I call the success/failure factor, case.temp


176Chapter 10 – Models for Matched PairsA. Summary <strong>of</strong> Chapter 10, AgrestiMatched pairs data contain observations on two response variables where each observation fromone response variable pairs with one <strong>and</strong> only one observation from the other response variable. Thus,the two response variables are dependent. This chapter examines analyses <strong>of</strong> matched-pairs data withcategorical responses.The first section considers binary outcomes. Marginal homogeneity occurs when the marginalprobabilities for one response equal the corresponding marginal probabilities for the other response. Thisimplies that the <strong>of</strong>f-diagonal joint probabilities are also equal (symmetry). Mcnemar’s Test can be used tocompare the corresponding dependent proportions.“Subject-specific” tables have separate tables for each matched pair. They allow probabilities tovary by pair. (Thus, a better term might be pair-specific tables). Population-averaged tables sum thesubject-specific tables <strong>and</strong> correspond to marginal models. Subject-specific tables are used to fit logitmodels that allow each pair to have their own probability distribution. However, with these models, thereare usually more parameters than pairs, leading to problems with ML estimation. Solutions includeconditional ML estimation <strong>and</strong> r<strong>and</strong>om effects models.Extensions to matched-pairs with multinomial responses <strong>and</strong> additional explanatory variables usethe r<strong>and</strong>om effects approach. This is because the conditional ML approach can only estimate within-pairor within-cluster effects. Between-cluster effects (covariates that are constant within any given cluster)will cancel out <strong>of</strong> the conditional likelihood. Marginal models for matched-pairs for multinomial responsesare discussed in Section 10.3.Instead <strong>of</strong> modeling the marginal probabilities, one can model the joint cell probabilities. Cellprobabilities that satisfy symmetry (i.e., π ab= π bafor a≠b) also satisfy marginal homogeneity, but notnecessarily vice versa (unless quasi-symmetry also holds). The equivalent loglinear model has pairwiseassociation terms independent <strong>of</strong> the order <strong>of</strong> the subscripts (e.g., λ ab= λ bafor a≠b). Because <strong>of</strong> itssimplicity, a symmetry model fits well in very few applications. A quasi-symmetry loglinear model permitsthe main-effect terms in the symmetry model to differ, so that λ ab= λ bafor log µ ab<strong>and</strong> log µ ba, but themain-effect terms differ so that log µab≠ log µba, as it is for symmetry (see equation 10.19 in Agresti).Y Xλ − λ are equal to conditional MLEs <strong>of</strong>The differences in the MLEs <strong>of</strong> the main effects parameters { j j }the { β j } in a baseline-category logit model applied to a population-averaged table. Finally, a squaretable satisfies quasi-independence if the row <strong>and</strong> column variables are independent given that the row<strong>and</strong> column outcomes differ. Ordinal versions <strong>of</strong> quasi-symmetry <strong>and</strong> quasi-independence are given inSections 10.4.6 – 10.4.8.Matched-pairs models are used to analyze agreement between two observers. An independencemodel assumes no agreement. Quasi-independence <strong>and</strong> quasi-symmetry models imply someassociation. A numerical index <strong>of</strong> agreement is Cohen’s Kappa, ranging between 0 = agreement bychance only <strong>and</strong> 1 = perfect agreement.Paired preferences are common in analyses using preference data. The data may consist <strong>of</strong> an Ix I table whose cells contain the number <strong>of</strong> preferences for the column designation versus the rowdesignation. The Bradley-Terry logit model can be used to model influences on the probability that onemember is preferred to another. The model can be extended to h<strong>and</strong>le comparisons on ordinal scales.Matched-sets are the generalization <strong>of</strong> matched-pairs to three or more responses. There areanalogs for marginal homogeneity, symmetry, <strong>and</strong> quasi-symmetry for matched-sets. Marginalhomogeneity is the equality <strong>of</strong> marginal probabilities in the multi-way table. Symmetry is the equality <strong>of</strong>symmetric joint probabilities, where symmetric means permuted responses. The log-linear quasisymmetrymodel is the symmetry model, plus factors representing main effect terms for the marginals.


177B. Comparing Dependent ProportionsIn a 2 x 2 table, if π1 +denotes the marginal “success” probability for response 1, <strong>and</strong> π + 1denotesthe marginal “success” probability for response 2, then to test for marginal homogeneity ( H0 : π1 += π+1),one can use the difference between the corresponding sample proportions. The square <strong>of</strong> the scorestatistic for this test (equation 10.4 in Agresti) is the test statistic <strong>of</strong> Mcnemar’s Test, which has chisquareddistribution with 1 df. Mcnemar’s test does not use the counts on the main diagonal becausethey are irrelevant to decide whether the marginal probabilities π1 +<strong>and</strong> π + 1differ. Confidence intervalson the difference ( π1 − π1), based on large-sample theory, do depend on the main diagonal counts.+ +The Prime Minister Approval Rating example compares the proportion <strong>of</strong> survey respondentswho approved <strong>of</strong> the UK Prime Minister on two different occasions (the responses).table.10.1


178C. Conditional Logistic Regression for Binary Matched PairsA subject-specific table (or, more accurately, a pair-specific table) can be used to fit a logit model withpair-specific probabilities. Specifically, for binary response Yitfrom the tth observation from the ith pair[ PY ( )]logit = 1 = α + β xit i twhere x1= 0 <strong>and</strong> x2= 1. With this model, for each pair, the odds <strong>of</strong> success for observation 2 areexp( β ) times the odds for observation 1, <strong>and</strong> this odds ratio is common across pairs. Conditional on theαi<strong>and</strong> β , pairs are independent <strong>of</strong> each other, <strong>and</strong> within a pair, observations are independent <strong>of</strong> eachother. This means that the association within a pair is described completely by αi, <strong>and</strong> pairs are onlyassociated via the common effect, β .Conditional logistic regression can be used to find the MLE for β , where one conditions onsufficient statistics for the αi, which are the pairwise success totals { Si = yi1 + yi2}(see section 10.2.3 inAgresti). This distribution only depends on β when at least one <strong>of</strong> the yitis 1. Thus, inference on βonly depends on outcomes in different categories at the two observations.An alternative way to find the MLE for β is to treat the α ias r<strong>and</strong>om effects, with identicalnormal distributions. Integrating the likelihood with respect to the distributions <strong>of</strong> theα iyields a marginallikelihood for β , which is maximized to get the MLE. The model is called a generalized linear mixedmodel. The MLE is the same for either the conditional ML estimation or ML estimation via thegeneralized linear mixed model.Agresti illustrates a case-control study where 144 cases have myocardial infarction (MI) <strong>and</strong> 144controls, matched to cases by age <strong>and</strong> gender, do not. Thus, there are 144 pairs. Each member <strong>of</strong> apair was also asked whether they had diabetes (1 = yes, 0 = no). The response in this model is MI, whichis fixed. The predictor is diabetes, which is r<strong>and</strong>om. The study is thus retrospective, but we can still getan estimate <strong>of</strong> the XY odds ratio, exp( β ) in a logistic regression model, by using the fixed responses(see chapter 2). The model for subject t in pair i is[ PY ( )]logit = 1 = α + β xit i itWe can get the MLE <strong>of</strong> β using conditional logistic regression or by integrating out r<strong>and</strong>om effects. Thepopulation-averaged table is in Table 10.3 in Agresti. The pairs for the subject-specific table are in Table10.4. We input the data into S using the subject-specific form. The following data frame has thevariables: pair (either subject 1 or subject 2 <strong>of</strong> the matched pair), MI (myocardial infarction; yes=1, no=0),diabetes (yes=1, no=0).table.10.3


179summary(fit.CLR)n= 288coef exp(coef) se(coef) z pdiabetes 0.838 2.31 0.299 2.8 0.0051exp(coef) exp(-coef) lower .95 upper .95diabetes 2.31 0.432 1.29 4.16Rsquare= 0.029 (max possible= 0.5 )Likelihood ratio test= 8.55 on 1 df, p=0.00345Wald test = 7.85 on 1 df, p=0.00508Score (logrank) test = 8.32 on 1 df, p=0.00392In Section 10.2.6, Agresti shows how to fit this model using only s<strong>of</strong>tware for logistic regression.We can also fit a generalized linear mixed model (GLMM), where the αiare r<strong>and</strong>om effects with normaldistribution, mean 0 <strong>and</strong> unknown st<strong>and</strong>ard deviation. The MASS library fits GLMMs with its functionglmmPQL via penalized quasi-likelihood estimation. It uses the function lme, so the specification <strong>of</strong> itsarguments is very similar to lme, <strong>and</strong> its output is identical. Two additional R packages have functions forfitting GLMMs. glmmML in package glmmML fits GLMMs (binomial <strong>and</strong> Poisson families only) with r<strong>and</strong>om(normal) intercepts via maximum likelihood estimation. The repeated package also contains a glmmfunction, which fits via ML estimation. Gauss-Hermite quadrature is used to integrate with respect to ther<strong>and</strong>om effects.To use glmmPQL, we need to specify a fixed-effect formula <strong>and</strong> a r<strong>and</strong>om-effect formula. The fixed-effectformula will be like an ordinary S formula: response ~ predictor. Here, it is MI ~ diabetes becausediabetes is the predictor. To specify a r<strong>and</strong>om intercept within pair, we use ~ 1 | pair for the r<strong>and</strong>omargument. The 1 indicates a r<strong>and</strong>om intercept, <strong>and</strong> “| pair” means “within pair”. Thus, the call for bothS-<strong>PLUS</strong> <strong>and</strong> R islibrary(MASS)fit.glmmPQL


180In the output, under “R<strong>and</strong>om effects”, we get the st<strong>and</strong>ard deviation <strong>of</strong> the normal distribution <strong>of</strong> ther<strong>and</strong>om effects, which is quite small at 0.001827267. This may imply a very low association betweenmembers <strong>of</strong> the same pair, as it means that the αiare probably very similar (Recall that the associationis described completely by the αi, by definition <strong>of</strong> the model). “Residual”, I believe, is an estimate <strong>of</strong> thescale, which for the binomial is fixed at 1.0. We also get estimates <strong>of</strong> the fixed effects, notably theestimate <strong>of</strong> the effect <strong>of</strong> diabetes (0.8038742). The PQL estimate is slightly different from the conditionalML estimate from coxph.The function glmmML only allows a r<strong>and</strong>om intercept, <strong>and</strong> as such is less general than glmmPQL, althoughit is fine for this particular problem. The arguments for glmmML differ in that cluster defines the r<strong>and</strong>omfactor. Note that the cluster argument will be evaluated in the calling environment, not within the datalist. So, you cannot just set cluster=pair for analyzing table.10.3 above (you must include the dataframe as well). Using glmmML, we get the same numerical estimate <strong>of</strong> β as from glmmPQL. The estimate<strong>of</strong> the st<strong>and</strong>ard deviation <strong>of</strong> the r<strong>and</strong>om effects (7.12e-06) is much smaller, but the resulting conclusionabout low association is the same.library(glmmML)glmmML(MI ~ diabetes, cluster=table.10.3$pair, family = binomial, data = table.10.3)coef se(coef) z Pr(>|z|)(Intercept) -0.1942 0.1364 -1.423 0.15500diabetes 0.8039 0.2835 2.836 0.00457St<strong>and</strong>ard deviation in mixing distribution: 7.12e-06Std. Error: 0.05206Residual deviance: 390.9 on 285 degrees <strong>of</strong> freedom AIC: 396.9The definitions <strong>of</strong> AIC are the same in both functions (i.e., -2*loglik + 2*npar). Thus, the difference inthe AIC values across functions is due to the representation <strong>of</strong> the log likelihood, as glmmML gives -195.4561 for the log likelihood.The function glmm in the repeated library also fits r<strong>and</strong>om effects model, <strong>and</strong> allows several choices <strong>of</strong>error distributions besides binomial. The argument nest is for specifying the pair variable.fit.glmm|z|)(Intercept) -1.942e-01 1.365e-01 -1.423 0.1548diabetes 8.039e-01 2.837e-01 2.834 0.0046 **sd 6.829e-07 1.196e-01 5.71e-06 1.0000---Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1(Dispersion parameter for binomial family taken to be 1)Null deviance: 399.25 on 287 degrees <strong>of</strong> freedomResidual deviance: 390.80 on 285 degrees <strong>of</strong> freedomAIC: 396.8Number <strong>of</strong> Fisher Scoring iterations: 4Normal mixing variance: 4.662868e-13Thus, glmm gives the same estimates as glmmML.


181D. Marginal Models for Square Contingency TablesThis section discusses marginal models for matched-pairs with ordinal <strong>and</strong> nominal multinomialresponses. For an I x I table, marginal homogeneity is PY (1= a) = PY (2= a)for responses Y t, t = 1, 2<strong>and</strong> categories a= 1,..., I .1. Ordinal ClassificationsThe cumulative logit model for matched-pairs is[ ( )]logit PY≤ j = α + βx, t= 1, 2; j= 1,..., I−1t j twhere x1= 0 <strong>and</strong> x 2= 1. In this model, the odds <strong>of</strong> outcome Y 2≤ j is exp( β ) times the odds <strong>of</strong>outcome Y 1≤ j . Marginal homogeneity corresponds to β = 0 .In the Premarital <strong>and</strong> Extramarital Sex example, subjects responded with opinions aboutpremarital sex (1 = always wrong, 2 = almost always wrong, 3 = sometimes wrong, 4 = never wrong) <strong>and</strong>about extra-marital sex. Agresti fits a cumulative marginal logit model (equation 10.14) to these data,using maximum likelihood estimation. One way to do this is to use J. Lang’s mph.fit function for R,which uses a method <strong>of</strong> constrained maximum likelihood estimation. The model specification follows thatin Section 11.2.5 in Agresti. Briefly, if π denotes the complete set <strong>of</strong> multinomial joint probabilities, thena marginal logit model has the generalized loglinear form( π ) β (10.1)Clog A = XThe components <strong>of</strong> (10.1) are used in mph.fit.In using the function, I follow the notation <strong>of</strong> Lang’s documentation. First, I will fit a marginalhomogeneity model, which constrains the corresponding row <strong>and</strong> column marginal probabilities to beequal. This constraint is specified in the function h.fct, which is an optional argument to mph.fit. Asyou might perceive, the h function is the gradient <strong>of</strong> the Lagrangian.The observed counts are in y. The matrix Z describes the strata in the data. Here, we do not havestratification, so Z is a vector <strong>of</strong> ones. ZF = Z because the sample size is assumed fixed (see thedocumentation). M1, M2, <strong>and</strong> C.matrix are used in the constraint function. M1 <strong>and</strong> M2 are used to get row<strong>and</strong> column totals <strong>of</strong> the expected counts. C.matrix is a constraint matrix that specifies marginalhomogeneity.y


1823,8,byrow=T)h.fct


183CMarg


184from the Northeast, Midwest, South, <strong>and</strong> West regions <strong>of</strong> the U.S. in 1980 to any <strong>of</strong> those same regionsin 1985. The large majority remained in the same region for both years.To compute a LR test using glm, we need the cell counts <strong>and</strong> a set <strong>of</strong> dummy variables thatcorrespond to the constraints <strong>of</strong> marginal homogeneity. Table A.17 in Agresti shows the set <strong>of</strong> dummyvariables. In that table, corresponding marginal expected frequencies are constrained to be equal <strong>and</strong>are thus represented by only three variables: m1, m2, <strong>and</strong> m3 (m4 = n – m1 – m2 – m3), instead <strong>of</strong> sixvariables, differentiating row from column totals. Then, the observed counts are modeled as functions <strong>of</strong>the expected marginal frequencies as well as expected cell counts. For example, the observed (1, 4) cellis modeled as m1 – (m11 + m12 + m13), where mij gives the expected count in the ijth cell.So, we create a data frame with this matrix <strong>of</strong> dummy variables <strong>and</strong> the observed counts.dummies


185NE 11607.00000 98.08285 265.6867 93.96975MW 88.73298 13677.00000 379.0733 232.34523S 276.47479 350.80060 17819.0000 287.25936W 92.53157 251.26811 269.7747 10192.00000Of course, with such a large value <strong>of</strong> the statistic, we reject marginal homogeneity.The R function mph.fit can also be used to get the LR test. Example 3 <strong>of</strong> Lang’s documentation for thefunction gives the necessary steps, which can be applied almost verbatim to the Migration data. It is leftto the reader to try mph.fit on this data set, recalling that h.fct is the constraint function.A Wald test <strong>of</strong> marginal homogeneity can be computed using Bhapkar’s statistic (equation 10.16 inAgresti). This statistic is actually very easy to calculate if we follow Wickens (1989, pp. 313-314) <strong>and</strong>note that what we are testing is a set <strong>of</strong> simultaneous linear combinations <strong>of</strong> the cell probabilities. Theselinear combinations areπ + π + π = π + π + ππ π π π π ππ π π π π π12 13 14 21 31 4112+32+42=21+23+2413+23+43=31+32+34which imply marginal homogeneity if they hold simultaneously. If we represent the cell probabilities in a16 x 1 vector, π , then the following matrix, A, gives the three linear combinations above if post-multipliedby πA


186[1] 0E. Symmetry, Quasi-symmetry, <strong>and</strong> Quasi-independenceAn I x I table satisfying symmetry has πab = πba, a≠b. This implies the loglinear modellog µ ab= λ + λ a+ λ b+ λ abwhere all λab= λba. Note that the main-effect terms are the same for the two expected frequencies µab<strong>and</strong> µba. Quasi-symmetry allows the main-effect terms to differ so thatlog µ = λ + λ X + λ Y + λ ≠ log µ = λ + λ X + λ Y + λab a b ab ba b a abFor this model, the odds ratios on one side <strong>of</strong> the main diagonal are identical to the “mirror-image” oddsratios on the other side. The loglinear model for a table displaying quasi-independence adds a parameterfor each cell on the main diagonal:log µ = λ+ λ X+ λ Y+ δ Ia ab a b a( = b )When δa> 0 , µaais greater than under independence. Thus, this model would be used for a table thatshowed independence on the <strong>of</strong>f-diagonal cells, but had larger counts on the main diagonal.Agresti uses the Migration example to illustrate fitting these three models via maximum likelihoodestimation. We can use the definitions above to create a data frameresidence80


18710 MW S 225 2,311 S S 17819 3,312 W S 270 3,413 NE W 63 1,414 MW W 176 2,415 S W 286 3,416 W W 10192 4,4options(contrasts = c("contr.treatment", "contr.poly"))(fit.symm


188matrix(fitted(fit.qsymm), ncol = 4, byrow = T, dimnames =list(rev(levels(residence80)), rev(levels(residence85))))NE MW S WNE 11607.00000 95.78862 370.4375 123.7739MW 91.21138 13677.00000 501.6825 311.1061S 167.56253 238.31746 17819.0000 261.1200W 63.22609 166.89392 294.8800 10192.0000From equation (10.25) in Agresti, a test <strong>of</strong> the null hypothesis <strong>of</strong> marginal homogeneity is given by1 - pchisq(fit.symm$deviance - fit.qsymm$deviance, df = fit.symm$df - fit.qsymm$df)[1] 0showing strong evidence <strong>of</strong> marginal heterogeneity.A fit <strong>of</strong> the quasi-symmetry model for the Migration data is given in an example in the documentation forthe R package exactLoglinTest. In fact, the data are included in the library as residence.dat.library(exactLoglinTest)data(residence.dat)residence.daty res.1985 res.1980 sym.pair1 11607 NE NE 12 100 MW NE 23 366 S NE 34 124 W NE 45 87 NE MW 26 13677 MW MW 57 515 S MW 68 302 W MW 79 172 NE S 310 225 MW S 611 17819 S S 812 270 W S 913 63 NE W 414 176 MW W 715 286 S W 916 10192 W W 10The variable sym.pair serves the same purpose as symm above. We can use mcexact to check theasymptotic p-value.(fit.qsymm.exact


189To fit a quasi-independence model to the Migration data, we can add four dummy variables to the dataframe that represent the main diagonal cells.table.10.6$D1


190table.10.5$symm


191Value Std. Error t value(Intercept) 1.6094379 0.4472136 3.5988126symm3,4 1.6327093 0.4883751 3.3431460symm2,4 1.7419086 0.4842847 3.5968686symm1,4 3.2108947 0.4560560 7.0405708symm3,3 0.1823216 0.6055301 0.3010941symm2,3 1.1472015 0.5123936 2.2389066symm1,3 2.8054296 0.4603858 6.0936490symm2,2 -0.2231436 0.6708204 -0.3326428symm1,2 1.9299608 0.4781430 4.0363673symm1,1 3.3603754 0.4549115 7.3868777tau -4.1303475 0.4499372 -9.1798320(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 888.3778 on 15 degrees <strong>of</strong> freedomResidual Deviance: 15.51736 on 5 degrees <strong>of</strong> freedomNumber <strong>of</strong> Fisher Scoring Iterations: 5The estimated value <strong>of</strong> τ implies that the estimated probability that premarital sex is considered morewrong (i.e., closer to 1 than 4) than extramarital sex is exp(-4.13) = 0.016 times the estimated probabilitythat extramarital sex is considered more wrong.3. Quasi-Uniform AssociationEven after conditioning on the event that the two responses differ, sometimes independence still does nothold. Sometimes there is a monotone pattern to the probabilities. The loglinear modelX Ylog µ = λ + λ + λ + βuu + δ I( a=b)ab a b a b apermits linear-by-linear association <strong>of</strong>f the main diagonal. For equal-interval scores, it implies uniformlocal association, given that responses differ.Fitting this model to the above example requires first defining scores for the row variable, then creating aDelta vector respresenting δa.table.10.5$scores.a


192Delta 0.43837201 0.2737843 1.601158scores:scores.a 0.61632154 0.1142078 5.396493(Dispersion Parameter for Poisson family taken to be 1 )Null Deviance: 888.3778 on 15 degrees <strong>of</strong> freedomResidual Deviance: 5.797136 on 7 degrees <strong>of</strong> freedomNumber <strong>of</strong> Fisher Scoring Iterations: 4Thus, <strong>of</strong>f the main diagonal, the estimated local odds ratio is exp(0.616) = 1.85, which is slightly lowerthan Agresti’s result <strong>of</strong> 1.88.G. Measuring Agreement Between ObserversMatched-pairs models are used to measure agreement between ratings from two observers. Anindependence model would imply no agreement, whereas a quasi-independence or quasi-symmetrymodel implies some agreement in ratings.Section 10.5 in Agresti uses the Pathologists’ Tumor Ratings data to illustrate fitting differentmodels to assess degree <strong>of</strong> agreement between the two pathologists. The baseline model is theindependence model. The quasi-independence <strong>and</strong> quasi-symmetry models allow estimation <strong>of</strong> the odds<strong>of</strong> agreement.The data set is available in the R package exactLoglinTest. We dump the object <strong>and</strong> source it into an S-<strong>PLUS</strong> session. So, for example# Rlibrary(exactLoglinTest)data(pathologist.dat)dump("pathologist.dat", file="c:/program files/insightful/splus61/users/cda/pathologist.R")# S-<strong>PLUS</strong>source("pathologist.R")However, there are three differences between pathologist.dat <strong>and</strong> Table 10.8 in Agresti. First, Agrestionly uses the first 4 levels <strong>of</strong> each variable. Second, the A <strong>and</strong> B labels are reversed. Third, two <strong>of</strong> thecounts differ across the two data sets. We will fix the last two now, then subset the data in the glm call.We can switch the labels by changing the namesnames(pathologist.dat)


193options(contrasts = c("contr.treatment", "contr.poly"))(fit.ind


194[3,] 0.7646757 1.2353243 36.000000 4.366551e-005[4,] 1.8673623 3.0167010 13.115937 1.000000e+001A quasi-symmetry model would account for any association on the <strong>of</strong>f-diagonal. To fit that model, weneed to create a symm factor. To ensure that we use the correct levels, we create a new data framewithout the ratings <strong>of</strong> 5. Then, we drop the unused level 5 in the factors.pathologist.dat2


195H. Kappa Measure <strong>of</strong> AgreementA numerical index that summarizes agreement is Cohen’s Kappa. Kappa compares the probability <strong>of</strong>agreement, ∑ π , to that expected if the two sets <strong>of</strong> ratings were independent, a aa∑ π π a a + + a, as aproportion <strong>of</strong> the difference between perfect agreement (1.0) <strong>and</strong> chance agreement.κ =∑∑∑πa− π π1−π πaa a a+ + aaa+ + aKappa equals 0 when agreement equals that expected under independence, <strong>and</strong> equals 1.0 whenperfect agreement occurs. With ordered categories, one can weight disagreements in ratings differentlydepending on the difference between the ratings. Weighted Kappa isκ =w∑ ∑∑ ∑∑∑ ww π −1−a b ab ab a b ab a+ + babwπ πab a+ + bπ πFleiss <strong>and</strong> Cohen give weightsw a b I2 2ab= 1 −( − ) ( − 1) , which are larger for ratings closer together.We can compute Kappa <strong>and</strong> its ASE in S-<strong>PLUS</strong> using the functionKappa


196path.tab


197options(contrasts=c("contr.treatment", "contr.poly"))fit.BT


198µabX Y X Ylog = ( λ + λa + λb + λab ) − ( λ + λb + λa + λab)µbaX Y X Y= ( λ −λ ) −( λ − λ ) = β −βa a b b a bWe fit this quasi-symmetry model using the method from Section E <strong>of</strong> this chapter. First, I set up the dataset. The symm factor represents the interaction term.losing.team


199matrix(round(fitted(fit.BTQS),1),nr=7,nc=7,byrow=T,dimnames=list(win.team,losing.team))J. Bradley-Terry Model with Order EffectWe would like to see if there is a significant home team advantage. That is, is a team more likely to win ifthey play in their home city? We can incorporate a home team advantage for the team called a, via alogit model for the probability that team a beats team b when a is the home team:Πlog α ( βaβb)1− Π = + −*ab*abWhen α > 0 , there is a home team advantage. If the teams were equally matched ( βa= βb), the homeexp( α) 1+ exp( α)<strong>of</strong> winning.team has probability [ ]As mentioned in Agresti, the 42 pair sets (where order matters) can be viewed as 42 independentbinomial samples, where response is the number <strong>of</strong> successes (wins) for the home team out <strong>of</strong> the totalplayed. So, for example,response


200Thus, the estimate <strong>of</strong> the home team advantage parameter is positive, not surprisingly, implying a hometeam advantage, <strong>and</strong> its ASE is not too large.The fitted probabilities are obtained usingfitted.probs


201the family has a low income, (2) when the woman is not married <strong>and</strong> doesn’t want to marry the man, <strong>and</strong>(3) when the woman wants it for any reason. Gender was also recorded. So, there are four binaryresponses.Agresti first fits a complete symmetry model, with the same complete symmetry pattern within eachgender. So, there is a different intercept for each gender. The data are read below. Notice the order <strong>of</strong>the levels <strong>of</strong> the question variables.table.10.13


202Residual Deviance: 10.16 AIC: 112.8To see what the model predicts, we can look at the fitted probabilities.fitted.qs


203Chapter 11 – Analyzing Repeated Categorical ResponseDataA. Summary <strong>of</strong> Chapter 11, AgrestiRepeated categorical responses may come from repeated measurements over time on eachindividual or from a set <strong>of</strong> measurements that are related because they belong to the same group orcluster (e.g., measurements made on siblings from the same family, measurements made on a set <strong>of</strong>teeth from the same mouth). Observations within a cluster are not usually independent <strong>of</strong> each other, asthe response from one child <strong>of</strong> a family, say, may influence the response from another child, because thetwo grew up together. Matched-pairs are the special case <strong>of</strong> each cluster having two members.Using repeated measures within a cluster can be an efficient way to estimate the mean responseat each measurement time without estimating between-cluster variability. Many times, one is interestedin the marginal distribution <strong>of</strong> the response at each measurement time, <strong>and</strong> not substantially interested inthe correlation between responses across times. Estimation methods for marginal modeling includemaximum likelihood estimation <strong>and</strong> generalized estimating equations. Maximum likelihood estimation isdifficult because the likelihood is written in terms <strong>of</strong> the I T multinomial joint probabilities for T responseswith I categories each, but the model applies to the marginal probabilities. Lang <strong>and</strong> Agresti give amethod for maximum likelihood fitting <strong>of</strong> marginal models in Section 11.2.5. Modeling a repeatedmultinomial response or repeated ordinal response is h<strong>and</strong>led in the same way.Generalized estimating equations (GEE) are a multivariate generalization <strong>of</strong> quasi-likelihoodestimation. If there are T measurements per subject, then the quasi-likelihood method specifies a modelfor the mean <strong>and</strong> variance (as a function <strong>of</strong> the mean) for each measurement, without requiringknowledge <strong>of</strong> the distribution <strong>of</strong> the measurements (as ML estimation does). A “working” covariancestructure for the responses must be specified. The estimates <strong>of</strong> parameters in the mean response aresolutions <strong>of</strong> quasi-likelihood equations called generalized estimating equations. The estimates areconsistent even if the covariance matrix is incorrectly specified. However, this does not imply that onecan use an identity matrix for the correlation structure. The st<strong>and</strong>ard errors would be incorrect (probablyunderestimated). They are adjusted using the empirical dependence.However, as GEE does not have a likelihood function, then inference procedures based on alikelihood cannot be done. Instead, inference only uses Wald statistics <strong>and</strong> their asymptotic normality.Transitional models are special cases <strong>of</strong> logit or loglinear models for repeated categoricalobservations. When the repeated observations can be assumed to follow a Markov chain, an appropriateMarkov chain model can be fit. The first-order Markov property is satisfied when the prediction <strong>of</strong> a futureresponse only depends on the response immediately prior to it, <strong>and</strong> no other responses occurring beforethat previous response. Estimation <strong>of</strong> Markov chain models is via IRLS. Estimation can also be doneusing IPF if the model is a loglinear model. When there are explanatory variables, a regressive logisticmodel can be used (for a binary response) where the regressors are explanatory variables <strong>and</strong> alsoprevious responses.B. Comparing Marginal Distributions: Multiple ResponsesWith T repeated binary measurements, one may be interested in comparing the probability <strong>of</strong>success at each <strong>of</strong> the T times. The marginal logit model for T responses islogit[ PY ( = 1)] = α + β , t= 1,..., T(11.1)tBecause there are T probabilities with T + 1 parameters, one <strong>of</strong> the parameters must be constrained tobe zero. In that case, the model is saturated, <strong>and</strong> MLEs <strong>of</strong> the marginal probabilities are the sampleproportions with yt= 1. The multinomial likelihood is written in terms <strong>of</strong> the joint probabilities <strong>of</strong> response.Each sample cell proportion is the MLE <strong>of</strong> the joint probability corresponding to that cell. Marginalt


204homogeneity corresponds to PY (1= 1) = ... = PY ( T= 1) . That is, there are T identical responsedistributions across the T time periods. A likelihood ratio test compares the likelihood maximized undermarginal homogeneity to the ML fit under model (11.1). Maximum likelihood estimation under marginalhomogeneity is discussed in Section 11.2.5 in Agresti <strong>and</strong> in Section D in Chapter 10 <strong>of</strong> this manual, forillustrating Lang’s mph.fit function. The generalized loglinear form <strong>of</strong> the marginal logit model is given in(11.8) <strong>of</strong> Agresti, <strong>and</strong> repeated in (10.1) <strong>of</strong> this manual. For the case <strong>of</strong> a 2 T table, there are T marginalprobabilities. The model <strong>of</strong> marginal homogeneity with no covariates has the form given at the bottom <strong>of</strong>p. 464 in Agresti when T = 2. The Lagrangian includes constraints that set the marginal probabilitiesequal to one another, as well as identifiability constraints. The fitting method <strong>of</strong> Lang gives consistentestimates <strong>of</strong> the regression coefficients, β , if the marginal model holds, regardless <strong>of</strong> the form <strong>of</strong> the jointdistribution. This is because the marginal parameters β <strong>and</strong> the joint parameters π are orthogonal.Thus, for consistency <strong>of</strong> the marginal parameter estimates, the method is robust to the specification <strong>of</strong> thejoint distribution.Agresti tests marginal homogeneity for the Crossover Drug Comparison Example (Table 11.1) whereeach subject used each <strong>of</strong> three drugs at three different times for the treatment <strong>of</strong> a chronic condition.The binary response at each time was favorable/unfavorable response. We first use the functionmph.fit for R (available from J. Lang. See Agresti p. 649) to test marginal homogeneity.First, I source in the appropriate files.source("C:/Program Files/R/rw1080/CDA/mph.r")source("C:/Program Files/R/rw1080/CDA/mph.supp.fcts.r")I happen to set up the data as a data frame, but we only use the count vector within mph.fit for the test.table.11.1


205h.fct


206I have copied the appropriate data lines <strong>and</strong> column headers from Agresti’s CDA data set web site, <strong>and</strong>pasted them into a file, which I’ve renamed “depress.txt”. The time variable is transformed using logbase 2.table.11.2


207To begin, we define the indices g to index the four groups (1=Severe/New; 2=Severe/St<strong>and</strong>ard,3=Mild/New, 4=Mild/St<strong>and</strong>ard). We also define the indices t 1to t 3to index the outcome values at thethree measurement times. Then, we set the vector π equal to the 32 cell probabilities { πgt123t t}. Forexample, the probability <strong>of</strong> a normal outcome at all three times from an individual on the new drug, withsevere depression is π1111. The marginal probability <strong>of</strong> normal response for this individual at time 0 is theanti-logit <strong>of</strong> α + β1+ β2.We define the A <strong>and</strong> C matrices to get the expression on the left <strong>of</strong> (11.2). For example, to write themarginal probability <strong>of</strong> Normal response at time 0, for group 1, we compute the sum <strong>of</strong> cell probabilities,∑ ∑ . Thus, the row <strong>of</strong> A that corresponds to this sum has a 1 in each <strong>of</strong> the spots that1 1πt3= 0 t 1,1,2=0 t2,t3corresponds to each <strong>of</strong> the terms <strong>of</strong> the sum. Each row <strong>of</strong> the matrix C has exactly one element with a –1<strong>and</strong> exactly one element with a +1, the remaining being 0. This matrix forms the logits (a ratio <strong>of</strong> logs <strong>of</strong>sums <strong>of</strong> cell probabilities). Finally, the matrix X has one column <strong>of</strong> all ones (for the intercept), twocolumns for the group designation, <strong>and</strong> a final column for the value <strong>of</strong> time (0, 1, 2). Using thesedefinitions, we can run mph.fit for R.First, we must “unstack” the data frame table.11.2 so that time is three variables (log week 0, 1, <strong>and</strong> 2)instead <strong>of</strong> one.table.11.2a


2081 time treat diagnose1 1 0 1 12 1 1 1 13 1 2 1 14 1 0 0 15 1 1 0 16 1 2 0 17 1 0 1 08 1 1 1 09 1 2 1 010 1 0 0 011 1 1 0 012 1 2 0 0The sampling matrix has a column for each group <strong>and</strong> a row for each <strong>of</strong> the 32 cells, where the rows arein the same order as the Y vector. In each column, there is a 1 for each cell that corresponds to a cell forthe group for that column.Z


2090,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0, # g1 t0=00,0,0,0, 0,0,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0, 1,0,0,0, # g1 t1=00,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, # g1 t2=00,1,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0, # g2 t0=10,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, # g2 t1=10,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, # g2 t2=1# complement0,0,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0, # g2 t0=00,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, # g2 t1=00,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, # g2 t2=00,0,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0, # g3 t0=10,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, # g3 t1=10,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, # g3 t2=1# complement0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,1,0, # g3 t0=00,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,1,0, # g3 t1=00,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, # g3 t2=00,0,0,1, 0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,1, 0,0,0,0, # g4 t0=10,0,0,1, 0,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,1, 0,0,0,0, 0,0,0,0, # g4 t1=10,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, # g4 t2=1# complement0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,1, # g4 t0=00,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,1, # g4 t1=00,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1), # g4 t2=0nr=24,nc=32, byrow=TRUE)When multiplied by the log <strong>of</strong> the entries in Aπ , C.matrix gives the logits <strong>of</strong> the 12 marginalprobabilities.C.matrix


210link4 -1.32492541 -1.77220372 0.1580641 1.8976785link5 -0.94446161 -0.86527205 0.1129916 -0.4220890link6 -0.16034265 0.04165961 0.1320449 -1.3442489link7 0.11441035 0.36019844 0.1542620 -1.3096890link8 1.29928298 1.26713007 0.1434996 0.1283795link9 3.52636052 2.17406086 0.1851307 3.8787830link10 0.05001042 -0.52647349 0.1481831 3.2436321link11 0.35364004 0.38045817 0.1208956 -0.1390165link12 0.73088751 1.28738984 0.1554322 -2.4992189CONVERGENCE STATISTICS...iterations = 100norm.diff = 0.371203norm.score = 0.000702893Original counts used.FITTING PROGRAM USED: mph.fit, version 1.0, 6/5/02Warning message:the condition has length > 1 <strong>and</strong> only the first element will be used in: if (a$L !="NA")The warning message given is apparently a bug in the mph.summary function, but would not affect results.It can be eliminated by changing the lineif (a$L != "NA") {toif (!all(is.na(a$L))) {in the mph.summary function.Here we see that this model gives a LR statistics <strong>of</strong> around 34.6. A much smaller LR statistic comes fromadding an interaction term in treatment <strong>and</strong> time, implying that the effect <strong>of</strong> time varies over time.X$trtxtime


211link8 1.29928298 1.38743932 0.1510355 -0.3417647link9 3.52636052 2.87709738 0.2337484 1.3582751link10 0.05001042 -0.04385680 0.1698035 0.6448222link11 0.35364004 0.43879603 0.1201678 -0.4368103link12 0.73088751 0.92144887 0.1647290 -1.0296818CONVERGENCE STATISTICS...iterations = 100norm.diff = 1.25822norm.score = 9.27206e-10Original counts used.FITTING PROGRAM USED: mph.fit, version 1.0, 6/5/02The beta coefficients correspond to the columns <strong>of</strong> the X matrix. Thus, at time t, the estimated odds <strong>of</strong>Normal response for the new drug are exp(–0.06 + 1.01t) times the odds for the st<strong>and</strong>ard drug, for eachdiagnosis level.D. Marginal Modeling: Maximum Likelihood Approach. Modeling a RepeatedMultinomial ResponseWith a repeated multinomial response with I categories, there are I – 1 logits for each observation ormeasurement. For observation t, the logit takes the formTlogit ( t) = α + β x , j = 1,..., I −1(11.3)j j j tFor example, for an ordinal response, a proportional odds model uses logit ( t) logit [ P( Y j)]cumulative link, <strong>and</strong> βj= β .j= ≤ , aThe data in Table 11.4 in Agresti give the results <strong>of</strong> a r<strong>and</strong>omized clinical trial comparing an activehypnotic drug with a placebo in patients who have insomnia. The response is the reported time to fallasleep (with categories < 20 minutes, 20 – 30 minutes, 30 – 60 minutes, or > 60 minutes). For this dataset, I copied the data from Agresti’s website, pasted it into a text file called “insomnia.txt”, <strong>and</strong> added afirst line <strong>of</strong> variable headings. We will not be using the final variable called “count” in this data set.We read in the data set using read.table.table.11.4


212[,1] [,2] [,3] [,4][1,] 120 120 120 120[2,] 120 120 120 120, , 2[,1] [,2] [,3] [,4][1,] 119 119 119 119[2,] 119 119 119 119Now, we get the marginal counts using xtabs, <strong>and</strong> divide these counts by n, which is an array <strong>of</strong> thesame size (thus the division can be done).xtabs(~time+outcome+treat, data=table.11.4)/n# note that we divide by n, , treat = 0outcometime 1 2 3 40 0.11666667 0.16666667 0.29166667 0.425000001 0.25833333 0.24166667 0.29166667 0.20833333, , treat = 1outcometime 1 2 3 40 0.10084034 0.16806723 0.33613445 0.394957981 0.33613445 0.41176471 0.15966387 0.09243697We next fit the proportional odds model in equation (11.6) in Agresti, using mph.fit. This model permitsinteraction between occasion <strong>and</strong> treatment.The data set has a four-category (I = 4) ordinal response at T = 2 occasions. We follow the same stepsas for section C. First, we “unstack” the data frame table.11.4 so that time is two variables (initial,follow-up) instead <strong>of</strong> one.table.11.4a


213count is for treat=1: active treatment, for initial <strong>and</strong> follow-up time < 20 minutes (coded 1). The secondcount is for treat=0, with the same initial <strong>and</strong> follow-up time.Y


214[28,] 0 1[29,] 1 0[30,] 0 1[31,] 1 0[32,] 0 1Each row <strong>of</strong> the A matrix, when multiplied by the π vector, results in one <strong>of</strong> the cumulative marginalprobabilities or its complement. For example, the first row <strong>of</strong> A.matrix below, when multiplied by πgives the probability that a r<strong>and</strong>omly chosen observation from group 1 (active) takes < 20 minutes to fallasleep at initial time. The third row gives its complement.A.matrix


215nr=12,nc=24,byrow=T)The function L.fct computes the right h<strong>and</strong> side <strong>of</strong> equation 11.8 in Agresti.L.fct


216GEE methodology is a generalization <strong>of</strong> quasi-likelihood for multivariate responses. As with quasilikelihood,one specifies the mean for the set <strong>of</strong> responses <strong>and</strong> a link function relating the linear predictorto the mean, as well as the variance as a function <strong>of</strong> the mean <strong>and</strong> a guess at the correlation structureamong the responses. Estimation is accomplished via a set <strong>of</strong> estimating equations, yielding consistentestimates <strong>of</strong> the coefficients in the linear predictor regardless <strong>of</strong> the correctness <strong>of</strong> the correlationstructure. However, the st<strong>and</strong>ard errors <strong>of</strong> the estimates will be affected by the specification <strong>of</strong> thecorrelation structure. Thus, it is advantageous to try to specify an approximately accurate correlationstructure.Common correlation structures are independence (no correlation), exchangeable (all pairs <strong>of</strong>responses have the same correlation), <strong>and</strong> unstructured. The unstructured correlation matrix requiresthe most new parameters to be estimated, <strong>and</strong> will not be efficient if there are many responses.GEE differs from ML estimation in that no multivariate distribution has to be specified for themultivariate response. Thus, there is no likelihood with GEE. However, this also means there is nolikelihood-based inference. Instead, Wald statistics <strong>and</strong> their asymptotic distributions are used.Agresti uses GEE to fit a model to the Longitudinal Mental Depression Example from Table 11.2 (SectionC, this chapter). First, we read in the data, <strong>and</strong> change the binary variables to factors.# S-<strong>PLUS</strong> or Rtable.11.2


217The version <strong>of</strong> yags for S-<strong>PLUS</strong> 6.1 does not have a data argument. Thus, it is necessary to attach thedata set.library(yags)attach(table.11.2b) # S-<strong>PLUS</strong> onlyoptions(contrasts=c("contr.treatment", "contr.poly"))fit.yags


218gee S-function, version 5.2 modified 99/06/03 (1998)Model:Link:LogitVariance to Mean Relation: BinomialCorrelation Structure: ExchangeableCoefficients:Estimate Naive S.E. Naive z Robust S.E. Robust z(Intercept) -0.02809866 0.1625499 -0.1728617 0.1741791 -0.1613205diagnose -1.31391033 0.1448627 -9.0700418 0.1459630 -9.0016667treat -0.05926689 0.2205340 -0.2687427 0.2285569 -0.2593091time 0.48246420 0.1141154 4.2278625 0.1199383 4.0226037treat:time 1.01719312 0.1877051 5.4191018 0.1877014 5.4192084Estimated Scale Parameter: 0.985392Number <strong>of</strong> Iterations: 2Working Correlation[,1] [,2] [,3][1,] 1.000000000 -0.003432732 -0.003432732[2,] -0.003432732 1.000000000 -0.003432732[3,] -0.003432732 -0.003432732 1.000000000R also has a package called geepack, with largely identical arguments. However, it allows different linkfunctions for the mean, scale, <strong>and</strong> correlation. That is, the scale <strong>and</strong> correlation can depend oncovariates, being related to them via link functions.library(geepack)fit.geese


219The output only gives the robust s.e.s (called san.se, here).F. Marginal Modeling: GEE Approach. Modeling a Repeated Multinomial OrdinalResponseFor repeated ordinal responses, GEE can also be used to estimate parameters from the logit model. Forexample, for the the proportional odds cumulative logit model, we get parameter estimates for thecutpoints, as well as the coefficients for the covariates. We can also get parameter estimates for theassociation parameters in the covariance matrix, relating the responses to one another.The estimating equations are similar to those for univariate responses. The general form is givenin section 11.4.4 <strong>of</strong> Agresti for multinomial responses. For ordinal responses in particular, the responsevariable yichanges. Suppose we have an ordinal response with I = 4 categories <strong>and</strong> two responses perindividual. Then, each yiis length 2 x (4 – 1) = 6, with elements ( yi1(1), yi 1(2),..., yi2(3)). The elementyit(1) is equal to 1 if the tth response for individual i is greater than 1. The element yit(2) equals 1 if thethe tth response for individual i is greater than 2, etc. Thus, an individual with responses 3 <strong>and</strong> 4 at times1 <strong>and</strong> 2 has yi= (1,1,0,1,1,1) .For the proportional odds model, the vector µigives the probabilities associated with yi. Thus,for the example, µi= ( Py (i1 > 1),..., Py (i2> 3)) , where Py ( ) ( 1 exp( T )) 1it> j= + αj +xiβ −. Also, thecovariance structure <strong>of</strong> the elements <strong>of</strong> y iis put into Vi. Ordinary GEE (GEE1) uses the estimatingequations in section 11.4.4 <strong>of</strong> Agresti to estimate the regression coefficients <strong>and</strong> cut-points, usingsomething like Fisher scoring (with intial estimates for the covariance parameters). Then to estimate thecovariance parameters, a method <strong>of</strong> moments update is done, using the Fisher scoring estimates for theregression coefficients. These two steps are repeated until “convergence”. The “s<strong>and</strong>wich” estimator <strong>of</strong>the covariance matrix for the regression coefficients is given in section 11.4.4 <strong>of</strong> Agresti.A different estimation procedure, called GEE2, builds a set <strong>of</strong> estimating equations for thecovariance parameters too, updating both sets <strong>of</strong> parameters simultaneously. Differences between thetwo estimation methods are found in the Heagerty <strong>and</strong> Zeger (1996) reference in Agresti. Certain R <strong>and</strong>S-<strong>PLUS</strong> functions (namely, ordgee in package geepack, <strong>and</strong> the function ord.EE for S-<strong>PLUS</strong> Unix) claimto use “the” method <strong>of</strong> Heagerty <strong>and</strong> Zeger, which is apparently GEE1, although Heagerty <strong>and</strong> Zegercompare both GEE1 <strong>and</strong> GEE2. According to computations from these functions <strong>and</strong> from SAS’sGENMOD, they give different estimates than SAS does. I illustrate this for the insomnia data from table11.4 in Agresti, that was analyzed in Section D <strong>of</strong> this chapter.The function ordgee requires the data to be sorted by individual <strong>and</strong> response within individual. So, afterreading in the data, we modify table.11.4 as follows.table.11.4ord


220fit.ordgee


221Scale Parameter:1.013369Number <strong>of</strong> iterations : 6Number <strong>of</strong> observations : 478Number <strong>of</strong> clusters : 239These estimates match those from SAS. Also, notice that our scale parameter estimate is close to one,which is the theoretical value for a multinomial model.G. Markov Chains: Transitional ModelingDiscrete-time Markov chains are discrete-time stochastic processes with a discrete state space. Thatmeans that the r<strong>and</strong>om variable (potentially) changes states at discrete time points (say, every hour), <strong>and</strong>the states come from a set <strong>of</strong> discrete (many times, also finite) possible states. So, if the state <strong>of</strong> ther<strong>and</strong>om variable at time t is represented by y t, then the stochastic process is represented by{ y0, y1, y2,...}.This section deals with first-order Markov chains. Thus, for all t, the conditional distribution <strong>of</strong>yt + 1given { y0, y1, y2,..., y t} is identical to the distribution <strong>of</strong> yt + 1given only yt. In words, this means thatin order to predict the state at the next time point, we only need to consider the current state. Any statesprior to the current state add nothing to predictability <strong>of</strong> the next state. This is sometimes called theMarkov property.The conditional probability, PY (t= j| Yt−1= i) = πji |( t), is called a one-step transition probability,because it is the probability <strong>of</strong> transition from state i at time t – 1 to state j at time t one time-step away.The sum <strong>of</strong> these probabilities over j is equal to 1 because at time t, the process must take on one <strong>of</strong> thepossible states, even if it remains at its current state. Because <strong>of</strong> the Markov property, the jointdistribution for a first-order Markov chain depends only on the one-step transition probabilities <strong>and</strong> on themarginal distribution for the initial state <strong>of</strong> the process.Because association is only present between pairs <strong>of</strong> adjacent, consecutive states, a first-orderMarkov chain can be fit to a sample <strong>of</strong> realizations from the chain by fitting the loglinear model( YY0 1, YY1 2,..., YT− 1YT)for T realizations. This model says that, for example, at any combination <strong>of</strong> states atthe time points 2,…, T, the odds ratios describing the association between Y0<strong>and</strong> Y1are the same.To illustrate how these models are fit, we fit several loglinear models to the Respiratory Illness data(Table 11.7 in Agresti). This was a longitudinal study <strong>of</strong> the effects <strong>of</strong> air pollution on respiratory illness inchildren, with yearly assessments done on children from age 9 to age 12. At each measurement, a childwas classified according to the presence or absence <strong>of</strong> wheeze. Thus, the data represent a four-waycontingency table, with two categories per dimension (presence, absence).First, we read in the data.table.11.7


222library(MASS)(fit.loglm1 X^2)Likelihood Ratio 122.9025 8 0Pearson 129.5971 8 0The LR statistic is very high at 122.9, on 8 degrees <strong>of</strong> freedom.A second-order MC model assumes that the prediction at 12 years is independent <strong>of</strong> the response at 9years, given both the responses at 10 years <strong>and</strong> at 11 years. This model is fit by(fit.loglm2 X^2)Likelihood Ratio 23.86324 4 8.507772e-05Pearson26.67938 4 2.307740e-05Again, the LR statistic is high at 23.9, on 4 degrees <strong>of</strong> freedom.The loglinear model with all pairwise associations gives the following.(fit.loglm3 X^2)Likelihood Ratio 1.458592 5 0.9177982Pearson 1.446029 5 0.9192127To get the parameter estimates, we call fit.loglm3$param. To get the conditional log odds ratios <strong>of</strong>Table 11.8 in Agresti, we do the same manipulating that we did in Chapter 8. For example, at eachcombination <strong>of</strong> 9 <strong>and</strong> 10-year responses, the odds <strong>of</strong> wheezing at 12 years old areexp(fit.loglm3$param$Y11.Y12[1,1] + fit.loglm3$param$Y11.Y12[2,2] -(fit.loglm3$param$Y11.Y12[1,2] + fit.loglm3$param$Y11.Y12[2,1]))[1] 6.358584times higher than the odds <strong>of</strong> wheezing at 11 years old. (The anti-log <strong>of</strong> this odds ratio gives the log oddsratio in Table 11.8).The loglinear model with all pairwise associations, but constrained to have the one-year pair associationsidentical, <strong>and</strong> the greater-than-one-year associations identical fits well too. To fit this model, we createtwo new variables that represent, respectively, the one-year association <strong>and</strong> the greater-than-one-yearassociation. These variables just count the number <strong>of</strong> such associations.oneyear


223We will treat the two new variables as continuous instead <strong>of</strong> categorical. So, we can’t use loglm to dothe fitting (which is IPF). We will use glm instead. With glm, the factors in the model are not consideredcategorical.options(contrasts=c("contr.treatment", "contr.poly")) # S-<strong>PLUS</strong> only(fit.glm4


224table.11.9


Recall that a LRT on any single coefficient is obtained by fitting another model without that coefficient,<strong>and</strong> subtracting residual deviances.225


226Chapter 12 – R<strong>and</strong>om Effects: Generalized Linear MixedModels for Categorical ResponsesA. Summary <strong>of</strong> Chapter 12, AgrestiWhen observations occur in clusters, we can model the dependence within a cluster using(latent) r<strong>and</strong>om effects. With this model, there are as many r<strong>and</strong>om effects as observations. They takethe same value for each observation in a cluster, but take different values across clusters. A cluster canbe an observation itself, in which case the r<strong>and</strong>om effects will differ across observations. In a r<strong>and</strong>omeffects model, the r<strong>and</strong>om effects apply to a sample <strong>of</strong> clusters instead <strong>of</strong> to a fixed set <strong>of</strong> clusters. Thatis, the cluster effects are considered r<strong>and</strong>omly sampled from a distribution <strong>of</strong> such effects, <strong>and</strong> the actualrealization <strong>of</strong> clusters will tend to differ across samples.R<strong>and</strong>om effects models are similar to the marginal models <strong>of</strong> Chapter 11 in that both modelsincorporate dependence among observations. However, with the marginal models the dependence isaveraged over, <strong>and</strong> the marginal probability distribution is <strong>of</strong> more interest. With r<strong>and</strong>om effects models,we model the probability distribution within each cluster. Also, the (positive) correlation betweenobservations within a cluster is assumed to have arose because the r<strong>and</strong>om effects vary across clusters.Generalized linear mixed models (GLIMMs) extend generalized linear models (GLIMs) toincorporate r<strong>and</strong>om effects. Conditional on the r<strong>and</strong>om effect, a GLIMM resembles a GLIM. Forobservation t in cluster i, the linear predictor isT Tg ( µ ) = x β + z u(12.1)it it it ifor link function g <strong>and</strong> conditional mean µ it. The r<strong>and</strong>om effect vector, u i , is assumed to have amultivariate normal distribution N(, 0 Σ), where Σ describes the covariances among the elements <strong>of</strong> u i .Conditional on u i , the observations {y it} are independent over i <strong>and</strong> t. With the r<strong>and</strong>om effect appearingon the scale <strong>of</strong> the explanatory variables, subject-specific r<strong>and</strong>om effects can represent heterogeneitycaused by omitting certain explanatory variables. R<strong>and</strong>om effects can also represent r<strong>and</strong>ommeasurement error in the explanatory variables.Because the u i are r<strong>and</strong>om effects, their inclusion only increases the number <strong>of</strong> parameters inthe model by the number <strong>of</strong> unique elements in their covariance matrix. On the other h<strong>and</strong>, if they werefixed effects, then the number <strong>of</strong> parameters would increase with the number <strong>of</strong> clusters. In that case,one can condition on the sufficient statistics for u i , making the resulting conditional likelihood independent<strong>of</strong> them. Maximizing this likelihood for β gives consistent estimates. Advantages <strong>of</strong> using a conditionallikelihood include not having to specify a distribution for the r<strong>and</strong>om effects. Also, if the clusters are notr<strong>and</strong>omly sampled (such as for a retrospective study), then conditional likelihood estimation isappropriate. However, conditioning removes the source <strong>of</strong> variability needed to estimate between-clustereffects (such as explanatory variable effects that are not repeated within clusters). Recall that conditionalMLE only allows estimation <strong>of</strong> within-cluster effects (such a treatment effect within pairs in a matchedpairsdesign). Also, a conditional model will not allow prediction <strong>of</strong> the individual r<strong>and</strong>om effects. Finally,sufficient statistics for the u i have to exist to use the approach.The fixed effects parameters β , both within-cluster <strong>and</strong> between-cluster, in a GLIMM haveconditional interpretations, given the r<strong>and</strong>om effect. For example, in a cross-over drug trial, where eachsubject takes both the control drug <strong>and</strong> experimental drug, the drug effect (a within-subject effect),conditional on the subject’s r<strong>and</strong>om effect is the coefficient that multplies the drug dummy variable. Also,in a multi-center drug trial, with a r<strong>and</strong>om center effect, where each center has patients who take thecontrol drug as well as patients who take the experimental drug, the drug effect (a between-center effect),again conditional on a given center’s r<strong>and</strong>om effect is the coefficient that multiples the drug dummyvariable. This is only true when the control <strong>and</strong> experimental patient come from the same center. Thus,comparisons are done at a fixed value <strong>of</strong> the r<strong>and</strong>om effect. In contrast, effects in marginal models are


227averaged over all clusters. When the link function is nonlinear, these effects are <strong>of</strong>ten smaller inmagnitude than their analogous cluster-specific effects. The approximate relationship between the twoparameters in a logistic-normal model has been shown to be that the marginal model coefficient is about2 1/2[1 + 0.6 σ ]− <strong>of</strong> the conditional coefficient, making it smaller in magnitude. Agresti’s Figure 12.1illustrates this attenuation.With a multinomial outcome (ordinal or nominal), a r<strong>and</strong>om effects model is represented via theobvious extension. That is, one adds a r<strong>and</strong>om effect to the linear predictor in the same way as for abinomial outcome.B. Logit GLIMM for Binary Matched PairsWhen g in (12.1) is the logit link, <strong>and</strong> µitis the conditional expectation <strong>of</strong> a Bernoulli r<strong>and</strong>om variablegiven u i, then we have the logit GLIMM. Clustering occurs if the data are paired, so that each (matched)pair is a cluster. Suppose cluster i <strong>of</strong> n contains the matched pair ( yi1, yi2), where yit= 1 or 0, <strong>and</strong> givenu i,[ ]logit PY ( = 1| u)= α + β x+ u(12.2)it i t i2where x1= 0 <strong>and</strong> x 2= 1, <strong>and</strong> ui~ N(0, σ ) . Then, we have a logit GLIMM for binary matched pairs.The two elements <strong>of</strong> the pair are independent given u i. So, their only dependence occurs through thisr<strong>and</strong>om effect. If we compute the log odds ratio <strong>of</strong> responding 1 in each <strong>of</strong> the two pairs, we get β . Thatis,[ ] [ ]logit PY ( = 1| u) − logit PY ( = 1| u)= βi2 i i1iwithin each cluster. Thus, β is called the cluster-specific log odds ratio. The model in (12.2) is called ar<strong>and</strong>om intercept model because the intercept <strong>of</strong> the linear predictor is a r<strong>and</strong>om effect instead <strong>of</strong> a fixedconstant.The marginal distribution <strong>of</strong>Ytnyi=1 it{ exp( α βt ) [ 1 exp( α βt )]}= ∑ is binomial with index n <strong>and</strong> probability parameterE + x + U + + x + U , with expectation with respect to U a2N(0, σ ) r<strong>and</strong>omvariable. The model implies a non-negative correlation between Y 1<strong>and</strong> Y2, with greater association2resulting from greater heterogeneity (larger σ ). Greater heterogeneity means that all the u iare spreadapart, where there are both large positive <strong>and</strong> large negative values. Large positive values <strong>of</strong> u imake itmore likely that ( yi1, yi2)are both 1. Large negative values <strong>of</strong> u imake it more likely that ( yi1, yi2)areboth 0. This correspondence between the two elements <strong>of</strong> the match pair traverses to a correspondencebetween the two sums Y1<strong>and</strong> Y2, leading to higher positive association.The 2 x 2 population-averaged table represents the frequency distribution <strong>of</strong> ( yi1, yi2). When the samplelog odds ratio is non-negative, then the MLE <strong>of</strong> β can be shown to be equal to log( n21 / n12), which is theconditional MLE, using conditional maximum likelihood estimation as discussed in Agresti’s chapter 10.2A sample log odds ratio that is negative means that the estimate <strong>of</strong> the variance, σ , is zero, implyingindependence. Then, the MLE <strong>of</strong> β is identical to that from the marginal model for matched pairs.


228Agresti uses the Prime Minister Ratings data to illustrate a r<strong>and</strong>om intercept model. The matched pairsare the clusters. The data are available in table form from Agresti’s website. I copied the data <strong>and</strong>placed it into a text file called primeminister.txt. Then, I read it into R/S-<strong>PLUS</strong>.(table.12.1


229(Intr)occasion -0.376St<strong>and</strong>ardized Within-Group Residuals:Min Q1 Med Q3 Max-2.1613715 -0.4746219 0.3097340 0.4117406 2.0973320Number <strong>of</strong> Observations: 3200Number <strong>of</strong> Groups: 1600The MLE for the (cluster-specific) log odds ratio <strong>of</strong> approval (beta) is –0.569, with SE = 0.073, so that theestimated odds <strong>of</strong> approval at the second survey are exp(–0.569) = 0.57 times that at the first survey.The estimate <strong>of</strong> the st<strong>and</strong>ard deviation <strong>of</strong> the r<strong>and</strong>om effects is 3.26.Other R functions that fit GLIMMs use other types <strong>of</strong> approximations (namely, maximization <strong>of</strong> thenumerically integrated likelihood -- integrated over the normal distrubtion <strong>of</strong> the r<strong>and</strong>om effects). Therepeated library has a function called glmm.library(repeated)fit.glmm|z|)(Intercept) 1.3190 0.1111 11.87 < 2e-16 ***occasion -0.5540 0.1332 -4.16 3.18e-05 ***sd 4.6830 0.1759 26.62 < 2e-16 ***---Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1(Dispersion parameter for binomial family taken to be 1)Null deviance: 4373.2 on 3199 degrees <strong>of</strong> freedomResidual deviance: 3501.1 on 3197 degrees <strong>of</strong> freedomAIC: 3507.1Number <strong>of</strong> Fisher Scoring iterations: 20In the call, the argument nest specifies the cluster variable.In the output, the MLE for the (cluster-specific) log odds ratio <strong>of</strong> approval (beta) is –0.554, with SE =0.133, so that the estimated odds <strong>of</strong> approval at the second survey are exp(–0.554) = 0.57 times that atthe first survey. The estimate <strong>of</strong> the st<strong>and</strong>ard deviation <strong>of</strong> the r<strong>and</strong>om effects is 4.68. These numbersare closer to what SAS NLMIXED gets for Agresti. (In fact, it is the same fitting algorithm as NLMIXED).The R package glmmML also fits GLIMMs by maximizing the numerically integrated likelihoodlibrary(glmmML)(fit.glmmML|z|)(Intercept) 1.3404 0.2495 5.372 7.78e-08occasion -0.5563 0.1353 -4.113 3.91e-05St<strong>and</strong>ard deviation in mixing distribution: 5.138Std. Error: 0.4339Residual deviance: 3502 on 3197 degrees <strong>of</strong> freedom AIC: 3508


230In the call, the argument cluster specifies the cluster variable. Apparently, the variable is not taken fromthe data frame, <strong>and</strong> must be referenced (e.g., by using the $).In the output, the MLE for the (cluster-specific) log odds ratio <strong>of</strong> approval (beta) is –0.556, with SE =0.135, so that the estimated odds <strong>of</strong> approval at the second survey are exp(–0.556) = 0.57 times that atthe first survey. The estimate <strong>of</strong> the st<strong>and</strong>ard deviation <strong>of</strong> the r<strong>and</strong>om effects is 5.14. These numbersare closest to what SAS NLMIXED gives. Using the approximate relationship given in Section A, thisimplies a marginal effect <strong>of</strong> about –0.135.Another R function called glmm in package GLMMGibbs fits GLIMMs via MCMC sampling.The extension <strong>of</strong> (12.2) to more than 2 observations in each cluster is sometimes called theRasch Model. In this case the model would look like[ ]logit PY ( = 1| u)= β + u(12.3)it i t ifor t = 1,…,T. Note that the log odds ratio <strong>of</strong> responding 1 at t = 1 versus t = 2 , for a given subject i is thedifference, β1 − β2. This is not the same as the population-averaged log odds ratio, which would belogit PY ( = 1) − logit PY ( = 1) , <strong>and</strong> is the subject <strong>of</strong> marginal models.[ ] [ ]1 2C. Examples <strong>of</strong> R<strong>and</strong>om Effects Models for Binary Data1. Small Area Estimation <strong>of</strong> Binomial ProportionsSmall area estimation models are r<strong>and</strong>om effects models. These models treat each area as a clusterwith its own r<strong>and</strong>om effect coming from a common distribution <strong>of</strong> the r<strong>and</strong>om effects. The modelestimates each area’s mean by borrowing information from other areas. The borrowing takes placethrough the parameters <strong>of</strong> the common distribution. The resulting estimates <strong>of</strong> the area means areshrunk toward the population mean. The amount <strong>of</strong> shrinkage is controlled by the size <strong>of</strong> the estimatedvariance <strong>of</strong> the r<strong>and</strong>om effects distribution, <strong>and</strong> the size <strong>of</strong> the area. A lower variance <strong>and</strong> lower sizeresult in less shrinkage.Let Yitbe tth binary response for the ith area, t = 1,…,T i. Conditional on the r<strong>and</strong>om effect, u i~2N(0, σ ) ,logit PY ( = 1| u)= α + u(12.4)[ ]it i iso that the estimate <strong>of</strong> πi= PY (it= 1| ui)is a function <strong>of</strong> the predicted r<strong>and</strong>om effect, u ˆi, which isestimated by its marginal posterior mean.Table 12.2 in Agresti gives data on simulated voting proportions in the 1996 presidential election for each<strong>of</strong> the 50 states. The states are the areas.After reading in the data (which are available on Agresti’s web site for the text), we fit a r<strong>and</strong>om interceptmodel using glmmPQL from the MASS library (available in both S-<strong>PLUS</strong> <strong>and</strong> R). The response is theobserved proportion, with “case” weights as the number <strong>of</strong> observations per state.table.12.2


231fit.glmmPQL


232We can add covariates to model (12.3), <strong>and</strong> then consider the multiple questions or items for anindividual as part <strong>of</strong> a single cluster. Agresti illustrates fitting this model using the abortion data in Table10.13. In that data set, each subject (cluster) answered three yes/no questions. Subjects were classifiedby sex. A r<strong>and</strong>om effects logistic model for the response assumes a positive correlation betweenresponses from the three questions by a single individual.We start with the previous data frame called table.10.13 (see chapter 10, above).table.10.13gender question1 question2 question3 count1 M Y Y Y 3422 F Y Y Y 4403 M N Y Y 64 F N Y Y 145 M Y N Y 116 F Y N Y 147 M N N Y 19etcWe reshape the data so that it is in “long” format instead <strong>of</strong> “wide” format. In R, one can use the functionreshape or stack for this; in S-<strong>PLUS</strong>, one can use the function menuStackColumns. reshape is definitelyeasier to use than menuStackColumns.menuStackColumns(target=table.10.13a,source=table.10.13,group.col.name="question",target.col.spec="response", source.col.spec=c("question1","question2","question3"),rep.source.col.spec=c("gender","count"),rep.target.col.spec=list(gender="gender",count="count"),show.p=F)#R: table.10.13a


233Linear mixed-effects model fit by maximum likelihoodData: table.10.13bAIC BIC logLik32091.19 32130.92 -16039.59R<strong>and</strong>om effects:Formula: ~ 1 | id(Intercept) ResidualStdDev: 4.308437 0.4507951Variance function:Structure: fixed weightsFormula: ~ invwtFixed effects: response ~ gender + questionValue Std.Error DF t-value p-value(Intercept) -0.5108888 0.1710876 3698 -2.98612 0.0028gender 0.0064619 0.2215622 1848 0.02917 0.9767questionquestion2 0.3067315 0.0722624 3698 4.24469


234∫∫∏PY ( = y , Y = y , Y = y ) = PY ( = y , Y = y , Y = y | u) f( u)dui1 i1 i2 i2 i3 i3 i1 i1 i2 i2 i3 i3i i i3= PY ( = y | u) f( u)dut=1it it i i iThe probabilities PY (it= yit | ui) are defined in equation (12.10) in Agresti (p. 504), <strong>and</strong> the density <strong>of</strong> u isnormal. So, we numerically integrate. The function in R/S-<strong>PLUS</strong> to integrate is called integrate, <strong>and</strong> isvery easy to use, theoretically. Another function that works correctly for this problem is in the R packagermutil, <strong>and</strong> is called int. For either, we first define the function to be numerically integrated. Here, Icompute the probability <strong>of</strong> a positive response for each <strong>of</strong> the three questions, for a female respondent.Then, to get the expected frequency for that cell <strong>of</strong> the contingency table, we multiply the estimatedprobability by 1037, the number <strong>of</strong> female observations.First, we get the estimates <strong>of</strong> the parameters from the fit. Then, we set a variable called female to 1, toindicate a female.beta.coef


235The data are already in the object table.11.2 (see above). Here, we fit a r<strong>and</strong>om intercept model usingthe R function glmmML in the glmmML package, the R function glmm in the repeated package, <strong>and</strong> thefunction glmmPQL in the MASS library.First, the glmmML R function, because it gives the “correct” MLEs.library(glmmML)(fit.glmmML|z|)(Intercept) -0.02795 0.1641 -0.1703 8.65e-01diagnose -1.31521 0.1546 -8.5046 0.00e+00treat -0.05970 0.2225 -0.2684 7.88e-01time 0.48284 0.1160 4.1638 3.13e-05treat:time 1.01842 0.1924 5.2930 1.20e-07St<strong>and</strong>ard deviation in mixing distribution: 0.06581Std. Error: 1.242Residual deviance: 1162 on 1014 degrees <strong>of</strong> freedom AIC: 1174As Agresti notes, the subject-specific parameter estimates are very close to the GEE <strong>and</strong> ML marginalestimates. The reason he gives is that the estimate <strong>of</strong> the st<strong>and</strong>ard deviation <strong>of</strong> the r<strong>and</strong>om effectsdistribution is close to zero (0.07), implying little heterogeneity among subjects. Thus, the derivedpairwise correlation among observations on the same subject is not high (see equation (12.6) in Agresti).Now, we fit the same model using glmm in the repeated R package.library(repeated)fit.glmm|z|)(Intercept) -0.02741 0.16514 -0.166 0.868150diagnose -1.33422 0.14783 -9.025 < 2e-16 ***treat -0.06115 0.22378 -0.273 0.784652time 0.48917 0.11560 4.231 2.32e-05 ***sd 0.26182 0.07263 3.605 0.000313 ***treat:time 1.03267 0.19017 5.430 5.62e-08 ***---Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1(Dispersion parameter for binomial family taken to be 1)Null deviance: 1411.9 on 1019 degrees <strong>of</strong> freedomResidual deviance: 1161.7 on 1014 degrees <strong>of</strong> freedomAIC: 1173.7


236The function glmmPQL in the MASS library does the same.library(MASS)fit.glmmPQL


237Agresti uses the clinical trial data from Chapter 6 (Table 6.9, repeated in Table 12.5), comparing a drug toa control, to illustrate fitting a logistic-normal model to multicenter clinical trial data. For a subject incenter i using treatment t (1 = drug, 2 = control), let y it= 1 denote success. A general model includes atreatment-by-center interaction:logit PY ( = 1| u, b) = α + ( β + b) / 2 + u[ ]i1i i i i[ ]logit PY ( = 1| u, b) = α − ( β + b) / 2 + ui2i i i i22where ui~ N(0, σa)<strong>and</strong> bi~ N(0, σb), all independent <strong>of</strong> one another. With this model, β + biis thecenter-specific log odds ratio for the ith center, <strong>and</strong> β is the expected center-specific log odds ratio.2Thus, we have r<strong>and</strong>om center effects <strong>and</strong> r<strong>and</strong>om treatment-by-center effects. When σb= 0, the logodds ratio between treatment <strong>and</strong> response is constant over centers.Recall that we read these data into S-<strong>PLUS</strong>/R as a data frame in Section E <strong>of</strong> Chapter 6. I make a fewchanges to this data frame so that we have the coding used by Agresti.table.6.9$TreatC


238Number <strong>of</strong> Observations: 273Number <strong>of</strong> Groups: 8The estimate <strong>of</strong> 0.721 (SE = 0.286) is a little <strong>of</strong>f from Agresti’s MLE <strong>of</strong> 0.739 (SE = 0.300). Note Agresti’sdiscussion <strong>of</strong> penalized quasi-likelihood in Section 12.6.4.Now, I fit the treatment-by-center interaction as well.fit.glmmPQL.ia


239(Intercept) TreatCa -0.8998236 0.7692297b 0.9721856 0.4584772c 0.1236859 0.6716927d -2.1502939 1.0664178e -1.4833405 0.9778767f -2.4923268 1.1419009g -1.5186057 0.9455523h 0.9443708 0.4365184Now, I get the predicted odds ratios.invlogit


240Tit⎡ exp( xitβ + ui) ⎤ ⎡ 1 ⎤p( yi | ui)= ∏ ⎢ T Tt 1+ exp(it+ ui )⎥ ⎢1+ exp(it+ ui)⎥⎣ x β ⎦ ⎣ x β ⎦2then treat the y i ‘s as multinomial draws, with probabilities given by p( yi) = ∫ p( yi| ui) p( ui| σ ) du ,u<strong>and</strong> index N. Maximizing this likelihood gives the MLE <strong>of</strong> N, <strong>and</strong> the other parameters. This isstraightforward to do in S-<strong>PLUS</strong> or R, using a function like optim. However, to match the estimateobtained by Agresti, I will also illustrate a conditional approach that Agresti used in Coull <strong>and</strong> Agresti,1999 (see references in Agresti).First, we should enter the data from Table 12.6. I do this in several steps so that I can the data indifferent forms throughout this section.Here, I enter the 0/1 patterns, separately from the counts.temp


241#S-<strong>PLUS</strong>: menuStackColumns(source=table.12.6a,target=table.12.6b,source.col.spec=names(table.12.6a)[1:6],group.col.name="Survey",target.col.spec="Capture",rep.source.col.spec="id",show.p=F)#S-<strong>PLUS</strong>: names(table.12.6b)


242c(result.values[1:6],exp(result.values[7:8]))Survey1 Survey2 Survey3 Survey4 Survey5 Survey6-1.4378836 -0.9094129 -1.7539897 -0.6751367 -1.2273151 -1.0325512 0.9075391 20.9831241The last value (plus 68) is N, <strong>and</strong> the penultimate value is σ . These are close to those obtained byAgresti, 24+68 <strong>and</strong> 0.97, respectively.Next, I compute the pr<strong>of</strong>ile-likelihood confidence interval, using the function plkhci, which is onlyavailable in the R package, Bhat. To obtain the interval in S-<strong>PLUS</strong>, one could use a brute force methodby inputting different values <strong>of</strong> the missing cell count, <strong>and</strong> obtaining the log likelihood.Recall that to use the function plkhci, we create a list with the labels, starting values, <strong>and</strong> lower <strong>and</strong>upper bounds. Then, we input this list as an argument, followed by the negative log-likelihood function,<strong>and</strong> the label <strong>of</strong> the parameter being pr<strong>of</strong>iled. A confidence interval is returned. Here, it is about (74,141), similar to that obtained by Agresti (75, 154).library(Bhat)x


243}Now, we minimize the negative log likelihood <strong>and</strong> obtain the MLEs <strong>of</strong> the parameters. The MLE <strong>of</strong> N isobtained using the expression given above.junk


244table.11.4$treat


245}-sum(log(probs))Now, we use the optim function. Here, I start the optimization at the estimates from polr, but to have itconverge in any reasonable amount <strong>of</strong> time, you have to start very close to the maximizer, which appearsto be c(-3.4858986, -1.4830128, 0.5606742,1.602,.058,1.081,log(1.9))optim(par=c(fit$zeta,fit$coefficients[c("time","treat","treat:time")],.5), fn=func,control=list(trace=2,REPORT=1), method="BFGS", matrix.i= as.matrix(table.11.4a))$par[1] -3.48824875 -1.48407698 0.56211694 1.60158887 0.05754634 1.08140528[7] 0.64423760$value[1] 592.9728$countsfunction gradient20 4$convergence[1] 0$messageNULLFitting adjacent-categories logit model follows the same idea, by defining the likelihood as a product <strong>of</strong>probabilities within each category.E. Multivariate R<strong>and</strong>om Effects Models for Binary DataOne form <strong>of</strong> multivariate r<strong>and</strong>om effects is the presence <strong>of</strong> both a r<strong>and</strong>om intercept <strong>and</strong> a r<strong>and</strong>om slopefor a regression that “varies” across clusters, like individuals. Another type is nested r<strong>and</strong>om effects,where for example, one can have r<strong>and</strong>om effects for schools <strong>and</strong> r<strong>and</strong>om effects for students withinschools. Finally, one can have a model with more than one response variable, where each responsevariable has an associated r<strong>and</strong>om subject effect, say.1. Bivariate Binary ResponseAgresti gives an example <strong>of</strong> a bivariate binary response where schoolboys were interviewed twice,several months apart, <strong>and</strong> asked about their self-perceived membership in the leading crowd (yes or no)<strong>and</strong> asked about whether they sometimes felt they needed to go against their principles in order to fit in tothat crowd (yes or no). Thus, the two binary responses are called Membership <strong>and</strong> Attitude. There aresubject r<strong>and</strong>om effects for each variable.Following Agresti, for subject i, let yitvbe the response at interview time t on variable v. Then, we usethe logit modellogit [ PY (itv= 1| uiv )]= βtv + uivwhere (u iAtt, u iMem) ~ N(, 0 Σ). Thus, the correlation between Membership <strong>and</strong> Attitude is considered to bepossibly nonzero.The data in Table 12.8 in Agresti are available on his website. Here I read in the data set after copying it<strong>and</strong> saving it as a text file called “crowd.txt”.table.12.8


246There is no library function for either R or S-<strong>PLUS</strong> (yet) that automatically fits multivariate logit modelswith r<strong>and</strong>om effects via maximum likelihood or any other method. There are several ways we might dothe fitting. One straightforward method is to maximize the integrated likelihood, where integration is overthe distribution <strong>of</strong> the bivariate r<strong>and</strong>om effects (<strong>of</strong> course, we use numerical integration). MultivariateGaussian quadrature can be accomplished either via the adapt function in the adapt library or using asimple 2D numerical integration function, called GL.integrate.2D, from Diego Kuonen, <strong>and</strong> modifiedslightly below.GL.integrate.2D


247att2


248Another option is to take advantage <strong>of</strong> an existing function for generalized linear mixed models, such asglmmPQL from library MASS, <strong>and</strong> do some tricks to emulate a multivariate response with multivariater<strong>and</strong>om effects. A couple <strong>of</strong> emails from the R-news newsgroup deal with fitting these models, <strong>and</strong> onereferences a write-up in the multilevel website (http://multilevel.ioe.ac.uk/s<strong>of</strong>trev/reviewmixed.pdf) whichexplains how to fit a bivariate normal response (not repeated measures) with r<strong>and</strong>om effects, using lme. Iapplied this method to glmmPQL (which uses lme) for a bivariate binary response.Here, I set up the original data set so that have one column for “response”, <strong>and</strong> add two additionalindicator columns that indicate from which variable the response comes. I also add a time indicatorvariable that indicates whether the response is measured at time 1 or time 2.data1


249Structure: General positive-definiteStdDev Corrvar1 2.034878 var1var2 1.578995 0.265Residual 0.771258Variance function:Structure: fixed weightsFormula: ~ invwtFixed effects: response ~ -1 + var1 + var2 + var1:time + var2:timeValue Std.Error DF t-value p-valuevar1 -0.4992500 0.05009042 10191 -9.966977


250malformation given survival. Also, let x dbe the dosage for group d. In the model, we use litter-specificr<strong>and</strong>om effects: ui ( d )= ( ui ( d )1, ui ( d )2) sampled from a N(, 0 Σ d). The bivariate r<strong>and</strong>om effect allowsdiffering amounts <strong>of</strong> dispersion for the two probabilities. We also fit separate fixed dose effects for thetwo logits, logit( ϖi( d) j)= ui( d)j+ αj+ βjxd, where j is the index for the logit.Here I read in the data from Table 12.9, which I have placed in a text file called toxic.txt. This file hascolumn headings: litter, dose, dead, malformed, <strong>and</strong> normal. The last 3 columns come directly from thenumbers in Table 12.9 (I will have the data posted on the main website for this manual).table.12.9


251Now, I estimate some starting values, using a continuation-ratio model without r<strong>and</strong>om effects. We c<strong>and</strong>o this with vglm from library VGAM in R (discussed in Section 7F <strong>of</strong> ths manual). Note that I useaggregate to eliminate the litter column.library(VGAM)table.12.9a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!