---
title: "Week 5 Lecture 1"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gapminder)
titanic <- read.csv("titanic.csv")
```

### Categorical vs. Continous Variables

We reviewed the material from the [previous lecture](http://guerzhoy.mycpanel.princeton.edu/201s19/lec/W04/lec2/titanic.html). See there for details.

Let's fit two models: one with `Pclass` as a continous variable, and one with `Pclass` as a discrete variable.

```{r}
fit.titanic.cont <- glm(Survived ~ Age + Sex + Pclass, family = binomial, data=titanic)
fit.titanic.categ <- glm(Survived ~ Age + Sex + as.factor(Pclass), family = binomial, data=titanic)
```

Let's predict using the model that uses `Pclass` as a continuous. Here, we are predicting for the person who appears first in the Titanic dataset:

```{r}
titanic[1,]
```

Here's the prediction using `plogis`:

```{r}
plogis(predict(fit.titanic.cont, newdata = titanic[1,]))
```

We can get the same prediction using `type="response"`

```{r}
predict(fit.titanic.cont, newdata = titanic[1,], type="response")
```

Finally, we can look at the fit to see what the coefficients are:

```{r}
fit.titanic.cont$coefficients
```

So we can predict "manually":

```{r}
plogis(4.87851 - 0.03436 * 22 -2.58916 - 1.23054* 3)
```

Now, consider the cateogrical variable version.

```{r}
fit.titanic.categ
```

The "manual" prediction would be

```{r}
plogis(3.63492 -0.03427 * 22 -2.58872 -2.45544 )
```

That is almost the same. As we discussed in class, the model with the cateogrical version has strictly more felxibility as far as predicting different values for different classes, so we expect that on the training set, the prediction using the categorical variable version would work at least as well or better.


### Predicting "Survived" or Died

Like we mentioned before, we can decide to guess "Survived" if the probability is greater or equal to 0.5.


```{r}
titanic[, "pred"] <- predict(fit.titanic.cont, newdata = titanic, type="response") >= .5
```

(Note that this is the same as predicting "Survived" if the sum $a_0 + a_1 x_ 1 + ...$ is greater than 0, since $\sigma(0) = 0.5$.)


### The "baseline" classifier 

The simplest possible classifier simply predicts the same thing (in our case, "did not survive") every time. We can compute how often the classifier will be correct:

```{r}
mean(titanic$pred == titanic$Survived)
```

And now we can compute how often the baseline classifier will be correct

```{r}
mean(titanic$pred == 0)
```

### Other measures of how good a classifier/predictor/model is


### False positive rate

The false positive rate is the  rate at which the model outputs "positive", when considering the negative examples (i.e., the model says "Survived" when the person did not survive):

$$FPR = \frac{\text{# of times the model said "positive" and was wrong}}{\text{# of negatives }}$$


The false nevative rate is the rate at which the model outputs "negative", when considering the positive examples (i.e., the model says "Did not survive" when the person actually did survive)

$$FNR = \frac{\text{# of times the model said "negative" and was wrong}}{\text{# of positives }}$$

The positive predictive value is the rate at which the model is correct when it says "positive":

$$PPV = \frac{\text{# of times the model said "positive" and was correct}}{\text{# of times the model said "positive"}}$$

```{r}
total.positive.actually.negative <- sum((titanic$pred == T) & (titanic$Survived == 0))
total.negative <- sum((titanic$pred == F))
FPR <- total.positive.actually.negative/total.negative
```

### Which is "positive" and which is "negative"?

There is no hard-and-fast rule, but usually "positive" would be the suprising or rare or important event (e.g., the patient has a rare/significant disease).




