R/testing-utilities.R

Defines functions check_selection_weights check_selmodel_summary pull_argument print_and_parse check_profiling_equivalence eval_beta_range check_beta_score_hessian_bias check_step_score_hessian_bias check_model_structure check_target_m check_valence_equivalence check_dims check_against_metafor_selmodel get_selmodel_params get_RE_params check_all_derivatives differentiate_loglik

#-------------------------------------------------------------------------------
# Variant of selection_model() with bootstrap default R = 4L

quick_boot_selection_args <- formals(selection_model)
quick_boot_selection_args$R <- 4L
quick_boot_selection_model <- selection_model
formals(quick_boot_selection_model) <- quick_boot_selection_args


#-------------------------------------------------------------------------------
# Functions for comparing scores to numerical derivatives

differentiate_loglik <- function(
    param,
    f_ll, f_score, f_hess,
    steps,
    theta,
    from, to, N,
    yi,
    sei,
    Hsgn
) {
  
  param_seq <- seq(from, to, length.out = N + 2)
  
  f_ll_ <- function(x) {
    theta[param] <- x
    f_ll(theta = theta, yi = yi, sei = sei, steps = steps, Hsgn = Hsgn)
  }
  ll_seq <- sapply(param_seq, f_ll_)
  
  f_score_ <- function(x) {
    theta[param] <- x
    f_score(theta = theta, yi = yi, sei = sei, steps = steps, Hsgn = Hsgn)
  }
  score_seq <- sapply(param_seq, f_score_)

  f_hess_ <- function(x) {
    theta[param] <- x
    f_hess(theta = theta, yi = yi, sei = sei, steps = steps, Hsgn = Hsgn)[,param]
  }
  hess_seq <- sapply(param_seq, f_hess_)
  delta <- param_seq[3:(N + 2)] - param_seq[1:N]
  num_score <- (ll_seq[3:(N + 2)] - ll_seq[1:N]) / delta
  
  num_hess <- t(score_seq[,3:(N + 2)] - score_seq[,1:N]) / delta 
  
  data.frame(
    val = param_seq[2:(N + 1)],
    d_num = num_score,
    score = score_seq[param, 2:(N + 1)],
    h_num = num_hess,
    hess = t(hess_seq[,2:(N + 1)])
  )
}

check_all_derivatives <- function(
    steps = .025,
    selection_type = "step",
    estimator = "CML",
    ...,
    N = 100,
    crit = qnorm(0.975),
    params = "all"
) {
  
  cl <- match.call(expand.dots = TRUE)
  cl$N <- NULL
  cl$crit <- NULL
  cl[[1]] <- quote(selection_model)
  suppressWarnings(
    selmod_fit <- eval(cl, parent.frame())
  )
  
  m <- match(c("data","yi", "sei"), names(cl), 0L)
  mf <- cl[c(1L, m)]
  mf[[1L]] <- quote(build_model_frame)
  mf <- eval(mf, parent.frame())
  
  # Evaluate yi, sei from model frame
  yi <- eval(cl$yi, envir = mf)
  sei <- eval(cl$sei, envir = mf)
  
  theta <- selmod_fit$est$Est
  
  if (identical(params,"all")) params <- 1:length(theta)
  
  from <- selmod_fit$est$Est - crit * selmod_fit$est$SE
  to <- selmod_fit$est$Est + crit * selmod_fit$est$SE
  Hsgn <- if (selmod_fit$alternative == "greater") 1L else -1L
  
  if (selection_type == "step") {
    if (estimator %in% c("ML","CML")) {
      f_ll <- step_loglik
      f_score <- step_score
      f_hess <- step_hessian
    } else if (estimator %in% c("hybrid","ARGL")) {
      f_ll <- step_weighted_logpartlik
      f_score <- step_hybrid_score
      f_hess <- step_hybrid_jacobian
    }
  } else if (selection_type == "beta") {
    f_ll <- beta_loglik
    f_score <- beta_score
    f_hess <- beta_hessian
  }
  
  derivs <- mapply(
    differentiate_loglik, 
    param = params, from = from[params], to = to[params],
    MoreArgs = list(
      f_ll = f_ll,
      f_score = f_score,
      f_hess = f_hess,
      steps = steps,
      theta = theta, 
      yi = yi,
      sei = sei,
      Hsgn = Hsgn,
      N = N
    ),
    SIMPLIFY = FALSE
  )
  
  names(derivs) <- selmod_fit$est$param[params]
  score_max_diff <- sapply(derivs, \(x) max(abs(x$d_num - x$score), na.rm = TRUE))
  score_range <- sapply(derivs, \(x) diff(range(x$score, na.rm = TRUE)))
  score_diff_over_range <- score_max_diff / pmax(1, score_range)
  p_dim <- length(theta)
  hess_max_diff <- sapply(derivs, \(x) apply(abs(x[,3 + 1:p_dim] - x[, 3 + p_dim + 1:p_dim]), 2, max, na.rm = TRUE))
  hess_range <- sapply(derivs, \(x) apply(x[,3 + p_dim + 1:p_dim], 2, \(x) diff(range(x, na.rm = TRUE)))) 
  hess_diff_over_range <- hess_max_diff / pmax(1, hess_range)
  deriv_dat <- do.call(rbind, derivs)
  
  deriv_dat$param <- rep(names(derivs), each = N)
  
  return(list(
    selmod_fit = selmod_fit, 
    score_diff_over_range = score_diff_over_range, 
    hess_diff_over_range = hess_diff_over_range,
    data = deriv_dat
  ))
  
}

#-------------------------------------------------------------------------------
# Functions for checking selection_model()


