Load libraries

source("~/Projects/Other/Ranalysis/useful_dplyr.R")
library(tidyr)
library(magrittr)
library(lme4)
library(lmerTest)

Load data

data <- read.csv('sarah_honors_data.csv')

long.data <- gather(data,item,response,bugs:pen) %>%
  mutate(cond = factor(cond,levels=c("normal","implausible"))) %>%
  group_by(cond,item,subj_id) %>%
  rename(Literal = response) %>%
  mutate(Literal = 1 - Literal)

lm0 <-  glmer(Literal~ (1|subj_id) + (1|item),
             family="binomial",data=long.data)

lm.cond <- glmer(Literal~ cond + (1|subj_id) + (1|item),
             family="binomial",data=long.data)

anova(lm0,lm.cond)
## Data: long.data
## Models:
## lm0: Literal ~ (1 | subj_id) + (1 | item)
## lm.cond: Literal ~ cond + (1 | subj_id) + (1 | item)
##         Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)    
## lm0      3 390.12 401.17 -192.06   384.12                             
## lm.cond  4 380.18 394.92 -186.09   372.18 11.936      1  0.0005505 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lm.age.cond <- glmer(Literal~ cond + age + (1|subj_id) + (1|item),
             family="binomial",data=long.data)

anova(lm.cond,lm.age.cond)
## Data: long.data
## Models:
## lm.cond: Literal ~ cond + (1 | subj_id) + (1 | item)
## lm.age.cond: Literal ~ cond + age + (1 | subj_id) + (1 | item)
##             Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)
## lm.cond      4 380.18 394.92 -186.09   372.18                         
## lm.age.cond  5 380.53 398.94 -185.26   370.53 1.6547      1     0.1983
lm.gender.cond <- glmer(Literal~ cond + gender + (1|subj_id) + (1|item),
             family="binomial",data=long.data)

anova(lm.cond,lm.gender.cond)
## Data: long.data
## Models:
## lm.cond: Literal ~ cond + (1 | subj_id) + (1 | item)
## lm.gender.cond: Literal ~ cond + gender + (1 | subj_id) + (1 | item)
##                Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)
## lm.cond         4 380.18 394.92 -186.09   372.18                         
## lm.gender.cond  5 379.96 398.38 -184.98   369.96 2.2194      1     0.1363
lm.gender.cond.int <- glmer(Literal~ cond * gender + (1|subj_id) + (1|item),
             family="binomial",data=long.data)

anova(lm.cond,lm.gender.cond.int)
## Data: long.data
## Models:
## lm.cond: Literal ~ cond + (1 | subj_id) + (1 | item)
## lm.gender.cond.int: Literal ~ cond * gender + (1 | subj_id) + (1 | item)
##                    Df    AIC    BIC  logLik deviance  Chisq Chi Df
## lm.cond             4 380.18 394.92 -186.09   372.18              
## lm.gender.cond.int  6 375.19 397.29 -181.60   363.19 8.9887      2
##                    Pr(>Chisq)  
## lm.cond                        
## lm.gender.cond.int    0.01117 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
long.data$predicted <- inv.logit(predict(lm.cond))

ms <- long.data %>%
  group_by(cond) %>%
  summarise_each(funs(mean,ci.high,ci.low),Literal) %>%
  mutate(type = "data")

predicted <- long.data %>%
  group_by(cond) %>%
  summarise_each(funs(mean,ci.high,ci.low),predicted) %>%
  mutate(type = "model") %>%
  bind_rows(ms)

Plot

#Plot alone
ggplot(ms, aes(x=cond, y=mean,fill=type)) +
  geom_bar(stat="identity",position=position_dodge(1))+
  geom_linerange(aes(ymin = mean-ci.low,
                      ymax = mean+ci.high),
                  size = .8,
                  show_guide = FALSE,
                 position=position_dodge(1)) +
  scale_fill_brewer(palette="Set1") +
  theme_bw(base_size=14) +
  theme(legend.position="none", panel.grid=element_blank()) +
  scale_x_discrete(name = "Condition")+
  scale_y_continuous(name = "Proportion Literal Choices",
                     limits=c(0,.8))

#Plot with model
ggplot(predicted, aes(x=cond, y=mean,fill=type)) +
  geom_bar(stat="identity",position=position_dodge(1))+
  geom_linerange(aes(ymin = mean-ci.low,
                      ymax = mean+ci.high),
                  size = .8,
                  show_guide = FALSE,
                 position=position_dodge(1)) +
  scale_fill_brewer(palette="Set1") +
  theme_bw(base_size=14) +
  theme(legend.position=c(.2,.8), panel.grid=element_blank()) +
  scale_x_discrete(name = "Condition")+
  scale_y_continuous(name = "Proportion Literal Choices",
                     limits=c(0,.8))