Last updated: 2022-08-22

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 file has unstaged changes. 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/

Unstaged changes:
    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.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/main-analysis.Rmd) and HTML (docs/main-analysis.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html 02e628a Jake Hughey 2022-08-21 Build site.
Rmd a3f43eb Jake Hughey 2022-08-21 updated main analysis.
html f9ab6c4 Jake Hughey 2022-08-17 again.
Rmd d79cdd6 Jake Hughey 2022-08-17 Build site.
html d79cdd6 Jake Hughey 2022-08-17 Build site.
Rmd 2702044 Jake Hughey 2022-08-17 again.
html 5d8c8de Jake Hughey 2022-08-17 Build site.
html c9d37eb Jake Hughey 2022-08-17 still trying.
html 5a451bd Jake Hughey 2022-08-17 updated ubuntu version.
Rmd bd34143 Jake Hughey 2022-08-17 updated main analysis.
html bd34143 Jake Hughey 2022-08-17 updated main analysis.
Rmd a6c4acb Jake Hughey 2022-08-17 added analysis file.

Load packages and data.

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

Run linear regression on each outcome (although a couple are binary) for both codings of treatment variable, and for the “aggregated” treatment.

lm() automatically removes missing values.

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

Run an anova comparison to test equality of coefficients.

dFit[, anova_comp := .(.(anova(fit_sep[[1L]], fit_agg[[1L]]))),
     by = .(treat_type, outcome_name)]
dFit[, p_value_comp := anova_comp[[1L]]$`Pr(>F)`[2L],
     by = .(treat_type, outcome_name)]
dFit[treat_type == 'treat_pool', p_value_comp_label := 'SMS Only = Phone + SMS']
dFit[treat_type == 'treat_target', p_value_comp_label := 'Not Targeted = Targeted']

Extract model statistics from the fits.

dTidy = dFit[
  , tidy(fit_sep[[1L]], conf.int = TRUE),
  keyby = .(treat_type, outcome_name)]

dTidy[, term := factor(
  term,
  c('(Intercept)', 'treat_poolSMS Only', 'treat_poolPhone + SMS',
    'treat_targetNot Targeted', 'treat_targetTargeted', 'tarl_prev'),
  c('Intercept', 'SMS Only', 'Phone + SMS', 'Not Targeted', 'Targeted', 'Previous TARL'))]

Plot coefficients.

p = ggplot(dTidy[!(term %in% c('Intercept', 'Previous TARL'))]) +
  geom_hline(yintercept = 0, color = 'gray', linetype = 'dashed') +
  geom_point(
    aes(x = term, y = estimate, color = outcome_name, shape = outcome_name),
    position = position_dodge(width = 0.5), size = 3) +
  geom_linerange(
    aes(x = term, ymin = conf.low, ymax = conf.high, color = outcome_name),
    position = position_dodge(width = 0.5), size = 1, alpha = 0.5) +
  labs(x = 'Treatment', y = 'Learning gains (in standard deviations)',
       color = 'Outcome', shape = 'Outcome') +
  scale_color_brewer(palette = 'Set2')
p

Version Author Date
02e628a Jake Hughey 2022-08-21

Produce regression tables.

fits = dFit[treat_type == 'treat_pool']$fit_sep
names(fits) = dFit[treat_type == 'treat_pool']$outcome_name

huxreg(
  fits, ci_level = 0.95,
  error_format = "({std.error})\n[{p.value}]\n{{{conf.low}, {conf.high}}}",
  coefs = c('SMS Only' = 'treat_poolSMS Only', 'Phone + SMS' = 'treat_poolPhone + SMS'),
  statistics = c('Observations' = 'nobs'))
Average levelPlace valueFractions
SMS Only0.024   0.009  0.047 
(0.046)
[0.600]
{-0.066, 0.114}  
(0.044)
[0.833]
{-0.078, 0.096} 
(0.046)
[0.305]
{-0.043, 0.136}
Phone + SMS0.121 **0.114 *0.075 
(0.046)
[0.008]
{0.031, 0.210}  
(0.044)
[0.010]
{0.027, 0.201} 
(0.046)
[0.099]
{-0.014, 0.165}
Observations2815       2881      2751     
*** p < 0.001; ** p < 0.01; * p < 0.05.
fits = dFit[treat_type == 'treat_target']$fit_sep
names(fits) = dFit[treat_type == 'treat_target']$outcome_name

huxreg(
  fits, ci_level = 0.95,
  error_format = "({std.error})\n[{p.value}]\n{{{conf.low}, {conf.high}}}",
  coefs = c('Not Targeted' = 'treat_targetNot Targeted', 'Targeted' = 'treat_targetTargeted'),
  statistics = c('Observations' = 'nobs'))
Average levelPlace valueFractions
Not Targeted0.070 0.026  0.029  
(0.046)
[0.127]
{-0.020, 0.159}
(0.044)
[0.564]
{-0.061, 0.113} 
(0.046)
[0.521]
{-0.060, 0.119} 
Targeted0.076 0.098 *0.093 *
(0.046)
[0.099]
{-0.014, 0.165}
(0.044)
[0.027]
{0.011, 0.185} 
(0.046)
[0.042]
{0.003, 0.182} 
Observations2815     2881      2751      
*** p < 0.001; ** p < 0.01; * p < 0.05.

Produce table of p-values for comparing treatments.

dComp = dcast(
  dFit, p_value_comp_label ~ outcome_name, value.var = 'p_value_comp')
setnames(dComp, 'p_value_comp_label', 'Comparison')

kable_paper(kbl(dComp, digits = 3), 'hover', full_width = FALSE)
Comparison Average level Place value Fractions
Not Targeted = Targeted 0.897 0.103 0.165
SMS Only = Phone + SMS 0.034 0.019 0.533

Calculate mean outcome values for the controls.

dSummary = dMelt[
  treatment == 0,
  .(mean_control_outcome = mean(outcome_value, na.rm = TRUE)),
  keyby = outcome_name]
dSummary = dcast(
  dSummary, . ~ outcome_name, value.var = 'mean_control_outcome')[, !'.']

kable_paper(kbl(dSummary, digits = 3), 'hover', full_width = FALSE)
Average level Place value Fractions
1.974 1.774 1.605

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.39        kableExtra_1.3.4  huxtable_5.5.0    haven_2.5.0      
 [5] ggplot2_3.3.6     foreach_1.5.2     data.table_1.14.3 cowplot_1.1.1    
 [9] broom_1.0.0       workflowr_1.7.0  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9         svglite_2.1.0      tidyr_1.2.0        getPass_0.2-2     
 [5] ps_1.7.1           assertthat_0.2.1   rprojroot_2.0.3    digest_0.6.29     
 [9] utf8_1.2.2         R6_2.5.1           backports_1.4.1    evaluate_0.16     
[13] highr_0.9          httr_1.4.4         pillar_1.8.1       rlang_1.0.4       
[17] rstudioapi_0.13    whisker_0.4        callr_3.7.1        jquerylib_0.1.4   
[21] rmarkdown_2.15     labeling_0.4.2     webshot_0.5.3      readr_2.1.2       
[25] stringr_1.4.0      munsell_0.5.0      compiler_4.2.1     httpuv_1.6.5      
[29] xfun_0.32          systemfonts_1.0.4  pkgconfig_2.0.3    htmltools_0.5.3   
[33] tidyselect_1.1.2   tibble_3.1.8       codetools_0.2-18   viridisLite_0.4.0 
[37] fansi_1.0.3        tzdb_0.3.0         crayon_1.5.1       dplyr_1.0.9       
[41] withr_2.5.0        later_1.3.0        commonmark_1.8.0   grid_4.2.1        
[45] jsonlite_1.8.0     gtable_0.3.0       lifecycle_1.0.1    DBI_1.1.3         
[49] git2r_0.30.1       magrittr_2.0.3     scales_1.2.1       cli_3.3.0         
[53] stringi_1.7.8      cachem_1.0.6       farver_2.1.1       fs_1.5.2          
[57] promises_1.2.0.1   xml2_1.3.3         bslib_0.4.0        ellipsis_0.3.2    
[61] generics_0.1.3     vctrs_0.4.1        RColorBrewer_1.1-3 iterators_1.0.14  
[65] tools_4.2.1        forcats_0.5.2      glue_1.6.2         purrr_0.3.4       
[69] hms_1.1.2          processx_3.7.0     fastmap_1.1.0      yaml_2.3.5        
[73] colorspace_2.0-3   rvest_1.0.3        sass_0.4.2