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

```

## ANOVA

# Two way factorial ANOVA

BeainflÃ¼sst die Laune die ZwangstÃrung, sich zu vergewissen, dass nichts vergessen wurde? Dabei haben die Leute zwei verschiedene Regeln zum AufhÃ¶ren benutzt: 1) 'as many as can' und 2) 'as long as they feel like continuing'.


```{r}
df1 <- read.table('OCD.dat', sep = '', header = TRUE)
knitr::kable(df1)

ggplot(data = df1, aes(x = as.factor(Stop_Rule), y = Checks)) + geom_boxplot() +
  theme(text = element_text(size = 20))

ggplot(data = df1, aes(x = as.factor(Mood), y = Checks)) + geom_boxplot() +
  theme(text = element_text(size = 20))

ggplot(data = df1, aes(x = as.factor(Stop_Rule), y = log10(Checks), colour = as.factor(Mood))) + geom_boxplot() +
  theme(text = element_text(size = 20))


ggplot(data = df1, aes(x = as.factor(Mood), y = log10(Checks), colour = as.factor(Stop_Rule))) + geom_boxplot() +
  theme(text = element_text(size = 20))


```

# Interaktionen

\tiny
```{r}
# library(Hmisc)
ggplot(data = df1, aes(x = as.factor(Mood), y = Checks, group = Stop_Rule, colour = as.factor(Stop_Rule))) +
stat_summary(fun.y = mean, geom = "point") + stat_summary(fun.y = mean, geom = "line") +
  theme(text = element_text(size = 20)) + stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2)

ggplot(data = df1, aes(x = as.factor(Stop_Rule), y = Checks, group = as.factor(Mood), colour = as.factor(Mood))) +
stat_summary(fun.y = mean, geom = "point") + stat_summary(fun.y = mean, geom = "line") +
  theme(text = element_text(size = 20)) + stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.2)


```

# Leven-Test

Nicht signifikante Test bedeuten die ErfÃ¼llung der Anforderungen -- HomogenitÃ¤t der Varianz.
\tiny
```{r}
library(car)
leveneTest(df1$Checks, df1$Mood)
leveneTest(df1$Checks, df1$Stop_Rule)
leveneTest(df1$Checks, interaction(df1$Mood, df1$Stop_Rule))
```


## Zweifache ANOVA


```{r}
model <- aov(Checks ~ as.factor(Stop_Rule)*as.factor(Mood), data = df1)
Anova(model, type = "III")
summary.lm(model)
```

## Kontraste

```{r}
df1$Mood <- as.factor(df1$Mood)
df1$Stop_Rule <- as.factor(df1$Stop_Rule)
contrasts(df1$Mood) <- cbind(c(-2,1,1), c(0,-1,1))
contrasts(df1$Stop_Rule) <- c(-1,1)
contrasts(df1$Mood)
contrasts(df1$Stop_Rule)

model <- aov(Checks ~ Stop_Rule*Mood, data = df1)
Anova(model, type = "III")
summary.lm(model)

```