get_RE_params <- function(mod) {
  beta <- as.numeric(mod$beta)
  gamma <- log(mod$tau2)
  list(beta = beta, gamma = gamma)
}

get_selmodel_params <- function(sel_mod, theta = TRUE) {
  beta <- as.numeric(sel_mod$beta)
  gamma <- log(sel_mod$tau2)
  zeta <- if (sel_mod$type == "stepfun") log(sel_mod$delta[-1]) else log(sel_mod$delta)
  if (theta) {
    c(beta, gamma, zeta)
  } else {
    list(beta = beta, gamma = gamma, zeta = zeta)
  }
}

check_against_metafor_selmodel <- function(
    mod, type = "stepfun", alternative = "greater", steps = .025,
    tol_LRT = 1e-8, tol_ll = 1e-4, tol_score = 5e-5, 
    tol_param = 1e-4, tol_SE = 1e-4,
    ...,
    verbose = FALSE
) {

  suppressWarnings(
    if (is.null(steps)) {
      sel_mod <- metafor::selmodel(
        mod, type = type, alternative = alternative,
        control=list(optimizer = "nlminb", rel.tol = 1e-10, ...)
      )
      steps <- c(1e-5, 1 - 1e-5)
    } else {
      sel_mod <- metafor::selmodel(
        mod, type = type, alternative = alternative, steps = steps, 
        control=list(optimizer = "nlminb", rel.tol = 1e-10, ...)
      )
    }
  )

  theta <- get_selmodel_params(sel_mod)
  
  dat <- mod$data
  yi <- mod$call$yi
  sei <- mod$call$sei
  mods <- mod$call$mods
  
  if (type == "stepfun") {
    
    ll_full <- step_loglik(theta = theta, 
                           yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                           X = sel_mod$X,
                           steps = steps)
    ll_null <- step_loglik(yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                           X = sel_mod$X,
                           steps = steps,
                           beta = as.numeric(mod$beta), 
                           gamma = log(mod$tau2),
                           zeta = rep(0, length(steps)))
    score_full <- step_score(theta = theta, 
                             yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                             X = sel_mod$X,
                             steps = steps)
    
    if (mod$method == "FE") {
      score_full <- score_full[-(length(mod$beta)+1)]
    } else {
      
      metafor_est <- c(
        sel_mod$beta,
        log(sel_mod$tau2),
        log(sel_mod$delta[-1])
      )
      
      metafor_SE <- c(
        sel_mod$se, 
        sel_mod$se.tau2 / sel_mod$tau2, 
        sel_mod$se.delta[-1] / sel_mod$delta[-1]
      )
      
      my_est <- selection_model(
        data = dat,
        yi = yi,
        sei = sei,
        selection_type = "step",
        alternative = alternative,
        steps = steps,
        mean_mods = mods,
        priors = NULL,
        estimator = "CML",
        vcov_type = "model-based",
        theta = metafor_est
      )
      
    }
    
  } else if (type == "beta") {
    
    ll_full <- beta_loglik(theta = theta, 
                           yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                           X = sel_mod$X,
                           steps = steps)
    ll_null <- beta_loglik(yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                           X = sel_mod$X,
                           steps = steps,
                           beta = as.numeric(mod$beta), 
                           gamma = log(mod$tau2),
                           zeta = rep(0, 2))
    score_full <- beta_score(theta = theta, 
                             yi = sel_mod$yi, sei = sqrt(sel_mod$vi), 
                             X = sel_mod$X,
                             steps = steps)
    
    if (mod$method == "FE") {
      score_full <- score_full[-(length(mod$beta)+1)]
    } else {
      
      metafor_est <- c(
        sel_mod$beta,
        log(sel_mod$tau2),
        log(sel_mod$delta)
      )
      
      metafor_SE <- c(
        sel_mod$se, 
        sel_mod$se.tau2 / sel_mod$tau2, 
        sel_mod$se.delta / sel_mod$delta
      )
      
      my_est <- selection_model(
        data = dat,
        yi = yi,
        sei = sei,
        selection_type = "beta",
        alternative = alternative,
        steps = steps,
        mean_mods = mods,
        priors = NULL,
        estimator = "CML",
        vcov_type = "model-based",
        theta = metafor_est
      )
      
    }
  }

  # Likelihood ratio test statistic
  testthat::expect_equal(2 * (ll_full - ll_null), sel_mod$LRT, tolerance = tol_LRT)
  
  # Score vector
  if (max(abs(score_full)) > tol_score) print(score_full)
  testthat::expect_lt(max(abs(score_full)), tol_score)
  
  if (mod$method != "FE") {

    # Log-likelihood
    ll_selmod <- stats::logLik(sel_mod)
    attr(ll_selmod, "nall") <- NULL
    testthat::expect_equal(
      stats::logLik(my_est), 
      ll_selmod, 
      tolerance = tol_ll
    )
    
    # parameter estimates
    est_compare <- data.frame(
      param = my_est$est$param,
      metafor = metafor_est,
      package = my_est$est$Est,
      diff = metafor_est - my_est$est$Est,
      metafor_SE = metafor_SE,
      package_SE = my_est$est$SE,
      SE_diff = metafor_SE - my_est$est$SE
    )
    
    if (verbose) print(est_compare)
    
    if (!is.infinite(tol_param)) testthat::expect_equal(est_compare$package, est_compare$metafor, tolerance = tol_param)
    if (!is.infinite(tol_SE)) testthat::expect_equal(est_compare$package_SE, est_compare$metafor_SE, tolerance = tol_SE)
    
  }
}

check_dims <- function(mf, rows, cols) {
  testthat::expect_identical(nrow(mf), rows)
  testthat::expect_identical(ncol(mf), cols)
}


