---
title: "Logistic Regression for Binomial Counts"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Logistic Regression for Binomial Counts
Read in the data
```{r}
require(Sleuth3)
krunnit <- case2101
```

Let's plot the logits of the proportions vs. the logits of the probability of going extinct.

```{r message=F}
require(gtools)
require(ggplot2)
krunnit$pi_hat <- krunnit$Extinct/krunnit$AtRisk

```

```{r}
qplot(log(Area), logit(pi_hat), data=krunnit)
```

(Note: here, the interpretation is that the total number of species is AtRisk, and out of those, the number of specise that went extinct is Extinct)

```{r}
fit <- glm(  cbind(Extinct, AtRisk-Extinct) ~ log(Area), family="binomial", data=krunnit)
summary(fit)
```

```{r}
fit.sq <- glm(cbind(Extinct, AtRisk - Extinct) ~ log(Area) + I(log(Area)^2), family= "binomial", data= krunnit)
summary(fit.sq)
```

(Note: if you do not enclose log(Area)^2 in `I()`, things don't work. You have to either do that or add an additional column to the data frame with the value `log(Area)^2`)

(We haven't covered this, but BIC is used similarly to AIC)

```{r}
BIC(fit)
BIC(fit.sq)
```