---
title: "Friedman test results"
author: "Dariusz Brzezinski"
date: "20 sierpnia 2016"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```

```{r libraries, message=FALSE}
library(dplyr)
library(tidyr)
library(knitr)
library(PMCMR)
library(scmamp)
library(DT)
```

```{r functions}
reportFriedman <- function(df, metric, rank.df){
    cat("\n\n")
    cat(paste("##", metric, "\n\n"))
    averages = df %>%
        #filter(Classifier != "VFDT", Classifier != "VFDTAdaptive") %>%
        select_("Dataset", "Classifier", metric) %>%
        spread_("Classifier", metric) %>%
        rename(HT = VFDT, HTADWIN = VFDTAdaptive, Bag = Bagging, NB = Bayes) %>%
        select(-Dataset) %>%
        data.matrix()
    
    ranks <- t(apply(-averages, 1, rank)) %>% as.data.frame 
    
    tmp <- t(colMeans(ranks, na.rm=TRUE)) %>% 
            as.data.frame %>% 
            mutate(Measure = metric) %>%
            select(Measure, NB, HT, HTADWIN, Bag, AUE, SERA)
    if (is.null(rank.df)){
        rank.df <- tmp
    } else {
        rank.df <- rbind(rank.df, tmp)
    }
    
    ranks <- ranks[,order(colMeans(ranks, na.rm=TRUE))]

    print(friedman.test(averages))
    print(kable(t(colMeans(ranks, na.rm=TRUE)), digits = 2))
    cat("\n\n")
    setEPS()
    postscript(paste(metric, "Friedman.eps", sep=""))#, width = 7, height = 3.4)
    #svg(paste(metric, "Friedman.svg", sep=""))
    plotCD(results.matrix = averages, alpha = 0.05)
    dev.off()
    plotCD(results.matrix = averages, alpha = 0.05)
    
    rank.df
}
```

```{r, results="asis"}
r <-NULL
df <- read.csv("Averages.csv")

r <- reportFriedman(df, "Accuracy", r)
r <- reportFriedman(df, "AUC", r)
r <- reportFriedman(df, "G.Mean", r)
r <- reportFriedman(df, "Kappa", r)
r <- reportFriedman(df, "KappaM", r)
r <- reportFriedman(df, "Recall", r)

r <- r %>% 
    mutate(Measure = c("Accuracy", "AUC", "G-mean", "Kappa", "Kappa_M", "Recall")) %>%
    select(Measure, NB, Bag, HT, HTADWIN, AUE, SERA)

write.csv(r, "Friedman.csv")

kable(r, digits = 2)
```
