Skip to Content
The R Book
book

The R Book

by Michael J. Crawley
June 2007
Beginner to intermediate
950 pages
27h 8m
English
Wiley
Content preview from The R Book

Using Log-linear Models for Simple Contingency Tables

It is worth repeating these simple examples with a log-linear model so that when we analyse more complex cases you have a feel for what the GLM is doing. Recall that the deviance for a log-linear model of count data (p. 516) is

images

where O is a vector of observed counts and E is a vector of expected counts. Our first example had 29 males and 18 females and we wanted to know if the sex ratio was significantly male-biased:

observed<-c(29,18)
summary(glm(observed~1,poisson))

    Null deviance: 2.5985 on 1 degrees of freedom
Residual deviance: 2.5985 on 1 degrees of freedom
AIC: 14.547
Number of Fisher Scoring iterations: 4

Only the bottom part of the summary table is informative in this case. The residual deviance is compared to the critical value of chi-squared in tables with 1 d.f.:

1-pchisq(2.5985,1)

[1] 0.1069649

We accept the null hypothesis that the sex ratio is 50:50 (p = 0.106 96).

In the case of Mendel's peas we had a four-level categorical variable (i.e. four phenotypes) and the null hypothesis was a 9:3:3:1 distribution of traits:

observed<-c(315,101,108,32)

We need vectors of length 4 for the two seed traits, shape and colour:

shape<-factor(c("round","round","wrinkled","wrinkled"))
colour<-factor(c("yellow","green","yellow","green"))

Now we fit a saturated model (model1) and a model without the interaction term (model2) ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book, 3rd Edition

The R Book, 3rd Edition

Elinor Jones, Simon Harden, Michael J. Crawley
The Book of R

The Book of R

Tilman M. Davies

Publisher Resources

ISBN: 9780470510247Purchase book