# Manuel Dahnert
# User study: Looking beyond the horizon: Evaluation of four compact visualization techniques for time series in a spatial context
# Evaluation of confidence
# Check for normality / log-normality visually (histograms, qq-plots)
# Check for normality / log-normality with test (Shapiro-Wilk)

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

# ------------------------------------------------------------------------------
#  NORMALITY (Histograms, QQ-plots, Shapiro-Wilk)
# ------------------------------------------------------------------------------
output.width <- 600
output.height <- 600

# Create histogram maitrx for each task to check for normal distribution
output.dir <- paste(export.root, "histograms/", sep = "")
dir.create(output.dir, showWarnings = FALSE) # Create directory

# Loop through all tasks
for (index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  output.name <- paste(output.dir, index, ".png", sep = "")
  
  png(output.name, w = output.width, h = output.height) # start output rendering
  par(mfrow = c(2,2)) # Define matrix
  
  # Generate a histogram for each technique
  for (tech in techniques.long) {
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    
    if (nrow(tech.data) == 0) next # Skip for task 3
    
    hist(tech.data$givenAnswer, main = paste(task.name, tech, sep = " - "),
         xlab = "confidence")
  }
  
  dev.off() # End output rendering
}


# Create QQ-plot maitrx for each task to check for normal distribution
output.dir <- paste(export.root, "qq-plots/", sep = "")
dir.create(output.dir, showWarnings = FALSE) # Create directory

# Loop through all tasks
for (index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  output.name <- paste(output.dir, index, ".png", sep = "")
  
  png(output.name, w = output.width, h = output.height) # start output rendering
  par(mfrow = c(2,2)) # Define matrix
  
  # Generate a histogram for each technique
  for (tech in techniques.long) {
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    
    if (nrow(tech.data) == 0) next # Skip for task 3
    
    qqnorm(tech.data$givenAnswer, main = paste(task.name, tech, sep = " - "),
         xlab = "confidence")
    qqline(tech.data$givenAnswer) # Add regression line
  }
  
  dev.off() # End output rendering
}


# Perform Shapiro-Wilk test and save results in file
output.file <- paste(export.root, "shapiro.txt", sep = "")

sink(output.file)
cat("Shapiro-Wilk test for normality test\n")
cat("Task \t", paste0(techniques, sep = "\t\t\t"), "\n", sep = "") # Add headlines
for (index in tasks.index) {
  
  cat(sprintf("%02d", index), " ->\t", sep = "")
  for (tech in techniques.long) {
    task.name <- paste("task", index, sep = "")
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    
    if (nrow(tech.data) == 0) {
      cat("\t\t\t")
      next # Skip for task 3
    }
    
    tech.short <- techniques[which(techniques.long == tech)] # Get abbr of tech name
    shapiro.result <- shapiro.test(tech.data$givenAnswer) # Perform test
    cat(sprintf("%.6f", shapiro.result$p.value), "\t")
  }
  cat("\n")
}
sink()
# -> not normal distributed



# ------------------------------------------------------------------------------
#  LOG-NORMALITY (Histograms, QQ-plots, Shapiro-Wilk)
# ------------------------------------------------------------------------------
output.width <- 600
output.height <- 600

# Create histogram maitrx for each task to check for normal distribution
output.dir <- paste(export.root, "histograms-log/", sep = "")
dir.create(output.dir, showWarnings = FALSE) # Create directory

# Loop through all tasks
for (index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  output.name <- paste(output.dir, index, ".png", sep = "")
  
  png(output.name, w = output.width, h = output.height) # start output rendering
  par(mfrow = c(2,2)) # Define matrix
  
  # Generate a histogram for each technique
  for (tech in techniques.long) {
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    tech.data$givenAnswer <- log10(tech.data$givenAnswer)
    
    if (nrow(tech.data) == 0) next # Skip for task 3
    
    hist(tech.data$givenAnswer, main = paste(task.name, tech, sep = " - "),
         xlab = "log10(confidence)")
  }
  
  dev.off() # End output rendering
}


# Create QQ-plot maitrx for each task to check for normal distribution
output.dir <- paste(export.root, "qq-plots-log/", sep = "")
dir.create(output.dir, showWarnings = FALSE) # Create directory

# Loop through all tasks
for (index in tasks.index) {
  task.name <- paste("task", index, sep = "")
  output.name <- paste(output.dir, index, ".png", sep = "")
  
  png(output.name, w = output.width, h = output.height) # start output rendering
  par(mfrow = c(2,2)) # Define matrix
  
  # Generate a histogram for each technique
  for (tech in techniques.long) {
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    tech.data$givenAnswer <- log10(tech.data$givenAnswer)
    
    if (nrow(tech.data) == 0) next # Skip for task 3
    
    qqnorm(tech.data$givenAnswer, main = paste(task.name, tech, sep = " - "),
           xlab = "log10(confidence)")
    qqline(tech.data$givenAnswer) # Add regression line
  }
  
  dev.off() # End output rendering
}


# Perform Shapiro-Wilk test and save results in file
output.file <- paste(export.root, "shapiro.txt", sep = "")

sink(output.file, append = TRUE)
cat("Shapiro-Wilk test for log-normality test\n")
cat("Task \t", paste0(techniques, sep = "\t\t\t"), "\n", sep = "") # Add headlines
for (index in tasks.index) {
  
  cat(sprintf("%02d", index), " ->\t", sep = "")
  for (tech in techniques.long) {
    task.name <- paste("task", index, sep = "")
    tech.data <- data[data$task.1 == task.name & data$technique == tech, ]
    tech.data$givenAnswer <- log10(tech.data$givenAnswer)
    
    if (nrow(tech.data) == 0) {
      cat("\t\t\t")
      next # Skip for task 3
    }
    
    tech.short <- techniques[which(techniques.long == tech)] # Get abbr of tech name
    shapiro.result <- shapiro.test(tech.data$givenAnswer) # Perform test
    cat(sprintf("%.6f",shapiro.result$p.value), "\t")
  }
  cat("\n")
}
sink()
# -> don't assume log-normal distributed!
