---
title: "Übung 4"
author: "Vitaly Belik"
output:
  html_document:
    depth: 3
    fig_caption: yes
    highlight: tango
    number_sections: yes
    theme: paper
    toc: yes
  pdf_document:
    toc: yes
---




```{r, echo=FALSE, message=FALSE, warning=FALSE}
#Install the relevant libraries - do this one time
# install.packages("data.table")
# install.packages("dplyr")
# install.packages("formattable")
# install.packages("tidyr")
# #Load the libraries
# library(data.table)
# library(dplyr)
# library(formattable)
# library(tidyr)
library(ggplot2)
```


# Titanic

```{r,echo = F, out.width="80%", warning=FALSE}
library(ggplot2)
theme_update(text = element_text(size=20))
df <- read.table('/Users/vitaly/Lehre/WS1920/Beuth/2_Deskriptive_Statistik/Data/titanic.csv', sep = ',', header = TRUE)
ggplot(data = df, aes(x = age, y = log10(fare), color = sex, 
                      size = as.factor(pclass),
                      shape = as.factor(survived))) + geom_point(alpha = 0.5)
```

- Welche Variablen kann man mit einander korrelieren?
- Was können wir über Überlebungschancen sagen?

# Regression, Beispiel 2

```{r, echo = TRUE}
# Impact of the amount of money spent on advertising on album sales
df <- read.table('/Users/vitaly/Lehre/WS1920/Beuth/4_Statistische_Tests_and_Regression/Data/Data\ files/Album\ Sales\ 2.dat', sep = '', header = TRUE)
#df <- read.table('Album_Sales_2.dat', sep = '', header = TRUE)

colnames(df)

library(ggplot2)
ggplot(df, aes(x = adverts, y = sales)) +
theme(text = element_text(size = 20)) +
geom_point(alpha = 0.8, size = 4) +
labs(x = "advers (thousands of pounds)", y = "sales (thousands)") +
geom_smooth(fill = NA, color = "red", size = 1, method = "lm")
```

## Airplay

```{r}
ggplot(df, aes(x = airplay, y = sales, color = adverts)) +
theme(text = element_text(size = 20)) +
geom_point(alpha = 0.8, size = 4) +
geom_smooth(fill = NA, color = "red", size = 1, method = "lm")
```

## Airplay

```{r}
ggplot(df, aes(x = airplay, y = sales, color = adverts, size = attract)) +
theme(text = element_text(size = 20)) +
geom_point(alpha = 0.8) +
geom_smooth(fill = NA, color = "red", size = 1, method = "lm")
```


## Eigentliches Modell

```{r}
model <- lm(sales ~ adverts, data = df)
summary(model)
```

## ANOVA-Tabelle
\tiny
```{r, echo = TRUE, warning=FALSE}
anova(model)
```
```{r}
AIC(model)
```

```{r}
model1 <- lm(sales ~ airplay, data = df)
summary(model1)
AIC(model1)
```

```{r}
model2 <- lm(sales ~ attract, data = df)
summary(model2)
AIC(model2)
```


# Mehrfache Regression

```{r}
model <- lm(sales ~ adverts + airplay + attract, data = df)
summary(model)
AIC(model)
```
## Interaktionen

```{r}
model <- lm(sales ~ adverts + airplay + attract + adverts*attract, data = df)
summary(model)
AIC(model)
```

```{r}
model <- lm(sales ~ adverts + airplay + attract + airplay*attract, data = df)
summary(model)
AIC(model)
```

```{r}
model <- lm(sales ~ adverts + airplay + attract + airplay*adverts, data = df)
summary(model)
AIC(model)
```
```{r}
df_titanic <- read.table('/Users/vitaly/Lehre/WS1920/Beuth/2_Deskriptive_Statistik/Data/titanic.csv', sep = ',', header = TRUE)
colnames(df_titanic)
```
```{r}
model_titanic <- lm(survived ~ age, data = df_titanic)
summary(model_titanic)
```

```{r}
ggplot(df_titanic, aes(x = age, y = survived)) + geom_point() +
  geom_smooth(fill = NA, color = "red", size = 1, method = "lm")
```

# Modeldiagnostik

```{r}
fit <- lm(sales ~ adverts + airplay + attract, data = df)
```

## Q-Q-Plot (Überprüfen der Normalverteilung der Residuen)

```{r}
# 4 panels 
par(mfrow = c(2,2))
par(ask=F)

plot(fit, bty='l', cex.main = 1, cex.lab=1.5, cex.axis=2, cex = 1)
```

### Shapiro-Wilk test

Wenn signifikant ($p<0.05$), dann *nicht normal*.

```{r}
shapiro.test(resid(fit))
```


## Homoskedazität

```{r}
library(car)
ncvTest(fit)
```

```{r}
ggplot(fit, aes(.fitted, .resid)) +
  geom_hline(yintercept = 0) +
  geom_point() +
  geom_smooth(se = FALSE)
```

## Variance Inflation Factor

```{r}
vif(fit)
sqrt(vif(fit)) > 2
```


# Outlier Test

```{r}
outlierTest(fit)
```
## Influence Plot

```{r}
influencePlot(fit, id.method="identify", main="Influence Plot",
              sub="Circle size is proportional to Cook's distance")
```

## Einzelne Diagnostik-Plots

```{r}
#plot(fit, which = c(2), bty='l', cex.main=2, cex.lab=1.5, cex.axis=2, cex = 2)
```


