library(glmmTMB)
library(emmeans)
library(performance)
library(DHARMa)

setwd("...")
xdata=read.table(file="data.txt", header=T, sep="\t")
str(xdata)
hist(xdata$response)

# FULL-NULL
res=glmmTMB(response ~ as.factor(condition)*trials +
	side.chosen + (1|subject), family="binomial", data=xdata)
null=glmmTMB(response ~ 
	side.chosen + (1|subject), family="binomial", data=xdata)
anova(null, res, test="Chisq")

drop1(res, test="Chisq")
res=glmmTMB(response ~ as.factor(condition) + trials +
	side.chosen + (1|subject), , family="binomial", data=xdata)
drop1(res, test="Chisq")

summary(res)
confint(res)

# POST HOCS 
emmeans(res, pairwise ~ as.factor(condition), type = 'response')

# CHECKS
check_collinearity(res)
simulateResiduals(res, n = 100000, refit = F, integerResponse = NULL, plot = T, 
	seed = 123, method = c("PIT", "traditional")) 