check_valence_equivalence <- function(
    yi, yi_neg, ..., steps = .025, 
    check_gamma = TRUE,
    tol = 1e-6,
    seed = as.integer(Sys.Date())
) {
  
  # base model
  
  cl_pos_gt <- match.call()
  cl_pos_gt$alternative <- "greater"
  cl_pos_gt$yi_neg <- NULL
  cl_pos_gt$check_gamma <- NULL
  cl_pos_gt$tol <- NULL
  cl_pos_gt[[1L]] <- quote(selection_model)
  
  set.seed(seed)
  pos_gt <- eval(cl_pos_gt, parent.frame())
  p_b <- pos_gt$param_dim[1]
  p_g <- pos_gt$param_dim[2]
  p_bg <- p_b + p_g
  p_z <- sum(pos_gt$param_dim[3])
  
  
  # switch valence, reverse steps
  
  steps_rev <- rev(1 - steps)
  cl_pos_ls <- cl_pos_gt
  cl_pos_ls$alternative <- "less"
  cl_pos_ls$steps <- steps_rev
  
  set.seed(seed)
  if ("valence_check" %in% names(cl_pos_gt) && !eval(cl_pos_gt$valence_check)) {
    pos_ls <- eval(cl_pos_ls, parent.frame())  
  } else {
    testthat::expect_warning(
      pos_ls <- eval(cl_pos_ls, parent.frame()),
      regexp = "Most of the effect size estimates are"
    )
  }

  # check betas are equal
  testthat::expect_equal(pos_gt$est[1:p_b,], pos_ls$est[1:p_b,], tolerance = tol)
  
  # check gamma are equal
  if (check_gamma) {
    testthat::expect_equal(
      pos_gt$est[p_b + 1:p_g,], 
      pos_ls$est[p_b + 1:p_g,], 
      tolerance = tol
    )
  }
  
  # check zetas are equivalent after translation
  if (inherits(pos_gt, "step.selmodel")) {
    if (p_z > 1L) {
      testthat::expect_equal(
        pos_gt$est$Est[p_bg + 1:p_z], 
        c(pos_ls$est$Est[p_bg + (p_z - 1):1], 0) - pos_ls$est$Est[p_bg + p_z],
        tolerance = tol
      )
    } else {
      testthat::expect_equal(
        pos_gt$est$Est[p_bg + p_z], 
        - pos_ls$est$Est[p_bg + p_z],
        tolerance = tol
      )
    }
  } else if (inherits(pos_gt, "beta.selmodel")) {
    testthat::expect_equal(
      pos_gt$est[p_bg + 1:p_z,-2],
      pos_ls$est[p_bg + p_z:1,-2],
      ignore_attr = TRUE,
      tolerance = tol
    )
  }
  
  # switch sign of outcome, reverse steps
  
  cl_neg_gt <- match.call()
  cl_neg_gt$alternative <- "greater"
  cl_neg_gt$yi <- cl_neg_gt$yi_neg
  cl_neg_gt$yi_neg <- NULL
  cl_neg_gt$check_gamma <- NULL
  cl_neg_gt$tol <- NULL
  cl_neg_gt$steps <- steps_rev
  cl_neg_gt[[1L]] <- quote(selection_model)

  set.seed(seed)
  if ("valence_check" %in% names(cl_pos_gt) && !eval(cl_pos_gt$valence_check)) {
    neg_gt <- eval(cl_neg_gt, parent.frame())  
  } else {
    testthat::expect_warning(
      neg_gt <- eval(cl_neg_gt, parent.frame()),
      regexp = "Most of the effect size estimates are"
    )
  }

  # check beta equal magnitude but opposite sign
  
  to_flip <- !(names(neg_gt$est) %in% c("estimator","param","SE","p_value","bootstraps"))
  testthat::expect_equal(
    apply(pos_gt$est[1:p_b,to_flip], 1, \(x) sort(as.numeric(x))), 
    apply(-1 * neg_gt$est[1:p_b,to_flip], 1, \(x) sort(as.numeric(x))), 
    tolerance = tol
  )
  testthat::expect_equal(
    pos_gt$est[1:p_b,!to_flip], 
    neg_gt$est[1:p_b,!to_flip], 
    tolerance = tol
  )
  
  # check gamma are equal
  if (check_gamma) {
    testthat::expect_equal(
      pos_gt$est[p_b + 1:p_g,], 
      neg_gt$est[p_b + 1:p_g,], 
      tolerance = tol
    )
  }
  
  # check zetas are equivalent after translation

  if (inherits(pos_gt, "step.selmodel")) {
    if (p_z > 1L) {
      testthat::expect_equal(
        pos_gt$est$Est[p_bg + 1:p_z], 
        c(neg_gt$est$Est[p_bg + (p_z - 1):1], 0) - neg_gt$est$Est[p_bg + p_z],
        tolerance = tol
      )
    } else {
      testthat::expect_equal(
        pos_gt$est$Est[p_bg + p_z], 
        - neg_gt$est$Est[p_bg + p_z],
        tolerance = tol
      )
    }
  } else if (inherits(pos_gt, "beta.selmodel")) {
    testthat::expect_equal(
      pos_gt$est[p_bg + 1:p_z,-2],
      neg_gt$est[p_bg + p_z:1,-2],
      ignore_attr = TRUE
    )
  }
  
  
  # switch sign of outcome, reverse steps
  
  cl_neg_ls <- cl_neg_gt
  cl_neg_ls$alternative <- "less"
  cl_neg_ls$steps <- cl_pos_gt$steps
  
  set.seed(seed)
  neg_ls <- eval(cl_neg_ls, parent.frame())
  

  # check beta equal magnitude but opposite sign
  testthat::expect_equal(
    apply(pos_gt$est[1:p_b,to_flip], 1, \(x) sort(as.numeric(x))), 
    apply(-1 * neg_ls$est[1:p_b,to_flip], 1, \(x) sort(as.numeric(x))), 
    tolerance = tol
  )
  testthat::expect_equal(
    pos_gt$est[1:p_b,!to_flip], 
    neg_ls$est[1:p_b,!to_flip], 
    tolerance = tol
  )
  
  # check gamma are equal
  if (check_gamma) {
    testthat::expect_equal(
      pos_gt$est[p_b + 1:p_g,], 
      neg_ls$est[p_b + 1:p_g,], 
      tolerance = tol
    )
  }
  
  # check zetas are equal
  testthat::expect_equal(
    pos_gt$est[p_bg + 1:p_z,], 
    neg_ls$est[p_bg + 1:p_z,], 
    tolerance = tol
  )
  
}

