Preliminaries for analysis.

## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.4     ✓ dplyr   1.0.2
## ✓ 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()
## here() starts at /Users/abcar/Google Drive/Postdoc 2/multiRMTS/Tania thesis/analysis/RMTS-India
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## Loading required package: Rcpp
## Loading 'brms' package (version 2.14.4). Useful instructions
## can be found by typing help('brms'). A more detailed introduction
## to the package is available through vignette('brms_overview').
## 
## Attaching package: 'brms'
## The following object is masked from 'package:stats':
## 
##     ar
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Attaching package: 'lme4'
## The following object is masked from 'package:brms':
## 
##     ngrps

Load data. Clean up for merge.

raw <- read_csv(here("multi_India_2019.csv")) 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   subject = col_character(),
##   age = col_double(),
##   firstBlock = col_character(),
##   trial1 = col_character(),
##   trial2 = col_character(),
##   trial3 = col_character(),
##   trial4 = col_character(),
##   trial5 = col_character(),
##   trial6 = col_character(),
##   trial7 = col_character(),
##   trial8 = col_character(),
##   exclude = col_character(),
##   sideBias = col_character()
## )
pnas <- read_csv(here("dataFromPNAS.csv"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   AgeGroup = col_character(),
##   Country = col_character(),
##   Sex = col_character(),
##   AgeInMonths = col_double(),
##   Condition = col_character(),
##   Score = col_double()
## )
d <- raw  %>% 
  filter(!is.na(age)) %>%
  gather(trial, response, starts_with("trial")) %>% 
  arrange(subject, trial) %>%
  mutate(subject = tolower(subject), 
         trial = as.numeric(str_replace(trial, "trial", "")),
         Condition = ifelse(trial<5, 
                       ifelse(firstBlock=="same", "Same", "Different"),
                       ifelse(firstBlock=="different", "Same", "Different")),
         Score = ifelse(trial<5,  
                          ifelse(firstBlock==response, 1, 0), 
                          ifelse(firstBlock!=response, 1, 0)),
         first_block = ifelse(firstBlock=="same", "same", "different"), 
         trial = ifelse(trial<5, trial, trial-4)) %>% # renumber trials
  rename(choice = response) %>%
  select(subject, age, trial, Condition, first_block, Score) %>%
  mutate(Country = "India") %>%
  rename(AgeInMonths = age)

Combine PNAS 3-year-olds and India data.

d_all <- pnas %>% filter(AgeGroup=="36-48") %>%
  mutate(subject = paste0(Country,"-",1:n()),
         trial = 1,
         first_block = Condition) %>%
  dplyr::select(-Sex, -AgeGroup) %>%
  bind_rows(d %>% filter(AgeInMonths <= 48)) %>%
  mutate(age_centered = AgeInMonths - mean(AgeInMonths)) %>%
  mutate(Country = factor(Country, levels = c("India","US","China")))

Combine PNAS all ages and India data.

d_all_ages <- pnas %>%
  mutate(subject = paste0(Country,"-",1:n()),
         trial = 1,
         first_block = Condition) %>%
  dplyr::select(-Sex, -AgeGroup) %>%
  bind_rows(d %>% filter(AgeInMonths <= 48)) %>%
  mutate(age_centered = AgeInMonths - mean(AgeInMonths)) %>%
  mutate(Country = factor(Country, levels = c("India","US","China")))

Demographics and exclusions

We have 230 participants.

d_all %>% group_by(subject, Country) %>%
  summarise(acc = mean(Score)) %>% group_by(Country) %>%
  summarise(N = n()) %>%
  kable()
## `summarise()` regrouping output by 'subject' (override with `.groups` argument)
## `summarise()` ungrouping output (override with `.groups` argument)
Country N
India 77
US 76
China 77

Exploratory analyses

India <- d_all %>% filter(Country=="India")

# effect of block order? does condition order impact learning? Nope
blockOrder = lm(Score ~ age_centered * Condition  + first_block, data=India)
summary(blockOrder)
## 
## Call:
## lm(formula = Score ~ age_centered * Condition + first_block, 
##     data = India)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.6233 -0.5171  0.3864  0.4736  0.5337 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 0.569677   0.034932  16.308   <2e-16 ***
## age_centered               -0.009689   0.007588  -1.277    0.202    
## ConditionSame              -0.058555   0.040332  -1.452    0.147    
## first_blocksame            -0.017986   0.040552  -0.444    0.658    
## age_centered:ConditionSame  0.013802   0.010691   1.291    0.197    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4995 on 611 degrees of freedom
## Multiple R-squared:  0.006443,   Adjusted R-squared:  -6.133e-05 
## F-statistic: 0.9906 on 4 and 611 DF,  p-value: 0.412
# effect of trial? do kids get exploratory on later trials? Nope
trial = lm(Score ~ age_centered * Condition  + Condition * trial, data=India)
summary(trial)
## 
## Call:
## lm(formula = Score ~ age_centered * Condition + Condition * trial, 
##     data = India)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.6611 -0.5140  0.3743  0.4814  0.5363 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 0.495795   0.069742   7.109 3.28e-12 ***
## age_centered               -0.009399   0.007560  -1.243    0.214    
## ConditionSame               0.022614   0.098630   0.229    0.819    
## trial                       0.025974   0.025457   1.020    0.308    
## age_centered:ConditionSame  0.013802   0.010692   1.291    0.197    
## ConditionSame:trial        -0.032468   0.036002  -0.902    0.368    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4995 on 610 degrees of freedom
## Multiple R-squared:  0.007922,   Adjusted R-squared:  -0.0002097 
## F-statistic: 0.9742 on 5 and 610 DF,  p-value: 0.4327
# add caregiver education

Goal: plot posteriors on coefficients

when everything looks right, use brms to figure out effects of condition x culture

Fit linear model

# lmer with minimal random effect (1 | subject) doesn't converge, so trying lm
lfit = lm(Score ~ age_centered * Condition * Country + trial, data=d_all)
summary(lfit)
## 
## Call:
## lm(formula = Score ~ age_centered * Condition * Country + trial, 
##     data = d_all)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8063 -0.5139  0.2902  0.4733  0.6309 
## 
## Coefficients:
##                                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                              0.536379   0.052844  10.150   <2e-16
## age_centered                            -0.009399   0.007499  -1.253   0.2104
## ConditionSame                           -0.058555   0.040006  -1.464   0.1437
## CountryUS                               -0.149977   0.089652  -1.673   0.0948
## CountryChina                             0.225978   0.098868   2.286   0.0226
## trial                                    0.009740   0.017854   0.546   0.5855
## age_centered:ConditionSame               0.013802   0.010605   1.302   0.1935
## age_centered:CountryUS                   0.013720   0.025139   0.546   0.5854
## age_centered:CountryChina                0.032755   0.029078   1.126   0.2603
## ConditionSame:CountryUS                  0.216558   0.122875   1.762   0.0784
## ConditionSame:CountryChina              -0.011493   0.129130  -0.089   0.9291
## age_centered:ConditionSame:CountryUS     0.010184   0.034997   0.291   0.7711
## age_centered:ConditionSame:CountryChina -0.051615   0.036526  -1.413   0.1580
##                                            
## (Intercept)                             ***
## age_centered                               
## ConditionSame                              
## CountryUS                               .  
## CountryChina                            *  
## trial                                      
## age_centered:ConditionSame                 
## age_centered:CountryUS                     
## age_centered:CountryChina                  
## ConditionSame:CountryUS                 .  
## ConditionSame:CountryChina                 
## age_centered:ConditionSame:CountryUS       
## age_centered:ConditionSame:CountryChina    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4954 on 756 degrees of freedom
## Multiple R-squared:  0.02743,    Adjusted R-squared:  0.01199 
## F-statistic: 1.777 on 12 and 756 DF,  p-value: 0.04801
ms <- d_all %>%
  group_by(Condition, subject, AgeInMonths, Country) %>%
  summarize(correct=mean(Score, na.rm=TRUE))
## `summarise()` regrouping output by 'Condition', 'subject', 'AgeInMonths' (override with `.groups` argument)
ggplot(ms, aes(x=AgeInMonths, y=correct, color=Country)) + 
  geom_point(alpha=.3, position=position_jitter(width=.1, height=.02)) + 
  facet_wrap(~ Condition) +
  stat_smooth(method="lm", formula=y~poly(x,2)) + 
  ylab("Proportion correct") + 
  xlab("Age in months") +
  #guides(color=guide_legend(title="Condition", labels = c("Different", "Same"))) +
  geom_hline(yintercept = .5, lty = 2)  +
  theme_classic() + 
  ggthemes::scale_color_solarized() + 
  theme(legend.position = "bottom")

#ggsave("trajectory.pdf", width=6, height=4)

Fit brms model

fit <- brm(Score ~ age_centered * Condition * Country + trial + (1 | subject), data=d_all,
           family = binomial())
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
## Only 2 levels detected so that family 'bernoulli' might be a more efficient choice.
## Compiling Stan program...
## Trying to compile a simple C file
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/include   -fPIC  -Wall -g -O2  -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:16: error: expected ';' after top level declarator
## namespace Eigen {
##                ^
##                ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
##          ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
## Start sampling
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 1).
## Chain 1: 
## Chain 1: Gradient evaluation took 0.000371 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.71 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1: 
## Chain 1: 
## Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 1: 
## Chain 1:  Elapsed Time: 8.62233 seconds (Warm-up)
## Chain 1:                4.46039 seconds (Sampling)
## Chain 1:                13.0827 seconds (Total)
## Chain 1: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 2).
## Chain 2: 
## Chain 2: Gradient evaluation took 0.000215 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 2.15 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2: 
## Chain 2: 
## Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 2: 
## Chain 2:  Elapsed Time: 8.26251 seconds (Warm-up)
## Chain 2:                2.83465 seconds (Sampling)
## Chain 2:                11.0972 seconds (Total)
## Chain 2: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 3).
## Chain 3: 
## Chain 3: Gradient evaluation took 0.000191 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 1.91 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3: 
## Chain 3: 
## Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 3: 
## Chain 3:  Elapsed Time: 8.35612 seconds (Warm-up)
## Chain 3:                2.9619 seconds (Sampling)
## Chain 3:                11.318 seconds (Total)
## Chain 3: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 4).
## Chain 4: 
## Chain 4: Gradient evaluation took 0.00019 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 1.9 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4: 
## Chain 4: 
## Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
## Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
## Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
## Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
## Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
## Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
## Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
## Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
## Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
## Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
## Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
## Chain 4: 
## Chain 4:  Elapsed Time: 8.51709 seconds (Warm-up)
## Chain 4:                2.83907 seconds (Sampling)
## Chain 4:                11.3562 seconds (Total)
## Chain 4:
summary(fit)
##  Family: binomial 
##   Links: mu = logit 
## Formula: Score ~ age_centered * Condition * Country + trial + (1 | subject) 
##    Data: d_all (Number of observations: 769) 
## Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup samples = 4000
## 
## Group-Level Effects: 
## ~subject (Number of levels: 230) 
##               Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)     0.09      0.07     0.00     0.25 1.00     2768     2024
## 
## Population-Level Effects: 
##                                         Estimate Est.Error l-95% CI u-95% CI
## Intercept                                   0.15      0.22    -0.28     0.58
## age_centered                               -0.04      0.03    -0.10     0.02
## ConditionSame                              -0.24      0.16    -0.56     0.07
## CountryUS                                  -0.64      0.38    -1.39     0.12
## CountryChina                                1.15      0.51     0.22     2.19
## trial                                       0.04      0.07    -0.11     0.18
## age_centered:ConditionSame                  0.06      0.04    -0.03     0.14
## age_centered:CountryUS                      0.06      0.11    -0.15     0.28
## age_centered:CountryChina                   0.18      0.14    -0.09     0.47
## ConditionSame:CountryUS                     0.93      0.53    -0.10     1.96
## ConditionSame:CountryChina                 -0.17      0.64    -1.44     1.05
## age_centered:ConditionSame:CountryUS        0.05      0.15    -0.25     0.35
## age_centered:ConditionSame:CountryChina    -0.28      0.18    -0.63     0.07
##                                         Rhat Bulk_ESS Tail_ESS
## Intercept                               1.00     6537     3018
## age_centered                            1.00     3690     3116
## ConditionSame                           1.00     5689     3140
## CountryUS                               1.00     3932     3097
## CountryChina                            1.00     2769     2917
## trial                                   1.00     6937     2867
## age_centered:ConditionSame              1.00     4079     3316
## age_centered:CountryUS                  1.00     3647     3091
## age_centered:CountryChina               1.00     2822     2719
## ConditionSame:CountryUS                 1.00     3891     3173
## ConditionSame:CountryChina              1.00     2966     3195
## age_centered:ConditionSame:CountryUS    1.00     3612     2942
## age_centered:ConditionSame:CountryChina 1.00     2916     2833
## 
## Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).

Plot posterior

plot(fit, pars = c("age_centered", "Condition", "Country"))

Plot conditional effects

ce <- conditional_effects(fit, effects = "Condition:Country")
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
p <- plot(conditional_effects(fit, effects = "Condition:Country"), plot=F)[[1]] + 
  #scale_color_manual(values=c("#50a145", "#123b75", "#f22805")) + #flag colors
  scale_color_manual(values=c("#7CAE00", "#00BFC4", "#F8766D")) + #ggplot colors
  theme_classic()
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
plot(p)

plot(conditional_effects(fit, effects = "age_centered:Country"))
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.
## Using the maximum response value as the number of trials.
## Warning: Using 'binomial' families without specifying 'trials' on the left-hand
## side of the model formula is deprecated.