function (content, measure, analysis, year, pmid, sortby, lefthand, righthand, type, independent_variable, cofactorlabel, topic, label_location, theme) { if (length(sortby) == 0 || sortby == "sortby") { sortby = "none" } first.row <- substr(content, 1, regexpr("\n", content)) num.columns <- str_count(first.row, ",") num.cofactors <- num.columns - 8 temp <- content temp <- gsub("\r", " ", fixed = TRUE, temp) temp <- gsub("\n", " ", fixed = TRUE, temp) temp <- gsub("\t", " ", fixed = TRUE, temp) temp <- gsub(",", "\",\"", fixed = TRUE, temp) temp <- paste("\"", temp, "\"", sep = "") temp <- paste("Mymatrix <- matrix(c(", temp, "), ncol=", num.columns, ", byrow=TRUE)") x <- eval(parse(file = "", n = NULL, text = temp)) first.row.header <- FALSE if (is.na(as.numeric(x[1, 2])) == TRUE) { first.row.header <- TRUE } if (first.row.header == TRUE) { x <- x[-c(1), ] } x <- x[!(is.na(as.numeric(x[, 2])) == TRUE), ] column.names <- c("Study", "year", "pmid", "registration", "exp_events", "exp_total", "control_events", "control_total") for (i in 1:num.cofactors) { column.names <- append(column.names, paste("cofactor", i, sep = "")) } dimnames(x) <- list(NULL, column.names) myframe <- data.frame(x) myframe <- na.omit(myframe) remove(x) myframe$Study <- gsub("'", "", fixed = TRUE, myframe$Study) myframe$Study <- as.character(str_trim(myframe$Study)) myframe$year <- as.numeric(as.character(str_trim(myframe$year))) myframe$pmid <- as.numeric(as.character(str_trim(myframe$pmid))) myframe$registration <- as.character(str_trim(myframe$registration)) myframe$registration <- ifelse(toupper(myframe$registration) %in% c("NO"), "Registration: no", ifelse(toupper(myframe$registration) %in% c("NA", ""), "Registration: unknown", "Registration: yes")) if (type == "subgroup.registration") { myframe$cofactor <- myframe$registration } else if (grepl("^subgroup([1-9]|10)$", type)) { selected_cofactor <- sub("^subgroup", "cofactor", type) if (!selected_cofactor %in% names(myframe)) { stop(paste("Selected", selected_cofactor, "is not present in the input data.")) } myframe$cofactor <- myframe[[selected_cofactor]] } PosParenth1 <- regexpr("(", myframe$exp_events[1], fixed = TRUE) if (PosParenth1 > 0) { PosParenth1 <- regexpr("(", myframe$exp_events, fixed = TRUE) PosParenth2 <- regexpr(")", myframe$exp_events, fixed = TRUE) myframe$exp_mean <- as.numeric(substring(myframe$exp_events, 1, PosParenth1 - 1)) myframe$exp_sd <- as.numeric(substring(myframe$exp_events, PosParenth1 + 1, PosParenth2 - 1)) PosParenth1 <- regexpr("(", myframe$control_events, fixed = TRUE) PosParenth2 <- regexpr(")", myframe$control_events, fixed = TRUE) myframe$control_mean <- as.numeric(substring(myframe$control_events, 1, PosParenth1 - 1)) myframe$control_sd <- as.numeric(substring(myframe$control_events, PosParenth1 + 1, PosParenth2 - 1)) } else { myframe$exp_events <- as.numeric(as.character(str_trim(myframe$exp_events))) myframe$control_events <- as.numeric(as.character(str_trim(myframe$control_events))) } myframe$exp_total <- as.numeric(as.character(str_trim(myframe$exp_total))) myframe$control_total <- as.numeric(as.character(str_trim(myframe$control_total))) if (sortby == "weight") { sortvalue <- NULL } if (sortby == "cofactor1") { sortvalue <- myframe$cofactor1 } if (sortby == "cofactor2") { sortvalue <- myframe$cofactor2 } if (sortby == "cofactor3") { sortvalue <- myframe$cofactor3 } if (sortby == "cofactor4") { sortvalue <- myframe$cofactor4 } if (sortby == "cofactor5") { sortvalue <- myframe$cofactor5 } if (sortby == "cofactor6") { sortvalue <- myframe$cofactor6 } if (sortby == "cofactor7") { sortvalue <- myframe$cofactor7 } if (sortby == "cofactor8") { sortvalue <- myframe$cofactor8 } if (sortby == "cofactor9") { sortvalue <- myframe$cofactor9 } if (sortby == "cofactor10") { sortvalue <- myframe$cofactor10 } if (sortby == "baseline") { if (PosParenth1 > 0) { sortvalue <- myframe$control_mean } else { sortvalue <- myframe$control_events/myframe$control_total } } if (sortby == "year") { sortvalue <- myframe$year } if (sortby == "study") { sortvalue <- myframe$Study } for (i in 1:length(myframe$Study)) { if (myframe$exp_events[i] == 0 & myframe$control_events[i] == 0) { } } attach(myframe) KUBlue = "#0022B4" SkyBlue = "#6DC6E7" pubbiastext = "Test for funnel plot asymmetry" if (analysis == "RE-Knapp-Hartung") { hartung = TRUE fixed = FALSE Random = TRUE analyticmethod = "Random effects model (Hartung-Knapp)" } else if (analysis == "RE") { hartung = FALSE fixed = FALSE Random = TRUE analyticmethod = "Random effects model" } else { hartung = FALSE fixed = TRUE Random = FALSE analyticmethod = "Fixed effects model" } if (type == "ignore") { if (measure %in% c("ROM", "MD", "SMD")) { meta1 <- metacont(exp_total, exp_mean, exp_sd, control_total, control_mean, control_sd, data = myframe, sm = measure, hakn = hartung, tau.common = TRUE, studlab = paste(Study, ", ", year, sep = "")) if (measure == "MD") { xlimits = "s" } else { xlimits = c(-2, 2) } if (length(myframe$Study) > 9) { pubbias = metabias(meta1, method.bias = "linreg", plotit = FALSE) pubbiastext = paste(pubbiastext, " (Egger): p= ", round(pubbias$p.value, 3), sep = "") } else { pubbiastext = paste(pubbiastext, ": too few studies to test", sep = "") } } else { meta1 <- metabin(exp_events, exp_total, control_events, control_total, data = myframe, sm = measure, hakn = hartung, tau.common = TRUE, method = "Inverse", level = 0.95, incr = "TA", digits = 2, digits.se = 2, allstudies = TRUE, studlab = paste(Study, ", ", year, sep = "")) xlimits = c(0.1, 10) if (length(myframe$Study) > 5) { meta1.as <- metabin(exp_events, exp_total, control_events, control_total, data = myframe, sm = "ASD", method = "I") pubbias = metabias(meta1.as, plotit = FALSE, k.min = 6) pubbiastext = paste(pubbiastext, " (Rucker): p= ", round(pubbias$p.value, 3), " (may be falsely significant if < 10 studies)", sep = "") } else { pubbiastext = paste(pubbiastext, ": too few studies to test", sep = "") } } if (sortby == "weight") { sortvalue <- 1/meta1$w.random } meta::forest(meta1, sortvalue = sortvalue, xlim = xlimits, col.diamond = "blue", col.diamond.lines = "blue", title = topic, fixed = FALSE, common = FALSE, random = TRUE, resid.hetstat = TRUE, addrows = 2, print.I2.ci = TRUE, print.tau2 = FALSE, print.p = FALSE, label.left = lefthand, label.right = righthand, text.random = analyticmethod, fs.random = 12, ff.random = 1, ff.hetstat = 2, fs.hetstat = 12) grid.text(topic, 0.5, 0.97, gp = gpar(fontsize = 14, fontface = "bold")) grid.text(pubbiastext, 0.1, 0.02, hjust = 0, gp = gpar(fontsize = 12, fontface = "bold")) } if (grepl("subgroup", type)) { myframe$cofactor <- gsub("'", "", fixed = TRUE, myframe$cofactor) myframe$cofactor <- as.character(str_trim(myframe$cofactor)) if (measure %in% c("ROM", "MD", "SMD")) { meta1 <- metacont(exp_total, exp_mean, exp_sd, control_total, control_mean, control_sd, tau.common = TRUE, data = myframe, sm = measure, print.tau2 = FALSE, hakn = hartung, studlab = paste(Study, ", ", year, sep = ""), label.left = lefthand, label.right = righthand, title = topic, subgroup = myframe$cofactor, print.subgroup.name = FALSE) if (measure == "MD") { xlimits = "s" } else { xlimits = c(-2, 2) } if (length(myframe$Study) > 9) { meta1.egger <- metacont(exp_total, exp_mean, exp_sd, control_total, control_mean, control_sd, tau.common = TRUE, data = myframe, print.tau2 = FALSE, sm = measure) pubbias = metabias(meta1.egger, method.bias = "linreg", plotit = FALSE) pubbiastext = paste(pubbiastext, " (Egger): p= ", round(pubbias$p.value, 3), sep = "") } else { pubbiastext = paste(pubbiastext, ": too few studies to test", sep = "") } } else { meta1 <- metabin(exp_events, exp_total, control_events, control_total, tau.common = TRUE, data = myframe, sm = measure, method = "Inverse", hakn = hartung, level = 0.95, incr = "TA", allstudies = TRUE, studlab = paste(Study, ", ", year, sep = ""), label.left = lefthand, label.right = righthand, title = topic, subgroup = myframe$cofactor, print.subgroup.name = FALSE) xlimits = c(0.1, 10) if (length(myframe$Study) > 9) { meta1.as <- metabin(exp_events, exp_total, control_events, control_total, tau.common = TRUE, data = myframe, sm = "ASD", method = "I") pubbias = metabias(meta1.as, plotit = FALSE) pubbiastext = paste(pubbiastext, " (Rucker): p= ", round(pubbias$p.value, 3), sep = "") } else { pubbiastext = paste(pubbiastext, ": too few studies to test", sep = "") } } if (sortby == "weight") { sortvalue <- 1/meta1$w.random } forest(meta1, sortvalue = sortvalue, col.diamond = "blue", col.diamond.lines = "blue", fixed = FALSE, common = FALSE, random = TRUE, subgroup = TRUE, print.Q.subgroup = FALSE, print.pval.Q = TRUE, resid.hetstat = TRUE, addrows = 2, print.I2.ci = TRUE, print.tau2 = FALSE, print.Q = FALSE, print.p = FALSE, label.left = lefthand, label.right = righthand, text.random = analyticmethod, fs.random = 12, ff.random = 1, ff.hetstat = 2, fs.hetstat = 12) grid.text(topic, 0.5, 0.97, gp = gpar(fontsize = 14, fontface = "bold")) grid.text(pubbiastext, 0.1, 0.04, hjust = 0, gp = gpar(fontsize = 12, fontface = "bold")) } if (type == "metaregression") { if (length(independent_variable) == 0 || is.na(independent_variable) || independent_variable %in% c("", "na")) { independent_variable <- "cf1" } moderator_categorical <- FALSE moderator_levels <- NULL if (independent_variable == "year") { myframe$x <- myframe$year } else if (independent_variable == "size") { myframe$x <- myframe$exp_total + myframe$control_total } else if (independent_variable == "cr") { if (measure %in% c("MD", "SMD", "ROM")) { myframe$x <- myframe$control_mean } else { myframe$x <- myframe$control_events/myframe$control_total } } else if (independent_variable == "tr") { if (measure %in% c("MD", "SMD", "ROM")) { myframe$x <- myframe$exp_mean } else { myframe$x <- myframe$exp_events/myframe$exp_total } } else if (grepl("^cf([1-9]|10)$", independent_variable)) { moderator_column <- sub("^cf", "cofactor", independent_variable) if (!moderator_column %in% names(myframe)) { stop(paste("Selected", moderator_column, "is not present in the input data.")) } moderator_text <- trimws(as.character(myframe[[moderator_column]])) moderator_text <- trimws(gsub("^'|'$", "", moderator_text)) moderator_text[toupper(moderator_text) %in% c("", "NA", "N/A", "NULL")] <- NA_character_ observed <- moderator_text[!is.na(moderator_text)] if (length(observed) == 0) { stop(paste("No non-missing values for", moderator_column, ".")) } observed_numeric <- suppressWarnings(as.numeric(observed)) if (all(is.finite(observed_numeric))) { myframe$x <- suppressWarnings(as.numeric(moderator_text)) } else if (all(is.na(observed_numeric)) && length(unique(observed)) == 2) { moderator_categorical <- TRUE moderator_levels <- sort(unique(observed)) myframe$x <- match(moderator_text, moderator_levels) - 1L } else { stop(paste("For meta-regression,", moderator_column, "must contain numeric values or exactly two distinct text categories.", "Mixed numeric/text values and more than two text categories are not supported.")) } } else { stop("Select an independent variable for meta-regression.") } excluded_moderator <- sum(!is.finite(myframe$x)) myframe <- myframe[is.finite(myframe$x), , drop = FALSE] if (nrow(myframe) < 3) { stop("Meta-regression needs at least three studies with non-missing moderator values.") } if (length(unique(myframe$x)) < 2) { stop("Meta-regression needs at least two distinct moderator values.") } if (measure %in% c("OR", "RR")) { dat <- metafor::escalc(measure = measure, ai = exp_events, bi = exp_total - exp_events, ci = control_events, di = control_total - control_events, data = myframe) ylabel <- if (measure == "OR") "Log odds ratio" else "Log risk ratio" ratio_label <- if (measure == "OR") "Ratio of odds ratios" else "Ratio of risk ratios" } else if (measure %in% c("MD", "SMD", "ROM")) { dat <- metafor::escalc(measure = measure, m1i = exp_mean, sd1i = exp_sd, n1i = exp_total, m2i = control_mean, sd2i = control_sd, n2i = control_total, data = myframe) ylabel <- switch(measure, MD = "Mean difference", SMD = "Standardized mean difference", ROM = "Log ratio of means") ratio_label <- if (measure == "ROM") "Ratio of ratios of means" else NULL } else { stop(paste("Unsupported meta-regression effect measure:", measure)) } valid_outcome <- is.finite(dat$yi) & is.finite(dat$vi) & dat$vi > 0 excluded_outcome <- sum(!valid_outcome) dat <- dat[valid_outcome, , drop = FALSE] if (nrow(dat) < 3 || length(unique(dat$x)) < 2) { stop("Too few studies with valid effect estimates and different moderator values.") } if (measure %in% c("MD", "SMD", "ROM")) { res <- metafor::rma(yi, vi, mods = ~x, method = "DL", test = "knha", data = dat) } else { res <- metafor::rma(yi, vi, mods = ~x, data = dat) } pred_x <- if (moderator_categorical) c(0, 1) else seq(min(dat$x), max(dat$x), length.out = 101) predictions <- predict(res, newmods = pred_x) pred_y <- as.numeric(predictions$pred) pred_lb <- as.numeric(predictions$ci.lb) pred_ub <- as.numeric(predictions$ci.ub) wi <- 1/sqrt(dat$vi) point_size <- if (diff(range(wi)) > 0) { 0.7 + 2.2 * (wi - min(wi))/diff(range(wi)) } else { rep(1.4, length(wi)) } oldpar <- par(no.readonly = TRUE) on.exit(par(oldpar), add = TRUE) par(mar = c(6, 5, 6.5, 2) + 0.1) plot_y <- range(c(dat$yi, pred_lb, pred_ub), finite = TRUE) plot_margin <- max(diff(plot_y) * 0.12, 0.1) plot_y <- plot_y + c(-plot_margin, plot_margin) plot_x <- if (moderator_categorical) c(-0.2, 1.2) else { x_span <- diff(range(dat$x)) range(dat$x) + c(-0.08, 0.08) * x_span } plot(dat$x, dat$yi, pch = 19, cex = point_size, xlim = plot_x, ylim = plot_y, xaxt = if (moderator_categorical) "n" else "s", xlab = if (nzchar(cofactorlabel)) cofactorlabel else if (grepl("^cf", independent_variable)) "Cofactor" else independent_variable, ylab = ylabel, main = paste("Meta-regression of", topic), las = 1, bty = "l", cex.lab = 1.2) if (moderator_categorical) { axis(1, at = c(0, 1), labels = moderator_levels, cex.axis = 0.9) segments(pred_x, pred_lb, pred_x, pred_ub, col = "blue", lwd = 2) points(pred_x, pred_y, pch = 18, col = "blue", cex = 1.7) legend("topleft", legend = c("Study effect", "Fitted category effect and 95% CI"), pch = c(19, 18), col = c("black", "blue"), bty = "n", cex = 0.85) } else { lines(pred_x, pred_y) lines(pred_x, pred_lb, lty = "dashed", col = "blue") lines(pred_x, pred_ub, lty = "dashed", col = "blue") legend("topleft", legend = c("Regression line", "95% confidence interval"), lty = c("solid", "dashed"), col = c("black", "blue"), bty = "n") } abline(h = 0, lty = "dotted") beta <- as.numeric(res$b[2]) beta_lb <- as.numeric(res$ci.lb[2]) beta_ub <- as.numeric(res$ci.ub[2]) p_text <- format.pval(as.numeric(res$pval[2]), digits = 3, eps = 0.001) if (moderator_categorical) { mtext(paste0("Comparison: ", moderator_levels[2], " vs ", moderator_levels[1], " (reference)"), side = 3, line = 3, cex = 0.9) mtext(sprintf("Between-category beta = %.3f (95%% CI %.3f to %.3f); p = %s", beta, beta_lb, beta_ub, p_text), side = 3, line = 1.8, cex = 0.9) if (!is.null(ratio_label)) { mtext(sprintf("%s = %.3f (95%% CI %.3f to %.3f)", ratio_label, exp(beta), exp(beta_lb), exp(beta_ub)), side = 3, line = 0.6, cex = 0.9) } } else { mtext(sprintf("Slope per unit = %.3f; p = %s", beta, p_text), side = 3, line = 2.2, cex = 0.95) mtext(sprintf("R2 = %.1f%%; residual I2 = %.1f%%", res$R2, res$I2), side = 3, line = 1, cex = 0.9) } label_pos <- suppressWarnings(as.integer(label_location)) if (length(label_pos) == 1 && !is.na(label_pos) && label_pos %in% 1:4) { text(dat$x, dat$yi, paste(dat$Study, dat$year, sep = ", "), cex = 0.8, pos = label_pos, offset = 1) } if (excluded_moderator + excluded_outcome > 0) { mtext(sprintf("Excluded studies: %d missing moderator, %d invalid effects", excluded_moderator, excluded_outcome), side = 1, line = 4.3, cex = 0.75) } } if (type == "funnel") { if (measure %in% c("ROM", "MD", "SMD")) { meta1 <- metacont(exp_total, exp_mean, exp_sd, control_total, control_mean, control_sd, data = myframe, sm = measure, hakn = hartung, studlab = paste(Study, ", ", year, sep = ""), label.left = lefthand, label.right = righthand, title = topic, subgroup = myframe$cofactor, print.subgroup.name = FALSE) } else { meta1 <- metabin(exp_events, exp_total, control_events, control_total, data = myframe, sm = measure, method = "Inverse", tau.common = TRUE, hakn = hartung, level = 0.95, incr = "TA", allstudies = TRUE, studlab = paste(Study, ", ", year, sep = ""), label.left = lefthand, label.right = righthand, title = topic, subgroup = myframe$cofactor, print.subgroup.name = FALSE) } funnel(meta1, common = TRUE) if (length(meta1$studlab) > 5) { meta1.as <- metabin(exp_events, exp_total, control_events, control_total, tau.common = TRUE, data = myframe, sm = "ASD", method = "I") pubbias = metabias(meta1.as, plotit = FALSE, k.min = 6) pubbiastext = paste(pubbiastext, ":\np (Rucker) = ", round(pubbias$p.value, 3), " (may be falsely significant if < 10 studies)", sep = "") } else { pubbiastext = paste(pubbiastext, ":\ntoo few studies to test", sep = "") } grid.text(topic, 0.5, 0.97, gp = gpar(fontsize = 14, fontface = "bold")) grid.text(pubbiastext, 0.125, 0.8, hjust = 0, gp = gpar(fontsize = 12, fontface = "bold")) } }