## Italy WS-TD Inequity Game

library(readr)
WS_IA_data <- read.csv("Inequity_Game_Trial_by_Trial.csv")

# install packages: lme4, reshape2, binom, multcomp, optimx, data.table, nlme
library(nlme)

library(lme4)
library(reshape2)
library(binom)
library(multcomp)
library(optimx)
library(data.table)
library(plyr)

is.numeric(WS_IA_data$Trial_Num)

WS_IA_data$ID <- as.factor(WS_IA_data$ID)
WS_IA_data$Order <- as.factor(WS_IA_data$Order)
WS_IA_data$Group <- as.factor(WS_IA_data$Group)
WS_IA_data$Condition <- as.factor(WS_IA_data$Condition)
WS_IA_data$Distribution <- as.factor(WS_IA_data$Distribution)
WS_IA_data$Decision <- as.factor(WS_IA_data$Decision)

is.factor(WS_IA_data$ID)

ws.0 <- glmer (Decision ~ 1 + (1|ID), data = WS_IA_data, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))

ws.1 <- glmer (Decision ~ (Distribution + Condition + Group)^3 + (1|ID), data = WS_IA_data, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.0,ws.1)
# better fit with ws.1
summary(ws.1)
# distribution x condition interaction so subset by condition

## Figure to show 3-way interaction

IG_count <- read.csv("IG_count.csv")
IG_count$Group <- as.factor(IG_count$Group)
IG_count$Condition <- as.factor(IG_count$Condition)
IG_count$Distribution <- as.factor(IG_count$Distribution)

levels(IG_count$Distribution)
IG_count$Distribution <- relevel(IG_count$Distribution, ref = "E")

# boxplox
jmv::descriptives(
  formula = Rejections ~ Group:Condition:Distribution,
  data = IG_count,
  freq = TRUE,
  box = TRUE,
  dot = TRUE,
  dotType = "stack",
  boxLabelOutliers = FALSE)



WS_Adv <- subset(WS_IA_data, Condition=="Adv")
WS_Disadv <- subset(WS_IA_data, Condition=="Disadv")

## Disadvantageous models
ws.disadv.0 <- glmer (Decision ~ 1 + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))

ws.disadv.1 <- glmer (Decision ~ Distribution * Group + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.disadv.0,ws.disadv.1)
# some improvement in fit over null, p = .073

summary(ws.disadv.1)
# only distribution sig, interaction ns

# test main effects model
ws.disadv.2 <- glmer (Decision ~ Distribution + Group + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.disadv.0,ws.disadv.2)
# improvement over null, p = .0322
summary(ws.disadv.2)
# no diff between groups; higher rejections of unequal than equal for all

## does adding other variables improve fit over model 2?
ws.disadv.3 <- glmer (Decision ~ Distribution + Group + Trial_Num + Order + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.disadv.2,ws.disadv.3)
# no improvement

## add MA, CA and IQ as covariates
ws.disadv.4 <- glmer (Decision ~ Distribution + Group + MA + CA + IQ + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.disadv.2,ws.disadv.4)
# no improvement

