library(papaja)
library(rmarkdown)
library(tidyverse) 
library(here)
library(glue)
library(metafor)
library(knitr)
library(gridExtra)
library(here)
library(heatmaply)
library(MuMIn)
library(glmulti)
library(PRISMAstatement)

DATA_PATH <- here("data/processed/syntactic_bootstrapping_tidy_data.csv") 
RAW_DATA_PATH <- here("data/raw/syntactic_bootstrapping_raw_data.csv")

ma_data <- read_csv(DATA_PATH) %>% filter(paradigm_type == "action_matching")
get_MA_params <- function(moderator, df) {
  
  this_data <- df
  n = nrow(this_data)
  
  if (moderator == "mean_age"){
      model <- rma.mv(d_calc ~ log(mean_age), V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]

  } else if (moderator == "NULL"){
      model <- rma.mv(d_calc, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- NA
      this_moderator_estimate.cil <- NA
      this_moderator_estimate.cih <- NA
      this_moderator_z <- NA
      this_moderator_p <- NA
    
  }else if (moderator == "sentence_structure"){
    model <- rma.mv(d_calc~sentence_structure, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
  }else if (moderator == "productive_vocab_median"){
    model <- rma.mv(d_calc~productive_vocab_median, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
  }else if (moderator == "agent_argument_type_clean"){
    model <- rma.mv(d_calc~agent_argument_type_clean, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "patient_argument_type_clean"){
    model <- rma.mv(d_calc~patient_argument_type_clean, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "agent_argument_number"){
    model <- rma.mv(d_calc~agent_argument_number, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "n_repetitions_sentence"){
    model <- rma.mv(d_calc~n_repetitions_sentence, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
  }else if (moderator == "stimuli_modality"){
     model <- rma.mv(d_calc~stimuli_modality, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
    
  }else if (moderator == "n_repetitions_video"){
    model <- rma.mv(d_calc~n_repetitions_video, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
    
  }else if (moderator == "stimuli_actor"){
    model <- rma.mv(d_calc~stimuli_actor, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
  }else if (moderator == "transitive_event_type"){
    model <- rma.mv(d_calc~transitive_event_type, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "intransitive_event_type"){
    model <- rma.mv(d_calc~intransitive_event_type, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "visual_stimuli_pair"){
    model <- rma.mv(d_calc~visual_stimuli_pair, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "test_method"){
    model <- rma.mv(d_calc~test_method, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "presentation_type"){
    model <- rma.mv(d_calc~presentation_type, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "character_identification"){
    model <- rma.mv(d_calc~character_identification, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "practice_phase"){
     model <- rma.mv(d_calc~practice_phase, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
    
  }else if (moderator == "test_mass_or_distributed"){
    model <- rma.mv(d_calc~test_mass_or_distributed, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "n_train_test_pair"){
     model <- rma.mv(d_calc~n_train_test_pair, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }else if (moderator == "n_test_trial_per_pair"){
    model <- rma.mv(d_calc~n_test_trial_per_pair, V = d_var_calc,
                      random = ~ 1 | short_cite/same_infant/x_1,
                      method = "REML",
                      data = this_data)
      
      this_moderator_estimate <- model$b[2]
      this_moderator_estimate.cil <- model$ci.lb[2]
      this_moderator_estimate.cih <- model$ci.ub[2]
      this_moderator_z <- model$zval[2]
      this_moderator_p <- model$pval[2]
    
  }
  
  params <- data.frame(this_moderator = moderator,
                       n = n,
                       estimate = model$b[1],
                       estimate.cil = model$ci.lb[1],
                       estimate.cih = model$ci.ub[1],
                       z = model$zval[1],
                       p = model$pval[1],
                       mod_estimate = this_moderator_estimate,
                       mod_estimate.cil = this_moderator_estimate.cil, 
                       mod_estimate.cih = this_moderator_estimate.cih,
                       moderator_z = this_moderator_z,
                       moderator_p = this_moderator_p,
                       Q = model$QE,
                       Qp = model$QEp)
}


v <- c( "NULL", "mean_age","productive_vocab_median", "sentence_structure", "agent_argument_type_clean", "patient_argument_type_clean","agent_argument_number", "n_repetitions_sentence", "n_repetitions_video", "stimuli_modality", "stimuli_actor", "transitive_event_type","intransitive_event_type", "visual_stimuli_pair", "test_method","presentation_type","character_identification", "practice_phase", "test_mass_or_distributed", "n_train_test_pair", "n_test_trial_per_pair" )


all_models <- map_df(v,ma_data, .f = get_MA_params)
  
all_models
##                 this_moderator  n   estimate estimate.cil estimate.cih
## 1                         NULL 80 0.54372609   0.32634347    0.7611087
## 2                     mean_age 80 1.26517839  -2.85575976    5.3861165
## 3      productive_vocab_median 80 0.83304058   0.11402605    1.5520551
## 4           sentence_structure 80 0.29078025  -0.03440018    0.6159607
## 5    agent_argument_type_clean 80 0.40372034   0.02200663    0.7854341
## 6  patient_argument_type_clean 80 0.29235297  -0.03207797    0.6167839
## 7        agent_argument_number 80 0.49058464   0.21354939    0.7676199
## 8       n_repetitions_sentence 80 0.56760816   0.18135958    0.9538567
## 9          n_repetitions_video 80 0.62166656   0.20930891    1.0340242
## 10            stimuli_modality 80 0.52318717   0.12255427    0.9238201
## 11               stimuli_actor 80 0.48368019   0.15854106    0.8088193
## 12       transitive_event_type 80 0.46527843   0.17455830    0.7559986
## 13     intransitive_event_type 80 0.08587173  -0.74653205    0.9182755
## 14         visual_stimuli_pair 80 0.08513332  -0.77261510    0.9428817
## 15                 test_method 80 0.48877244   0.23779180    0.7397531
## 16           presentation_type 80 0.50668527   0.17021917    0.8431514
## 17    character_identification 80 0.54202907   0.24391194    0.8401462
## 18              practice_phase 80 0.46758962   0.12582941    0.8093498
## 19    test_mass_or_distributed 80 0.52912579   0.24438243    0.8138691
## 20           n_train_test_pair 80 0.63638321   0.18569215    1.0870743
## 21       n_test_trial_per_pair 80 0.48414313  -0.16151979    1.1298061
##            z            p mod_estimate mod_estimate.cil mod_estimate.cih
## 1  4.9023403 9.470159e-07           NA               NA               NA
## 2  0.6017329 5.473519e-01 -0.106774003     -0.716310641      0.502762635
## 3  2.2707879 2.315982e-02 -0.006941832     -0.022283612      0.008399947
## 4  1.7526234 7.966668e-02  0.428187474      0.009430533      0.846944415
## 5  2.0729602 3.817598e-02  0.133117003     -0.408373698      0.674607705
## 6  1.7661734 7.736676e-02  0.392552459     -0.084939834      0.870044752
## 7  3.4707794 5.189501e-04  0.092770599     -0.433632886      0.619174083
## 8  2.8802476 3.973629e-03 -0.002614142     -0.038504252      0.033275967
## 9  2.9548235 3.128480e-03 -0.028323165     -0.156232509      0.099586178
## 10 2.5595202 1.048168e-02  0.030482315     -0.448757340      0.509721970
## 11 2.9156618 3.549350e-03  0.111653449     -0.329217327      0.552524224
## 12 3.1367934 1.708065e-03  0.200635947     -0.259573181      0.660845076
## 13 0.2021921 8.397665e-01  0.503762340     -0.397150461      1.404675141
## 14 0.1945305 8.457605e-01  0.538330662     -0.481873119      1.558534443
## 15 3.8169334 1.351206e-04  0.224944373     -0.281881434      0.731770180
## 16 2.9515154 3.162187e-03  0.088033355     -0.393961428      0.570028138
## 17 3.5635573 3.658628e-04  0.005299301     -0.435793561      0.446392162
## 18 2.6815843 7.327445e-03  0.129845937     -0.315124725      0.574816599
## 19 3.6421130 2.704093e-04  0.037976907     -0.409932472      0.485886287
## 20 2.7675014 5.648779e-03 -0.036307901     -0.191628833      0.119013031
## 21 1.4696571 1.416546e-01  0.035534992     -0.322344033      0.393414017
##    moderator_z moderator_p        Q           Qp
## 1           NA          NA 443.6688 3.550434e-52
## 2  -0.34333162  0.73134900 442.3177 2.591585e-52
## 3  -0.88684244  0.37516373 160.5343 2.439720e-18
## 4   2.00410297  0.04505903 436.2674 3.171850e-51
## 5   0.48182643  0.62992925 436.9261 4.178829e-52
## 6   1.61131120  0.10711191 405.1890 8.685177e-47
## 7   0.34541381  0.72978330 441.0000 1.858539e-52
## 8  -0.14275869  0.88648076 443.5178 1.575623e-52
## 9  -0.43399788  0.66428998 443.6623 1.483907e-52
## 10  0.12466464  0.90078905 440.7145 5.036090e-52
## 11  0.49637388  0.61963065 439.0096 1.020198e-51
## 12  0.85447942  0.39283941 438.7228 4.784070e-52
## 13  1.09595073  0.27310034 442.0426 1.205069e-52
## 14  1.03421368  0.30103628 436.5166 3.395079e-53
## 15  0.86989033  0.38436034 439.3029 9.035840e-52
## 16  0.35797525  0.72036183 436.1230 1.406290e-51
## 17  0.02354706  0.98121390 441.5061 3.627968e-52
## 18  0.57193290  0.56736744 443.2801 1.738860e-52
## 19  0.16617953  0.86801567 443.2344 1.772110e-52
## 20 -0.45816219  0.64683592 443.6627 1.483661e-52
## 21  0.19461131  0.84569725 443.2991 1.725184e-52
mod_print <- all_models %>%
             mutate(esimate_print =  round(estimate, 2),
                    CI_print = paste0(" [", 
                                      round(estimate.cil, 2),
                                     ", ",
                                     round(estimate.cih, 2),
                                     "]"),
                   estimate_print_full = paste(esimate_print, CI_print),
                   z_print = round(z, 2),
                   p_print = round(p, 2),
                   p_print = ifelse(p_print <.001, "<.001", paste0("= ", p_print)),
                   mod_estimate_print = round(mod_estimate, 2),
                   mod_CI_print = paste0(" [", 
                                      round(mod_estimate.cil, 2),
                                     ", ",
                                     round(mod_estimate.cih, 2),
                                     "]"),
                   mod_estimate_print_full = paste(mod_estimate_print, mod_CI_print),

                   mod_z_print =  round(moderator_z, 2),
                   mod_p_print =  round(moderator_p, 2),
                   mod_p_print = ifelse(mod_p_print < .001, "<.001", 
                                        paste0("= ", mod_p_print)),
                   Q_print = round(Q, 2),
                   Qp_print = round(Qp, 2),
                   Qp_print = ifelse(Qp_print < .001, "<.001", paste0("= ", Qp_print)))
mod_print
##                 this_moderator  n   estimate estimate.cil estimate.cih
## 1                         NULL 80 0.54372609   0.32634347    0.7611087
## 2                     mean_age 80 1.26517839  -2.85575976    5.3861165
## 3      productive_vocab_median 80 0.83304058   0.11402605    1.5520551
## 4           sentence_structure 80 0.29078025  -0.03440018    0.6159607
## 5    agent_argument_type_clean 80 0.40372034   0.02200663    0.7854341
## 6  patient_argument_type_clean 80 0.29235297  -0.03207797    0.6167839
## 7        agent_argument_number 80 0.49058464   0.21354939    0.7676199
## 8       n_repetitions_sentence 80 0.56760816   0.18135958    0.9538567
## 9          n_repetitions_video 80 0.62166656   0.20930891    1.0340242
## 10            stimuli_modality 80 0.52318717   0.12255427    0.9238201
## 11               stimuli_actor 80 0.48368019   0.15854106    0.8088193
## 12       transitive_event_type 80 0.46527843   0.17455830    0.7559986
## 13     intransitive_event_type 80 0.08587173  -0.74653205    0.9182755
## 14         visual_stimuli_pair 80 0.08513332  -0.77261510    0.9428817
## 15                 test_method 80 0.48877244   0.23779180    0.7397531
## 16           presentation_type 80 0.50668527   0.17021917    0.8431514
## 17    character_identification 80 0.54202907   0.24391194    0.8401462
## 18              practice_phase 80 0.46758962   0.12582941    0.8093498
## 19    test_mass_or_distributed 80 0.52912579   0.24438243    0.8138691
## 20           n_train_test_pair 80 0.63638321   0.18569215    1.0870743
## 21       n_test_trial_per_pair 80 0.48414313  -0.16151979    1.1298061
##            z            p mod_estimate mod_estimate.cil mod_estimate.cih
## 1  4.9023403 9.470159e-07           NA               NA               NA
## 2  0.6017329 5.473519e-01 -0.106774003     -0.716310641      0.502762635
## 3  2.2707879 2.315982e-02 -0.006941832     -0.022283612      0.008399947
## 4  1.7526234 7.966668e-02  0.428187474      0.009430533      0.846944415
## 5  2.0729602 3.817598e-02  0.133117003     -0.408373698      0.674607705
## 6  1.7661734 7.736676e-02  0.392552459     -0.084939834      0.870044752
## 7  3.4707794 5.189501e-04  0.092770599     -0.433632886      0.619174083
## 8  2.8802476 3.973629e-03 -0.002614142     -0.038504252      0.033275967
## 9  2.9548235 3.128480e-03 -0.028323165     -0.156232509      0.099586178
## 10 2.5595202 1.048168e-02  0.030482315     -0.448757340      0.509721970
## 11 2.9156618 3.549350e-03  0.111653449     -0.329217327      0.552524224
## 12 3.1367934 1.708065e-03  0.200635947     -0.259573181      0.660845076
## 13 0.2021921 8.397665e-01  0.503762340     -0.397150461      1.404675141
## 14 0.1945305 8.457605e-01  0.538330662     -0.481873119      1.558534443
## 15 3.8169334 1.351206e-04  0.224944373     -0.281881434      0.731770180
## 16 2.9515154 3.162187e-03  0.088033355     -0.393961428      0.570028138
## 17 3.5635573 3.658628e-04  0.005299301     -0.435793561      0.446392162
## 18 2.6815843 7.327445e-03  0.129845937     -0.315124725      0.574816599
## 19 3.6421130 2.704093e-04  0.037976907     -0.409932472      0.485886287
## 20 2.7675014 5.648779e-03 -0.036307901     -0.191628833      0.119013031
## 21 1.4696571 1.416546e-01  0.035534992     -0.322344033      0.393414017
##    moderator_z moderator_p        Q           Qp esimate_print       CI_print
## 1           NA          NA 443.6688 3.550434e-52          0.54   [0.33, 0.76]
## 2  -0.34333162  0.73134900 442.3177 2.591585e-52          1.27  [-2.86, 5.39]
## 3  -0.88684244  0.37516373 160.5343 2.439720e-18          0.83   [0.11, 1.55]
## 4   2.00410297  0.04505903 436.2674 3.171850e-51          0.29  [-0.03, 0.62]
## 5   0.48182643  0.62992925 436.9261 4.178829e-52          0.40   [0.02, 0.79]
## 6   1.61131120  0.10711191 405.1890 8.685177e-47          0.29  [-0.03, 0.62]
## 7   0.34541381  0.72978330 441.0000 1.858539e-52          0.49   [0.21, 0.77]
## 8  -0.14275869  0.88648076 443.5178 1.575623e-52          0.57   [0.18, 0.95]
## 9  -0.43399788  0.66428998 443.6623 1.483907e-52          0.62   [0.21, 1.03]
## 10  0.12466464  0.90078905 440.7145 5.036090e-52          0.52   [0.12, 0.92]
## 11  0.49637388  0.61963065 439.0096 1.020198e-51          0.48   [0.16, 0.81]
## 12  0.85447942  0.39283941 438.7228 4.784070e-52          0.47   [0.17, 0.76]
## 13  1.09595073  0.27310034 442.0426 1.205069e-52          0.09  [-0.75, 0.92]
## 14  1.03421368  0.30103628 436.5166 3.395079e-53          0.09  [-0.77, 0.94]
## 15  0.86989033  0.38436034 439.3029 9.035840e-52          0.49   [0.24, 0.74]
## 16  0.35797525  0.72036183 436.1230 1.406290e-51          0.51   [0.17, 0.84]
## 17  0.02354706  0.98121390 441.5061 3.627968e-52          0.54   [0.24, 0.84]
## 18  0.57193290  0.56736744 443.2801 1.738860e-52          0.47   [0.13, 0.81]
## 19  0.16617953  0.86801567 443.2344 1.772110e-52          0.53   [0.24, 0.81]
## 20 -0.45816219  0.64683592 443.6627 1.483661e-52          0.64   [0.19, 1.09]
## 21  0.19461131  0.84569725 443.2991 1.725184e-52          0.48  [-0.16, 1.13]
##    estimate_print_full z_print p_print mod_estimate_print   mod_CI_print
## 1   0.54  [0.33, 0.76]    4.90   <.001                 NA       [NA, NA]
## 2  1.27  [-2.86, 5.39]    0.60  = 0.55              -0.11   [-0.72, 0.5]
## 3   0.83  [0.11, 1.55]    2.27  = 0.02              -0.01  [-0.02, 0.01]
## 4  0.29  [-0.03, 0.62]    1.75  = 0.08               0.43   [0.01, 0.85]
## 5    0.4  [0.02, 0.79]    2.07  = 0.04               0.13  [-0.41, 0.67]
## 6  0.29  [-0.03, 0.62]    1.77  = 0.08               0.39  [-0.08, 0.87]
## 7   0.49  [0.21, 0.77]    3.47   <.001               0.09  [-0.43, 0.62]
## 8   0.57  [0.18, 0.95]    2.88   <.001               0.00  [-0.04, 0.03]
## 9   0.62  [0.21, 1.03]    2.95   <.001              -0.03   [-0.16, 0.1]
## 10  0.52  [0.12, 0.92]    2.56  = 0.01               0.03  [-0.45, 0.51]
## 11  0.48  [0.16, 0.81]    2.92   <.001               0.11  [-0.33, 0.55]
## 12  0.47  [0.17, 0.76]    3.14   <.001               0.20  [-0.26, 0.66]
## 13 0.09  [-0.75, 0.92]    0.20  = 0.84               0.50    [-0.4, 1.4]
## 14 0.09  [-0.77, 0.94]    0.19  = 0.85               0.54  [-0.48, 1.56]
## 15  0.49  [0.24, 0.74]    3.82   <.001               0.22  [-0.28, 0.73]
## 16  0.51  [0.17, 0.84]    2.95   <.001               0.09  [-0.39, 0.57]
## 17  0.54  [0.24, 0.84]    3.56   <.001               0.01  [-0.44, 0.45]
## 18  0.47  [0.13, 0.81]    2.68  = 0.01               0.13  [-0.32, 0.57]
## 19  0.53  [0.24, 0.81]    3.64   <.001               0.04  [-0.41, 0.49]
## 20  0.64  [0.19, 1.09]    2.77  = 0.01              -0.04  [-0.19, 0.12]
## 21 0.48  [-0.16, 1.13]    1.47  = 0.14               0.04  [-0.32, 0.39]
##    mod_estimate_print_full mod_z_print mod_p_print Q_print Qp_print
## 1             NA  [NA, NA]          NA        <NA>  443.67    <.001
## 2      -0.11  [-0.72, 0.5]       -0.34      = 0.73  442.32    <.001
## 3     -0.01  [-0.02, 0.01]       -0.89      = 0.38  160.53    <.001
## 4       0.43  [0.01, 0.85]        2.00      = 0.05  436.27    <.001
## 5      0.13  [-0.41, 0.67]        0.48      = 0.63  436.93    <.001
## 6      0.39  [-0.08, 0.87]        1.61      = 0.11  405.19    <.001
## 7      0.09  [-0.43, 0.62]        0.35      = 0.73  441.00    <.001
## 8         0  [-0.04, 0.03]       -0.14      = 0.89  443.52    <.001
## 9      -0.03  [-0.16, 0.1]       -0.43      = 0.66  443.66    <.001
## 10     0.03  [-0.45, 0.51]        0.12       = 0.9  440.71    <.001
## 11     0.11  [-0.33, 0.55]        0.50      = 0.62  439.01    <.001
## 12      0.2  [-0.26, 0.66]        0.85      = 0.39  438.72    <.001
## 13        0.5  [-0.4, 1.4]        1.10      = 0.27  442.04    <.001
## 14     0.54  [-0.48, 1.56]        1.03       = 0.3  436.52    <.001
## 15     0.22  [-0.28, 0.73]        0.87      = 0.38  439.30    <.001
## 16     0.09  [-0.39, 0.57]        0.36      = 0.72  436.12    <.001
## 17     0.01  [-0.44, 0.45]        0.02      = 0.98  441.51    <.001
## 18     0.13  [-0.32, 0.57]        0.57      = 0.57  443.28    <.001
## 19     0.04  [-0.41, 0.49]        0.17      = 0.87  443.23    <.001
## 20    -0.04  [-0.19, 0.12]       -0.46      = 0.65  443.66    <.001
## 21     0.04  [-0.32, 0.39]        0.19      = 0.85  443.30    <.001
mod_print_plot <- mod_print %>% mutate(
  p_val = case_when(
    moderator_p < 0.05 ~ "<0.05",
    moderator_p > 0.05 & moderator_p < 0.1 ~ "0.05<p<0.1",
    TRUE ~ "NS"
  )
)


mod_print_plot %>% 
  ggplot(aes(x = this_moderator, y = mod_estimate, color = moderator_p)) + 
  geom_point(size = 2) + 
  geom_linerange(aes(ymin =  mod_estimate.cil, ymax =  mod_estimate.cih)) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  coord_flip()

mod_print_plot %>% 
  ggplot(aes(x = this_moderator, y = mod_estimate, color = p_val)) + 
  geom_point(size = 2) + 
  geom_linerange(aes(ymin =  mod_estimate.cil, ymax =  mod_estimate.cih)) + 
  geom_hline(aes(yintercept = 0), linetype = 2) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  coord_flip()