R version 3.1.1 (2014-07-10) -- "Sock it to Me" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-unknown-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > foo <- read.table("squirrel.txt", header = TRUE) > foo count active passive 1 1 R S 2 5 R T 3 8 R U 4 9 R V 5 0 R W 6 29 S R 7 14 S T 8 46 S U 9 4 S V 10 0 S W 11 0 T R 12 0 T S 13 0 T U 14 0 T V 15 0 T W 16 2 U R 17 3 U S 18 1 U T 19 38 U V 20 2 U W 21 0 V R 22 0 V S 23 0 V T 24 0 V U 25 1 V W 26 9 W R 27 25 W S 28 4 W T 29 6 W U 30 13 W V > bar <- xtabs(count ~ active + passive, data = foo) > bar passive active R S T U V W R 0 1 5 8 9 0 S 29 0 14 46 4 0 T 0 0 0 0 0 0 U 2 3 1 0 38 2 V 0 0 0 0 0 1 W 9 25 4 6 13 0 > > # Distribution of genital display in a colony of six squirrel monkeys, > # as reported by Ploog (1967), taken from Fienberg (1980, The Analysis > # of Cross-Classified Categorical Data, MIT Press), Table 8-2. > > # diagonal elements are "structural zeros" or "fixed zeros" > # (monkeys do not display to themselves -- the don't have mirrors) > # xtabs apparently does not have this concept > > # we can fit this with loglin by specifying starting values that have > # zero on the diagonal > sss <- (bar + 1)^0 > diag(sss) <- 0 > sss passive active R S T U V W R 0 1 1 1 1 1 S 1 0 1 1 1 1 T 1 1 0 1 1 1 U 1 1 1 0 1 1 V 1 1 1 1 0 1 W 1 1 1 1 1 0 > lout <- loglin(bar, list(1, 2), start = sss, fit = TRUE) 4 iterations: deviation 0.05112509 > lout $lrt [1] 135.1691 $pearson [1] NaN $df [1] 25 $margin $margin[[1]] [1] "active" $margin[[2]] [1] "passive" $fit passive active R S T U V W R 0.00000000 5.25991683 2.48071962 8.21546265 6.64803183 0.39576634 S 19.18802539 0.00000000 10.32281246 34.18632223 27.66390257 1.64686959 T 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 U 10.93448629 12.47289692 5.88255691 0.00000000 15.76454884 0.93848495 V 0.21996467 0.25091226 0.11833704 0.39189979 0.00000000 0.01887913 W 9.65752365 11.01627399 5.19557398 17.20631533 13.92351676 0.00000000 > # loglin gets the fit right, I think, but does not count the structural > # zeros correctly > # OTOH, glm has no problem (because the structural zeros just are not > # there in the data to confuse it. > gout <- glm(count ~ active + passive, family = poisson, data = foo) > sout <- summary(gout) > sout Call: glm(formula = count ~ active + passive, family = poisson, data = foo) Deviance Residuals: Min 1Q Median 3Q Max -5.6438 -0.8886 -0.1449 0.9290 4.7316 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 1.5285 0.2745 5.568 2.58e-08 *** activeS 1.4257 0.2395 5.952 2.65e-09 *** activeT -18.6578 1362.2115 -0.014 0.98907 activeU 0.8636 0.2631 3.282 0.00103 ** activeV -3.0428 1.0228 -2.975 0.00293 ** activeW 0.7393 0.2490 2.969 0.00299 ** passiveS 0.1316 0.2548 0.516 0.60557 passiveT -0.6199 0.2592 -2.391 0.01679 * passiveU 0.5776 0.2110 2.738 0.00618 ** passiveV 0.3658 0.2030 1.803 0.07146 . passiveW -2.4554 0.6002 -4.091 4.29e-05 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 433.14 on 29 degrees of freedom Residual deviance: 135.17 on 19 degrees of freedom AIC: 227.43 Number of Fisher Scoring iterations: 15 > names(sout) [1] "call" "terms" "family" "deviance" [5] "aic" "contrasts" "df.residual" "null.deviance" [9] "df.null" "iter" "deviance.resid" "coefficients" [13] "aliased" "dispersion" "df" "cov.unscaled" [17] "cov.scaled" > sout$deviance [1] 135.1691 > sout$df.residual [1] 19 > pchisq(sout$deviance, sout$df.residual, lower.tail = FALSE) [1] 1.52349e-19 > > # This does not get the right degrees of freedom either because of > # "solutions at infinity". More on this later. > > > > proc.time() user system elapsed 0.205 0.024 0.348