# Based on Reviewer 2 and given results for Adv below with MA, test interactions
ws.disadv.5a <- glmer (Decision ~ (Distribution + Group) * MA + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
summary(ws.disadv.5a)
# get MA effect with Group, trend for interaction when use CA, no effect for IQ
# no Distribution x Group effect

ws.disadv.5 <- glmer (Decision ~ Distribution + Group * MA + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.disadv.2,ws.disadv.5)
# improved fit over just main effects
summary(ws.disadv.5)

library(emmeans)
emtrends(ws.disadv.5, ~Group, var="MA")
# here see sig more rejections with MA for TD only (0.45, LCI = .11, UCI = .80)
# note that this collapses Distribution

# need 3-way interaction model to visualize
ws.disadv.6 <- glmer (Decision ~ (Distribution + Group + MA)^3 + (1|ID), data = WS_Disadv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
summary(ws.disadv.6)

library(ggplot2)
library(interactions)
library(jtools)
library(effects)
library(ggpubr)
library(scales)

interact_plot(ws.disadv.6, pred = "MA", 
              modx = "Distribution", 
              mod2 = "Group",
              interval=TRUE, 
              vary.lty = FALSE,
              colors="Qual1", 
              x.label="Mental Age (yrs)", 
              y.label="Probability of Rejecting",
              modx.labels = c("Equal","Unequal"),
              mod2.labels = c("TD","WS"),
              main.title = c("Disadvantageous Condition: Group x Mental Age")) +
  theme_apa(legend.pos="top") + 
  scale_x_continuous(breaks=seq(4,9,1), limits=c(4,9)) + 
  scale_y_continuous(breaks=seq(0,1,.25), limits=c(0,1)) + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  theme(panel.background = element_rect(fill = 'white', colour = 'black')) 

ggsave("IG_Disadv_Group_MA.tiff", dpi=300, height=4, width=5, units="in")


## Advantageous models
ws.adv.0 <- glmer (Decision ~ 1 + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))

ws.adv.1 <- glmer (Decision ~ Distribution * Group + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.0,ws.adv.1)
# improved fit over null, p = .0018

summary(ws.adv.1)
# only distribution sig at p = .054; interaction not sig

# test main effects model
ws.adv.2 <- glmer (Decision ~ Distribution + Group + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.0,ws.adv.2)
anova(ws.adv.1,ws.adv.2)
# improved fit over null, p < .0013; not improved over interaction model so go with simpler model (ws.adv.2)

summary(ws.adv.2)
# only distribution significant, higher rejections of equal

## does adding other variables improve fit over model 2?
ws.adv.3 <- glmer (Decision ~ Distribution + Group + Trial_Num + Order + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.3)
# no improvement

# add MA and IQ as covariates
ws.adv.4 <- glmer (Decision ~ Distribution + Group + MA + IQ + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.4)
# no improvement
# final model just main effects of distribution and group

## Given the higher rejections of equal trials, test if this effect increases over trials
ws.adv.5 <- glmer (Decision ~ (Distribution + Group + Trial_Num)^3 + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
ws.adv.5 <- glmer (Decision ~ (Distribution + Group) * Trial_Num + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
ws.adv.5 <- glmer (Decision ~ Group + Distribution * Trial_Num + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
ws.adv.5 <- glmer (Decision ~ Group + Distribution + Trial_Num + (1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.5)
summary(ws.adv.5)
## none improve fit over main effects model

## Reviewer questions about effects of MA and CA on rejections (also IQ)
ws.adv.5 <- glmer (Decision ~ Distribution + Group * MA +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.5)
summary(ws.adv.5)
# adding MA interaction with Group improves fit, p = .04; no effect for CA or IQ

# test interaction with Distribution
ws.adv.6 <- glmer (Decision ~ (Distribution + Group) * MA +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.6)
# no improvement
summary(ws.adv.6)
# same interaction wiht MA, none with Distribution

ws.adv.6 <- glmer (Decision ~ (Distribution + Group + MA)^3 +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.6)
# no improvement

# try CA
ws.adv.6 <- glmer (Decision ~ (Distribution + Group) * CA +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
anova(ws.adv.2,ws.adv.6)
# no improvement

# final model for Adv
ws.adv.5 <- glmer (Decision ~ Distribution + Group * MA +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
#ws.adv.5 <- glmer (Decision ~ Distribution + Group * MA + CA +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
summary(ws.adv.5)
# interaction p = .02; same when add CA to model as control
# do not get same when use IQ instead of MA

library(performance)
check_outliers(ws.adv.5, method=c("mahalanobis"))
# no outliers detected

library(emmeans)
emtrends(ws.adv.5, ~Group, var="MA")
# simple slopes show no diff for TD by MA (0.23, SE=.25; LCI = -.255, UCI = .73)
# but sig neg slope for WS (-.054, SE = .23, LCI=-.98, UCI = -.09)
## Conclusion - fewer rejections overall with increasing MA for WS
## this suggests that increased cognitive abilities, as opposed to life experience lead to more typical pattern for AI
## this holds when add CA to model as control variable

# to show both groups and distribution, used 3-way interaction model
ws.adv.6 <- glmer (Decision ~ (Distribution + Group + MA)^3 +(1|ID), data = WS_Adv, family = binomial, control = glmerControl(optimizer = "optimx", calc.derivs = FALSE,optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))

interact_plot(ws.adv.6, pred = "MA", 
              modx = "Distribution", 
              mod2 = "Group",
              interval=TRUE, 
              vary.lty = FALSE,
              colors="Qual1", 
              x.label="Mental Age (yrs)", 
              y.label="Probability of Rejecting",
              modx.labels = c("Equal","Unequal"),
              mod2.labels = c("TD","WS"),
              main.title = c("Advantageous Condition: Group x Mental Age")) +
  theme_apa(legend.pos="top") + 
  scale_x_continuous(breaks=seq(4,9,1), limits=c(4,9)) + 
  scale_y_continuous(breaks=seq(0,1,.25), limits=c(0,1)) + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  theme(panel.background = element_rect(fill = 'white', colour = 'black')) 

ggsave("IG_Adv_Group_MA.tiff", dpi=300, height=4, width=5, units="in")
