Last updated: 2022-08-27

Checks: 6 1

Knit directory: schoolsout/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20220817) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version c91fde5. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  analysis/bayesian-analysis.Rmd

Unstaged changes:
    Modified:   analysis/index.Rmd
    Modified:   analysis/main-analysis.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


Load packages and data.

library('bayesplot')
library('rstanarm')
# library('broom')
library('cowplot')
library('data.table')
library('foreach')
library('ggplot2')
library('haven')
# library('huxtable')
library('kableExtra')
library('knitr')

# theme_set(
#   theme_bw() +
#     theme(axis.text = element_text(color = 'black'),
#           panel.grid.minor = element_blank(),
#           legend.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = 'cm')))

dataDir = 'data'

dOrig = setDT(read_dta(file.path(dataDir, 'master.dta')))
treat_types = c('treat_pool', 'treat_target')

Melt data.table to long format, scale outcomes by standard deviation of the control group, and rename stuff.

outcomes = data.table(
  level = c('average_level', 'place_value_correct', 'operation_frac_correct'),
  label = c('Average level', 'Place value', 'Fractions'))

dMelt = melt(
  dOrig,
  id.vars = c('unique_id', 'treatment', 'treat_pool', 'treat_target', 'tarl_prev'),
  measure.vars = outcomes$level, variable.name = 'outcome_name',
  value.name = 'outcome_value', variable.factor = FALSE)
dMelt[, outcome_value := as.numeric(outcome_value)]

dMelt[
  , outcome_value := outcome_value / sd(outcome_value[treatment == 0], na.rm = TRUE),
  by = outcome_name]

dMelt[, outcome_name := factor(outcome_name, outcomes$level, outcomes$label)]

for (j in treat_types) {
  a = attr(dOrig[[j]], 'labels')
  dMelt[, x := factor(x, a, names(a)), env = list(x = j)]}

Fit Bayesian models

dFit = foreach(treat_type = treat_types, .combine = rbind) %do% {
  dMelt[
    , .(treat_type = treat_type,
        fit_sep = list(stan_glm(
          outcome_value ~ x + tarl_prev, data = .SD, refresh = 0)),
        fit_agg = list(stan_glm(
          outcome_value ~ I(x != 'Control') + tarl_prev, data = .SD, refresh = 0))),
    keyby = outcome_name, env = list(x = treat_type)]}

dFit[, draws := list(list(as.array(fit_sep[[1L]]))), by = 1:nrow(dFit)]

Plot densities of posterior draws

pList = foreach(i = 1:nrow(dFit)) %do% {
  p = mcmc_areas(dFit[i]$draws[[1L]], regex_pars = '^treat', prob = 0.95)
  p = p + scale_y_discrete(labels = function(x) sub('^treat_(pool|target)', '', x))
  if (i <= 3) p = p + ggtitle(dFit$outcome_name[i])
  p}

p = plot_grid(plotlist = pList, nrow = 2L, align = 'hv')
p

Plot uncertainty intervals of posterior draws

pList = foreach(i = 1:nrow(dFit)) %do% {
  p = mcmc_intervals(dFit[i]$draws[[1L]], regex_pars = '^treat', prob_outer = 0.95)
  p = p + scale_y_discrete(labels = function(x) sub('^treat_(pool|target)', '', x))
  if (i <= 3) p = p + ggtitle(dFit$outcome_name[i])
  p}

p = plot_grid(plotlist = pList, nrow = 2L, align = 'hv')
p

Compute 90% posterior intervals

dInt = dFit[
  , data.table(posterior_interval(fit_sep[[1L]], prob = 0.9, regex_pars = '^treat')),
  keyby = .(outcome_name, treat_type)]
setnames(dInt, 3:4, c('ci_low', 'ci_high'))
kable_paper(kbl(dInt, digits = 3), 'hover', full_width = FALSE)
outcome_name treat_type ci_low ci_high
Average level treat_pool -0.050 0.097
Average level treat_pool 0.046 0.195
Average level treat_target -0.003 0.146
Average level treat_target -0.003 0.149
Place value treat_pool -0.064 0.084
Place value treat_pool 0.041 0.189
Place value treat_target -0.048 0.100
Place value treat_target 0.025 0.168
Fractions treat_pool -0.025 0.121
Fractions treat_pool 0.002 0.151
Fractions treat_target -0.044 0.106
Fractions treat_target 0.019 0.168

sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] knitr_1.40        kableExtra_1.3.4  haven_2.5.1       ggplot2_3.3.6    
 [5] foreach_1.5.2     data.table_1.14.3 cowplot_1.1.1     rstanarm_2.21.3  
 [9] Rcpp_1.0.9        bayesplot_1.9.0  

loaded via a namespace (and not attached):
  [1] minqa_1.2.4          colorspace_2.0-3     ellipsis_0.3.2      
  [4] ggridges_0.5.3       rprojroot_2.0.3      markdown_1.1        
  [7] base64enc_0.1-3      fs_1.5.2             rstudioapi_0.14     
 [10] farver_2.1.1         rstan_2.21.5         DT_0.24             
 [13] fansi_1.0.3          xml2_1.3.3           codetools_0.2-18    
 [16] splines_4.2.1        cachem_1.0.6         shinythemes_1.2.0   
 [19] jsonlite_1.8.0       workflowr_1.7.0      nloptr_2.0.3        
 [22] shiny_1.7.2          readr_2.1.2          compiler_4.2.1      
 [25] httr_1.4.4           backports_1.4.1      assertthat_0.2.1    
 [28] Matrix_1.4-1         fastmap_1.1.0        cli_3.3.0           
 [31] later_1.3.0          htmltools_0.5.3      prettyunits_1.1.1   
 [34] tools_4.2.1          igraph_1.3.4         gtable_0.3.0        
 [37] glue_1.6.2           posterior_1.3.0      reshape2_1.4.4      
 [40] dplyr_1.0.9          jquerylib_0.1.4      vctrs_0.4.1         
 [43] svglite_2.1.0        nlme_3.1-159         iterators_1.0.14    
 [46] crosstalk_1.2.0      tensorA_0.36.2       xfun_0.32           
 [49] stringr_1.4.1        ps_1.7.1             lme4_1.1-30         
 [52] rvest_1.0.3          mime_0.12            miniUI_0.1.1.1      
 [55] lifecycle_1.0.1      gtools_3.9.3         MASS_7.3-58.1       
 [58] zoo_1.8-10           scales_1.2.1         colourpicker_1.1.1  
 [61] hms_1.1.2            promises_1.2.0.1     parallel_4.2.1      
 [64] inline_0.3.19        shinystan_2.6.0      yaml_2.3.5          
 [67] gridExtra_2.3        loo_2.5.1            StanHeaders_2.21.0-7
 [70] sass_0.4.2           stringi_1.7.8        highr_0.9           
 [73] dygraphs_1.1.1.6     checkmate_2.1.0      boot_1.3-28         
 [76] pkgbuild_1.3.1       systemfonts_1.0.4    rlang_1.0.4         
 [79] pkgconfig_2.0.3      matrixStats_0.62.0   distributional_0.3.0
 [82] evaluate_0.16        lattice_0.20-45      purrr_0.3.4         
 [85] labeling_0.4.2       rstantools_2.2.0     htmlwidgets_1.5.4   
 [88] processx_3.7.0       tidyselect_1.1.2     plyr_1.8.7          
 [91] magrittr_2.0.3       R6_2.5.1             generics_0.1.3      
 [94] DBI_1.1.3            pillar_1.8.1         withr_2.5.0         
 [97] xts_0.12.1           abind_1.4-5          survival_3.4-0      
[100] tibble_3.1.8         crayon_1.5.1         utf8_1.2.2          
[103] tzdb_0.3.0           rmarkdown_2.16       grid_4.2.1          
[106] callr_3.7.2          git2r_0.30.1         forcats_0.5.2       
[109] threejs_0.3.3        webshot_0.5.3        digest_0.6.29       
[112] xtable_1.8-4         httpuv_1.6.5         RcppParallel_5.1.5  
[115] stats4_4.2.1         munsell_0.5.0        viridisLite_0.4.1   
[118] bslib_0.4.0          shinyjs_2.1.0