# Manuel Dahnert
# User study: Looking beyond the horizon: Evaluation of four compact visualization techniques for time series in a spatial context
# Evaluation of errors
# 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)

# Exclude trainings, demography and post-tasks
data <- data[data$Training == "null" & data$skipped == "false" &
               ! data$task %in% c("demography", "postTask"), ]
data <- data[, c(1, 6, 12, 16, 18)]

# Special case 4, Error(0) = TRUE, else FALSE
data.dt <- data.table(data)
data.dt[task.1 == "task4", ]$error <- as.numeric(data.dt[task.1 == "task4", ]$error != 0)

# Average duration and error of all repetitions per participant
data.dt <- data.dt[, .("duration_ms" = mean(duration_ms), "error" = mean(error)), 
                   by = c("participant", "task.1", "technique")]
data <- data.dt

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", "task.1")]
  
  if (index == 3) {
    task.techs <- task.techs[tech == 1, ]
  } else {
    task.techs <- task.techs[tech == 4, ]
  }
  
  task.data <- task.dt[task.techs, participant : error, on = "participant"]
  data2 <- rbind(data2, task.data)
}
data <- data2

# Define output directory
export.root <- "export/errors/"
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, ]

  # Combine data to a Group~Value table
  friedman.data <- cbind(task.data[task.data$technique==techniques.long[1],]$error,
                         task.data[task.data$technique==techniques.long[2],]$error,
                         task.data[task.data$technique==techniques.long[3],]$error,
                         task.data[task.data$technique==techniques.long[4],]$error)
  colnames(friedman.data) <- c("hg", "chg", "bhg", "bp")

  # Perform actual tests
  friedman.test <- friedman.test(friedman.data)
  friedman.post <- pairwise.wilcox.test(task.data$error, 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.increase <- data[data$task.1 == "task2" & data$technique == "colhorgraphs", ]
task.decrease <- data[data$task.1 == "task3" & data$technique == "colhorgraphs", ]

min.length <- min(c(length(task.increase), length(task.decrease)))

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 : error, on = "participant"]


# Wilcoxon test
sink(output.file)
cat("Task 2/3 - CHG")
wilcox.test(task.data[task.1 == "task2"]$error, task.data[task.1 == "task3"]$error, 
            paired = T, exact = F,
            p.adj = "bonferroni")
sink()
