---
title: "Exercise 2"
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)
```

# Korrelationskoeffizient

```{r}
df1 <- read.table('Album_Sales_2.txt', sep = '', header = TRUE)
cor(df1$sales, df1$adverts)
library(ggplot2)
ggplot(data = df1, aes(x = adverts, y = sales)) + geom_point() +
  geom_smooth(fill = NA, color = "red", size = 1, method = "lm")

cor(df1$sales, df1$airplay)
ggplot(data = df1, aes(x = airplay, y = sales)) + geom_point() +
  geom_smooth(fill = NA, color = "red", size = 1, method = "lm")

cor(df1$adverts, df1$airplay)
ggplot(data = df1, aes(x = airplay, y = adverts)) + geom_point() +
  geom_smooth(fill = NA, color = "red", size = 1, method = "lm")

cor(df1)

cor(df1, method = 'spearman')
```

# Titanic

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

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



# Corrgram package

```{r}
library(corrgram)

corrgram(df1)

corrgram(df1, lower.panel = panel.ellipse, upper.panel = panel.pts, diag.panel = panel.density)

corrgram(df1, lower.panel = panel.pie, upper.panel = panel.pts, diag.panel = panel.density)

df2 <- read.table('titanic.csv', sep = ',', header = TRUE)

corrgram(df2, lower.panel = panel.pie, upper.panel = panel.pts, diag.panel = panel.density)

```

# Regression, Beispiel 2

```{r, echo = TRUE}
# Impact of the amount of money spent on advertising on album sales
df <- read.table('Album_Sales_2.txt', 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)
```


