Skip to content
Prev 4742 / 5636 Next

[R-meta] Extracting meta regression elements automatically

Many thanks for the prompt response and information Reza - your tips were
exactly what I needed to achieve my goal! For those interested, here is the
final function:

run_meta.moderator <- function(df_list, df_names, moderators, scale_mods =
NULL) {
  output_list <- list()

  for (i in seq_along(df_list)) {
    df <- df_list[[i]]
    df_name <- df_names[i]
    moderator_results <- list()

    for (j in seq_along(moderators)) {
      moderator <- moderators[[j]]

      # Check the number of levels in the moderator variable
      if (length(unique(df[[moderator]])) <= 1) {
        # Skip the moderator if there is only one level
        next
      }

      # Scale continuous moderators if specified
      if (moderator %in% scale_mods) {
        df[[moderator]] <- scale(df[[moderator]])
      }
      # Perform the meta-analytic computations for the current moderator
      # Wrap the computations in a tryCatch block to handle convergence
issues
      tryCatch({
        anova_result <- rma.mv(yi, vi,
                               data = df,
                               level = 95,
                               method = "REML",
                               tdist = TRUE,
                               mods = as.formula(paste("~", moderator)),
                               random = ~ 1 | study_id/esid,
                               control=list(rel.tol=1e-8))

        mods_result <- rma.mv(yi, vi,
                              data = df,
                              level = 95,
                              method = "REML",
                              tdist = TRUE,
                              mods = as.formula(paste("~", moderator, "-
1")),
                              random = ~ 1 | study_id/esid,
                              control=list(rel.tol=1e-8))

        # Extract the coefficients table from the summary
        coef_table <- coef(summary(mods_result))

        # Create the moderator results data frame for the current moderator
        moderator_df <- data.frame(
          Data_Frame = df_name,
          Moderator = moderator,
          Estimate = anova_result$QM,
          Df1 = anova_result$QMdf[1],
          Df2 = anova_result$QMdf[2],
          p_value = anova_result$QMp,
          estimate = coef_table[, "estimate"],
          se = coef_table[, "se"],
          ci.lb = coef_table[, "ci.lb"],
          ci.ub = coef_table[, "ci.ub"],
          stringsAsFactors = FALSE
        )

        # Store the moderator results for the current data frame
        moderator_results[[moderator]] <- moderator_df

      }, error = function(e) {
        # Print a warning message if convergence fails for the current
moderator
        warning(paste("Convergence failed for moderator:", moderator, "in
data frame:", df_name))
      })

    }

    # Assign the moderator results for the current data frame to the output
list
    output_list[[df_name]] <- moderator_results
  }

  # Merge results for all data frames into a single data frame
  output_df <- do.call(rbind, unlist(output_list, recursive = FALSE))

  return(output_df)
}

# run the function for each moderator individually
df_list <- list(df1 = df.psychosocial.protective,
                df2 = df.psychosocial.risk,
                df3 = df.beh_inadvertent.protective,
                df4 = df.beh_inadvertent.risk,
                df5 = df.use.protective,
                df6 = df.use.risk)
df_names <- c("df.psychosocial.protective",
              "df.psychosocial.risk",
              "df.beh_inadvertent.protective",
              "df.beh_inadvertent.risk",
              "df.use.protective",
              "df.use.risk")

moderators <- list("data_adjusted", "data_type", "study_design",
"participant_category",
                   "sport_type", "sport_level", "gender", "substance_type",
"stage_1", "stage_2", "age")
scale_mods <- c("age")  # Specify the moderators to be scaled
results.meta.moderator <- run_meta.moderator(df_list, df_names, moderators,
scale_mods)

print(results.meta.moderator)

# Convert the data frame to Excel format
write_xlsx(results.meta.moderator, path = "meta.regression.xlsx")
write.csv(results.meta.moderator, file = "meta.regression.csv", row.names =
TRUE)

Cheers,
Daniel
On Mon, Jun 26, 2023 at 6:41?AM Reza Norouzian <rnorouzian at gmail.com> wrote: