# Packages needed
require(metafor)
require(wildmeta)
require(readxl)
require(robumeta)
require(tidyverse)
require(clubSandwich)

#Reading the dataset
data<-read_excel(path = "Dataset_example.xlsx")


# MODEL 1: Multivariate model ---------------------------------------------

#First, the variance covariance matrix is generated assuming a correlation of 0.6 between effect sizes
V_mat <- with(data, impute_covariance_matrix(vi = Var_d, cluster = Study, r = 0.6))
                                      
#Second, the multivariate model is fitted
rma.mv(d ~ factor(Feedback), V = V_mat , data = data,
      random = ~ 1| Study, method = "REML", sparse = TRUE)


# MODEL 2: Three-level model  ---------------------------------------------
rma.mv(y=d, V=Var_d, mods = ~ factor(Feedback), random =  ~ 1  | Study/outcome, data=data)


# MODEL 3: Three-level with additional random effect  ---------------------------------------------
rma.mv(d , Var_d, mod= ~ factor(Feedback), data = data, 
       random =list( ~ 1 |outcome,  ~ factor(Feedback)| Study), struct=c("UN"),
       method = "REML", sparse = TRUE)


# MODEL 4: Three-level model with robust variance estimation (RVE)                         
robust(rma.mv(y=d, V=Var_d, mods = ~ factor(Feedback), random =  ~ 1  | Study/outcome, data=data),
       cluster=data$Study, clubSandwich=TRUE)


# MODEL 5: Correlated-effects model with robust variance estimation (RVE) method                         
robu(d ~  factor(Feedback),   studynum = Study,  var.eff.size = Var_d,
    small = TRUE, data = data, rho=0.6)


# MODEL 6:  Correlated-effects model with RVE and Cluster Wild Bootstrapping

#First we save the results from the previous model in a new object:
res_rve<-robu(d ~  factor(Feedback),   studynum = Study,  var.eff.size = Var_d,
              small = TRUE, data = data, rho=0.6)

#Then we apply the CWB
Wald_test_cwb(res_rve, 
            constraints = constrain_zero(2),
            R = 999,
            seed = 12345)
                                      
                                      

