# Manuel Dahnert
# User study: Looking beyond the horizon: Evaluation of four compact visualization techniques for time series in a spatial context
# Evaluation of timings
# Null hypothesis testing
# Parametric tests RM-ANOVA for interval/ratio timings
# Pairwise t-tests, Post-Hoc Tukey HSD test
# Assume log-normal distribution of timings

source("eval-functions.R")

# Load necessary librarys
loadLibrary("RColorBrewer")
loadLibrary("data.table")
loadLibrary("nlme")
loadLibrary("multcomp")
loadLibrary("ez")

# 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)] # Exclude unnecessary columns
data$duration_ms <- log10(data$duration_ms) # Assume log-normality
data$technique <- factor(data$technique)

# Average duration and error of all repetitions per participant
data.dt <- data.table(data)
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/timings/"
dir.create(export.root, showWarnings = FALSE) # Create directory


# ------------------------------------------------------------------------------
#  NULL HYPOTHESIS TESTING for Task 1,2,4-10
# ------------------------------------------------------------------------------

# Perform ANOVA for repeated measures and save verbose results
output.file <- paste(export.root, "anova-verbose.txt", sep = "")
sink(output.file)
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]

  if (length(unique(task.data$technique)) <= 1) next # Skip task 3
  
  # Perform ANOVA test
  aov.test <- aov(duration_ms ~ technique + 
                    Error(factor(participant)/factor(technique)),
                  data = task.data)
  aov.summary <- summary(aov.test)
  
  cat(task.name, "\n")
  print(aov.test)
  cat("\n")
  print(aov.summary)
  cat("\n\n\n")
}
sink()


# Perform ezANOVA for repeated measures and save verbose results
options(contrasts=c("contr.sum", "contr.poly"))

output.file <- paste(export.root, "anova-verbose-ez.txt", sep = "")
sink(output.file)
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  task.data$participant <- factor(task.data$participant)
  
  if (length(unique(task.data$technique)) <= 1) next # Skip task 3
  
  # Perform ANOVA test
  aov <- ezANOVA(data = task.data, dv = .(duration_ms), 
                 wid = .(participant), within = .(technique),
                 detailed = TRUE, type = 3)
  
  cat(task.name, "\n")
  print(aov)
  cat("\n\n\n")
}
sink()

# Perform ANOVA for repeated measures and save verbose results
# NLME package
output.file <- paste(export.root, "anova-verbose-2.txt", sep = "")
sink(output.file)
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  
  if (length(unique(task.data$technique)) <= 1) next # Skip task 3
  
  # Perform ANOVA test
  baseline <- lme(duration_ms ~ 1, random = ~1 | participant/technique, 
                  data = task.data, method = "ML")
  
  model <- lme(duration_ms ~ technique, random = ~1 | participant/technique, 
               data = task.data, method = "ML")
  
  aov.test <- anova(baseline, model)
  aov.summary <- summary(model)

  cat(task.name, "\n")
  print(aov.test)
  cat("\n")
  print(aov.summary)
  cat("\n\n\n")
}
sink()


# Perform post-hoc analysis of repeated-measures ANOVA with pairwise t-test
output.file <- paste(export.root, "posthoc-verbose.txt", sep = "")
sink(output.file)
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  
  if (length(unique(task.data$technique)) <= 1) next # Skip task 3
  
  # Perform pairwise t test
  post.test <- pairwise.t.test(task.data$duration_ms, task.data$technique,
                  p.adj = "bonferroni", paired = T)
  
  cat(task.name, "\n")
  print(post.test)
  cat("\n\n\n")
}
sink()

# Perform post-hoc analysis of repeated-measures ANOVA with Tukey's HSD
output.file <- paste(export.root, "posthoc-verbose-2.txt", sep = "")
sink(output.file)
for(index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  task.data <- data[data$task.1 == task.name, ]
  
  if (length(unique(task.data$technique)) <= 1) next # Skip task 3
  
  # Perform ANOVA test
  baseline <- lme(duration_ms ~ 1, random = ~1 | participant/technique, 
                  data = task.data, method = "ML")
  
  model <- lme(duration_ms ~ technique, random = ~1 | participant/technique, 
               data = task.data, method = "ML")
  
  aov.test <- anova(baseline, model)
  aov.summary <- summary(model)
  
  posthoc <- glht(model, linfct = mcp(technique = "Tukey"))
  posthoc.summary <- summary(posthoc)
  
  cat(task.name, "\n")
  print(posthoc.summary)
  cat("\n\n\n")
}
sink()


# ------------------------------------------------------------------------------
#  NULL HYPOTHESIS TESTING for Task 2/3 (CHG)
# ------------------------------------------------------------------------------

# Check if slope increase / decrease has effect on timing
output.file <- paste(export.root, "t2_3-t-test-chg.txt", sep = "")
task.increase <- data[data$task.1 == "task2" & data$technique == "colhorgraphs", ]
task.decrease <- data[data$task.1 == "task3" & data$technique == "colhorgraphs", ]

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"]

sink(output.file)
cat("Task 2/3 - CHG")
t.test(task.data[task.1 == "task2"]$duration_ms, 
       task.data[task.1 == "task3"]$duration_ms, paired = TRUE, 
       p.adj = "bonferroni")
sink()
