# Dombrovski*, Luna, Hallquist*, Nature Communications, 2020
# brain-to-behavior analyses with anterior (low entropy) hippocampal cluster betas
# first run beta_cluster_import_pca_clean.R if not run once already

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.0.5     ✓ dplyr   1.0.3
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(lmerTest)
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(car)
## Loading required package: carData
## Registered S3 methods overwritten by 'car':
##   method                          from
##   influence.merMod                lme4
##   cooks.distance.influence.merMod lme4
##   dfbeta.influence.merMod         lme4
##   dfbetas.influence.merMod        lme4
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(emmeans)

# clock_folder <- "~/Data_Analysis/clock_analysis" #michael
clock_folder <- "~/code/clock_analysis_mlm" #alex

setwd(file.path(clock_folder, 'fmri/keuka_brain_behavior_analyses/'))

### load data
# load('trial_df_and_vhdkfpe_clusters.Rdata')
# cleaner version with only H, PE and uncertainty trial vars
unsmoothed = F
if (unsmoothed) {
  load('trial_df_and_vh_pe_clusters_u_unsmoothed.Rdata')
} else { load('trial_df_and_vh_pe_clusters_u.Rdata') }

# inspect behavioral data
# make sure no one always responds immediately
# raw, all subjects and trials
ggplot(df, aes(trial, rt_csv, color = rewFunc)) + geom_line() + facet_wrap(~id)

# smoothed
ggplot(df, aes(run_trial, rt_csv, color = rewFunc)) + geom_smooth(method = "gam") + facet_wrap(~id)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

# inspect AH betas
ggplot(df, aes(h_HippAntL_neg)) + geom_histogram() + xlab("Anterior hippocampal response to low entropy")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# how do we model RT swings? ----
# naive definition: abs(RT - RT_lag)
hist(df$rt_swing)

# ugly, 0-inflated distribution
# We will model RT swings as the [negative] slope of RT_lag, RT ~ RT_lag

## Simple model with condition ---- 
# trial_neg_inv_sc: scale(-1/trial) to better describe the learning curve
model_condition <-  lmer(rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rewFunc + last_outcome + 
                       (1|id/run), df %>% filter(!is.na(rt_vmax_lag_sc)))
summary(model_condition)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rewFunc + last_outcome +  
##     (1 | id/run)
##    Data: df %>% filter(!is.na(rt_vmax_lag_sc))
## 
## REML criterion at convergence: 63227.7
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.0793 -0.5752 -0.0668  0.5492  4.4419 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  run:id   (Intercept) 0.06851  0.2618  
##  id       (Intercept) 0.06866  0.2620  
##  Residual             0.56890  0.7543  
## Number of obs: 27253, groups:  run:id, 560; id, 70
## 
## Fixed effects:
##                        Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)           5.537e-02  4.627e-02  2.181e+02   1.197   0.2327    
## trial_neg_inv_sc      4.420e-02  8.618e-03  2.671e+04   5.128 2.94e-07 ***
## rt_lag_sc             3.342e-01  5.862e-03  2.695e+04  57.008  < 2e-16 ***
## rewFuncCEVR           1.229e-01  4.803e-02  4.612e+02   2.558   0.0108 *  
## rewFuncDEV           -2.022e-01  3.913e-02  4.572e+02  -5.168 3.54e-07 ***
## rewFuncIEV            2.182e-01  3.915e-02  4.581e+02   5.575 4.24e-08 ***
## last_outcomeOmission -1.835e-01  1.038e-02  2.687e+04 -17.680  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) trl___ rt_lg_ rFCEVR rwFDEV rwFIEV
## trl_ng_nv_s -0.021                                   
## rt_lag_sc    0.024 -0.104                            
## rewFuncCEVR -0.509  0.003  0.009                     
## rewFuncDEV  -0.630 -0.008  0.049  0.611              
## rewFuncIEV  -0.635  0.003 -0.057  0.606  0.744       
## lst_tcmOmss -0.081 -0.017 -0.233 -0.080 -0.022  0.038
## With RT_Vmax ----
# RT_Vmax is the best response time according to the SCEPTIC model
# its effect reflects exploitation (convergence on the best)
model_RT_Vmax <-  lmer(rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
              (1|id/run), df %>% filter(!is.na(rt_vmax_lag_sc)))
summary(model_RT_Vmax)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: 
## rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome +  
##     (1 | id/run)
##    Data: df %>% filter(!is.na(rt_vmax_lag_sc))
## 
## REML criterion at convergence: 62993.7
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.9792 -0.5687 -0.0687  0.5441  4.4313 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  run:id   (Intercept) 0.06970  0.2640  
##  id       (Intercept) 0.04433  0.2106  
##  Residual             0.56443  0.7513  
## Number of obs: 27253, groups:  run:id, 560; id, 70
## 
## Fixed effects:
##                        Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)           8.111e-02  2.820e-02  7.017e+01   2.876  0.00533 ** 
## trial_neg_inv_sc      4.084e-02  8.583e-03  2.665e+04   4.759 1.96e-06 ***
## rt_lag_sc             3.024e-01  6.138e-03  2.721e+04  49.268  < 2e-16 ***
## rt_vmax_lag_sc        1.458e-01  6.752e-03  2.373e+04  21.599  < 2e-16 ***
## last_outcomeOmission -1.931e-01  1.023e-02  2.713e+04 -18.868  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) trl___ rt_lg_ rt_v__
## trl_ng_nv_s -0.037                     
## rt_lag_sc    0.033 -0.095              
## rt_vmx_lg_s  0.001 -0.005 -0.340       
## lst_tcmOmss -0.140 -0.018 -0.209 -0.003
# naturally, including RT V-max instead of condition improves the fit by 300 AIC points (not a true model comparison, not nested)
anova(model_RT_Vmax, model_condition)
## refitting model(s) with ML (instead of REML)
## Data: df %>% filter(!is.na(rt_vmax_lag_sc))
## Models:
## model_RT_Vmax: rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
## model_RT_Vmax:     (1 | id/run)
## model_condition: rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rewFunc + last_outcome + 
## model_condition:     (1 | id/run)
##                 npar   AIC   BIC logLik deviance Chisq Df Pr(>Chisq)
## model_RT_Vmax      8 62973 63038 -31478    62957                    
## model_condition   10 63204 63286 -31592    63184     0  2          1
## add 2-way interactions ----
model_RT_Vmax_interactions <-  lmer(rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome)^2 + 
              (1|id/run), df %>% filter(!is.na(rt_vmax_lag_sc)))
