library(SimDesign) library(ReplicationSuccess) library(BayesRep) library(MASS) # Attempt to load BayesFactor safely has_bf <- require(BayesFactor, quietly = TRUE) # ============================================================================== # Simulation Design Matrix: Hybrid Imputation Model (Three-Trials Rule) # Objective: Evaluate replication success metrics for sequential trials # ============================================================================== # 1. Load empirical JEPLMC parameters (Trials 1 & 2) # File generated by our extraction script empirical_data <- read.csv("jep_lmc_parameters.csv", stringsAsFactors = FALSE) # 2. Define the computational parameters for Trial 3 n_r3_levels <- c(20, 50, 150, 500) dist_levels <- c("normal", "lognormal", "negative lognormal", "chi-squared") # Cross the empirical studies with the Trial 3 parameters Design_448 <- createDesign( study_id = 1:nrow(empirical_data), n_r3 = n_r3_levels, distribution = dist_levels ) cat("Total Monte Carlo Conditions:", nrow(Design_448), "\n") # Attach fixed empirical data to pass into SimDesign functions fixed_objects <- list(empirical_data = empirical_data) # ============================================================================== # Generate Data Function (SimDesign Generate) # ============================================================================== Generate <- function(condition, fixed_objects) { # Extract empirical parameters for Trials 1 & 2 study <- fixed_objects$empirical_data[condition$study_id, ] n_1 <- study$T_N..O. n_2 <- study$T_N..R. r_1 <- study$T_r..O. r_2 <- study$T_r..R. # Protect against r=1 or r=-1 r_1 <- max(min(r_1, 0.9999), -0.9999) r_2 <- max(min(r_2, 0.9999), -0.9999) # Trial 3 parameters n_3 <- condition$n_r3 dist_type <- condition$distribution # Impute Trial 3 dataset computationally # We assume the "true" effect for Trial 3 is the pooled effect of Trials 1 and 2 z_1 <- atanh(r_1) z_2 <- atanh(r_2) w_1 <- n_1 - 3 w_2 <- n_2 - 3 pooled_z <- (w_1 * z_1 + w_2 * z_2) / (w_1 + w_2) rho_3 <- tanh(pooled_z) # Generate Trial 3 sample data to compute correlation # Using standard base R functions (SimDesign will safely wrap RNG streams for parallel=TRUE) gen_data <- function(n, rho, dist_type) { if (abs(rho) < 1e-10) { if (dist_type == "normal") { x <- rnorm(n); y <- rnorm(n) } else if (dist_type == "lognormal") { x <- rlnorm(n); y <- rlnorm(n) } else if (dist_type == "negative lognormal") { x <- -rlnorm(n); y <- -rlnorm(n) } else { x <- rchisq(n, 3); y <- rchisq(n, 3) } return(cor(x, y)) } # Bivariate Normal Copula structure sigma <- matrix(c(1, rho, rho, 1), nrow=2) Z <- MASS::mvrnorm(n, mu=c(0,0), Sigma=sigma) if (dist_type == "normal") { return(cor(Z[,1], Z[,2])) } # Margins conversion U <- pnorm(Z) if (dist_type == "lognormal") { X <- qlnorm(U[,1]); Y <- qlnorm(U[,2]) } else if (dist_type == "negative lognormal") { X <- -qlnorm(U[,1]); Y <- -qlnorm(U[,2]) } else if (dist_type == "chi-squared") { X <- qchisq(U[,1], df=3); Y <- qchisq(U[,2], df=3) } return(cor(X, Y)) } r_3 <- gen_data(n_3, rho_3, dist_type) r_3 <- max(min(r_3, 0.9999), -0.9999) dat <- c( r_1 = r_1, n_1 = n_1, r_2 = r_2, n_2 = n_2, r_3 = r_3, n_3 = n_3, pooled_z = pooled_z ) return(dat) } # ============================================================================== # Analyze Function (SimDesign Analyze) # ============================================================================== Analyze <- function(condition, dat, fixed_objects) { # Extract values r_1 <- unname(dat["r_1"]); n_1 <- unname(dat["n_1"]) r_2 <- unname(dat["r_2"]); n_2 <- unname(dat["n_2"]) r_3 <- unname(dat["r_3"]); n_3 <- unname(dat["n_3"]) # Fisher's Z transformations and SEs z_1 <- atanh(r_1); se_1 <- 1/sqrt(n_1 - 3) z_2 <- atanh(r_2); se_2 <- 1/sqrt(n_2 - 3) z_3 <- atanh(r_3); se_3 <- 1/sqrt(n_3 - 3) # Normal-approximated test statistics t_1 <- z_1 / se_1 t_2 <- z_2 / se_2 t_3 <- z_3 / se_3 # Meta-analytic pooling of Trial 1 and 2 w_1 <- 1 / (se_1^2); w_2 <- 1 / (se_2^2) z_12 <- (w_1 * z_1 + w_2 * z_2) / (w_1 + w_2) se_12 <- 1 / sqrt(w_1 + w_2) t_12 <- z_12 / se_12 # Variance ratios c_2_vs_1 <- (se_1^2) / (se_2^2) c_3_vs_12 <- (se_12^2) / (se_3^2) # --------------------------------------------------------- # Two-Trials Rule (Trial 2 vs Trial 1) # --------------------------------------------------------- pScep_nom_2 <- ReplicationSuccess::pSceptical(zo = t_1, zr = t_2, c = c_2_vs_1, type = "nominal") pScep_gol_2 <- ReplicationSuccess::pSceptical(zo = t_1, zr = t_2, c = c_2_vs_1, type = "golden") # Sceptical Bayes Factor (from BayesRep) BFscep_2 <- BayesRep::BFs(zo = t_1, zr = t_2, c = c_2_vs_1) # --------------------------------------------------------- # Three-Trials Rule (Trial 3 vs Pooled 1+2) # --------------------------------------------------------- pScep_nom_3 <- ReplicationSuccess::pSceptical(zo = t_12, zr = t_3, c = c_3_vs_12, type = "nominal") pScep_gol_3 <- ReplicationSuccess::pSceptical(zo = t_12, zr = t_3, c = c_3_vs_12, type = "golden") # Sceptical Bayes Factor (from BayesRep) BFscep_3 <- BayesRep::BFs(zo = t_12, zr = t_3, c = c_3_vs_12) # --------------------------------------------------------- # JZS Bayes Factor (Optional fallback) # --------------------------------------------------------- jzs_bf_2 <- NA jzs_bf_3 <- NA if ("BayesFactor" %in% loadedNamespaces()) { # Convert r to t-statistic for accurate BayesFactor computation tstat_1 <- r_1 * sqrt((n_1 - 2) / (1 - r_1^2)) tstat_2 <- r_2 * sqrt((n_2 - 2) / (1 - r_2^2)) tstat_3 <- r_3 * sqrt((n_3 - 2) / (1 - r_3^2)) try({ bf_obj_2 <- BayesFactor::ttest.tstat(t = tstat_2, n1 = n_2) jzs_bf_2 <- exp(bf_obj_2[['bf']]) bf_obj_3 <- BayesFactor::ttest.tstat(t = tstat_3, n1 = n_3) jzs_bf_3 <- exp(bf_obj_3[['bf']]) }, silent = TRUE) } ret <- c( pScep_nom_2 = pScep_nom_2, pScep_gol_2 = pScep_gol_2, BFscep_2 = BFscep_2, jzs_bf_2 = jzs_bf_2, pScep_nom_3 = pScep_nom_3, pScep_gol_3 = pScep_gol_3, BFscep_3 = BFscep_3, jzs_bf_3 = jzs_bf_3, # Returning raw values for summarize r_3 = r_3 ) # Prevent SimDesign from fatal redraw loops on NA/NaN (which happen if BF math overflows) ret[is.na(ret) | is.nan(ret) | is.infinite(ret)] <- -999 return(ret) } # ============================================================================== # Summarize Function (SimDesign Summarize) # ============================================================================== Summarize <- function(condition, results, fixed_objects) { # Filter out the -999 placeholders we used to prevent redraw loops clean_ttr2_nom <- results[, "pScep_nom_2"][results[, "pScep_nom_2"] != -999] clean_ttr2_gol <- results[, "pScep_gol_2"][results[, "pScep_gol_2"] != -999] clean_ttr3_nom <- results[, "pScep_nom_3"][results[, "pScep_nom_3"] != -999] clean_ttr3_gol <- results[, "pScep_gol_3"][results[, "pScep_gol_3"] != -999] clean_BFscep_2 <- results[, "BFscep_2"][results[, "BFscep_2"] != -999] clean_BFscep_3 <- results[, "BFscep_3"][results[, "BFscep_3"] != -999] # Two-Trials Rule Power / Success rate (Alpha = 0.05) power_ttr2_nom <- mean(clean_ttr2_nom < 0.05, na.rm = TRUE) power_ttr2_gol <- mean(clean_ttr2_gol < 0.05, na.rm = TRUE) # Three-Trials Rule Power / Success rate (Alpha = 0.05) power_ttr3_nom <- mean(clean_ttr3_nom < 0.05, na.rm = TRUE) power_ttr3_gol <- mean(clean_ttr3_gol < 0.05, na.rm = TRUE) # Sceptical BF Success rate (BF < 1/3 indicates success for alternative hypothesis of replication) power_BFscep_2 <- mean(clean_BFscep_2 < (1/3), na.rm = TRUE) power_BFscep_3 <- mean(clean_BFscep_3 < (1/3), na.rm = TRUE) ret <- c( power_ttr2_nom = power_ttr2_nom, power_ttr2_gol = power_ttr2_gol, power_ttr3_nom = power_ttr3_nom, power_ttr3_gol = power_ttr3_gol, power_BFscep_2 = power_BFscep_2, power_BFscep_3 = power_BFscep_3 ) return(ret) } # ============================================================================== # Execution (September milestone) # ============================================================================== # To test rapidly, replications = 100. For final run, set to 10000. res <- runSimulation(design=Design_448, replications=10000, generate=Generate, analyse=Analyze, summarise=Summarize, fixed_objects=fixed_objects, parallel=TRUE, ncores=4, save=TRUE, save_results=FALSE, save_details=list(save_results_dirname = "sim_results"))