# Manuel Dahnert
# User study: Looking beyond the horizon: Evaluation of four compact visualization techniques for time series in a spatial context
# Evaluation of confidence
# Null hypothesis testing
# Non-parametric tests for repeated-measures (Friedman)

# Load common functions and constants
source("eval-functions.R")

# Load necessary librarys
loadLibrary("RColorBrewer")
loadLibrary("data.table")

# Load raw study data and perform general filtering
data.csv <- read.csv("rawdata_study.csv", header = TRUE, sep =";")
data <- filterData(data.csv, max.skips = 10, min.time = 30)

data <- data[data$questionId == "confidence", ]
data <- data[, c(1, 13, 16, 18)] # Exclude unnecessary columns
data$givenAnswer <- as.numeric(factor(data$givenAnswer))

data <- data.table(data)

data2 <- data.table()
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  
  task.dt <- data[task.1 == task.name, ]
  task.techs <- task.dt[, .("tech" = length(technique)), by = c("participant")]
  
  if (index == 3) {
    task.techs <- task.techs[tech == 1, ]
  } else {
    task.techs <- task.techs[tech == 4, ]
  }
  
  task.data <- task.dt[task.techs, participant : technique, on = "participant"]
  data2 <- rbind(data2, task.data)
}
data <- data2

# Define output directory
export.root <- "export/confidence/"
dir.create(export.root, showWarnings = FALSE) # Create directory

# ------------------------------------------------------------------------------
#  NULL HYPOTHESIS TESTING for Task 1,2,4-8,10
# ------------------------------------------------------------------------------


# Perform Friedman test and save verbose results
output.file <- paste(export.root, "friedman-verbose.txt", sep = "")
sink(output.file)
for (index in c(1,2,4,5,6,7,8,9,10)) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  task.data$technique <- factor(task.data$technique)
  
  # Combine data to a Group~Value table
  friedman.data <- cbind(task.data[task.data$technique==techniques.long[1],]$givenAnswer,
                         task.data[task.data$technique==techniques.long[2],]$givenAnswer,
                         task.data[task.data$technique==techniques.long[3],]$givenAnswer,
                         task.data[task.data$technique==techniques.long[4],]$givenAnswer)
  colnames(friedman.data) <- c("hg", "chg", "bhg", "bp")
  
  # Perform actual tests
  friedman.test <- friedman.test(friedman.data)
  friedman.post <- pairwise.wilcox.test(task.data$givenAnswer, task.data$technique,
                                        p.adj="bonferroni", exact=FALSE,
                                        paired = TRUE)
  cat(task.name,"(", nrow(task.data)/4, ")",  "\n")
  print(friedman.test)
  print(friedman.post)
  cat("\n\n")
}
sink()

# Special casse task 2/3 for CHG
# Check if slope increase / decrease has effect on error
output.file <- paste(export.root, "t2_3-wilcoxon.txt", sep = "")

task.dt <- data[(task.1 == "task2" | task.1 == "task3") & technique == "colhorgraphs", ]
task.techs <- task.dt[, .("tech" = length(task.1)), by = c("participant")]

task.techs <- task.techs[tech == 2, ]

task.data <- task.dt[task.techs, participant : technique, on = "participant"]


# Wilcoxon test
sink(output.file)
cat("Task 2/3 - CHG")
wilcox.test(task.data[task.1 == "task2"]$givenAnswer, 
            task.data[task.1 == "task3"]$givenAnswer, paired = T, exact = F,
            p.adj = "bonferroni")
sink()