summary(model_RT_Vmax_interactions)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc +  
##     last_outcome)^2 + (1 | id/run)
##    Data: df %>% filter(!is.na(rt_vmax_lag_sc))
## 
## REML criterion at convergence: 61697.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.4550 -0.5502 -0.0760  0.5119  4.5605 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  run:id   (Intercept) 0.05764  0.2401  
##  id       (Intercept) 0.04027  0.2007  
##  Residual             0.53871  0.7340  
## Number of obs: 27253, groups:  run:id, 560; id, 70
## 
## Fixed effects:
##                                         Estimate Std. Error         df t value
## (Intercept)                            1.077e-01  2.689e-02  7.205e+01   4.004
## trial_neg_inv_sc                       9.928e-03  1.149e-02  2.668e+04   0.864
## rt_lag_sc                              4.795e-01  8.039e-03  2.722e+04  59.646
## rt_vmax_lag_sc                         1.240e-01  8.067e-03  2.564e+04  15.368
## last_outcomeOmission                  -1.545e-01  1.037e-02  2.703e+04 -14.898
## trial_neg_inv_sc:rt_lag_sc            -6.981e-02  1.032e-02  2.676e+04  -6.762
## trial_neg_inv_sc:rt_vmax_lag_sc        7.185e-02  9.176e-03  2.718e+04   7.831
## trial_neg_inv_sc:last_outcomeOmission  1.203e-02  1.823e-02  2.668e+04   0.660
## rt_lag_sc:rt_vmax_lag_sc               1.907e-03  4.941e-03  2.668e+04   0.386
## rt_lag_sc:last_outcomeOmission        -3.760e-01  1.150e-02  2.706e+04 -32.688
## rt_vmax_lag_sc:last_outcomeOmission    7.090e-02  1.166e-02  2.703e+04   6.083
##                                       Pr(>|t|)    
## (Intercept)                           0.000149 ***
## trial_neg_inv_sc                      0.387494    
## rt_lag_sc                              < 2e-16 ***
## rt_vmax_lag_sc                         < 2e-16 ***
## last_outcomeOmission                   < 2e-16 ***
## trial_neg_inv_sc:rt_lag_sc            1.39e-11 ***
## trial_neg_inv_sc:rt_vmax_lag_sc       5.03e-15 ***
## trial_neg_inv_sc:last_outcomeOmission 0.509447    
## rt_lag_sc:rt_vmax_lag_sc              0.699483    
## rt_lag_sc:last_outcomeOmission         < 2e-16 ***
## rt_vmax_lag_sc:last_outcomeOmission   1.20e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) trl___ rt_lg_ rt_v__ lst_tO tr___:__ t___:___ t___:_O r__:__
## trl_ng_nv_s -0.053                                                             
## rt_lag_sc    0.059 -0.137                                                      
## rt_vmx_lg_s -0.003 -0.044 -0.391                                               
## lst_tcmOmss -0.144  0.131 -0.108 -0.010                                        
## trl_ng__:__ -0.037  0.420 -0.136 -0.060  0.032                                 
## trl_n__:___  0.013 -0.005 -0.002 -0.018  0.021 -0.260                          
## trl_ng__:_O  0.036 -0.604  0.084  0.027 -0.247 -0.187   -0.059                 
## rt_lg_s:___ -0.099 -0.061 -0.140  0.047 -0.003  0.007   -0.130    0.003        
## rt_lg_sc:_O -0.030  0.039 -0.642  0.310 -0.074 -0.014    0.004   -0.077   0.071
## rt_vmx__:_O  0.027  0.005  0.361 -0.568  0.000  0.008   -0.011    0.029  -0.233
##             r__:_O
## trl_ng_nv_s       
## rt_lag_sc         
## rt_vmx_lg_s       
## lst_tcmOmss       
## trl_ng__:__       
## trl_n__:___       
## trl_ng__:_O       
## rt_lg_s:___       
## rt_lg_sc:_O       
## rt_vmx__:_O -0.555
# the interaction that helps the most is rt_lag * last_outcome
# win-stay/lose-shift
anova(model_RT_Vmax, model_RT_Vmax_interactions)
## refitting model(s) with ML (instead of REML)
## Data: df %>% filter(!is.na(rt_vmax_lag_sc))
## Models:
## model_RT_Vmax: rt_csv_sc ~ trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
## model_RT_Vmax:     (1 | id/run)
## model_RT_Vmax_interactions: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + 
## model_RT_Vmax_interactions:     last_outcome)^2 + (1 | id/run)
##                            npar   AIC   BIC logLik deviance  Chisq Df
## model_RT_Vmax                 8 62973 63038 -31478    62957          
## model_RT_Vmax_interactions   14 61644 61759 -30808    61616 1340.9  6
##                            Pr(>Chisq)    
## model_RT_Vmax                            
## model_RT_Vmax_interactions  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
###############
# Main analyses including model-derived behavioral variables 
# hippocampal model-based analysis
model_AH_beta_fMRI <-  lmer(rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
                                    v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 + 
                       rt_lag_sc:last_outcome:h_HippAntL_neg + 
                       rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg + 
                       (1|id/run), df)
