#install the packages readxl and SingleCaseES
install.packages("readxl")
install.packages("SingleCaseES")

#load the packages readxl and SingleCaseES
library(readxl)
library(SingleCaseES)

#import the excel file. 
#Assumption 1: Different participants have a unique identifier under a variable called ID
#Assumption 2: What phase the measurement is part of is in a variable called Phase
#Assumption 3: The outcomes are under variables called ARAT, BBT, ABILHAND, KinTMT, KinNMU, KinTD
#Assumption 4: The excel (.xlsx) file is in long format, it is called Additional file 2, and it's within R's working directory
#Assumption 5: There are no outcomes for any individual with no data in the excel file, the package SingleCaseES does not like that. Fill in completely blank outcomes with zeroes in the excel file and ignore the result for them.
Data <- read_excel("Additional file 2.xlsx")

#Run the statistical test Tau-U and store the respective results in data tables
Results_ARAT      <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = ARAT, 
                                   improvement = "increase",
                                   ES = "Tau-U")
Results_BBT       <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = BBT, 
                                   improvement = "increase",
                                   ES = "Tau-U")
Results_ABILHAND  <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = ABILHAND,
                                   improvement = "increase",
                                   ES = "Tau-U")
Results_KinTMT    <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = KinTMT,
                                   improvement = "decrease",
                                   ES = "Tau-U")
Results_KinNMU    <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = KinNMU,
                                   improvement = "decrease",
                                   ES = "Tau-U")
Results_KinTD    <- batch_calc_ES(dat = Data, 
                                   grouping = ID, 
                                   condition = Phase, 
                                   outcome = KinTD,
                                   improvement = "decrease",
                                   ES = "Tau-U")

#Combine the results into a single data table
Results <- cbind(Results_ARAT[,], 
                 Results_BBT[,3], 
                 Results_ABILHAND[,3],
                 Results_KinTMT[,3],
                 Results_KinNMU[,3],
                 Results_KinTD[,3])

#Rename the variables more appropriate names
colnames(Results) <- c("ID", 
                       "Tau-U index", 
                       "ARAT", 
                       "BBT", 
                       "ABILHAND",
                       "KinTMT",
                       "KinNMU",
                       "KinTD")

#Display the results
print(Results)

#Export the results as a .csv file in the working directory
write.csv(Results, 
          "Results.csv")