#-------------------------------------------------------------------------------
# Functions for checking r_meta()

check_target_m <- function(
    mean_smd, tau, omega, m, 
    cor_mu = 0.6, cor_sd = 0, 
    censor_fun = step_fun(), 
    n_ES_sim = n_ES_param(40, 1), 
    m_multiplier = 2
) {
  
  max_param_length <- max(lengths(list(mean_smd, tau, omega, m, cor_mu, cor_sd, censor_fun)))
  
  dat <- r_meta_categories(
    mean_smd = mean_smd, 
    tau = tau, 
    omega = omega, 
    m = m, 
    cor_mu = cor_mu, 
    cor_sd = cor_sd, 
    censor_fun = censor_fun, 
    n_ES_sim = n_ES_sim, 
    m_multiplier = m_multiplier
  )
  
  m_per_cat <- rep(m, length.out = length(table(dat$X)))
  studies_per_cat <- tapply(dat$studyid, dat$X, \(x) nlevels(droplevels(x)))
  
  # Number of levels of X equal to maximum number of parameter values
  testthat::expect_identical(max_param_length, nlevels(dat$X))

  # Number of studies per level of X equal to specified m's
  testthat::expect_identical(as.integer(studies_per_cat), m_per_cat)
  testthat::expect_identical(sum(m_per_cat), nlevels(dat$studyid))
  
  # Number of effect sizes per level of X gte specified m's
  testthat::expect_true(all(table(dat$X) >= m_per_cat))
  
}


check_model_structure <- function(
    mean_smd, tau, omega, m, 
    cor_mu = 0.6, cor_sd = 0, 
    censor_fun = step_fun(), 
    n_ES_sim = n_ES_param(40, 1), 
    m_multiplier = 1,
    check_varcomp = TRUE,
    verbose = FALSE
) {
  
  # Generate data with specified parameters
  
  dat <- r_meta_categories(
    mean_smd = mean_smd, 
    tau = tau, 
    omega = omega, 
    m = m, 
    cor_mu = cor_mu, 
    cor_sd = cor_sd, 
    censor_fun = censor_fun, 
    n_ES_sim = n_ES_sim, 
    m_multiplier = m_multiplier
  )

  
  # Determine moderator specification
  
  if (length(mean_smd) > 1L) {
    mods <- ~ 0 + X
  } else {
    mods <- ~ 1
  }
  
  
  # Determine random effects specification
  
  if (length(tau) > 1L) {
    RE_study <- ~ X | studyid
    struct_study <- "DIAG"
  } else {
    RE_study <- ~ 1 | studyid
    struct_study <- NULL
  }
  
  if (length(omega) > 1L) {
    RE_effect <- ~ X | esid
    struct_effect <- "DIAG"
  } else {
    RE_effect <- ~ 1 | esid
    struct_effect <- NULL
  } 
  
  random_spec <- list(RE_study, RE_effect)
  
  
  # Determine random effects structure and order of variance components in output
  
  if (is.null(struct_study) & is.null(struct_effect)) {
    struct <- "CS"
    var_params <- c(sigma.1 = tau, sigma.2 = omega)
  } else {
    struct <- c(struct_study, struct_effect)
    if (is.null(struct_study)) {
      var_params <- c(sigma = tau, tau. = omega)
    } else if (is.null(struct_effect)) {
      var_params <- c(sigma = omega, tau. = tau)
    } else {
      var_params <- c(tau. = tau, gamma. = omega)
    }
  }
  
  
  # Fit model in metafor
  
  if (cor_mu > 0) {
    Vmat <- metafor::vcalc(
      vi = dat$var_d, cluster = dat$studyid, obs = dat$esid,  
      rho = cor_mu, checkpd = FALSE, sparse = TRUE
    )
    
    suppressWarnings(
      m_fit <- metafor::rma.mv(
        yi = dat$d, V = Vmat, data = dat,
        mods = mods,
        random = random_spec, struct = struct,
        sparse = TRUE
      )
    )
    
  } else {
    
    suppressWarnings(
      m_fit <- metafor::rma.mv(
        yi = dat$d, V = dat$var_d, data = dat,
        mods = mods,
        random = random_spec, struct = struct,
        sparse = TRUE
      )
    )
    
  }
  
  # Check CIs for fixed effects
  
  if (verbose) {
    print(m_fit)
    cat("mean_smd: ", mean_smd, "\n")
  }

  testthat::expect_true(all(mean_smd > m_fit$ci.lb))
  testthat::expect_true(all(mean_smd < m_fit$ci.ub))
  
  
  # Check CIs for variance components
  
  if (check_varcomp) {
    
    varcomp_CIs <- metafor::confint.rma.mv(m_fit, verbose = verbose)
    
    for (i in seq_along(var_params)) {
      var_name <- names(var_params)[i]
      if (verbose) {
        print(varcomp_CIs[[i]])
        cat(var_name, ":", var_params[i], "\n")
      }
      testthat::expect_gte(var_params[i], varcomp_CIs[[i]]$random[var_name,"ci.lb"])
      testthat::expect_lte(var_params[i], varcomp_CIs[[i]]$random[var_name,"ci.ub"])
    }
    
  }

}

