### Example Data Frame Setup Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.fivenum.html Sets up an example data frame with mixed five-number summary values and direct mean/SD values for demonstration purposes. ```R dat <- data.frame(case=c(1:3,NA), min=c(2,NA,2,NA), q1=c(NA,4,4,NA), median=c(6,6,6,NA), q3=c(NA,10,10,NA), max=c(14,NA,14,NA), mean=c(NA,NA,NA,7.0), sd=c(NA,NA,NA,4.2), n=c(20,20,20,20)) dat ``` -------------------------------- ### Install metafor Package Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/00-index.md Installs the metafor package from CRAN. Load the library to use its functions. ```r install.packages("metafor") library(metafor) ``` -------------------------------- ### Meta-analysis Workflow Example Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/00-index.md A comprehensive example demonstrating a typical meta-analysis workflow using the metafor package, from data loading and effect size calculation to model fitting, visualization, and post-analysis. ```r library(metafor) # 1. Load and examine data data(dat.bcg) head(dat.bcg) # 2. Calculate effect sizes dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg, slab=paste(author, year, sep=", ")) # 3. Fit model res <- rma(yi, vi, data=dat, test="knha") res # 4. Visualize forest(res, atransf=exp) funnel(res) # 5. Post-analysis predict(res, transf=exp) confint(res) leave1out(res) regtest(res) # 6. Meta-regression (if moderators available) res.mr <- rma(yi, vi, mods=~ablat, data=dat) regplot(res.mr, mod="ablat") ``` -------------------------------- ### Get Help and Load Data in Metafor Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/README.md Provides R code to access help files for metafor functions, load example datasets, and view package vignettes. ```r # Get help ?rma # Main function help ?escalc # Effect size calculation help(package="metafor") # Package overview # Load data data(dat.bcg) # Example dataset data(package="metafor") # List all datasets # Read vignettes vignette("metafor") # Main vignette # Citation citation(package="metafor") ``` -------------------------------- ### Install Development Version of metafor Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/00-index.md Installs the latest development version of the metafor package from GitHub. Requires the 'remotes' package. ```r install.packages("remotes") remotes::install_github("wviechtb/metafor") ``` -------------------------------- ### Example: Fitting a Mixed-Effects Meta-Regression Model and Extracting Studentized Residuals Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/methods.list.rma.html This example demonstrates how to prepare data, fit a mixed-effects meta-regression model using `rma`, and then extract and filter studentized residuals using `rstudent`. Note the special behavior for subsetting residuals with 'rma.list' objects. ```R ### copy data into 'dat' and examine data dat <- dat.viechtbauer2021 ### calculate log odds ratios and corresponding sampling variances dat <- escalc(measure="OR", ai=xTi, n1i=nTi, ci=xCi, n2i=nCi, add=1/2, to="all", data=dat) ### fit mixed-effects meta-regression model res <- rma(yi, vi, mods = ~ dose, data=dat) ### get studentized residuals sav <- rstudent(res) sav #> #> resid se z #> 1 0.5344 0.4849 1.1021 #> 2 -0.3653 0.5163 -0.7076 #> 3 -0.0468 0.4815 -0.0972 #> 4 -0.3142 0.4177 -0.7522 #> 5 0.1743 0.4262 0.4089 #> 6 -0.9963 0.3195 -3.1187 #> 7 0.0250 0.3723 0.0672 #> 8 0.9197 0.7572 1.2147 #> 9 0.7779 0.3678 2.1149 #> 10 0.3078 0.4548 0.6768 #> 11 -0.2261 0.3995 -0.5661 #> 12 -0.2763 0.4113 -0.6718 #> 13 -0.6072 0.4468 -1.3590 #> 14 0.9350 0.7213 1.2964 #> 15 0.2894 0.4754 0.6087 #> 16 -0.1011 0.5738 -0.1762 #> 17 0.2310 0.6001 0.3849 #> 18 0.0103 0.4944 0.0207 #> 19 0.3425 0.3941 0.8690 #> 20 -0.3079 0.4618 -0.6667 #> ### studies with studentized residuals larger than +-1.96 sav[abs(sav$z) > 1.96,] #> #> resid se z #> 6 -0.9963 0.3195 -3.1187 #> 9 0.7779 0.3678 2.1149 #> ### variables specified are automatically searched for within the object itself sav[abs(z) > 1.96,] #> #> resid se z #> 6 -0.9963 0.3195 -3.1187 #> 9 0.7779 0.3678 2.1149 #> ### note: this behavior is specific to 'rma.list' objects; this doesn't work for regular data frames ``` -------------------------------- ### Load Metafor Package Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/00-index.md This is the required command to load the metafor package before running any examples. Ensure this is executed first. ```r library(metafor) ``` -------------------------------- ### Rounding Coefficients with dfround Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/10-data-manipulation.md This example shows how to round the coefficients of a model to a specified number of decimal places using dfround. ```r # Round results as.data.frame(coef(res)) %>% dfround(digits=3) ``` -------------------------------- ### Example Output: Prediction Intervals Source: https://github.com/wviechtb/metafor/blob/master/README.md This output shows predicted values and their confidence and prediction intervals for a meta-analysis. ```text ## pred ci.lb ci.ub pi.lb pi.ub ## 0.49 0.33 0.73 0.14 1.76 ``` -------------------------------- ### Elaborate Example: BCG Vaccine Data Analysis Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.wald.html This example processes the BCG vaccine dataset, first using `escalc` to compute log odds ratios and their variances, and then fitting a random-effects model to the results. It showcases a typical workflow for meta-analysis with binary outcome data. ```R dat <- dat.bcg[,c(2:7)] dat #> author year tpos tneg cpos cneg #> 1 Aronson 1948 4 119 11 128 #> 2 Ferguson & Simes 1949 6 300 29 274 #> 3 Rosenthal et al 1960 3 228 11 209 #> 4 Hart & Sutherland 1977 62 13536 248 12619 #> 5 Frimodt-Moller et al 1973 33 5036 47 5761 #> 6 Stein & Aronson 1953 180 1361 372 1079 #> 7 Vandiviere et al 1973 8 2537 10 619 #> 8 TPT Madras 1980 505 87886 499 87892 #> 9 Coetzee & Berjak 1968 29 7470 45 7232 #> 10 Rosenthal et al 1961 17 1699 65 1600 #> 11 Comstock et al 1974 186 50448 141 27197 #> 12 Comstock & Webster 1969 5 2493 3 2338 #> 13 Comstock et al 1976 27 16886 29 17825 ### with complete data, we can use escalc() in the usual way dat1 <- escalc(measure="OR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat) dat1 #> #> author year tpos tneg cpos cneg yi vi #> 1 Aronson 1948 4 119 11 128 -0.9387 0.3571 #> 2 Ferguson & Simes 1949 6 300 29 274 -1.6662 0.2081 #> 3 Rosenthal et al 1960 3 228 11 209 -1.3863 0.4334 #> 4 Hart & Sutherland 1977 62 13536 248 12619 -1.4564 0.0203 #> 5 Frimodt-Moller et al 1973 33 5036 47 5761 -0.2191 0.0520 #> 6 Stein & Aronson 1953 180 1361 372 1079 -0.9581 0.0099 #> 7 Vandiviere et al 1973 8 2537 10 619 -1.6338 0.2270 #> 8 TPT Madras 1980 505 87886 499 87892 0.0120 0.0040 #> 9 Coetzee & Berjak 1968 29 7470 45 7232 -0.4717 0.0570 #> 10 Rosenthal et al 1961 17 1699 65 1600 -1.4012 0.0754 #> 11 Comstock et al 1974 186 50448 141 27197 -0.3408 0.0125 #> 12 Comstock & Webster 1969 5 2493 3 2338 0.4466 0.5342 #> 13 Comstock et al 1976 27 16886 29 17825 -0.0173 0.0716 #> ### random-effects model fitted to these data res1 <- rma(yi, vi, data=dat1) res1 #> #> Random-Effects Model (k = 13; tau^2 estimator: REML) #> #> tau^2 (estimated amount of total heterogeneity): 0.3378 (SE = 0.1784) #> tau (square root of estimated tau^2 value): 0.5812 #> ``` -------------------------------- ### Reconstruct 2x2 Table with Larger Frequencies Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.2x2.html This example shows the reconstruction of a 2x2 table with cell frequencies that are 10 times larger than the previous example. It follows the same logic of calculating summary statistics and then reconstructing the table. ```R ### same example but with cell frequencies that are 10 times as large dat <- data.frame(ai=c(360,NA,NA,NA), bi=c(860,NA,NA,NA), ci=c(200,NA,NA,NA), di=c(980,NA,NA,NA), oddsratio=NA, phi=NA, chisq=NA, ni=NA, n1i=NA, n2i=NA) dat$oddsratio[2] <- round(exp(escalc(measure="OR", ai=ai, bi=bi, ci=ci, di=di, data=dat)$yi[1]), 2) dat$phi[3] <- round(escalc(measure="PHI", ai=ai, bi=bi, ci=ci, di=di, data=dat)$yi[1], 2) ``` -------------------------------- ### Check metafor Package Version and Citation Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/00-index.md Retrieves the installed version of the metafor package and displays its citation information. ```r packageVersion("metafor") citation(package="metafor") ``` -------------------------------- ### Open metafor Package Diagram from R Source: https://github.com/wviechtb/metafor/blob/master/docs/articles/pkgdown/diagram.html Opens the metafor package diagram directly from an R console session. Requires the package to be installed. ```r vignette("diagram") ``` -------------------------------- ### Running Metafor Vignettes Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/13-configuration-reference.md Use this command to access the comprehensive vignettes for the metafor package, which provide detailed examples and explanations of its functionalities. ```r vignette("metafor") ``` -------------------------------- ### Example Data Frame for conv.fivenum Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.fivenum.html This snippet defines an example data frame containing five number summary statistics and sample sizes, which will be used as input for the `conv.fivenum` function. ```r # example data frame dat <- data.frame(case=c(1:3,NA), min=c(2,NA,2,NA), q1=c(NA,4,4,NA), median=c(6,6,6,NA), q3=c(NA,10,10,NA), max=c(14,NA,14,NA), mean=c(NA,NA,NA,7.0), sd=c(NA,NA,NA,4.2), n=c(20,20,20,20)) dat ``` -------------------------------- ### R Output for Posting Source: https://github.com/wviechtb/metafor/blob/master/docs/ISSUE_TEMPLATE.html Placeholder comment indicating where to insert R output for reproducible examples. ```r # use this for posting output (if applicable) ``` -------------------------------- ### Plotting with Trimmed Distributions Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/plot.permutest.rma.uni.html This example demonstrates plotting the permutation distribution after trimming a fraction of observations from the tails using the 'trim' argument. This can help in visualizing the central part of the distribution. ```R plot(object, trim=0.1) ``` -------------------------------- ### Set and Get Metafor Space Option Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/mfopt.html Sets the 'space' option to FALSE and then retrieves all current options to verify the change. ```R setmfopt(space=FALSE) getmfopt() ``` -------------------------------- ### Restructure Data to Wide Format Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/contrmat.html This example demonstrates how to restructure a dataset to wide format using the to.wide function, a prerequisite for using contrmat. ```r dat <- dat.senn2013 dat <- dat[c(1,4,3,2,5,6)] dat <- to.wide(dat, study="study", grp="treatment", ref="placebo", grpvars=4:6) dat ``` -------------------------------- ### Reconstruct 2x2 Table from Odds Ratio and Marginals (Example) Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.2x2.html Reconstructs a 2x2 table using a provided odds ratio and marginal counts. ```R conv.2x2(ri=0.54, ni=400, n1i=200, n2i=248) # using the true marginal counts ``` -------------------------------- ### Inspect Data Before Aggregation (Second Example) Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/aggregate.escalc.html Displays the first few rows of a different dataset to understand its structure before performing aggregation. This is useful for checking data integrity and variable types. ```R dat <- dat.assink2016 head(dat, 19) ``` -------------------------------- ### Extracting Coefficients from Permutation Test Results Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/coef.permutest.rma.uni.html This example demonstrates how to fit a mixed-effects model, conduct a permutation test, and then extract the results using `coef()`. ```r ### calculate log risk ratios and corresponding sampling variances dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg) ### fit mixed-effects model with absolute latitude and publication year as moderators res <- rma(yi, vi, mods = ~ ablat + year, data=dat) ### carry out permutation test set.seed(1234) # for reproducibility sav <- permutest(res) #> Running 1000 iterations for an approximate permutation test. coef(sav) #> estimate se zval pval ci.lb ci.ub #> intrcpt -3.545505079 29.09587983 -0.1218559 0.907 -60.57238164 53.481371479 #> ablat -0.028011275 0.01023404 -2.7370689 0.016 -0.04806963 -0.007952924 #> year 0.001907557 0.01468382 0.1299088 0.901 -0.02687219 0.030687307 ``` -------------------------------- ### View metafor Package NEWS File Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/metafor-package.html Displays the NEWS file of the metafor package after installation, providing details on recent updates and changes. ```r metafor.news() ``` -------------------------------- ### Example Output: Mixed-Effects Model Results Source: https://github.com/wviechtb/metafor/blob/master/README.md This output details the results of a mixed-effects meta-regression model, including estimates of heterogeneity, moderator effects, and model fit statistics. ```text ## Mixed-Effects Model (k = 13; tau^2 estimator: REML) ## ## tau^2 (estimated amount of residual heterogeneity): 0.0764 (SE = 0.0591) ## tau (square root of estimated tau^2 value): 0.2763 ## I^2 (residual heterogeneity / unaccounted variability): 68.39% ## H^2 (unaccounted variability / sampling variability): 3.16 ## R^2 (amount of heterogeneity accounted for): 75.62% ## ## Test for Residual Heterogeneity: ## QE(df = 11) = 30.7331, p-val = 0.0012 ## ## Test of Moderators (coefficient 2): ## F(df1 = 1, df2 = 11) = 12.5905, p-val = 0.0046 ## ## Model Results: ## ## estimate se tval df pval ci.lb ci.ub ## intrcpt 0.2515 0.2839 0.8857 11 0.3948 -0.3735 0.8764 ## ablat -0.0291 0.0082 -3.5483 11 0.0046 -0.0472 -0.0111 ** ## ## --- ``` -------------------------------- ### Apply Function During Splitting Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/blsplit.html This example illustrates using the 'fun' argument of blsplit to directly apply a function (cov2cor) to each sub-matrix as it is being split from the block diagonal matrix V. ```R blsplit(V, cluster=dat$study, cov2cor)[1:2] ``` -------------------------------- ### Demonstrate 2x2 Table Reconstruction Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.2x2.html This example demonstrates the reconstruction of a 2x2 table using the conv.2x2 function. It first calculates various summary statistics (odds ratio, phi, chi-squared) from initial cell frequencies and then uses these to reconstruct the table. ```R ### demonstration that the reconstruction of the 2x2 table works ### (note: the values in rows 2, 3, and 4 correspond to those in row 1) dat <- data.frame(ai=c(36,NA,NA,NA), bi=c(86,NA,NA,NA), ci=c(20,NA,NA,NA), di=c(98,NA,NA,NA), oddsratio=NA, phi=NA, chisq=NA, ni=NA, n1i=NA, n2i=NA) dat$oddsratio[2] <- round(exp(escalc(measure="OR", ai=ai, bi=bi, ci=ci, di=di, data=dat)$yi[1]), 2) dat$phi[3] <- round(escalc(measure="PHI", ai=ai, bi=bi, ci=ci, di=di, data=dat)$yi[1], 2) dat$chisq[4] <- round(chisq.test(matrix(c(t(dat[1,1:4])), nrow=2, byrow=TRUE))$statistic, 2) dat$ni[2:4] <- with(dat, ai[1] + bi[1] + ci[1] + di[1]) dat$n1i[2:4] <- with(dat, ai[1] + bi[1]) dat$n2i[2:4] <- with(dat, ai[1] + ci[1]) dat #> ai bi ci di oddsratio phi chisq ni n1i n2i #> 1 36 86 20 98 NA NA NA NA NA NA #> 2 NA NA NA NA 2.05 NA NA 240 122 56 #> 3 NA NA NA NA NA 0.15 NA 240 122 56 #> 4 NA NA NA NA NA NA 4.61 240 122 56 ### reconstruct cell frequencies for rows 2, 3, and 4 dat <- conv.2x2(ri=phi, ori=oddsratio, x2i=chisq, ni=ni, n1i=n1i, n2i=n2i, data=dat) dat #> ai bi ci di oddsratio phi chisq ni n1i n2i #> 1 36 86 20 98 NA NA NA NA NA NA #> 2 36 86 20 98 2.05 NA NA 240 122 56 #> 3 36 86 20 98 NA 0.15 NA 240 122 56 #> 4 36 86 20 98 NA NA 4.61 240 122 56 ``` -------------------------------- ### Logistic Regression (Binary) Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of using `rma.glmm` for logistic regression to analyze binary outcomes. ```APIDOC ## Logistic Regression (Binary) ### Description Use `rma.glmm` for standard binomial logistic regression with binary outcomes. ### Function Signature ```r rma.glmm(measure, ai, bi, ci, di, ...) ``` ### Parameters - `measure`: Set to `"OR"` for Odds Ratios. - `ai`: Events in the treatment group. - `bi`: Non-events in the treatment group. - `ci`: Events in the control group. - `di`: Non-events in the control group. ### Example ```r # Standard binomial GLMM res.glmm.log <- rma.glmm(measure="OR", ai=events, bi=non.events, ci=control.events, di=control.non.events) ``` ``` -------------------------------- ### Generate Meta-Regression Plot Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of creating a regression plot for a specific moderator from a meta-regression model. ```r # Regression plot regplot(res.mr, mod="ablat") ``` -------------------------------- ### R Code for Posting Source: https://github.com/wviechtb/metafor/blob/master/docs/ISSUE_TEMPLATE.html Placeholder comment indicating where to insert R code for reproducible examples. ```r # use this for posting code (if applicable) ``` -------------------------------- ### Simple Example: Convert Odds Ratios to Log Odds Ratios Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.wald.html This snippet demonstrates a basic conversion of odds ratios and their confidence intervals into log odds ratios with corresponding sampling variances. It uses the `log` transformation for the effect size. ```R dat <- data.frame(or=c(1.37,1.89), or.lb=c(1.03,1.60), or.ub=c(1.82,2.23)) dat #> or or.lb or.ub #> 1 1.37 1.03 1.82 #> 2 1.89 1.60 2.23 ### convert the odds ratios and CIs into log odds ratios with corresponding sampling variances dat <- conv.wald(out=or, ci.lb=or.lb, ci.ub=or.ub, data=dat, transf=log) dat #> #> or or.lb or.ub yi vi #> 1 1.37 1.03 1.82 0.3148 0.0211 #> 2 1.89 1.60 2.23 0.6366 0.0072 #> ``` -------------------------------- ### Fit Meta-Regression with Categorical Moderator Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of fitting a meta-regression model using a categorical moderator (group). ```r # Categorical moderator dat$group <- factor(dat$alloc) res.mr4 <- rma(yi, vi, mods=~group, data=dat) ``` -------------------------------- ### Constructing Pairwise Contrasts for RMA Objects Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/pairmat.html This example demonstrates how to construct a pairwise contrast matrix for a meta-regression model. It first fits a mixed-effects model with a categorical moderator and then uses pairmat to generate the contrast matrix. ```R ### calculate log risk ratios and corresponding sampling variances dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg) ### mixed-effects meta-regression model with the allocation method as a moderator; ### by removing the intercept term, we obtain the estimated average effect for each ### factor level from the model res <- rma(yi, vi, mods = ~ 0 + alloc, data=dat) res ``` -------------------------------- ### Get Specific Metafor Option Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/mfopt.html Retrieves the value of a specific global option, in this case, the 'space' option. ```R getmfopt(space) ``` -------------------------------- ### Fit Meta-Regression with Multiple Moderators Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of fitting a meta-regression model with multiple moderators (absolute latitude and year). ```r # Multiple moderators res.mr2 <- rma(yi, vi, mods=~ablat + year, data=dat) ``` -------------------------------- ### Apply and Round Function During Splitting Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/blsplit.html This example demonstrates applying a function (round) with an additional argument (3) during the splitting process of the block diagonal matrix V. The result is a list of rounded sub-matrices. ```R blsplit(V, cluster=dat$study, round, 3)[1:2] ``` -------------------------------- ### Fit Simple Meta-Regression Model Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of fitting a simple meta-regression model with absolute latitude as a single moderator. ```r # Simple meta-regression res.mr <- rma(yi, vi, mods=~ablat, data=dat) ``` -------------------------------- ### Load and Inspect Dataset Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/vcalc.html Loads the 'knapp2017' dataset and displays a subset of its columns. This is a common first step for data analysis. ```r dat <- dat.knapp2017 dat[-c(1:2)] ``` -------------------------------- ### Prepare Data for Meta-Analysis Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.wald.html This snippet shows how to assemble a dataset from study information, including handling missing values and rounding reported statistics. It prepares the data frame for subsequent meta-analysis calculations. ```R dat2 <- data.frame(summary(dat1)) dat2[c("yi", "ci.lb", "ci.ub")] <- data.frame(summary(dat1, transf=exp))[c("yi", "ci.lb", "ci.ub")] names(dat2)[which(names(dat2) == "yi")] <- "or" dat2[,c("or","ci.lb","ci.ub","pval")] <- round(dat2[,c("or","ci.lb","ci.ub","pval")], digits=2) dat2$vi <- dat2$sei <- dat2$zi <- NULL dat2$ntot <- with(dat2, tpos + tneg + cpos + cneg) dat2[c(1,12),c(3:6,9:10)] <- NA dat2[c(4,9), c(3:6,8)] <- NA dat2[c(2:3,5:8,10:11,13),c(7:10)] <- NA dat2$ntot[!is.na(dat2$tpos)] <- NA dat2 ``` -------------------------------- ### Recommended Analysis Pipeline in Metafor Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/13-configuration-reference.md This snippet outlines a comprehensive analysis pipeline for meta-analysis using the metafor package, including steps for calculating effect sizes, fitting initial models, assessing publication bias, checking for outliers, examining heterogeneity, exploring moderators, and final visualization. ```r # 1. Calculate effect sizes dat <- escalc(measure="RR", ai=a, bi=b, ci=c, di=d, data=raw) # 2. Initial model res <- rma(yi, vi, data=dat, test="knha") # 3. Assess publication bias regtest(res) # Egger's test ranktest(res) # Rank test funnel(res) # Visual assessment # 4. Check for outliers/influence leave1out(res) baujat(res) # 5. Examine heterogeneity confint(res) # CI for tau2 # 6. Explore moderators (if applicable) res.mr <- rma(yi, vi, mods=~moderator, data=dat, test="knha") anova(res, res.mr) # Test moderation # 7. Final visualization forest(res, atransf=exp) ``` -------------------------------- ### Common Transformation Functions Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/05-forest.md Provides examples of common transformation functions used in meta-analysis, such as log, logit, and z-score transformations. ```r # Log transformation (for log-scale effects) transf=exp, atransf=log # Logit-based transformations transf=transf.ilogit # Correlation z-score transformation transf=transf.ztor # Fisher's z-score back-transform transf=transf.ztor ``` -------------------------------- ### Estimate Average Risk Ratio with CI/PI Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/predict.rma.html This example demonstrates how to estimate the average risk ratio (by applying the exponential transformation) with its 95% confidence and prediction intervals, formatted to two decimal places. ```r predict(res, transf=exp, digits=2) ``` -------------------------------- ### Reset and Get Metafor Space Option Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/mfopt.html Resets the 'space' option to TRUE and then retrieves all current options to confirm the update. ```R setmfopt(space=TRUE) getmfopt() ``` -------------------------------- ### Specify Confidence Interval Method for Random-Effects Models Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/08-confint.md Demonstrates how to compute confidence intervals for a random-effects model using different methods, including the default, Q-profile, and generalized methods. ```r # Default confint(res) # Specify method confint(res, method="Q-profile") confint(res, method="generalized") ``` -------------------------------- ### Fixed-Effects Model for Heterogeneity Testing Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/rma.uni.html Demonstrates fitting a fixed-effects model to calculate the total heterogeneity (Q_Total). This is used to verify that Q_E + Q_M equals Q_Total for fixed-effects models. ```R res <- rma(yi, vi, data=dat, method="FE") res ``` -------------------------------- ### Fit Meta-Regression Model Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/robust.html Fit a meta-regression model with 'length' as a moderator. This is a standard starting point before applying robust inference. ```r res <- rma(yi, vi, mods = ~ length, data=dat) ``` -------------------------------- ### Displaying V Matrix as a List of Matrices Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/vcalc.html This example demonstrates how to display the constructed variance-covariance matrix (V) as a list of matrices, where each matrix corresponds to a study cluster. It uses the blsplit function to split the matrix and shows the first 5 study matrices. ```R blsplit(V, dat$study)[1:5] ``` -------------------------------- ### Construct Variance-Covariance Matrix with Clustering Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/vcalc.html Example of constructing a variance-covariance matrix where effect sizes are clustered by study. This is a common scenario for meta-analyses. ```R dat <- escalc(measure="ROM", ri=yi, ni=ni, data=dat, addx=TRUE) # Construct the V matrix for the random-effects model v <- vcalc(vi=dat$vi, cluster=dat$study) # Fit the random-effects model res <- rma.mv(yi, v, data=dat) print(res) ``` -------------------------------- ### Fit Meta-Regression with Interactions Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of fitting a meta-regression model including an interaction term between absolute latitude and a categorical allocation variable. ```r # Interactions res.mr3 <- rma(yi, vi, mods=~ablat * factor(alloc), data=dat) ``` -------------------------------- ### Example Interpretation of Prediction Output Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/07-predict.md Illustrates the interpretation of prediction output, distinguishing between confidence intervals (CI) and prediction intervals (PI). ```r # Prediction output # pred = 0.5, ci: [0.3, 0.7], pi: [-0.5, 1.5] # Interpretation: # - Average pooled effect estimate: 0.5 # - We're 95% confident the true average falls between 0.3 and 0.7 # - A new study would likely yield an effect between -0.5 and 1.5 ``` -------------------------------- ### S3 Method for class 'rma.uni' Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/weights.rma.html Extracts weights for 'rma.uni' objects. Specify 'type' to get diagonal elements or the full matrix. ```R weights(object, type="diagonal", ...) ``` -------------------------------- ### Fit Beta Selection Model (Citkowicz & Vevea, 2017) Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/selmodel.html This snippet demonstrates fitting a beta selection model as described in Citkowicz and Vevea (2017). It requires loading the 'dat.baskerville2012' dataset and then fitting the model. ```R # copy data into 'dat' and examine data dat <- dat.baskerville2012 dat #> author year score design alloconc blind itt fumonths retention country outcomes duration #> 1 Kottke et al. 1992 6 cct 0 1 1 19 83.0 US 2 18 #> 2 McBride et al. 2000 6 rct 0 0 0 18 100.0 US 4 12 #> 3 Stange et al. 2000 6 rct 0 0 0 24 NA US 35 12 #> 4 Lobo et al. 2004 6 rct 1 0 0 21 57.0 NL 16 21 #> 5 Roetzhiem et al. 2005 6 crct 0 1 0 24 100.0 US 3 24 #> 6 Hogg et al. 2008 6 cct 0 0 1 6 87.0 Can 26 12 #> 7 Aspy et al. 2008 6 cct 0 1 0 18 89.0 US 4 18 #> 8 Jaen et al. 2010 6 rct 0 1 0 26 86.0 US 11 26 #> 9 Cockburn et al. 1992 7 rct 0 0 0 3 79.0 Aus 2 2 ``` -------------------------------- ### Prepare Data for Meta-Analysis of Correlation Coefficients Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.delta.html An elaborate example showing how conv.delta() can be used in data preparation steps for a meta-analysis of correlation coefficients. This involves creating a data frame with various potential input variables and then applying the conversion function. ```r dat <- data.frame(study=1:6, ri=c(0.42,NA,NA,NA,NA,NA), tval=c(NA,2.85,NA,NA,NA,NA), phi=c(NA,NA,NA,0.27,NA,NA), ni=c(93,182,NA,112,NA,NA), ai=c(NA,NA,NA,NA,61,NA), bi=c(NA,NA,NA,NA,36,NA), ci=c(NA,NA,NA,NA,39,NA), di=c(NA,NA,NA,NA,57,NA), or=c(NA,NA,NA,NA,NA,1.86), lower=c(NA,NA,NA,NA,NA,1.12), upper=c(NA,NA,NA,NA,NA,3.10), m1i=c(NA,NA,54.1,NA,NA,NA), sd1i=c(NA,NA,5.79,NA,NA,NA), n1i=c(NA,NA,66,75,NA,NA), m2i=c(NA,NA,51.7,NA,NA,NA), sd2i=c(NA,NA,6.23,NA,NA,NA), n2i=c(NA,NA,65,88,NA,NA)) dat ``` -------------------------------- ### Illustrate Shrinkage of BLUPs Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/blup.html This example visualizes the shrinkage of BLUPs towards the estimated population average. It plots the observed study effects against the BLUPs, showing how the BLUPs are pulled closer to the overall mean, especially for studies with smaller variances. ```r res <- rma(yi, vi, data=dat) blups <- blup(res)$pred plot(NA, NA, xlim=c(0.8,2.4), ylim=c(-2,0.5), pch=19, xaxt="n", bty="n", xlab="", ylab="Log Risk Ratio") segments(rep(1,13), dat$yi, rep(2,13), blups, col="darkgray") points(rep(1,13), dat$yi, pch=19) points(rep(2,13), blups, pch=19) axis(side=1, at=c(1,2), labels=c("Observed\nValues", "BLUPs"), lwd=0) segments(0, res$beta, 2.15, res$beta, lty="dotted") text(2.3, res$beta, substitute(hat(mu)==muhat, list(muhat=round(res$beta[[1]], 2))), cex=1) ``` -------------------------------- ### Convert Five Number Summary with 'qe' Method Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/conv.fivenum.html This snippet shows how to use `conv.fivenum` with the 'qe' method for estimation, including setting a random seed for reproducibility. ```r set.seed(1234) conv.fivenum(min=min, q1=q1, median=median, q3=q3, max=max, n=n, data=dat, method="qe") ``` -------------------------------- ### Generate Analysis Report Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/reporter.html This snippet demonstrates how to generate an analysis report using the reporter function. It first prepares data, fits a random-effects model, and then calls reporter on the fitted model object. The output shows the process of report generation, including directory creation, file copying, and rendering. ```r ### copy BCG vaccine data into 'dat' dat <- dat.bcg ### calculate log risk ratios and corresponding sampling variances dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat, slab=paste(author, ", ", year, sep="")) ### fit random-effects model res <- rma(yi, vi, data=dat) ### generate report reporter(res) #> #> Directory for generating the report is: /tmp/RtmpoAPsbL #> Copying references.bib and apa.csl to report directory ... #> Saving model object to report_res.rdata ... #> Creating report_res.rmd file ... #> Rendering report_res.rmd file ... #> Generated /tmp/RtmpoAPsbL/report_res.html ... #> Opening report ... ``` -------------------------------- ### Poisson Regression (Count Data) Source: https://github.com/wviechtb/metafor/blob/master/_autodocs/11-specialized-models.md Example of using `rma.glmm` for Poisson regression to analyze count data, specifically for incidence rate ratios. ```APIDOC ## Poisson Regression (Count Data) ### Description Use `rma.glmm` with `measure="IRR"` for Poisson regression on count data to obtain incidence rate ratios. ### Function Signature ```r rma.glmm(measure, ai, ti, data, ...) ``` ### Parameters - `measure`: Set to `"IRR"` for Incidence Rate Ratios. - `ai`: Event counts. - `ti`: Time at risk. - `data`: Data frame containing the variables. ### Example ```r # For incidence rate ratios res.glmm.irr <- rma.glmm(measure="IRR", ai=events, ti=time, data=count.data) ``` ``` -------------------------------- ### Get Citation Information for Metafor Source: https://github.com/wviechtb/metafor/blob/master/README.md Use this command in R to retrieve citation details for the Metafor package. This is important for academic and research use. ```r citation(package='metafor') ``` -------------------------------- ### Fit Random-Effects Model Source: https://github.com/wviechtb/metafor/blob/master/docs/reference/plot.rma.uni.selmodel.html This snippet demonstrates fitting a random-effects model using the ML estimator for heterogeneity. It then prints the model results, including heterogeneity statistics and model coefficients. ```R dat <- dat.hackshaw1998 ### fit random-effects model using the log odds ratios res <- rma(yi, vi, data=dat, method="ML") res ```