library(tidyverse)
library(magrittr)
library(viridis)

library(irr) # Interrater Reliability and Agreement


# Set the working directory to the source location
# https://stackoverflow.com/a/35842119
#setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#Create a variable called behavdata and read into it, the data from the file allresfilter_3words_split.out.

#behavdata <- read_delim("allres_filtersplit.out", delim = " ",
#                        col_names = c("SNo", "Block", "Trial", "Item", "Condition", "Sentence", "ST", "CT", "GD", "RT", "Acceptability", "Answer", "Type"),
#                        trim_ws = TRUE) # Separator one-or-more-spaces with trim_ws in read_delim does not seem to work in newer versions of tidyverse!!!
#                                        # Use read_table instead: https://stackoverflow.com/a/52052132
#           

behavdata <- read_table("allres_filtersplit.out",
                        col_names = c("SNo", "Block", "Trial", "Item", "Condition", "Sentence", "ST", "CT", "GD", "RT", "Acceptability", "Answer", "Type"))

#The new versions of R do not take a purely numeric input column as a factor,
#unless it is EXPLICITLY DECLARED to be taken as a factor.  Since the 'Item' column in our input files
#is a purely numeric one, this has to be explicity declared to be taken as a factor, 
#as follows: - (Thanks to Petra for pointing this out).
behavdata$Item <- as.factor(behavdata$Item) 


# Example row from allres_filtersplit.out
# VA01 1   1   20    SAF   20SAF     S   ACP F  1.192 2 2 AGRTarget
# SNo  Bl  Tr  Item  Cond  Sentence  ST  CT  GD RT    Acceptability   Accuracy  Type => AGReement Target (i.e., not Verb-initial / Resumpro / Filler).

#behavdata_Correct <- behavdata[behavdata$F1=="2",]

b_t   <- behavdata[behavdata$Type=="AGRTarget",]  # Only AGRTagert sentences.
b_tc  <- b_t[b_t$Answer=="2",]    # Correctly answered targets;


###########
print("Itemwise Acceptability IRR")
## [1] "Itemwise Acceptability IRR"
###########
b_tc %>% 
  select(SNo, Condition, Sentence, Acceptability) %>%
  arrange(SNo, Condition, Sentence) %>%
  mutate(Acceptability = if_else(Acceptability == 1, "NOT", "ACC")) %>%
  pivot_wider(names_from = SNo, values_from = Acceptability) -> b_tc_itemwise_irr_categorical

# Due to the latin-square distribution, although all participants saw all conditions,
# the items were distributed such that not all participants saw all items,
# but groups of participants saw each set of items

print("Participants who saw Session List Itemset 1")
## [1] "Participants who saw Session List Itemset 1"
b_tc_itemwise_irr_categorical %>% 
  select(-Condition, -Sentence) %>%
  select(VA01, VA11, VA22, VA29, VA31, VA36) %>% 
  kappam.fleiss(exact = F, detail = F) -> K1

print(K1)