check_step_score_hessian_bias <- function(
    mean_smd, 
    tau, 
    m, 
    cor_mu = 0.6, cor_sd = 0, 
    steps = .025, 
    weights = 1,
    mean_N = 60,
    mean_kj = 1,
    m_multiplier = 1,
    check_Hotelling = TRUE,
    hessian_threshold = 1,
    verbose = TRUE,
    seed = NULL,
    just_pvals = FALSE,
    score_type = "score"
) {
  
  if (!is.null(seed)) set.seed(seed)
  
  # create censoring functions given steps and weights
  if (is.list(weights)) {
    censor_fun <- lapply(weights, \(w) step_fun(cut_vals = steps, weights = w))
  } else {
    censor_fun <- step_fun(cut_vals = steps, weights = weights)
  }
  
  # create sample size sampler
  n_ES_sim <- n_ES_param(mean_N = mean_N, mean_ES = mean_kj)
  
  # Generate data with specified parameters
  
  dat <- r_meta_categories(
    mean_smd = mean_smd, 
    tau = tau, 
    omega = 0, 
    m = m, 
    cor_mu = cor_mu, 
    cor_sd = cor_sd, 
    censor_fun = censor_fun, 
    n_ES_sim = n_ES_sim, 
    m_multiplier = m_multiplier
  )
  dat$sig_bucket <- cut(dat$p_onesided, c(0, steps, 1))
  
  if (verbose) {
    print(table(dat$X, dat$sig_bucket))
  }
  
  # Determine parameter values
  beta <- mean_smd
  gamma <- log(tau^2)
  zeta <- if (is.list(weights)) as.vector(t(sapply(weights, log))) else log(weights)
  
  if (nlevels(dat$X) > 1L) {
    Xmat <- model.matrix(~ 0 + X, data = dat)
  }
  
  # Determine X specification
  X <- if (length(mean_smd) > 1L) Xmat else NULL
  
  # Determine U specification
  U <- if (length(tau) > 1L) Xmat else NULL

  # Determine Z specification
  if (is.list(weights)) {
    Z <- Xmat
    if (length(steps) > 1L) Z <- rep(list(Z), length(steps))
  } else {
    Z <- NULL
  }
  
 
  # Calculate score contributions
  
  if (score_type == "score") {
    
    score_i <- step_score(
      yi = dat$d,
      sei = dat$sd_d,
      beta = beta,
      gamma = gamma,
      zeta = zeta,
      steps = steps,
      X = X,
      U = U,
      Z = Z,
      contributions = TRUE
    )
  } else if (score_type == "hybrid") {
    
    score_i <- step_hybrid_score(
      yi = dat$d,
      sei = dat$sd_d,
      beta = beta,
      gamma = gamma,
      zeta = zeta,
      steps = steps,
      X = X,
      U = U,
      Z = Z,
      contributions = TRUE
    )    
    
    
  }

  one_var_ps <- apply(score_i, 2, \(x) t.test(x)$p.value)
  Hotelling_T2 <- DescTools::HotellingsT2Test(score_i, mu = rep(0, ncol(score_i)))
  
  if (just_pvals) {
    pval_dat <- data.frame(
      component = c(names(one_var_ps), "Hotelling"), 
      pval = c(one_var_ps, Hotelling_T2$p.value)
    )
    return(pval_dat)
  }
  
  if (verbose) {
    summary_stats <- data.frame(
      M = colMeans(score_i), 
      SD = apply(score_i, 2, sd),
      pval = one_var_ps
    )
    print(summary_stats)
  }

  testthat::expect_true(all(one_var_ps > .1))
  
  if (check_Hotelling) {
    if (verbose) {
      print(Hotelling_T2)
    }
    
    testthat::expect_gt(Hotelling_T2$p.value, .1)
  }

  if (!is.null(hessian_threshold)) {
    hess_mat <- step_hessian(
      yi = dat$d,
      sei = dat$sd_d,
      beta = beta,
      gamma = gamma,
      zeta = zeta,
      steps = steps,
      X = X,
      U = U,
      Z = Z
    )
    
    var_score <- crossprod(score_i) / m
    Bartlett_diff <- var_score + hess_mat / m
    trace_stat <- sum(diag(var_score %*% solve(-hess_mat / m))) - nrow(hess_mat)
    if (verbose) {
      cat("-Hessian: \n")
      print(-hess_mat / m)
      cat("Var(score_i):\n")
      print(var_score)
      cat("Difference:\n")
      print(Bartlett_diff)
      cat("Maximum difference:", max(abs(Bartlett_diff)), "\n")
      cat("Trace statistic:", trace_stat, "\n")
    }
    
    testthat::expect_true(all(abs(Bartlett_diff) < hessian_threshold))
  }

}


