---
title: "Untitled"
author: "Vitaly Belik"
date: "1/19/2021"
output: html_document
---

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



```{r}
d = read.table("https://belik.userpage.fu-berlin.de/beuth/titanic.csv", sep = ",", header = TRUE)
d$survived
```

```{r}
#df <- read.table("titanic.csv", sep = ",", header = TRUE)
#head(df)
```

```{r}
d1 <- na.omit(d)
```

```{r}
library(tidyverse)
library(caret)
#data("iris")
#knitr::kable(head(iris))
head(d1)
```


```{r}
set.seed(123)
training.samples <- d1$survived %>% createDataPartition(p = 0.8, list = FALSE)
train.data <- d1[training.samples, ]
test.data <- d1[-training.samples, ]
```


```{r}
preproc.param <- train.data %>% preProcess(method = c("center", "scale"))
train.transformed <- preproc.param %>% predict(train.data)
test.transformed <- preproc.param %>% predict(test.data)
```

```{r}
library(MASS)
model <- lda(factor(survived) ~ factor(pclass) + sex + age + alone, data = train.transformed)
predictions <- model %>% predict(test.transformed)
mean(predictions$class == test.transformed$survived)
predictions <- model %>% predict(test.transformed)
#head(predictions)
```


```{r}
predmodel.train.lda = predict(model, data=train.data)
table(Predicted=predmodel.train.lda$class, Survived=train.data$survived)
ldahist(predmodel.train.lda$x[,1], g = predmodel.train.lda$class)
```


# Decision tree

```{r}
library(rpart)
library(gmodels)
library(rpart.plot)
library(rattle)
train <- sample(nrow(d), 0.7 * nrow(d))
df.train <- d1[train, ]
df.test <- d1[-train, ]
dtree <- rpart(survived ~ factor(pclass) + sex + age + alone, data = df.train, method = "class", parms = list(split = "gini"))
fancyRpartPlot(dtree)
```

