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


# Titanic

```{r load data}
library(dplyr)
library(readr)
titanic=read_csv("https://gist.githubusercontent.com/michhar/2dfd2de0d4f8727f873422c5d959fff5/raw/fa71405126017e6a37bea592440b4bee94bf7b9e/titanic.csv")

library(magrittr)

titanic %<>% mutate( Survived=factor(Survived, levels = c(0,1), labels = c("No", "Yes") ))
```

```{r}

# Fare
logistic.mod1 <- glm(Survived ~ Fare, data = titanic, # estimate a generalized linear model 
                     family = binomial(logit))
logistic.mod1 %>% summary()


fitted.results <- predict(logistic.mod1, newdata = titanic, type = "response")
fitted.results <- ifelse(fitted.results > 0.5, 1, 0) # convert to binary
fitted.results <- factor(fitted.results, levels = c(0,1), labels = c("No", "Yes"))

length(fitted.results)

library(caret)
cc=caret::confusionMatrix(fitted.results, titanic$Survived)
cc$table
caret::confusionMatrix(fitted.results, titanic[,"Survived"][[1]])$overall[1]
titanic$Survived==titanic[,"Survived"][[1]]


#Fare + Sex
logistic.mod2 <- glm(Survived ~ Fare+Sex, data = titanic, # estimate a generalized linear model 
                 family = binomial(logit))
logistic.mod2 %>% summary()
fitted.results <- predict(logistic.mod2, newdata = titanic, type = "response")
fitted.results <- ifelse(fitted.results > 0.5, 1, 0) # convert to binary
fitted.results <- factor(fitted.results, levels = c(0,1), labels = c("No", "Yes"))

caret::confusionMatrix(fitted.results, titanic$Survived)$overall[1]




```

### Gini

$1-\left(\frac{302}{498}\right)^2-\left(\frac{196}{498}\right)^2=$ `r 1-(302/498)^2-(196/498)^2`



### Gini extreme example: suppose all women would have survived and all men would have died:

#### Gini for men:
$1-\left(\frac{14}{316}\right)^2-\left(\frac{302}{316}\right)^2=$ `r 1-(14/316)^2-(302/316)^2` 

#### Gini for women:
$1-\left(\frac{0}{182}\right)^2-\left(\frac{182}{182}\right)^2=0$ 

Hence weighted average Gini (after classifying by sex) is 
$(1-(14/316)^2-(302/316)^2)*316/498=$
`r (1-(14/316)^2-(302/316)^2)*316/498`: an almost perfectly ordered world


```{r splitting}
1-(302/498)^2-(196/498)^2

#titanic %>% group_by(Sex,Survived) %>% summarise(n())
#titanic %>% group_by(Sex) %>% summarise(n())
#titanic %>% group_by(Survived) %>% summarise(n())
```



avaxhoaxid ~ AT_realDonaldTrump + AT_elonmusk + AT_KendallJenner + AT_kobebryant + AT_piersmorgan + atnumLT4



Further reading: https://medium.com/@mijordan3/artificial-intelligence-the-revolution-hasnt-happened-yet-5e1d5812e1e7