summary(model_AH_beta_fMRI)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc +  
##     last_outcome + v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 +  
##     rt_lag_sc:last_outcome:h_HippAntL_neg + rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg +  
##     (1 | id/run)
##    Data: df
## 
## REML criterion at convergence: 61513.7
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.5985 -0.5438 -0.0677  0.5055  4.6481 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  run:id   (Intercept) 0.05548  0.2355  
##  id       (Intercept) 0.04396  0.2097  
##  Residual             0.53299  0.7301  
## Number of obs: 27253, groups:  run:id, 560; id, 70
## 
## Fixed effects:
##                                                  Estimate Std. Error         df
## (Intercept)                                     1.045e-01  3.230e-02  7.056e+01
## trial_neg_inv_sc                                9.945e-03  1.272e-02  2.667e+04
## rt_lag_sc                                       4.589e-01  9.596e-03  2.721e+04
## rt_vmax_lag_sc                                  1.087e-01  9.746e-03  2.427e+04
## last_outcomeOmission                           -1.741e-01  1.210e-02  2.692e+04
## v_max_wi_lag                                    6.720e-03  6.902e-03  2.680e+04
## v_entropy_wi                                    2.165e-02  6.932e-03  2.673e+04
## h_HippAntL_neg                                  7.536e-03  1.394e-01  6.918e+01
## trial_neg_inv_sc:rt_lag_sc                     -7.907e-02  1.042e-02  2.676e+04
## trial_neg_inv_sc:rt_vmax_lag_sc                 7.129e-02  1.093e-02  2.716e+04
## trial_neg_inv_sc:last_outcomeOmission           1.411e-02  1.829e-02  2.668e+04
## trial_neg_inv_sc:v_max_wi_lag                   1.454e-02  6.590e-03  2.712e+04
## trial_neg_inv_sc:v_entropy_wi                  -6.476e-03  7.361e-03  2.718e+04
## trial_neg_inv_sc:h_HippAntL_neg                -1.484e-02  4.392e-02  2.691e+04
## rt_lag_sc:rt_vmax_lag_sc                        9.450e-04  5.141e-03  2.644e+04
## rt_lag_sc:last_outcomeOmission                 -3.886e-01  1.280e-02  2.703e+04
## rt_lag_sc:v_max_wi_lag                         -3.150e-02  6.123e-03  2.694e+04
## rt_lag_sc:v_entropy_wi                         -1.871e-02  6.114e-03  2.682e+04
## rt_lag_sc:h_HippAntL_neg                        8.869e-02  4.081e-02  2.689e+04
## rt_vmax_lag_sc:last_outcomeOmission             7.157e-02  1.181e-02  2.700e+04
## rt_vmax_lag_sc:v_max_wi_lag                     7.682e-02  5.928e-03  2.703e+04
## rt_vmax_lag_sc:v_entropy_wi                     7.577e-03  6.100e-03  2.701e+04
## rt_vmax_lag_sc:h_HippAntL_neg                   1.260e-01  3.809e-02  2.027e+04
## last_outcomeOmission:v_max_wi_lag              -1.892e-02  1.011e-02  2.680e+04
## last_outcomeOmission:v_entropy_wi              -3.191e-02  1.041e-02  2.677e+04
## last_outcomeOmission:h_HippAntL_neg             1.679e-01  5.137e-02  2.702e+04
## v_max_wi_lag:v_entropy_wi                       3.304e-03  4.710e-03  2.718e+04
## v_max_wi_lag:h_HippAntL_neg                    -4.427e-02  2.393e-02  2.665e+04
## v_entropy_wi:h_HippAntL_neg                     2.933e-02  2.462e-02  2.658e+04
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg   6.079e-02  4.723e-02  2.712e+04
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg  1.249e-01  4.529e-02  2.717e+04
##                                                t value Pr(>|t|)    
## (Intercept)                                      3.235 0.001851 ** 
## trial_neg_inv_sc                                 0.782 0.434250    
## rt_lag_sc                                       47.829  < 2e-16 ***
## rt_vmax_lag_sc                                  11.157  < 2e-16 ***
## last_outcomeOmission                           -14.395  < 2e-16 ***
## v_max_wi_lag                                     0.974 0.330301    
## v_entropy_wi                                     3.124 0.001789 ** 
## h_HippAntL_neg                                   0.054 0.957049    
## trial_neg_inv_sc:rt_lag_sc                      -7.587 3.38e-14 ***
## trial_neg_inv_sc:rt_vmax_lag_sc                  6.520 7.16e-11 ***
## trial_neg_inv_sc:last_outcomeOmission            0.771 0.440602    
## trial_neg_inv_sc:v_max_wi_lag                    2.207 0.027341 *  
## trial_neg_inv_sc:v_entropy_wi                   -0.880 0.379024    
## trial_neg_inv_sc:h_HippAntL_neg                 -0.338 0.735380    
## rt_lag_sc:rt_vmax_lag_sc                         0.184 0.854163    
## rt_lag_sc:last_outcomeOmission                 -30.352  < 2e-16 ***
## rt_lag_sc:v_max_wi_lag                          -5.146 2.68e-07 ***
## rt_lag_sc:v_entropy_wi                          -3.059 0.002220 ** 
## rt_lag_sc:h_HippAntL_neg                         2.174 0.029751 *  
## rt_vmax_lag_sc:last_outcomeOmission              6.060 1.38e-09 ***
## rt_vmax_lag_sc:v_max_wi_lag                     12.960  < 2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                      1.242 0.214182    
## rt_vmax_lag_sc:h_HippAntL_neg                    3.309 0.000939 ***
## last_outcomeOmission:v_max_wi_lag               -1.871 0.061332 .  
## last_outcomeOmission:v_entropy_wi               -3.066 0.002173 ** 
## last_outcomeOmission:h_HippAntL_neg              3.268 0.001085 ** 
## v_max_wi_lag:v_entropy_wi                        0.701 0.483088    
## v_max_wi_lag:h_HippAntL_neg                     -1.850 0.064300 .  
## v_entropy_wi:h_HippAntL_neg                      1.191 0.233562    
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg    1.287 0.198040    
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   2.758 0.005815 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation matrix not shown by default, as p = 31 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
Anova(model_AH_beta_fMRI, '3')
## Analysis of Deviance Table (Type III Wald chisquare tests)
## 
## Response: rt_csv_sc
##                                                    Chisq Df Pr(>Chisq)    
## (Intercept)                                      10.4672  1  0.0012151 ** 
## trial_neg_inv_sc                                  0.6114  1  0.4342432    
## rt_lag_sc                                      2287.6118  1  < 2.2e-16 ***
## rt_vmax_lag_sc                                  124.4858  1  < 2.2e-16 ***
## last_outcome                                    207.2120  1  < 2.2e-16 ***
## v_max_wi_lag                                      0.9478  1  0.3302922    
## v_entropy_wi                                      9.7564  1  0.0017870 ** 
## h_HippAntL_neg                                    0.0029  1  0.9568936    
## trial_neg_inv_sc:rt_lag_sc                       57.5633  1  3.273e-14 ***
## trial_neg_inv_sc:rt_vmax_lag_sc                  42.5095  1  7.034e-11 ***
## trial_neg_inv_sc:last_outcome                     0.5947  1  0.4405956    
## trial_neg_inv_sc:v_max_wi_lag                     4.8697  1  0.0273328 *  
## trial_neg_inv_sc:v_entropy_wi                     0.7739  1  0.3790166    
## trial_neg_inv_sc:h_HippAntL_neg                   0.1142  1  0.7353772    
## rt_lag_sc:rt_vmax_lag_sc                          0.0338  1  0.8541618    
## rt_lag_sc:last_outcome                          921.2556  1  < 2.2e-16 ***
## rt_lag_sc:v_max_wi_lag                           26.4778  1  2.666e-07 ***
## rt_lag_sc:v_entropy_wi                            9.3596  1  0.0022182 ** 
## rt_lag_sc:h_HippAntL_neg                          4.7241  1  0.0297424 *  
## rt_vmax_lag_sc:last_outcome                      36.7272  1  1.359e-09 ***
## rt_vmax_lag_sc:v_max_wi_lag                     167.9494  1  < 2.2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                       1.5430  1  0.2141709    
## rt_vmax_lag_sc:h_HippAntL_neg                    10.9477  1  0.0009372 ***
## last_outcome:v_max_wi_lag                         3.5013  1  0.0613212 .  
## last_outcome:v_entropy_wi                         9.3990  1  0.0021710 ** 
## last_outcome:h_HippAntL_neg                      10.6783  1  0.0010840 ** 
## v_max_wi_lag:v_entropy_wi                         0.4919  1  0.4830818    
## v_max_wi_lag:h_HippAntL_neg                       3.4231  1  0.0642889 .  
## v_entropy_wi:h_HippAntL_neg                       1.4191  1  0.2335510    
## rt_lag_sc:last_outcome:h_HippAntL_neg             1.6569  1  0.1980285    
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg    7.6079  1  0.0058113 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# include rt_lag and rt_vmax as random
model_AH_beta_fMRI_random <-  lmer(rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
                            v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 + 
               rt_lag_sc:last_outcome:h_HippAntL_neg + 
               rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg + 
               (rt_lag_sc + rt_vmax_lag_sc|id/run), df)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 0.0046599 (tol = 0.002, component 1)
