# Edps 589 # Fall 2018 # C.J.Andersion # # Answer for Homework 5, problem 1 (3.18 from Agresti) # library(MASS) setwd("C:\\Users\\cja\\Dropbox\\edps 589\\homework") fights <- read.table("a2007_318_data.txt",header=TRUE) # Method 1 summary(method1 <- glm(arrests ~ -1 + attend , data=fights, family=poisson(link="identity")) ) # take into account measured in 1,000s method1$coefficients[1]/1000 # Method 2 summary(method2 <- glm(arrests ~ offset(log(attend)), data=fights, family=poisson) ) alpha <- method2$coefficients[1] # take into account measured in 1,000s expalpha <- exp(alpha)/1000 # Graph plot(fights$attend,fights$arrests, type='p', xlim=c(75,500), xlab="Number Attending Game (in thousands)", ylab="Number Arrested", main="Soccer: Number of Arrests by Number Attending") j <- order(fights$attend) lines(fights$attend[j],method2$fitted[j],type="l",col="red") text(fights$attend,fights$arrests,fights$team) # Look at adjusted std residuals fights$res <- rstandard(method2,type="pearson") fights # Negative binomial summary( nb <- glm.nb(arrests ~ offset(log(attend)),data=fights) ) fights$res.nb <- rstandard(nb,type="pearson") fights