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
## 
## 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 unnecessary fields in d_vid.

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)) 

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)) 

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
China 77
India 77
US 76

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_ages)
summary(lfit)
## 
## Call:
## lm(formula = Score ~ age_centered * Condition * Country + trial, 
##     data = d_all_ages)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.7971 -0.5186  0.2634  0.4625  0.6547 
## 
## Coefficients:
##                                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                              0.728643   0.069700  10.454  < 2e-16
## age_centered                             0.009017   0.007522   1.199  0.23096
## ConditionSame                           -0.008894   0.094372  -0.094  0.92494
## CountryIndia                            -0.168742   0.080235  -2.103  0.03573
## CountryUS                               -0.291583   0.093175  -3.129  0.00181
## trial                                    0.009740   0.017634   0.552  0.58083
## age_centered:ConditionSame              -0.011953   0.009729  -1.229  0.21953
## age_centered:CountryIndia               -0.018415   0.010556  -1.745  0.08140
## age_centered:CountryUS                  -0.026032   0.010518  -2.475  0.01350
## ConditionSame:CountryIndia              -0.084202   0.106245  -0.793  0.42826
## ConditionSame:CountryUS                  0.159145   0.132184   1.204  0.22891
## age_centered:ConditionSame:CountryIndia  0.025756   0.014295   1.802  0.07192
## age_centered:ConditionSame:CountryUS     0.021036   0.013955   1.507  0.13204
##                                            
## (Intercept)                             ***
## age_centered                               
## ConditionSame                              
## CountryIndia                            *  
## CountryUS                               ** 
## trial                                      
## age_centered:ConditionSame                 
## age_centered:CountryIndia               .  
## age_centered:CountryUS                  *  
## ConditionSame:CountryIndia                 
## ConditionSame:CountryUS                    
## age_centered:ConditionSame:CountryIndia .  
## age_centered:ConditionSame:CountryUS       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4893 on 921 degrees of freedom
## Multiple R-squared:  0.03577,    Adjusted R-squared:  0.02321 
## F-statistic: 2.847 on 12 and 921 DF,  p-value: 0.0007434
ms <- d_all_ages %>%
  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_ages,
           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.00033 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.3 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: 17.3036 seconds (Warm-up)
## Chain 1:                6.92461 seconds (Sampling)
## Chain 1:                24.2282 seconds (Total)
## Chain 1: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 2).
## Chain 2: 
## Chain 2: Gradient evaluation took 0.000202 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 2.02 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: 18.698 seconds (Warm-up)
## Chain 2:                8.01789 seconds (Sampling)
## Chain 2:                26.7159 seconds (Total)
## Chain 2: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 3).
## Chain 3: 
## Chain 3: Gradient evaluation took 0.000198 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 1.98 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: 19.5778 seconds (Warm-up)
## Chain 3:                7.48272 seconds (Sampling)
## Chain 3:                27.0605 seconds (Total)
## Chain 3: 
## 
## SAMPLING FOR MODEL 'bfc44c804981277d14183c62e66124a8' NOW (CHAIN 4).
## Chain 4: 
## Chain 4: Gradient evaluation took 0.000234 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 2.34 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: 18.7994 seconds (Warm-up)
## Chain 4:                8.54895 seconds (Sampling)
## Chain 4:                27.3483 seconds (Total)
## Chain 4:
summary(fit)
##  Family: binomial 
##   Links: mu = logit 
## Formula: Score ~ age_centered * Condition * Country + trial + (1 | subject) 
##    Data: d_all_ages (Number of observations: 934) 
## Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup samples = 4000
## 
## Group-Level Effects: 
## ~subject (Number of levels: 395) 
##               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     2657     2248
## 
## Population-Level Effects: 
##                                         Estimate Est.Error l-95% CI u-95% CI
## Intercept                                   1.04      0.33     0.42     1.71
## age_centered                                0.04      0.03    -0.02     0.11
## ConditionSame                              -0.06      0.45    -0.95     0.82
## CountryIndia                               -0.79      0.37    -1.55    -0.11
## CountryUS                                  -1.31      0.43    -2.17    -0.50
## trial                                       0.04      0.07    -0.11     0.19
## age_centered:ConditionSame                 -0.06      0.05    -0.15     0.03
## age_centered:CountryIndia                  -0.08      0.05    -0.17     0.01
## age_centered:CountryUS                     -0.12      0.05    -0.22    -0.03
## ConditionSame:CountryIndia                 -0.32      0.50    -1.30     0.64
## ConditionSame:CountryUS                     0.69      0.60    -0.49     1.88
## age_centered:ConditionSame:CountryIndia     0.12      0.06    -0.01     0.24
## age_centered:ConditionSame:CountryUS        0.10      0.06    -0.03     0.22
##                                         Rhat Bulk_ESS Tail_ESS
## Intercept                               1.00     2204     2640
## age_centered                            1.00     1972     2508
## ConditionSame                           1.00     1969     2298
## CountryIndia                            1.00     2323     2666
## CountryUS                               1.00     2162     2395
## trial                                   1.00     9270     3266
## age_centered:ConditionSame              1.00     1913     2354
## age_centered:CountryIndia               1.00     2782     3103
## age_centered:CountryUS                  1.00     2367     3052
## ConditionSame:CountryIndia              1.00     2297     2462
## ConditionSame:CountryUS                 1.00     2177     2424
## age_centered:ConditionSame:CountryIndia 1.00     2814     2817
## age_centered:ConditionSame:CountryUS    1.00     2311     2982
## 
## 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

plot(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.

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.