# in case model no longer converges with more random effects,
# re-run in a loop until it does
while (any(grepl("failed to converge", model_AH_beta_fMRI_random@optinfo$conv$lme4$messages) )) {
  print(model_AH_beta_fMRI_random@optinfo$conv$lme4$conv)
  ss <- getME(model_AH_beta_fMRI_random,c("theta","fixef"))
  model_AH_beta_fMRI_random <- update(model_AH_beta_fMRI_random, start=ss)}
## NULL
summary(model_AH_beta_fMRI_random)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc +  
##     last_outcome + v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 +  
##     rt_lag_sc:last_outcome:h_HippAntL_neg + rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg +  
##     (rt_lag_sc + rt_vmax_lag_sc | id/run)
##    Data: df
## 
## REML criterion at convergence: 59926.9
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.6978 -0.5244 -0.0596  0.4711  4.7731 
## 
## Random effects:
##  Groups   Name           Variance Std.Dev. Corr       
##  run:id   (Intercept)    0.031689 0.17801             
##           rt_lag_sc      0.014588 0.12078  -0.02      
##           rt_vmax_lag_sc 0.006641 0.08149   0.07 -0.26
##  id       (Intercept)    0.035720 0.18900             
##           rt_lag_sc      0.050606 0.22496  -0.03      
##           rt_vmax_lag_sc 0.003225 0.05679   0.06 -0.05
##  Residual                0.493556 0.70254             
## Number of obs: 27253, groups:  run:id, 560; id, 70
## 
## Fixed effects:
##                                                  Estimate Std. Error         df
## (Intercept)                                     1.378e-01  2.904e-02  6.836e+01
## trial_neg_inv_sc                               -2.550e-03  1.254e-02  2.337e+04
## rt_lag_sc                                       4.991e-01  3.342e-02  7.364e+01
## rt_vmax_lag_sc                                  7.072e-02  1.408e-02  9.530e+01
## last_outcomeOmission                           -1.782e-01  1.172e-02  2.453e+04
## v_max_wi_lag                                    1.736e-02  6.786e-03  2.533e+04
## v_entropy_wi                                    2.715e-02  6.822e-03  2.574e+04
## h_HippAntL_neg                                 -7.762e-02  1.257e-01  6.781e+01
## trial_neg_inv_sc:rt_lag_sc                     -6.517e-02  1.030e-02  2.552e+04
## trial_neg_inv_sc:rt_vmax_lag_sc                 6.555e-02  1.086e-02  2.020e+04
## trial_neg_inv_sc:last_outcomeOmission           1.929e-02  1.782e-02  2.631e+04
## trial_neg_inv_sc:v_max_wi_lag                   8.437e-03  6.446e-03  2.594e+04
## trial_neg_inv_sc:v_entropy_wi                  -9.060e-03  7.203e-03  2.575e+04
## trial_neg_inv_sc:h_HippAntL_neg                 1.763e-02  4.365e-02  2.253e+04
## rt_lag_sc:rt_vmax_lag_sc                       -4.424e-02  5.982e-03  8.049e+03
## rt_lag_sc:last_outcomeOmission                 -3.896e-01  1.282e-02  2.198e+04
## rt_lag_sc:v_max_wi_lag                         -6.111e-03  6.094e-03  2.448e+04
## rt_lag_sc:v_entropy_wi                         -2.516e-02  6.050e-03  2.381e+04
## rt_lag_sc:h_HippAntL_neg                        1.876e-01  1.452e-01  7.399e+01
## rt_vmax_lag_sc:last_outcomeOmission             7.447e-02  1.172e-02  1.858e+04
## rt_vmax_lag_sc:v_max_wi_lag                     5.454e-02  5.920e-03  2.272e+04
## rt_vmax_lag_sc:v_entropy_wi                     2.041e-02  6.061e-03  2.283e+04
## rt_vmax_lag_sc:h_HippAntL_neg                   9.606e-02  5.823e-02  7.962e+01
## last_outcomeOmission:v_max_wi_lag              -2.396e-02  9.813e-03  2.665e+04
## last_outcomeOmission:v_entropy_wi              -3.684e-02  1.014e-02  2.654e+04
## last_outcomeOmission:h_HippAntL_neg             1.690e-01  4.968e-02  2.525e+04
## v_max_wi_lag:v_entropy_wi                       8.170e-03  4.590e-03  2.655e+04
## v_max_wi_lag:h_HippAntL_neg                    -3.677e-02  2.353e-02  2.396e+04
## v_entropy_wi:h_HippAntL_neg                     3.543e-02  2.430e-02  2.459e+04
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg   4.805e-02  4.646e-02  2.588e+04
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg  9.446e-02  4.534e-02  2.098e+04
##                                                t value Pr(>|t|)    
## (Intercept)                                      4.745 1.10e-05 ***
## trial_neg_inv_sc                                -0.203 0.838883    
## rt_lag_sc                                       14.934  < 2e-16 ***
## rt_vmax_lag_sc                                   5.022 2.38e-06 ***
## last_outcomeOmission                           -15.209  < 2e-16 ***
## v_max_wi_lag                                     2.559 0.010504 *  
## v_entropy_wi                                     3.979 6.93e-05 ***
## h_HippAntL_neg                                  -0.617 0.539018    
## trial_neg_inv_sc:rt_lag_sc                      -6.330 2.49e-10 ***
## trial_neg_inv_sc:rt_vmax_lag_sc                  6.038 1.59e-09 ***
## trial_neg_inv_sc:last_outcomeOmission            1.083 0.279008    
## trial_neg_inv_sc:v_max_wi_lag                    1.309 0.190566    
## trial_neg_inv_sc:v_entropy_wi                   -1.258 0.208466    
## trial_neg_inv_sc:h_HippAntL_neg                  0.404 0.686287    
## rt_lag_sc:rt_vmax_lag_sc                        -7.397 1.54e-13 ***
## rt_lag_sc:last_outcomeOmission                 -30.374  < 2e-16 ***
## rt_lag_sc:v_max_wi_lag                          -1.003 0.315933    
## rt_lag_sc:v_entropy_wi                          -4.159 3.21e-05 ***
## rt_lag_sc:h_HippAntL_neg                         1.292 0.200475    
## rt_vmax_lag_sc:last_outcomeOmission              6.355 2.14e-10 ***
## rt_vmax_lag_sc:v_max_wi_lag                      9.213  < 2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                      3.368 0.000759 ***
## rt_vmax_lag_sc:h_HippAntL_neg                    1.650 0.102968    
## last_outcomeOmission:v_max_wi_lag               -2.441 0.014646 *  
## last_outcomeOmission:v_entropy_wi               -3.634 0.000279 ***
## last_outcomeOmission:h_HippAntL_neg              3.401 0.000673 ***
## v_max_wi_lag:v_entropy_wi                        1.780 0.075076 .  
## v_max_wi_lag:h_HippAntL_neg                     -1.562 0.118201    
## v_entropy_wi:h_HippAntL_neg                      1.458 0.144953    
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg    1.034 0.301006    
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   2.083 0.037222 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation matrix not shown by default, as p = 31 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
Anova(model_AH_beta_fMRI_random, '3')
## Analysis of Deviance Table (Type III Wald chisquare tests)
## 
## Response: rt_csv_sc
##                                                   Chisq Df Pr(>Chisq)    
## (Intercept)                                     22.5191  1  2.081e-06 ***
## trial_neg_inv_sc                                 0.0413  1  0.8388811    
## rt_lag_sc                                      223.0357  1  < 2.2e-16 ***
## rt_vmax_lag_sc                                  25.2165  1  5.124e-07 ***
## last_outcome                                   231.3099  1  < 2.2e-16 ***
## v_max_wi_lag                                     6.5483  1  0.0104984 *  
## v_entropy_wi                                    15.8358  1  6.908e-05 ***
## h_HippAntL_neg                                   0.3812  1  0.5369496    
## trial_neg_inv_sc:rt_lag_sc                      40.0717  1  2.448e-10 ***
## trial_neg_inv_sc:rt_vmax_lag_sc                 36.4601  1  1.558e-09 ***
## trial_neg_inv_sc:last_outcome                    1.1720  1  0.2789983    
## trial_neg_inv_sc:v_max_wi_lag                    1.7133  1  0.1905544    
## trial_neg_inv_sc:v_entropy_wi                    1.5821  1  0.2084545    
## trial_neg_inv_sc:h_HippAntL_neg                  0.1631  1  0.6862836    
## rt_lag_sc:rt_vmax_lag_sc                        54.7084  1  1.398e-13 ***
## rt_lag_sc:last_outcome                         922.5658  1  < 2.2e-16 ***
## rt_lag_sc:v_max_wi_lag                           1.0057  1  0.3159233    
## rt_lag_sc:v_entropy_wi                          17.2980  1  3.195e-05 ***
## rt_lag_sc:h_HippAntL_neg                         1.6685  1  0.1964549    
## rt_vmax_lag_sc:last_outcome                     40.3805  1  2.090e-10 ***
## rt_vmax_lag_sc:v_max_wi_lag                     84.8762  1  < 2.2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                     11.3431  1  0.0007573 ***
## rt_vmax_lag_sc:h_HippAntL_neg                    2.7211  1  0.0990282 .  
## last_outcome:v_max_wi_lag                        5.9594  1  0.0146392 *  
## last_outcome:v_entropy_wi                       13.2092  1  0.0002786 ***
## last_outcome:h_HippAntL_neg                     11.5661  1  0.0006717 ***
## v_max_wi_lag:v_entropy_wi                        3.1687  1  0.0750643 .  
## v_max_wi_lag:h_HippAntL_neg                      2.4412  1  0.1181876    
## v_entropy_wi:h_HippAntL_neg                      2.1247  1  0.1449399    
## rt_lag_sc:last_outcome:h_HippAntL_neg            1.0698  1  0.3009959    
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   4.3408  1  0.0372094 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
########
# out-of-session replication with MEG behavioral data

