#Creating the dichotomous variable for high income. depress$inc.high <- 1*(income > 19) #Table 17.1, p. 412. attach(depress) depress$sex.fac <- factor(sex, labels=c("male", "female")) depress$inc.high.fac <- factor(inc.high, labels=c("low", "high")) table(depress$sex.fac, depress$inc.high.fac) #Creating the factor variable for treat. depress$treat.fac <- factor(treat, labels=c("No", "Yes")) depress$cases.fac <- factor(cases, labels=c("low", "high")) #Table 17.2, p. 413, table(depress$sex.fac, depress$inc.high.fac, depress$treat.fac) #Creating the factor variable for cases. depress$cases.fac <- factor(cases, labels=c("low", "high")) #Table 17.3, p. 414. table(depress$sex.fac, depress$inc.high.fac, depress$treat.fac, depress$cases.fac) #Output for chi-squared test, bottom p. 419. chisq.test(depress$inc.high, depress$sex, correct=F) #Table 17.7, p. 420. table1 <- table(depress$sex, depress$inc.high) loglin(table1, list(1:2), param=T, fit=T) #output for three-way model, p. 435-437. table3 <- table(depress$sex, depress$inc.high, depress$treat) loglin(table3, list(1:3), param=T, fit=T) #Calculations, top p. 437. #Note we need to be modeling a (0, 1) variable rather than a (1, 2) #variable. Also, we want the predictor sex to be (0, 1) rather (1, 2). depress$sex1 <- depress$sex - 1 depress$treat1 <- depress$treat -1 glm(treat1 ~ sex1 + inc.high, depress, family=binomial) #Detaching the data set if done. detach(depress)