check_beta_score_hessian_bias <- function(
    mean_smd, 
    tau, 
    m, 
    cor_mu = 0.6, cor_sd = 0, 
    steps = c(.025, .975),
    lambdas = c(1, 1),
    mean_N = 60,
    mean_kj = 1,
    m_multiplier = 1,
    check_Hotelling = TRUE,
    hessian_threshold = 1,
    verbose = TRUE,
    seed = NULL,
    just_pvals = FALSE,
    fit_mod = FALSE
) {
  
  if (!is.null(seed)) set.seed(seed)
  
  censor_fun <- beta_fun(delta_1 = lambdas[1], delta_2 = lambdas[2],
                             trunc_1 = steps[1], trunc_2 = steps[2])
  
  # create sample size sampler
  n_ES_sim <- n_ES_param(mean_N = mean_N, mean_ES = mean_kj)
  
  # Generate data with specified parameters
  
  dat <- r_meta_categories(
    mean_smd = mean_smd, 
    tau = tau, 
    omega = 0, 
    m = m, 
    cor_mu = cor_mu, 
    cor_sd = cor_sd, 
    censor_fun = censor_fun, 
    n_ES_sim = n_ES_sim, 
    m_multiplier = m_multiplier
  )
  
  dat$sig_bucket <- cut(dat$p_onesided, c(0, steps, 1))
  
  if (verbose) {
    print(table(dat$X, dat$sig_bucket))
  }
  
  
  # Determine parameter values
  beta <- mean_smd
  gamma <- log(tau^2)
  zeta <- log(lambdas)
  
  if (nlevels(dat$X) > 1L) {
    Xmat <- model.matrix(~ 0 + X, data = dat)
  }
  
  # Determine X specification
  if (length(mean_smd) > 1L) {
    X <- Xmat
    X_frm <- ~ X
  } else {
    X <- NULL
    X_frm <- NULL
  }
  
  # Determine U specification
  if (length(tau) > 1L) {
    U <- Xmat
    U_frm <- ~ X
  } else {
    U <- NULL
    U_frm <- NULL
  }

  # Fit model to the data
  if (fit_mod) {
    selmod_fit <- selection_model(
      data = dat, 
      yi = quote(d), sei = quote(sd_d), 
      mean_mods = X_frm,
      var_mods = U_frm,
      steps = steps, 
      selection_type = "beta"
    )
    selmod_fit$est$param_val <- c(beta, gamma, zeta)
    print(selmod_fit$est)
  }
  
  # Calculate score contributions
  
  score_i <- beta_score(
    yi = dat$d,
    sei = dat$sd_d,
    beta = beta,
    gamma = gamma,
    zeta = zeta,
    steps = steps,
    X = X,
    U = U,
    contributions = TRUE
  )
  
  one_var_ps <- apply(score_i, 2, \(x) t.test(x)$p.value)
  Hotelling_T2 <- DescTools::HotellingsT2Test(score_i, mu = rep(0, ncol(score_i)))

  if (just_pvals) {
    pval_dat <- data.frame(
      component = c(names(one_var_ps), "Hotelling"), 
      pval = c(one_var_ps, Hotelling_T2$p.value)
    )
    return(pval_dat)
  }
  
  if (verbose) {
    summary_stats <- data.frame(
      M = colMeans(score_i), 
      SD = apply(score_i, 2, sd),
      pval = one_var_ps
    )
    print(summary_stats)
  }
  
  testthat::expect_true(all(one_var_ps > .1))
  
  if (check_Hotelling) {
    if (verbose) {
      print(Hotelling_T2)
    }
    
    testthat::expect_gt(Hotelling_T2$p.value, .1)
  }

  if (!is.null(hessian_threshold)) {
    hess_mat <- beta_hessian(
      yi = dat$d,
      sei = dat$sd_d,
      beta = beta,
      gamma = gamma,
      zeta = zeta,
      steps = steps,
      X = X,
      U = U
    )
    
    var_score <- crossprod(score_i) / m
    Bartlett_diff <- var_score + hess_mat / m
    trace_stat <- sum(diag(var_score %*% solve(-hess_mat / m))) - nrow(hess_mat)
    if (verbose) {
      cat("-Hessian: \n")
      print(-hess_mat / m)
      cat("Var(score_i):\n")
      print(var_score)
      cat("Difference:\n")
      print(Bartlett_diff)
      cat("Maximum difference:", max(abs(Bartlett_diff)), "\n")
      cat("Trace statistic:", trace_stat, "\n")
    }
    
    testthat::expect_true(all(abs(Bartlett_diff) < hessian_threshold))
  }
}


eval_beta_range <- function(delta, steps, eps = 1e-3, plot = FALSE) {
  p <- seq(0,1,eps)
  b <- beta_fun(delta_1 = delta[1], delta_2 = delta[2], 
                    trunc_1 = steps[1], trunc_2 = steps[2])
  wts <- b(p)
  if (plot) plot(p, wts)
  max_val <- (delta[1] - 1) / (sum(delta) - 2)
  
  list(range = range(wts), max_val = max_val)
}