mmodel_AH_beta_MEG_session <-  lmer(rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
                            v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 + 
               rt_lag_sc:last_outcome:h_HippAntL_neg + 
               rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg + 
               (1|id/run), mdf)
summary(mmodel_AH_beta_MEG_session)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc +  
##     last_outcome + v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 +  
##     rt_lag_sc:last_outcome:h_HippAntL_neg + rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg +  
##     (1 | id/run)
##    Data: mdf
## 
## REML criterion at convergence: 68213.8
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.4755 -0.5003 -0.0409  0.4729  5.0646 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  run:id   (Intercept) 0.05521  0.2350  
##  id       (Intercept) 0.03880  0.1970  
##  Residual             0.47242  0.6873  
## Number of obs: 31999, groups:  run:id, 519; id, 65
## 
## Fixed effects:
##                                                  Estimate Std. Error         df
## (Intercept)                                     9.489e-02  3.155e-02  6.507e+01
## trial_neg_inv_sc                                6.394e-02  1.101e-02  3.147e+04
## rt_lag_sc                                       4.685e-01  9.273e-03  3.196e+04
## rt_vmax_lag_sc                                  1.478e-01  9.001e-03  3.141e+04
## last_outcomeOmission                           -1.397e-01  1.015e-02  3.191e+04
## v_max_wi_lag                                    2.012e-04  6.113e-03  3.162e+04
## v_entropy_wi                                    3.824e-02  5.950e-03  3.149e+04
## h_HippAntL_neg                                  4.781e-02  1.390e-01  6.390e+01
## trial_neg_inv_sc:rt_lag_sc                     -3.631e-03  8.883e-03  3.149e+04
## trial_neg_inv_sc:rt_vmax_lag_sc                 4.645e-02  9.343e-03  3.184e+04
## trial_neg_inv_sc:last_outcomeOmission           1.690e-02  1.544e-02  3.151e+04
## trial_neg_inv_sc:v_max_wi_lag                  -3.476e-03  5.224e-03  3.172e+04
## trial_neg_inv_sc:v_entropy_wi                  -2.770e-03  6.452e-03  3.180e+04
## trial_neg_inv_sc:h_HippAntL_neg                 3.670e-02  3.837e-02  3.162e+04
## rt_lag_sc:rt_vmax_lag_sc                       -2.066e-02  4.758e-03  3.073e+04
## rt_lag_sc:last_outcomeOmission                 -3.780e-01  1.190e-02  3.180e+04
## rt_lag_sc:v_max_wi_lag                         -1.699e-02  5.478e-03  3.171e+04
## rt_lag_sc:v_entropy_wi                          3.777e-02  5.678e-03  3.152e+04
## rt_lag_sc:h_HippAntL_neg                        3.829e-02  3.653e-02  3.179e+04
## rt_vmax_lag_sc:last_outcomeOmission             5.954e-02  1.074e-02  3.163e+04
## rt_vmax_lag_sc:v_max_wi_lag                     7.850e-02  5.216e-03  3.175e+04
## rt_vmax_lag_sc:v_entropy_wi                    -2.367e-02  5.631e-03  3.160e+04
## rt_vmax_lag_sc:h_HippAntL_neg                   6.850e-02  3.218e-02  2.916e+04
## last_outcomeOmission:v_max_wi_lag              -5.889e-03  8.787e-03  3.154e+04
## last_outcomeOmission:v_entropy_wi              -4.317e-02  9.025e-03  3.151e+04
## last_outcomeOmission:h_HippAntL_neg             5.938e-02  4.493e-02  3.189e+04
## v_max_wi_lag:v_entropy_wi                       1.124e-03  4.130e-03  3.178e+04
## v_max_wi_lag:h_HippAntL_neg                    -1.407e-02  2.092e-02  3.139e+04
## v_entropy_wi:h_HippAntL_neg                     1.871e-02  2.126e-02  3.137e+04
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg   1.984e-01  4.199e-02  3.190e+04
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg  1.267e-01  3.694e-02  3.188e+04
##                                                t value Pr(>|t|)    
## (Intercept)                                      3.007 0.003745 ** 
## trial_neg_inv_sc                                 5.805 6.48e-09 ***
## rt_lag_sc                                       50.527  < 2e-16 ***
## rt_vmax_lag_sc                                  16.417  < 2e-16 ***
## last_outcomeOmission                           -13.761  < 2e-16 ***
## v_max_wi_lag                                     0.033 0.973745    
## v_entropy_wi                                     6.428 1.31e-10 ***
## h_HippAntL_neg                                   0.344 0.732033    
## trial_neg_inv_sc:rt_lag_sc                      -0.409 0.682741    
## trial_neg_inv_sc:rt_vmax_lag_sc                  4.972 6.66e-07 ***
## trial_neg_inv_sc:last_outcomeOmission            1.094 0.273796    
## trial_neg_inv_sc:v_max_wi_lag                   -0.665 0.505804    
## trial_neg_inv_sc:v_entropy_wi                   -0.429 0.667722    
## trial_neg_inv_sc:h_HippAntL_neg                  0.956 0.338916    
## rt_lag_sc:rt_vmax_lag_sc                        -4.342 1.42e-05 ***
## rt_lag_sc:last_outcomeOmission                 -31.762  < 2e-16 ***
## rt_lag_sc:v_max_wi_lag                          -3.102 0.001922 ** 
## rt_lag_sc:v_entropy_wi                           6.651 2.96e-11 ***
## rt_lag_sc:h_HippAntL_neg                         1.048 0.294645    
## rt_vmax_lag_sc:last_outcomeOmission              5.544 2.98e-08 ***
## rt_vmax_lag_sc:v_max_wi_lag                     15.050  < 2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                     -4.203 2.64e-05 ***
## rt_vmax_lag_sc:h_HippAntL_neg                    2.129 0.033289 *  
## last_outcomeOmission:v_max_wi_lag               -0.670 0.502777    
## last_outcomeOmission:v_entropy_wi               -4.783 1.74e-06 ***
## last_outcomeOmission:h_HippAntL_neg              1.322 0.186248    
## v_max_wi_lag:v_entropy_wi                        0.272 0.785565    
## v_max_wi_lag:h_HippAntL_neg                     -0.673 0.501099    
## v_entropy_wi:h_HippAntL_neg                      0.880 0.378882    
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg    4.724 2.33e-06 ***
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   3.431 0.000603 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation matrix not shown by default, as p = 31 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
Anova(mmodel_AH_beta_MEG_session, '3')
## Analysis of Deviance Table (Type III Wald chisquare tests)
## 
## Response: rt_csv_sc
##                                                    Chisq Df Pr(>Chisq)    
## (Intercept)                                       9.0433  1  0.0026366 ** 
## trial_neg_inv_sc                                 33.7033  1  6.419e-09 ***
## rt_lag_sc                                      2553.0241  1  < 2.2e-16 ***
## rt_vmax_lag_sc                                  269.5255  1  < 2.2e-16 ***
## last_outcome                                    189.3651  1  < 2.2e-16 ***
## v_max_wi_lag                                      0.0011  1  0.9737449    
## v_entropy_wi                                     41.3208  1  1.292e-10 ***
## h_HippAntL_neg                                    0.1183  1  0.7309034    
## trial_neg_inv_sc:rt_lag_sc                        0.1671  1  0.6827387    
## trial_neg_inv_sc:rt_vmax_lag_sc                  24.7204  1  6.628e-07 ***
## trial_neg_inv_sc:last_outcome                     1.1977  1  0.2737879    
## trial_neg_inv_sc:v_max_wi_lag                     0.4427  1  0.5057993    
## trial_neg_inv_sc:v_entropy_wi                     0.1843  1  0.6677196    
## trial_neg_inv_sc:h_HippAntL_neg                   0.9146  1  0.3389085    
## rt_lag_sc:rt_vmax_lag_sc                         18.8503  1  1.414e-05 ***
## rt_lag_sc:last_outcome                         1008.7994  1  < 2.2e-16 ***
## rt_lag_sc:v_max_wi_lag                            9.6244  1  0.0019201 ** 
## rt_lag_sc:v_entropy_wi                           44.2322  1  2.916e-11 ***
## rt_lag_sc:h_HippAntL_neg                          1.0983  1  0.2946372    
## rt_vmax_lag_sc:last_outcome                      30.7333  1  2.960e-08 ***
## rt_vmax_lag_sc:v_max_wi_lag                     226.5075  1  < 2.2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                      17.6660  1  2.633e-05 ***
## rt_vmax_lag_sc:h_HippAntL_neg                     4.5313  1  0.0332807 *  
## last_outcome:v_max_wi_lag                         0.4491  1  0.5027718    
## last_outcome:v_entropy_wi                        22.8767  1  1.727e-06 ***
## last_outcome:h_HippAntL_neg                       1.7471  1  0.1862387    
## v_max_wi_lag:v_entropy_wi                         0.0740  1  0.7855634    
## v_max_wi_lag:h_HippAntL_neg                       0.4526  1  0.5010938    
## v_entropy_wi:h_HippAntL_neg                       0.7743  1  0.3788757    
## rt_lag_sc:last_outcome:h_HippAntL_neg            22.3138  1  2.315e-06 ***
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   11.7692  1  0.0006022 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# include rt_lag and rt_vmax as random
mmodel_AH_beta_MEG_session_random <-  lmer(rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc + last_outcome + 
                             v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 + 
                rt_lag_sc:last_outcome:h_HippAntL_neg + 
                rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg + 
                (rt_lag_sc + rt_vmax_lag_sc|id/run), mdf)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 0.0122109 (tol = 0.002, component 1)
