---
title: "PISA 2012 - multi dimensional Gaussian merging"
author: "Agnieszka Sitko"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{PISA 2012 - multi dimensional Gaussian merging}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE,
                      fig.height = 5, fig.width = 10)
```

# Libraries

```{r}
library(factorMerger)
library(ggplot2)
library(dplyr)
library(reshape2)
```


# Load data
```{r}
data("pisa2012")
```

# Explore

```{r}
pisa2012 %>% ggplot(aes(x = country)) + geom_bar() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1))
```

```{r}
meltedPisa <- pisa2012 %>% melt(na.rm = TRUE)
pisaResultsBySubject <-  meltedPisa %>% 
    ggplot(aes(x = reorder(country, value, FUN = median), y = value)) + geom_boxplot() + 
    facet_wrap(~variable) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Country") 

```

```{r}
pisaResultsBySubject + 
    geom_hline(data = meltedPisa %>% group_by(variable) %>% summarise(mean = mean(value)), 
               aes(yintercept = mean, group = variable), col = "red")
```

TODO: Find countries significantly better, worse and not significantly different from global averages. Cluster countries into three groups.


# Run MANOVA

```{r}
manova(cbind(math, reading, science) ~ country, pisa2012) %>% summary()
```

It seems that there exist some differences among countries included in PISA. Let's find them!

# Factor Merger

Let's now have a try using factorMerger for exploration.

It's faster to use "fast-adaptive" or "fast-fixed" methods on a big dataset.
They enable comparisons between neighbours only (neighbours are pairs of groups with close means). 


```{r, fig.width=10, fig.height=10}
pisaFMHClustEurope <- mergeFactors(response = pisa2012[,1:3],
                       factor = factor(pisa2012$country),
                       method = "fast-fixed") 

plot(pisaFMHClustEurope)

```
