# =========================================================
# Empirical Research Methods for Human-Computer Interaction
# =========================================================

# This R script does the ANOVA on the data collected in the in-class experiment
# for the above-noted course.  The input file is 'EntrySpeed.txt' which contains 
# eight header lines (for GoStats) followed by an n x 11 matrix of data, where n
# is the number of participants.  The first 10 columns of data are the entry 
# speed measurements (2 layouts x 5 trials/layout).  The 11th column contains 
# the group codes for counterbalancing ("1", "2", "1", etc.).
#
# Except for the formatting, the ANOVA data table created from this script is 
# the same as the ANOVA table in the spreadsheet, which was created using 
# GoStats.
#
# Some additional code is added to do Sheffe post hoc comparisons on the trial 
# data.  Since there are 5 trials, 10 comparisons are done.

# Reminder: before running this script in RStudio, set the working directory
# to the location of the source file and data file:
#
#    Session --> Set Working Directory --> To Source File Location

# fresh start
cat("\f")                       # clear console
rm(list = ls(all.names = TRUE)) # clear environment

# read data from file (adjust path as per local computer)
data1 = read.delim("EntrySpeed.txt", header=FALSE, sep="\t",  skip=8, na="", 
   col.names = c("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "Group"))

# show the data
data1

# experiment design
nP = nrow(data1); # n = number of participants
nF1 = 2 # 2 levels of 'layout'
nF2 = 5 # 5 levels of 'trial'
nF3 = 2 # 2 groups (for counterbalancing)

# reorganize data into "long" format
data2 = reshape(data1[1:10], direction="long", 
   varying=c("c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"), 
   v.names="Entry_Speed", timevar = "Condition", idvar="Participant")

# add column for levels of within-subjects factor, layout
data2$Layout = rep(c("Opti(A)", "Qwerty(B)"), each=nF2*nP)

# add column for levels of within-subjects factor, trial
data2$Trial = rep(c("T1","T2","T3","T4","T5") , each=nP, times=nF1)

# add column for group (alternating row by row)
data2$Group = rep(c("G1", "G2"), times=(nP/2)*nF1*nF2)

# show the data (long format)
data2

# need this library when using anova_test and get_anova_table functions
library(rstatix)

# voila! (do the anova)
res.aov = anova_test(
   data = data2, 
   formula = Entry_Speed ~ Group*Layout*Trial + Error(Participant/(Layout*Trial)),
   detailed=TRUE
)

# get the ANOVA result as a table
tbl = get_anova_table(res.aov) 

# show the table
tbl

# --- post hoc comparisons for 'trial'  ---

# need the DescTools library to use PostHocTest function
library(DescTools)

# create a simplified model first using aov function
model = aov(Entry_Speed~Layout+Trial, data=data2)
summary(model)

# do post hoc comparisons
PostHocTest(model, which=NULL, "scheffe")

# *** end ***