summary(mmodel_AH_beta_MEG_session_random)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: rt_csv_sc ~ (trial_neg_inv_sc + rt_lag_sc + rt_vmax_lag_sc +  
##     last_outcome + v_max_wi_lag + v_entropy_wi + h_HippAntL_neg)^2 +  
##     rt_lag_sc:last_outcome:h_HippAntL_neg + rt_vmax_lag_sc:trial_neg_inv_sc:h_HippAntL_neg +  
##     (rt_lag_sc + rt_vmax_lag_sc | id/run)
##    Data: mdf
## 
## REML criterion at convergence: 65687.5
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.8646 -0.4760 -0.0405  0.4476  5.3976 
## 
## Random effects:
##  Groups   Name           Variance Std.Dev. Corr       
##  run:id   (Intercept)    0.030911 0.17582             
##           rt_lag_sc      0.017430 0.13202   0.12      
##           rt_vmax_lag_sc 0.007242 0.08510   0.17 -0.15
##  id       (Intercept)    0.034254 0.18508             
##           rt_lag_sc      0.048808 0.22092   0.08      
##           rt_vmax_lag_sc 0.005712 0.07558  -0.07  0.10
##  Residual                0.427242 0.65364             
## Number of obs: 31999, groups:  run:id, 519; id, 65
## 
## Fixed effects:
##                                                  Estimate Std. Error         df
## (Intercept)                                     1.016e-01  2.915e-02  6.352e+01
## trial_neg_inv_sc                                5.582e-02  1.071e-02  3.003e+04
## rt_lag_sc                                       5.235e-01  3.399e-02  6.870e+01
## rt_vmax_lag_sc                                  8.338e-02  1.522e-02  8.377e+01
## last_outcomeOmission                           -1.379e-01  9.782e-03  3.066e+04
## v_max_wi_lag                                    6.878e-03  5.942e-03  3.070e+04
## v_entropy_wi                                    4.157e-02  5.794e-03  3.122e+04
## h_HippAntL_neg                                  4.665e-03  1.291e-01  6.364e+01
## trial_neg_inv_sc:rt_lag_sc                      8.836e-03  8.710e-03  2.979e+04
## trial_neg_inv_sc:rt_vmax_lag_sc                 3.962e-02  9.171e-03  2.718e+04
## trial_neg_inv_sc:last_outcomeOmission           1.227e-02  1.481e-02  3.145e+04
## trial_neg_inv_sc:v_max_wi_lag                  -2.514e-03  5.059e-03  3.097e+04
## trial_neg_inv_sc:v_entropy_wi                  -6.068e-03  6.259e-03  3.089e+04
## trial_neg_inv_sc:h_HippAntL_neg                 5.734e-02  3.774e-02  2.718e+04
## rt_lag_sc:rt_vmax_lag_sc                       -5.595e-02  5.368e-03  1.229e+04
## rt_lag_sc:last_outcomeOmission                 -3.865e-01  1.175e-02  2.818e+04
## rt_lag_sc:v_max_wi_lag                          1.221e-02  5.429e-03  2.877e+04
## rt_lag_sc:v_entropy_wi                          1.801e-02  5.596e-03  2.888e+04
## rt_lag_sc:h_HippAntL_neg                        1.148e-01  1.494e-01  6.675e+01
## rt_vmax_lag_sc:last_outcomeOmission             7.384e-02  1.065e-02  2.306e+04
## rt_vmax_lag_sc:v_max_wi_lag                     4.677e-02  5.191e-03  2.772e+04
## rt_vmax_lag_sc:v_entropy_wi                    -7.857e-03  5.549e-03  2.770e+04
## rt_vmax_lag_sc:h_HippAntL_neg                   4.418e-02  6.313e-02  6.412e+01
## last_outcomeOmission:v_max_wi_lag              -2.724e-03  8.429e-03  3.135e+04
## last_outcomeOmission:v_entropy_wi              -3.616e-02  8.700e-03  3.129e+04
## last_outcomeOmission:h_HippAntL_neg             3.731e-02  4.341e-02  3.039e+04
## v_max_wi_lag:v_entropy_wi                       7.356e-04  3.992e-03  3.165e+04
## v_max_wi_lag:h_HippAntL_neg                    -1.956e-02  2.045e-02  2.970e+04
## v_entropy_wi:h_HippAntL_neg                     7.276e-03  2.096e-02  3.041e+04
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg   1.890e-01  4.077e-02  3.114e+04
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg  8.690e-02  3.628e-02  2.914e+04
##                                                t value Pr(>|t|)    
## (Intercept)                                      3.487 0.000893 ***
## trial_neg_inv_sc                                 5.212 1.88e-07 ***
## rt_lag_sc                                       15.403  < 2e-16 ***
## rt_vmax_lag_sc                                   5.477 4.44e-07 ***
## last_outcomeOmission                           -14.093  < 2e-16 ***
## v_max_wi_lag                                     1.157 0.247105    
## v_entropy_wi                                     7.175 7.39e-13 ***
## h_HippAntL_neg                                   0.036 0.971280    
## trial_neg_inv_sc:rt_lag_sc                       1.014 0.310373    
## trial_neg_inv_sc:rt_vmax_lag_sc                  4.320 1.57e-05 ***
## trial_neg_inv_sc:last_outcomeOmission            0.829 0.407389    
## trial_neg_inv_sc:v_max_wi_lag                   -0.497 0.619214    
## trial_neg_inv_sc:v_entropy_wi                   -0.969 0.332326    
## trial_neg_inv_sc:h_HippAntL_neg                  1.519 0.128663    
## rt_lag_sc:rt_vmax_lag_sc                       -10.423  < 2e-16 ***
## rt_lag_sc:last_outcomeOmission                 -32.896  < 2e-16 ***
## rt_lag_sc:v_max_wi_lag                           2.249 0.024497 *  
## rt_lag_sc:v_entropy_wi                           3.218 0.001291 ** 
## rt_lag_sc:h_HippAntL_neg                         0.769 0.444813    
## rt_vmax_lag_sc:last_outcomeOmission              6.930 4.31e-12 ***
## rt_vmax_lag_sc:v_max_wi_lag                      9.009  < 2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                     -1.416 0.156858    
## rt_vmax_lag_sc:h_HippAntL_neg                    0.700 0.486602    
## last_outcomeOmission:v_max_wi_lag               -0.323 0.746573    
## last_outcomeOmission:v_entropy_wi               -4.157 3.24e-05 ***
## last_outcomeOmission:h_HippAntL_neg              0.860 0.390010    
## v_max_wi_lag:v_entropy_wi                        0.184 0.853780    
## v_max_wi_lag:h_HippAntL_neg                     -0.956 0.338884    
## v_entropy_wi:h_HippAntL_neg                      0.347 0.728442    
## rt_lag_sc:last_outcomeOmission:h_HippAntL_neg    4.635 3.58e-06 ***
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg   2.395 0.016613 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation matrix not shown by default, as p = 31 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 0.0122109 (tol = 0.002, component 1)
Anova(mmodel_AH_beta_MEG_session_random, '3')
## Analysis of Deviance Table (Type III Wald chisquare tests)
## 
## Response: rt_csv_sc
##                                                    Chisq Df Pr(>Chisq)    
## (Intercept)                                      12.1564  1  0.0004892 ***
## trial_neg_inv_sc                                 27.1689  1  1.864e-07 ***
## rt_lag_sc                                       237.2618  1  < 2.2e-16 ***
## rt_vmax_lag_sc                                   29.9997  1  4.321e-08 ***
## last_outcome                                    198.6073  1  < 2.2e-16 ***
## v_max_wi_lag                                      1.3396  1  0.2470958    
## v_entropy_wi                                     51.4806  1  7.231e-13 ***
## h_HippAntL_neg                                    0.0013  1  0.9711671    
## trial_neg_inv_sc:rt_lag_sc                        1.0291  1  0.3103644    
## trial_neg_inv_sc:rt_vmax_lag_sc                  18.6629  1  1.560e-05 ***
## trial_neg_inv_sc:last_outcome                     0.6864  1  0.4073823    
## trial_neg_inv_sc:v_max_wi_lag                     0.2470  1  0.6192105    
## trial_neg_inv_sc:v_entropy_wi                     0.9398  1  0.3323188    
## trial_neg_inv_sc:h_HippAntL_neg                   2.3087  1  0.1286515    
## rt_lag_sc:rt_vmax_lag_sc                        108.6456  1  < 2.2e-16 ***
## rt_lag_sc:last_outcome                         1082.1785  1  < 2.2e-16 ***
## rt_lag_sc:v_max_wi_lag                            5.0596  1  0.0244898 *  
## rt_lag_sc:v_entropy_wi                           10.3578  1  0.0012893 ** 
## rt_lag_sc:h_HippAntL_neg                          0.5908  1  0.4421021    
## rt_vmax_lag_sc:last_outcome                      48.0285  1  4.201e-12 ***
## rt_vmax_lag_sc:v_max_wi_lag                      81.1708  1  < 2.2e-16 ***
## rt_vmax_lag_sc:v_entropy_wi                       2.0044  1  0.1568466    
## rt_vmax_lag_sc:h_HippAntL_neg                     0.4897  1  0.4840706    
## last_outcome:v_max_wi_lag                         0.1044  1  0.7465710    
## last_outcome:v_entropy_wi                        17.2779  1  3.229e-05 ***
## last_outcome:h_HippAntL_neg                       0.7389  1  0.3900030    
## v_max_wi_lag:v_entropy_wi                         0.0340  1  0.8537791    
## v_max_wi_lag:h_HippAntL_neg                       0.9147  1  0.3388758    
## v_entropy_wi:h_HippAntL_neg                       0.1205  1  0.7284396    
## rt_lag_sc:last_outcome:h_HippAntL_neg            21.4869  1  3.563e-06 ***
## trial_neg_inv_sc:rt_vmax_lag_sc:h_HippAntL_neg    5.7375  1  0.0166063 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
############################
## Emtrends plot for betas -> behavior figure

emodel_condition <- as_tibble(emtrends(model_AH_beta_fMRI, var = "rt_vmax_lag_sc", specs = c("h_HippAntL_neg", "trial_neg_inv_sc"), at = list(h_HippAntL_neg = c(-.1, .37), trial_neg_inv_sc = c(-.7, 0.44)), options = list()))
## Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
## To enable adjustments, add the argument 'pbkrtest.limit = 27253' (or larger)
## [or, globally, 'set emm_options(pbkrtest.limit = 27253)' or larger];
## but be warned that this may result in large computation time and memory use.
## Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
## To enable adjustments, add the argument 'lmerTest.limit = 27253' (or larger)
## [or, globally, 'set emm_options(lmerTest.limit = 27253)' or larger];
## but be warned that this may result in large computation time and memory use.
emodel_condition$study = 'fMRI'
emodel_RT_Vmax <- as_tibble(emtrends(mmodel_AH_beta_MEG_session, var = "rt_vmax_lag_sc", specs = c("h_HippAntL_neg", "trial_neg_inv_sc"), at = list(h_HippAntL_neg = c(-.1, .37), trial_neg_inv_sc = c(-.7, 0.44)), options = list()))
## Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
## To enable adjustments, add the argument 'pbkrtest.limit = 31999' (or larger)
## [or, globally, 'set emm_options(pbkrtest.limit = 31999)' or larger];
## but be warned that this may result in large computation time and memory use.
## Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
## To enable adjustments, add the argument 'lmerTest.limit = 31999' (or larger)
## [or, globally, 'set emm_options(lmerTest.limit = 31999)' or larger];
## but be warned that this may result in large computation time and memory use.
emodel_RT_Vmax$study = 'Replication'
em4 <- rbind(emodel_condition, emodel_RT_Vmax)

ggplot(em4, aes(x=as.factor(trial_neg_inv_sc), y=rt_vmax_lag_sc.trend, ymin=asymp.LCL, ymax=asymp.UCL, color=as.factor(h_HippAntL_neg))) + 
  geom_point(position = position_dodge(width = .6), size=2.5) + 
  geom_errorbar(position = position_dodge(width=0.6), width=0.4, size=0.9) + 
  theme_bw(base_size=12) + facet_wrap(~study)+  ylab("Convergence on\nbest RT (AU)") +
  scale_color_manual("AH global max\nresponse", values=c("#403202", "#e2b407"), labels = c("10th %ile", "90th %ile")) +
  labs(shape = "AH global max\nresponse") +
  theme(axis.title.x=element_blank(), panel.grid.major.x=element_blank(),
  axis.text=element_text(size=8.5, color="grey10")) + 
  scale_x_discrete(name ="Trial", labels=c("-0.7" = "5", "0.44" = "50")) + scale_y_continuous(limits = c(.05, .3))

# save(file = 'vhd_u_meg_models.Rdata', list = ls(all.names = TRUE))
# load(file = 'vhd_u_meg_models.Rdata')