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

# Repeated measures ANOVA: monsters

Machen die Kinder ihre Hände zögerlich in eine Kiste mit dem Tier, worüber man schlecht geredet hat?

```{r}
df <- read.table('/Users/vitaly/Lehre/WS1920/Beuth/Data files/Field(2006).dat', sep = '', header = TRUE)
knitr::kable(df)
```

## Long format

```{r}
library(reshape2)
longMonsters <- melt(df, id = 'code', measured = c('bhvneg','bhvpos','bhvnone'))
names(longMonsters) <- c('Participant', 'Story', 'Time')
longMonsters$Story <- factor(longMonsters$Story)
knitr::kable(longMonsters)
```


## Bar and boxplots of the data

```{r}
library(ggplot2)
ggplot(data = longMonsters, aes(x = Story, y = Time))+
   geom_boxplot() + geom_jitter() + theme(text = element_text(size = 20))
```

## Log-transformation

```{r}
longMonsters$Time_log10 <- sapply(longMonsters$Time, log10)
ggplot(data = longMonsters, aes(x = Story, y = Time_log10))+
   geom_boxplot() + geom_jitter() + theme(text = element_text(size = 20))

```

## Descriptive Statistik

```{r}
library(pastecs)
by(longMonsters$Time_log10, longMonsters$Story, stat.desc)
```

## Klassische repeated measures ANOVA

```{r}
library(ez)
model_monsters <- ezANOVA(data = longMonsters, dv = .(Time_log10), wid = .(Participant), within = .(Story), detailed = TRUE, type = 3)
model_monsters
```

## Post hoc Tests

```{r}
library(ez)
pairwise.t.test(longMonsters$Time_log10, longMonsters$Story, paired = TRUE, p.adjust.method = "bonferroni")
```



## Repeated measures ANOVA als hierarchisches Modell

### Das eigentliche Modell

```{r}
library(nlme)
monster_ml <- lme(Time_log10 ~ Story, random = ~1|Participant/Story, data = longMonsters, method = "ML")
```

### Baseline

```{r}
monster_ml_0 <- lme(Time_log10 ~ 1, random = ~1|Participant/Story, data = longMonsters, method = "ML")
```

### Vergleich mit Baseline

```{r}
anova(monster_ml_0, monster_ml)

```

### Summary vom Modell

```{r}
summary(monster_ml)
```

### Post hoc-Tests 

```{r}
library(multcomp)
postHocs <- glht(monster_ml, linfct = mcp(Story = "Tukey") )
summary(postHocs)
confint(postHocs)
```

## ANOVA

# Two way factorial ANOVA

Beainflüsst die Laune das obzessive 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('/Users/vitaly/Lehre/WS1920/Beuth/Data files/Davey2003.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)

```
