---
title: "Übung 5"
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 setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE) 
```

## ANOVA cholesterol

```{r, message=FALSE}
library(multcomp)
attach(cholesterol)
colnames(cholesterol)
head(cholesterol)
tail(cholesterol)
unique(cholesterol$trt)
```

```{r}
library(ggplot2)
colnames(cholesterol)
ggplot(data = cholesterol, aes(x = trt, y = response))+
  geom_boxplot()
```


# Deskriptive Statistiken

```{r}
library(pastecs)
by(response, trt, stat.desc)
```

# Levene's Test

```{r}
library(car)
leveneTest(response, trt, center = median)
leveneTest(response, trt, center = mean)
```

If the *Levene's test* is non-significant, the data variances are very similar. 



# ANOVA als GLM

```{r}
model <- lm(response ~ trt, data = cholesterol)
summary(model)
contrasts(trt)
```

# ANOVA klassisch
```{r}
model1 <- aov(response ~ trt, data = cholesterol)
summary(model1)
qqPlot(lm(response ~ trt, data = cholesterol), simulate = TRUE, main = 'Q-Q Plot', labels = FALSE)
bartlett.test(response ~ trt, data = cholesterol)
```

Bartlett's Test nicht signifikant, die Varianz ist homogen.


```{r, message=FALSE}
library(gplots)
plotmeans(response ~ trt)
```

```{r}
model1 <- aov(response ~ trt, data = cholesterol)
par(mfrow=c(2,2))
plot(model1)
```


# Welch's F

```{r}
oneway.test(response ~ trt, data = cholesterol)
```

# Robuste ANOVA

```{r}
library(WRS2)
t1way(response ~ trt, data = cholesterol, tr = 0.1)
med1way(response ~ trt, data = cholesterol) # median
t1waybt(response ~ trt, data = cholesterol)
```

# *post hoc*-Tests
```{r}
pairwise.t.test(response, trt, p.adjust.method = "bonferroni")
```

```{r}
par(las=2)
par(mar=c(5,8,4,2))
plot(TukeyHSD(model1), cex = 1, cex.main=1, cex.lab=1, cex.axis=1)
```

```{r}
library(multcomp)
tuk <- glht(model1, linfct = mcp(trt = "Tukey"))

plot(cld(tuk, level = .05, col = "lightblue"), cex = 2, cex.main=2, cex.lab=2, cex.axis=2)

```