check_profiling_equivalence <- function(
    yi, sei, 
    steps, 
    ai = NULL, 
    cluster = NULL, 
    X = NULL, 
    U = NULL, 
    Z0 = NULL, 
    Z = NULL, 
    priors = NULL,
    subset = NULL,
    vcov_type = "robust",
    optimizer_control = list(),
    tol = 1e-4,
    score_tol = 1e-8,
    jac_tol = 1e-8,
    verbose = FALSE
) {
  
  
  hybrid_fit <- fit_selection_model(
    yi = yi, sei = sei, pi = pnorm(yi / sei, lower.tail = FALSE),
    steps = steps, 
    ai = ai, 
    cluster = cluster, 
    X = X, 
    U = U, 
    Z0 = Z0, 
    Z = Z, 
    priors = priors,
    subset = subset,
    vcov_type = vcov_type,
    selection_type = "step",
    estimator = "ARGL-full",
    optimizer_control = list(),
  )

  theta <- hybrid_fit$est
  x_index <- if (is.null(X)) 1L else 1:ncol(X)
  
  parse_params_full <- 
    parse_step_params(
      theta = theta,
      yi = yi,
      sei = sei,
      ai = ai,
      steps = steps,
      X = X,
      U = U,
      Z0 = Z0,
      Z = Z,
      calc_Ai = TRUE
    )
  
  scores_full <- step_hybrid_score(
    theta = theta,
    yi = yi,
    sei = sei,
    ai = ai,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    priors = priors
  )

  jac_full <- step_hybrid_jacobian(
    theta = theta,
    yi = yi,
    sei = sei,
    ai = ai,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    priors = priors
  )
  
  theta_na <- theta
  theta_na[x_index] <- NA_real_
  
  profile_fit <- fit_selection_model(
    yi = yi, sei = sei, pi = pnorm(yi / sei, lower.tail = FALSE),
    steps = steps, 
    ai = ai, 
    cluster = cluster, 
    X = X, 
    U = U, 
    Z0 = Z0, 
    Z = Z, 
    priors = priors,
    subset = subset,
    vcov_type = vcov_type,
    selection_type = "step",
    estimator = "hybrid",
    optimizer_control = list(),
  )
  
  parse_params_prof <- 
    parse_step_params(
      theta = theta_na,
      yi = yi,
      sei = sei,
      ai = ai,
      steps = steps,
      X = X,
      U = U,
      Z0 = Z0,
      Z = Z,
      priors = priors,
      calc_Ai = TRUE
    )
  
  scores_prof <- step_hybrid_profile_score(
    theta = theta_na[-x_index],
    yi = yi,
    sei = sei,
    ai = ai,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    priors = priors
  )
  
  jac_prof <- step_hybrid_profile_jacobian(
    theta = theta_na[-x_index],
    yi = yi,
    sei = sei,
    ai = ai,
    steps = steps,
    X = X,
    U = U,
    Z0 = Z0,
    Z = Z,
    priors = priors
  )

  if (verbose) {
    cat("hybrid estimates:", hybrid_fit$est, "\n")
    cat("profiled estimates:", profile_fit$est, "\n")
  }
  
  
  if (length(tol) == 1) tol <- rep(tol, 3L)
  testthat::expect_equal(parse_params_full$mu, parse_params_prof$mu, ignore_attr = FALSE, tolerance = tol[1])
  testthat::expect_equal(parse_params_full$eta, parse_params_prof$eta, ignore_attr = FALSE, tolerance = tol[2])
  testthat::expect_equal(parse_params_full$lambda_full, parse_params_prof$lambda_full, ignore_attr = FALSE, tolerance = tol[3])

  if (verbose) {
    cat("full scores:", scores_full, "\n")
    cat("profiled scores:", scores_prof, "\n")
  }
  
  if (!is.infinite(score_tol)) {
    testthat::expect_lt(max(abs(scores_full)), score_tol)
    testthat::expect_lt(max(abs(scores_prof)), score_tol)
  }

  if (verbose) {
    cat("full jacobian:\n", jac_full, "\n")
    cat("profiled jacobian:\n", jac_prof, "\n")
  }  
  
  testthat::expect_equal(jac_full[-x_index, -x_index],jac_prof, tolerance = jac_tol)
}


#-------------------------------------------------------------------------------
# Functions for checking print() and summary()

print_and_parse <- function(mod,...) {
  p <- testthat::capture_output_lines(print(mod,...), width = 1000)
  d <- do.call(rbind, strsplit(p, " +"))
  d[,-1]
}

pull_argument <- function(s, str) {
  x <- s[grepl(str, s)]
  trimws(substr(x, nchar(str) + 2L, nchar(x)))
}

check_selmodel_summary <- function(mod, ...) {
  s <- testthat::capture_output_lines(summary(mod, ...), width = 1000)
  p <- testthat::capture_output_lines(print(mod, ...), width = 1000)
  mod_str <- trimws(s[1])
  steps <- pull_argument(s, "Steps:") |> strsplit(",") |> unlist() |> as.numeric()
  estimator <- pull_argument(s,"Estimator:") 
  vcov_type <- pull_argument(s,"Variance estimator:") 
  boot_type <- pull_argument(s, "Bootstrap type:")
  R <- pull_argument(s, "Number of bootstrap replications:") |> as.integer()
  
  headers <- which(grepl("Coef.", s))
  n_panels <- length(headers)
  tails <- c(which(s==""), length(s) + 1)
  tails <- tails[(length(tails) - n_panels) + 1:n_panels]
  effect_strings <- lapply(1:n_panels, \(i) s[(headers[i] + 1):(tails[i] - 1)])
  effect_strings <- unlist(effect_strings)
  sum_effects <- do.call(rbind, strsplit(effect_strings, " +"))
  p_effects <- do.call(rbind, strsplit(p[-1], " +"))

  mod_type <- if (mod$selection_type == "step") "Step Function Model" else "Beta Density Model"
  if (inherits(mod, "boot.selmodel")) {
    mod_type <- paste(mod_type, "with Cluster Bootstrapping")
    
    testthat::expect_identical(boot_type, unique(mod$bootstrap_type))
    testthat::expect_identical(R, unique(mod$est$bootstraps))
  } 
  
  testthat::expect_identical(mod_str, mod_type)
  testthat::expect_identical(steps, mod$steps)
  
  estimator_type <- if (mod$estimator %in% c("ML","CML")) "composite marginal likelihood" else "augmented and reweighted Gaussian likelihood"
  testthat::expect_identical(estimator, estimator_type)
  testthat::expect_identical(vcov_type, mod$vcov_type)
 
  testthat::expect_true(all(p_effects[,2] %in% sum_effects[,2]))
}

