Load csv and packages
Create function for nback and loop through participants
shift <- function(x, n){
c(x[-(seq(n))], rep(NA, n))
}
#loop through each participant and shift variables back
for (i in unique(WPT2$Participant)) {
WPT2$OutcomeN1[WPT2$Participant==i] <- shift(WPT2$OutcomeN1[WPT2$Participant==i], 1) #1 back outcome
WPT2$OutcomeN2[WPT2$Participant==i] <- shift(WPT2$OutcomeN2[WPT2$Participant==i], 2) #2 back outcome
WPT2$OutcomeN3[WPT2$Participant==i] <- shift(WPT2$OutcomeN3[WPT2$Participant==i], 3) #3 back outcome
WPT2$ChoiceN1[WPT2$Participant==i] <- shift(WPT2$ChoiceN1[WPT2$Participant==i], 1) #1 back choice
WPT2$ChoiceN2[WPT2$Participant==i] <- shift(WPT2$ChoiceN2[WPT2$Participant==i], 2) #1 back choice
WPT2$ChoiceN3[WPT2$Participant==i] <- shift(WPT2$ChoiceN3[WPT2$Participant==i], 3) #1 back choice
}
Remove first trials and effects code condition
#remove the first trial as no Nback.
WPT2 <- WPT2[-which(WPT2$Trial ==1),]
#effects code condition
WPT2$Condition_eff <- factor(WPT2$Condition,
levels = c("steal", "steal_clouds", "weather_faces", "weather"))
contrasts(WPT2$Condition_eff) <- contr.sum(4)
colnames(contrasts(WPT2$Condition_eff)) = c("steal", "steal_clouds", "weather_faces")
#remove NAs for likelihood ratio tests
WPT2 <- WPT2[!is.na(WPT2$ChoiceN1),]
WPT2 <- WPT2[!is.na(WPT2$ChoiceN2),]
WPT2 <- WPT2[!is.na(WPT2$ChoiceN3),]
logistic regressions
#predicting choice
M2 <- glmer(Choice_numeric~Condition_eff+ChoiceN1+OutcomeN1 +ChoiceN2+OutcomeN2+ ChoiceN3+OutcomeN3+ (1|Participant), data = WPT2, family = "binomial")
summary(M2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula: Choice_numeric ~ Condition_eff + ChoiceN1 + OutcomeN1 + ChoiceN2 +
## OutcomeN2 + ChoiceN3 + OutcomeN3 + (1 | Participant)
## Data: WPT2
##
## AIC BIC logLik deviance df.resid
## 104434.2 104535.8 -52206.1 104412.2 75805
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.9472 -1.0511 0.8195 0.9353 1.2184
##
## Random effects:
## Groups Name Variance Std.Dev.
## Participant (Intercept) 0.03776 0.1943
## Number of obs: 75816, groups: Participant, 393
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.150574 0.019742 7.627 2.4e-14 ***
## Condition_effsteal 0.006483 0.021211 0.306 0.75989
## Condition_effsteal_clouds -0.045684 0.021500 -2.125 0.03360 *
## Condition_effweather_faces 0.056050 0.020885 2.684 0.00728 **
## ChoiceN1 -0.026241 0.018058 -1.453 0.14618
## OutcomeN1 -0.014603 0.017914 -0.815 0.41497
## ChoiceN2 0.032967 0.018072 1.824 0.06811 .
## OutcomeN2 -0.027221 0.017916 -1.519 0.12866
## ChoiceN3 0.068470 0.018058 3.792 0.00015 ***
## OutcomeN3 -0.053666 0.017895 -2.999 0.00271 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) Cndtn_ Cndtn_ffs_ Cndtn_ffw_ ChocN1 OtcmN1 ChocN2 OtcmN2
## Cndtn_ffstl 0.001
## Cndtn_ffst_ 0.006 -0.343
## Cndtn_ffwt_ -0.008 -0.323 -0.333
## ChoiceN1 -0.231 -0.001 0.009 -0.012
## OutcomeN1 -0.188 0.001 -0.003 0.006 -0.572
## ChoiceN2 -0.221 -0.001 0.009 -0.011 0.043 -0.050
## OutcomeN2 -0.202 0.001 -0.003 0.006 -0.021 0.033 -0.572
## ChoiceN3 -0.206 -0.001 0.008 -0.011 0.022 -0.031 0.044 -0.050
## OutcomeN3 -0.214 0.001 -0.002 0.005 -0.009 0.016 -0.020 0.033
## ChocN3
## Cndtn_ffstl
## Cndtn_ffst_
## Cndtn_ffwt_
## ChoiceN1
## OutcomeN1
## ChoiceN2
## OutcomeN2
## ChoiceN3
## OutcomeN3 -0.571
#predicting choice interacting with condition.
M3 <- glmer(Choice_numeric~Condition_eff*ChoiceN1+OutcomeN1 +Condition_eff*ChoiceN2+OutcomeN2+ Condition_eff*ChoiceN3+OutcomeN3+ (1|Participant), data = WPT2, family = "binomial")
summary(M3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula:
## Choice_numeric ~ Condition_eff * ChoiceN1 + OutcomeN1 + Condition_eff *
## ChoiceN2 + OutcomeN2 + Condition_eff * ChoiceN3 + OutcomeN3 +
## (1 | Participant)
## Data: WPT2
##
## AIC BIC logLik deviance df.resid
## 104428.3 104613.1 -52194.2 104388.3 75796
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.9691 -1.0496 0.8190 0.9345 1.2374
##
## Random effects:
## Groups Name Variance Std.Dev.
## Participant (Intercept) 0.03749 0.1936
## Number of obs: 75816, groups: Participant, 393
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.15245 0.01974 7.722 1.14e-14 ***
## Condition_effsteal 0.09437 0.03211 2.939 0.003294 **
## Condition_effsteal_clouds -0.07223 0.03186 -2.267 0.023377 *
## Condition_effweather_faces 0.00168 0.03153 0.053 0.957497
## ChoiceN1 -0.02470 0.01808 -1.366 0.171824
## OutcomeN1 -0.01771 0.01795 -0.987 0.323689
## ChoiceN2 0.03156 0.01809 1.744 0.081131 .
## OutcomeN2 -0.02726 0.01795 -1.519 0.128854
## ChoiceN3 0.06835 0.01808 3.780 0.000157 ***
## OutcomeN3 -0.05416 0.01793 -3.021 0.002523 **
## Condition_effsteal:ChoiceN1 -0.10657 0.02559 -4.164 3.13e-05 ***
## Condition_effsteal_clouds:ChoiceN1 0.05658 0.02582 2.192 0.028413 *
## Condition_effweather_faces:ChoiceN1 0.05684 0.02524 2.252 0.024326 *
## Condition_effsteal:ChoiceN2 -0.01896 0.02559 -0.741 0.458795
## Condition_effsteal_clouds:ChoiceN2 -0.00493 0.02582 -0.191 0.848555
## Condition_effweather_faces:ChoiceN2 0.01351 0.02524 0.535 0.592562
## Condition_effsteal:ChoiceN3 -0.03809 0.02558 -1.489 0.136475
## Condition_effsteal_clouds:ChoiceN3 -0.00081 0.02582 -0.031 0.974970
## Condition_effweather_faces:ChoiceN3 0.02919 0.02523 1.157 0.247249
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation matrix not shown by default, as p = 19 > 12.
## Use print(x, correlation=TRUE) or
## vcov(x) if you need it
#predicting accuracy
M1 <- glmer(acc~Condition_eff+ChoiceN1+OutcomeN1 +ChoiceN2+OutcomeN2+ ChoiceN3+OutcomeN3+ (1|Participant), data = WPT2, family = "binomial")
summary(M1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula: acc ~ Condition_eff + ChoiceN1 + OutcomeN1 + ChoiceN2 + OutcomeN2 +
## ChoiceN3 + OutcomeN3 + (1 | Participant)
## Data: WPT2
##
## AIC BIC logLik deviance df.resid
## 74198.4 74300.0 -37088.2 74176.4 75805
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -5.0426 0.2958 0.4066 0.5329 1.1159
##
## Random effects:
## Groups Name Variance Std.Dev.
## Participant (Intercept) 0.4757 0.6897
## Number of obs: 75816, groups: Participant, 393
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.41228 0.04126 34.226 < 2e-16 ***
## Condition_effsteal -0.20472 0.06253 -3.274 0.00106 **
## Condition_effsteal_clouds 0.09340 0.06360 1.468 0.14197
## Condition_effweather_faces -0.04326 0.06163 -0.702 0.48269
## ChoiceN1 -0.03199 0.02133 -1.500 0.13372
## OutcomeN1 0.02530 0.02119 1.194 0.23260
## ChoiceN2 -0.01482 0.02136 -0.694 0.48789
## OutcomeN2 0.02869 0.02119 1.354 0.17580
## ChoiceN3 -0.01529 0.02135 -0.716 0.47382
## OutcomeN3 0.01669 0.02116 0.789 0.43023
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) Cndtn_ Cndtn_ffs_ Cndtn_ffw_ ChocN1 OtcmN1 ChocN2 OtcmN2
## Cndtn_ffstl -0.003
## Cndtn_ffst_ 0.020 -0.342
## Cndtn_ffwt_ -0.023 -0.321 -0.333
## ChoiceN1 -0.153 0.000 0.004 -0.005
## OutcomeN1 -0.126 0.000 -0.001 0.002 -0.490
## ChoiceN2 -0.145 -0.001 0.004 -0.005 0.041 -0.058
## OutcomeN2 -0.135 0.000 -0.001 0.002 -0.017 0.035 -0.491
## ChoiceN3 -0.134 -0.001 0.003 -0.004 0.016 -0.033 0.042 -0.058
## OutcomeN3 -0.145 0.000 -0.001 0.001 -0.006 0.015 -0.017 0.034
## ChocN3
## Cndtn_ffstl
## Cndtn_ffst_
## Cndtn_ffwt_
## ChoiceN1
## OutcomeN1
## ChoiceN2
## OutcomeN2
## ChoiceN3
## OutcomeN3 -0.490