--- title: "empirical logits" author: "Chris Parrish" date: "January 11, 2016" output: pdf_document --- empirical logits reference: - Cannon, et al., Stat2, chapter 09, example 9.9 Migraines. Import the data. ```{r} migraines <- c(39, 61, 22, 78) dim(migraines) <- c(2, 2) dimnames(migraines) <- list("pain-free"=c("yes", "no"), treatment=c("TMS", "placebo")) migraines addmargins(migraines) ``` ```{r} migraines.table <- round(rbind(odds=c(39 / 61, 22 / 78), log.odds=c(log(39 / 61), log(22 / 78))), 3) colnames(migraines.table) <- c("TMS", "placebo") migraines.table slope <- log(39 / 61) - log(22 / 78) slope # slope = log(OR) = log(odds.TMS / odds.placebo) ``` glm. ```{r} migraines.data <- cbind(migraines[1, ], migraines[2, ]) treatment <- 1:0 migraines.glm <- glm(migraines.data ~ treatment, family=binomial) options(show.signif.stars=FALSE) summary(migraines.glm) beta1 <- coef(migraines.glm)[2] beta1 # beta1 = slope ``` Illustration. ```{r fig.width=6, fig.height=4.5} plot(x=c(0, 1), y=c(-1.27, -0.45), pch=20, las=1, col="darkred", xlim=c(-0.5, 1.5), ylim=c(-1.5, 0.5), xlab="Treatment: 0 = Placebo, 1 = TMS", ylab="log odds of pain-free") segments(0, -1.27, 1, -0.45, col="orange", lty="dashed") abline(h=0) abline(v=0) ``` Marriage Import the data. ```{r} marriage <- c(176, 134, 148, 142) dim(marriage) <- c(2, 2) dimnames(marriage) <- list("mother married"=c("yes", "no"), child=c("boy", "girl")) marriage addmargins(marriage) ``` ```{r} marriage.table <- round(rbind(odds=c(148 / 142, 176 / 134), log.odds=c(log(148 / 142), log(176 / 134))), 3) colnames(marriage.table) <- c("girl", "boy") marriage.table slope <- log(148 / 142) - log(176 / 134) slope # slope = log(OR) = log(odds.girl / odds.boy) ``` glm. ```{r} marriage.data <- cbind(marriage[2, ], marriage[1, ]) treatment <- 1:0 marriage.glm <- glm(marriage.data ~ treatment, family=binomial) summary(marriage.glm) beta1 <- coef(marriage.glm)[2] beta1 # beta1 = slope ``` Illustration. ```{r fig.width=6, fig.height=4.5} plot(x=c(0, 1), y=c(log(176 / 134), log(148 / 142)), pch=20, las=1, col="darkred", xlim=c(-0.5, 1.5), ylim=c(-1.5, 0.5), xlab="Treatment: 0 = Boy, 1 = Girl", ylab="log odds of marriage") segments(0, log(176 / 134), 1, log(148 / 142), col="orange", lty="dashed") abline(h=0) abline(v=0) ```