---
title: "simple-logit-example"
author: "Rob McCulloch"
date: "1/2/2022"
output: html_document
---

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

# Default Data

To illustrate logistic regression in R, let's use the default data form the ISLR package.

```{r}
library(ISLR)
data(Default)
head(Default)
summary(Default)
```

We observation corresponds to a credit card account.  

We want to relate the binary outcome default to x=balance.  
Is the outstanding balance on the card related to whether or not the account holder defaults.

# Plot the data

```{r}
boxplot(balance~default,Default)
```

# Fit a logit

```{r}
lgm = glm(default~balance,Default,family=binomial(link = "logit"))
summary(lgm)
```

```{r}
phat = predict(lgm,type="response")
summary(phat)
plot(Default$balance,phat)
```

# Let's check it!!

```{r}
bhat = lgm$coefficients
cat("bhat:",bhat,"\n")
eta = bhat[1] + bhat[2] * Default$balance

phat0 = 1/(1+exp(-eta))
summary(phat-phat0)
```

How do I check bhat is the MLE??