#-------------------------------------------------------------------------------
# Functions for checking selection_wts()

check_selection_weights <- function(dat, steps, estimator = "CML", bootstrap = "none", R = 9) {

  mod_args <- list(
    data = dat, 
    yi = as.symbol("d"), sei = as.symbol("sd_d"), cluster = as.symbol("studyid"), 
    steps = steps, estimator = estimator, 
    bootstrap = bootstrap, R = R
  )
  step_fit <- do.call(selection_model, args = mod_args)
  
  
  if (bootstrap == "none") {
    
    zeta_est <- step_fit$est[grepl("zeta", step_fit$est$param),]
    lambda_est <- exp(zeta_est$Est)
    
    dat_wts <- selection_wts(step_fit)
    
    unique_wts <- tapply(dat_wts$wt, cut(dat_wts$p, c(0,steps, 1), include.lowest = TRUE), unique, simplify = FALSE)[-1]
    testthat::expect_equal(as.integer(lengths(unique_wts)), rep(1L, length(steps)))
    testthat::expect_equal(as.numeric(unlist(unique_wts)), lambda_est)
    
    other_wts <- selection_wts(step_fit, pvals = seq(0, 1, 0.02))
    unique_other_wts <- tapply(other_wts$wt, cut(other_wts$p, c(0,steps, 1), include.lowest = TRUE), unique, simplify = FALSE)[-1]
    testthat::expect_equal(unique_wts, unique_other_wts)
    
  } else {
    
    zeta_est <- step_fit$bootstrap_reps[grepl("zeta", step_fit$bootstrap_reps$param),]
    lambda_est <- exp(zeta_est$Est)
    R <- nrow(zeta_est) / step_fit$param_dim["sel"]
    
    dat_wts <- selection_wts(step_fit)$boot_wts
    
    unique_wts <- tapply(dat_wts$wt, list(dat_wts$rep, cut(dat_wts$p, c(0,steps, 1), include.lowest = TRUE)), unique, simplify = FALSE)[,-1,drop=FALSE]
    dimnames(unique_wts) <- NULL
    testthat::expect_equal(lengths(unique_wts), matrix(1L, R, length(steps)))
    testthat::expect_equal(unlist(t(unique_wts)), lambda_est)
    
    other_wts <- selection_wts(step_fit, pvals = seq(0, 1, 0.02))$boot_wts
    unique_other_wts <- tapply(other_wts$wt, list(other_wts$rep, cut(other_wts$p, c(0,steps, 1), include.lowest = TRUE)), unique, simplify = FALSE)[,-1,drop=FALSE]
    dimnames(unique_other_wts) <- NULL
    testthat::expect_equal(unique_wts, unique_other_wts)
    
  }
  
}

# check_predictions <- function(
#     data, yi, sei, pi, ai, 
#     selection_type = "step", 
#     steps = NULL,
#     mean_mods = NULL,
#     var_mods = NULL,
#     sel_mods = NULL,
#     estimator = "ARGL",
#     check_subset = TRUE
# ) {
#   
#   cl <- match.call()
#   cl[[1L]] <- str2lang("metaselection:::selection_model")
#   suppressWarnings(
#     mod <- eval(cl, parent.frame())
#   )
#   
#   all_preds <- stats::predict(mod)
# 
#   params <- mod$est$param
#   beta <- mod$est$Est[grepl("^beta", params)]
#   gamma <- mod$est$Est[grepl("^gamma", params)]
#   zeta <- mod$est$Est[grepl("^zeta", params)]
#   
#   if (is.null(mean_mods)) {
#     testthat::expect_equal(beta, unique(all_preds$mu))
#   } else {
#     X <- stats::model.matrix(mean_mods, data = data)
#     mu <- as.numeric(X %*% beta)
#     testthat::expect_equal(mu, all_preds$mu)
#   }
#   
#   if (is.null(var_mods)) {
#     testthat::expect_equal(exp(gamma), unique(all_preds$tau2))
#   } else {
#     U <- stats::model.matrix(var_mods, data = data)
#     tau2 <- as.numeric(exp(U %*% gamma))
#     testthat::expect_equal(tau2, all_preds$tau2)
#   }
#   
#   if (selection_type == "step") {
#     if (is.null(sel_mods)) {
#       
#       lambda <- apply(all_preds[,grepl("^lambda", names(all_preds))], 2, unique)
#       testthat::expect_equal(exp(zeta), as.numeric(lambda[-1]))
#       
#       sel_wts <- selection_wts(mod)
#       testthat::expect_equal(
#         as.numeric(lambda[mod$predictions$cats]),
#         sel_wts$wt
#       )
#       
#     } else {
#       Z <- stats::model.matrix(sel_mods, data = data)
#       zeta_list <- split(zeta, rep(seq_along(mod$steps), each = ncol(Z)))
#       lambda_list <- lapply(zeta_list, \(zeta) as.numeric(exp(Z %*% zeta)))
#       lambda_preds <- all_preds[,grepl("^lambda", names(all_preds))]
#       testthat::expect_equal(lambda_preds[,-1], data.frame(lambda_list), ignore_attr = TRUE)
#     }
#   }
#   
#   # Check prediction of subset of observations
#   sub <- sample(1:nrow(data), 10L)
#   newdata <- data[sub,]
#   sub_preds <- stats::predict(mod, newdata = newdata)
#   testthat::expect_equal(sub_preds, all_preds[sub,])
#   
#   return(all_preds)
# }

Try the metaselection package in your browser

Any scripts or data that you put into this service are public.

metaselection documentation built on Aug. 31, 2026, 5:08 p.m.