Basic meta-analysis example

library("PEcAn.MA")
library("tibble", include.only = "tribble")

set.seed(20251222)

An example of a prior distributions table, demonstrating the expected format.

prior_distns <- tribble(
  ~row_name                     , ~distn    , ~parama , ~paramb , ~n  , #nolint
  "growth_resp_factor"          , "beta"    ,  2.63   ,   6.52  ,   0 , #nolint
  "leaf_turnover_rate"          , "weibull" ,  1.37   ,   1.43  , 363 , #nolint
  "root_respiration_rate"       , "unif"    ,  0.00   , 100.00  , NA  , #nolint
  "root_turnover_rate"          , "unif"    ,  0.00   ,  10.00  , NA  , #nolint
  "Amax"                        , "unif"    ,  0.00   ,  40.00  , NA  , #nolint
  "leaf_respiration_rate_m2"    , "weibull" ,  2.00   ,   6.00  , NA  , #nolint
  "SLA"                         , "lnorm"   ,  1.89   ,   0.61  , 455 , #nolint
  "leafC"                       , "norm"    , 50.60   ,   1.32  , 291 , #nolint
  "Vm_low_temp"                 , "norm"    ,  0.00   ,   3.00  , NA  , #nolint
  "AmaxFrac"                    , "unif"    ,  0.60   ,   0.90  , NA  , #nolint
  "psnTOpt"                     , "unif"    ,  5.00   ,  40.00  , NA  , #nolint
  "stem_respiration_rate"       , "unif"    ,  0.00   , 100.00  , NA  , #nolint
  "extinction_coefficient"      , "unif"    ,  0.38   ,   0.62  , NA  , #nolint
  "half_saturation_PAR"         , "unif"    ,  4.00   ,  27.00  , NA  , #nolint
  "dVPDSlope"                   , "unif"    ,  0.01   ,   0.25  , NA  , #nolint
  "dVpdExp"                     , "unif"    ,  1.00   ,   3.00  , NA  , #nolint
  "veg_respiration_Q10"         , "unif"    ,  1.40   ,   2.60  , NA  , #nolint
  "fine_root_respiration_Q10"   , "unif"    ,  1.40   ,   5.00  , NA  , #nolint
  "coarse_root_respiration_Q10" , "unif"    ,  1.40   ,   5.00  , NA  , #nolint
)

PEcAn.MA expects the code to be a base R data.frame with trait names as row names.

priors <- as.data.frame(prior_distns)
rownames(priors) <- priors[["row_name"]]
priors[["row_name"]] <- NULL

The format of the trait_data is a named list of data.frames, with each data.frame containing data for the corresponding trait. PEcAn database queries return all the columns the meta-analysis needs, in the expected format. If you are providing your own trait data, you will need the following columns:

To avoid having to load custom data, here is some simulated data that fits these criteria.

simulate_data <- function(n_rows, mean_lo, mean_hi, se_lo, se_hi) {
  return(data.frame(
    name = sprintf("t%02d", seq_len(n_rows)),
    mean = runif(n_rows, mean_lo, mean_hi),
    statname = "SE",
    stat = suppressWarnings(runif(n_rows, se_lo, se_hi)),
    n = sample(1:100, size = n_rows, replace = TRUE),
    greenhouse = sample(
      c(TRUE, FALSE),
      size = n_rows,
      replace = TRUE,
      prob = c(0.1, 0.9)
    ),
    control = TRUE,
    site_id = sample(1:10, size = n_rows, replace = TRUE),
    specie_id = sample(1:4, size = n_rows, replace = TRUE),
    citation_id = sample(1:8, size = n_rows, replace = TRUE),
    treatment_id = sample(1:3, size = n_rows, replace = TRUE),
    cultivar_id = NA,
    date = NA,
    time = NA
  ))
}

n_rows <- 40
trait_data <- list()
trait_data[["Amax"]] <- simulate_data(n_rows, 4.0, 20.0, 0.13, 4.0)
trait_data[["SLA"]] <- simulate_data(n_rows, 3.0, 15.0, 0.06, 1.7)
trait_data[["leaf_turnover_rate"]] <- simulate_data(n_rows, 0.1, 0.5, 0.01, 0.05)

Run the meta analysis, performing prior and posterior checks and summarizing the results.

ma_result <- meta_analysis_standalone(trait_data, priors, iterations = 1000)
## Each meta-analysis will be run with: 
## 1000 total iterations,
## 4 chains, 
## a burnin of 500 samples,
## , 
## thus the total number of samples will be 2000
## ################################################
## ------------------------------------------------
## starting meta-analysis for:
## 
##  Amax 
## 
## ------------------------------------------------
## prior for Amax
##                      (using R parameterization):
## unif(0, 40)
## data max: 19.7234384641051 
## data min: 4.49936119094491 
## mean: 11.1 
## n: 40
## stem plot of data points
## 
##   The decimal point is at the |
## 
##    4 | 57047
##    6 | 267123356
##    8 | 17288
##   10 | 6847
##   12 | 225712
##   14 | 36
##   16 | 0483
##   18 | 04457
## 
## stem plot of obs.prec:
## 
##   The decimal point is 1 digit(s) to the left of the |
## 
##   0 | 00000000000000000000000000000000011267
##   1 | 4
##   2 | 
##   3 | 
##   4 | 
##   5 | 
##   6 | 3
## Warning in rjags::jags.model(file = jag.model.file, data = data, inits =
## j.inits, : Non-numeric data values supplied for variable(s) ghs
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Fully observed stochastic nodes: 80
##    Unobserved stochastic nodes: 14
##    Total graph size: 334
## 
## Initializing model
## 
## 
## Iterations = 2002:12000
## Thinning interval = 2 
## Number of chains = 4 
## Sample size per chain = 5000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##                  Mean      SD  Naive SE Time-series SE
## beta.ghs[2]   -1.4480 0.47014 0.0033244      0.0038686
## beta.o        11.7930 1.05703 0.0074743      0.0618215
## beta.site[10] -2.8338 1.28116 0.0090592      0.0571091
## beta.site[1]   3.1444 1.16239 0.0082193      0.0607236
## beta.site[2]   6.5453 1.21139 0.0085658      0.0584998
## beta.site[3]   2.0457 1.11877 0.0079109      0.0603887
## beta.site[4]  -1.2159 1.15697 0.0081810      0.0592292
## beta.site[5]  -1.5613 1.16024 0.0082041      0.0579778
## beta.site[6]  -0.4517 1.09118 0.0077158      0.0618124
## beta.site[7]  -1.2693 1.13788 0.0080460      0.0590318
## beta.site[8]  -1.8624 1.13713 0.0080407      0.0588386
## beta.site[9]  -2.0473 1.12153 0.0079304      0.0604825
## sd.site        3.2597 0.92516 0.0065419      0.0160981
## sd.y           6.7507 0.07872 0.0005566      0.0006013
## 
## 2. Quantiles for each variable:
## 
##                  2.5%    25%     50%     75%   97.5%
## beta.ghs[2]   -2.3765 -1.764 -1.4474 -1.1311 -0.5302
## beta.o         9.5972 11.167 11.8038 12.4520 13.8467
## beta.site[10] -5.4030 -3.649 -2.8298 -2.0110 -0.3011
## beta.site[1]   0.8998  2.404  3.1172  3.8547  5.5448
## beta.site[2]   4.2499  5.747  6.5061  7.2932  9.0845
## beta.site[3]  -0.1340  1.335  2.0305  2.7312  4.3713
## beta.site[4]  -3.4890 -1.947 -1.2146 -0.5081  1.1438
## beta.site[5]  -3.8326 -2.299 -1.5640 -0.8418  0.7976
## beta.site[6]  -2.5602 -1.133 -0.4659  0.2116  1.8267
## beta.site[7]  -3.5228 -1.983 -1.2699 -0.5722  1.0339
## beta.site[8]  -4.1005 -2.580 -1.8727 -1.1646  0.4697
## beta.site[9]  -4.2825 -2.749 -2.0628 -1.3649  0.2593
## sd.site        1.9849  2.630  3.0866  3.6926  5.5449
## sd.y           6.5973  6.697  6.7500  6.8036  6.9083
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## ################################################
## ------------------------------------------------
## starting meta-analysis for:
## 
##  SLA 
## 
## ------------------------------------------------
## prior for SLA
##                      (using R parameterization):
## lnorm(1.89, 0.61)
## data max: 14.8496243376285 
## data min: 3.24236821196973 
## mean: 9.44 
## n: 40
## stem plot of data points
## 
##   The decimal point is at the |
## 
##    2 | 246
##    4 | 33833
##    6 | 257
##    8 | 13345589023
##   10 | 45780239
##   12 | 12456
##   14 | 13788
## 
## stem plot of obs.prec:
## 
##   The decimal point is at the |
## 
##   0 | 000000000000000000000000000000000011122
##   1 | 
##   2 | 
##   3 | 
##   4 | 
##   5 | 
##   6 | 
##   7 | 
##   8 | 
##   9 | 8
## Warning in rjags::jags.model(file = jag.model.file, data = data, inits =
## j.inits, : Non-numeric data values supplied for variable(s) ghs
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Fully observed stochastic nodes: 80
##    Unobserved stochastic nodes: 14
##    Total graph size: 327
## 
## Initializing model
## 
## 
## Iterations = 2002:12000
## Thinning interval = 2 
## Number of chains = 4 
## Sample size per chain = 5000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##                  Mean      SD  Naive SE Time-series SE
## beta.ghs[2]   -2.1072 0.31178 0.0022046      0.0022785
## beta.o         9.3790 0.92699 0.0065548      0.1016544
## beta.site[10] -3.1653 0.95624 0.0067616      0.0986169
## beta.site[1]   3.0022 0.93727 0.0066275      0.0992768
## beta.site[2]   0.2085 0.93947 0.0066431      0.0988976
## beta.site[3]  -0.6040 0.94491 0.0066816      0.0997141
## beta.site[4]  -2.9242 0.95052 0.0067212      0.0967737
## beta.site[5]  -2.5785 0.94638 0.0066919      0.1008916
## beta.site[6]   1.0122 0.93397 0.0066042      0.1010164
## beta.site[7]   4.8962 1.16679 0.0082504      0.0821131
## beta.site[8]   0.5023 0.98118 0.0069380      0.0972204
## beta.site[9]   1.0972 0.95581 0.0067586      0.0990189
## sd.site        2.8698 0.80024 0.0056586      0.0184650
## sd.y           3.3063 0.03094 0.0002188      0.0002299
## 
## 2. Quantiles for each variable:
## 
##                  2.5%     25%     50%       75%   97.5%
## beta.ghs[2]   -2.7150 -2.3175 -2.1063 -1.899774 -1.4934
## beta.o         7.5920  8.7836  9.3305  9.954542 11.4485
## beta.site[10] -5.2450 -3.7468 -3.1219 -2.544586 -1.3472
## beta.site[1]   0.9411  2.4177  3.0415  3.608470  4.8016
## beta.site[2]  -1.8649 -0.3698  0.2579  0.814687  2.0208
## beta.site[3]  -2.6998 -1.1882 -0.5663  0.004983  1.1970
## beta.site[4]  -5.0150 -3.5104 -2.8818 -2.301286 -1.1129
## beta.site[5]  -4.6504 -3.1575 -2.5351 -1.965768 -0.7629
## beta.site[6]  -1.0471  0.4351  1.0555  1.620051  2.8015
## beta.site[7]   2.6030  4.1223  4.8817  5.657463  7.2237
## beta.site[8]  -1.5993 -0.1077  0.5388  1.135742  2.3971
## beta.site[9]  -0.9822  0.5081  1.1371  1.716007  2.9259
## sd.site        1.7736  2.3111  2.7243  3.254814  4.8538
## sd.y           3.2455  3.2856  3.3064  3.326944  3.3668
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## ################################################
## ------------------------------------------------
## starting meta-analysis for:
## 
##  leaf_turnover_rate 
## 
## ------------------------------------------------
## prior for leaf_turnover_rate
##                      (using R parameterization):
## weibull(1.37, 1.43)
## data max: 0.494713499397039 
## data min: 0.109222034458071 
## mean: 0.264 
## n: 40
## stem plot of data points
## 
##   The decimal point is 1 digit(s) to the left of the |
## 
##   1 | 112223344
##   1 | 5578899
##   2 | 123
##   2 | 55689
##   3 | 000234
##   3 | 59
##   4 | 113
##   4 | 56799
## 
## stem plot of obs.prec:
## 
##   The decimal point is 5 digit(s) to the right of the |
## 
##   0 | 00000000000000000000000001111112234
##   0 | 56
##   1 | 3
##   1 | 5
##   2 | 
##   2 | 
##   3 | 
##   3 | 
##   4 | 
##   4 | 5
## Warning in rjags::jags.model(file = jag.model.file, data = data, inits =
## j.inits, : Non-numeric data values supplied for variable(s) ghs
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Fully observed stochastic nodes: 80
##    Unobserved stochastic nodes: 14
##    Total graph size: 345
## 
## Initializing model
## 
## 
## Iterations = 2002:12000
## Thinning interval = 2 
## Number of chains = 4 
## Sample size per chain = 5000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##                   Mean       SD  Naive SE Time-series SE
## beta.ghs[2]   -0.02557 0.014667 0.0001037      1.110e-04
## beta.o         0.49342 0.475493 0.0033622      1.528e-01
## beta.site[10] -0.30179 0.476170 0.0033670      1.482e-01
## beta.site[1]  -0.25042 0.475786 0.0033643      1.497e-01
## beta.site[2]  -0.22961 0.475477 0.0033621      1.531e-01
## beta.site[3]  -0.28842 0.475776 0.0033642      1.478e-01
## beta.site[4]  -0.20200 0.475556 0.0033627      1.529e-01
## beta.site[5]  -0.29235 0.476588 0.0033700      1.482e-01
## beta.site[6]  -0.22054 0.475491 0.0033622      1.518e-01
## beta.site[7]  -0.15805 0.474399 0.0033545      1.494e-01
## beta.site[8]  -0.35400 0.476973 0.0033727      1.483e-01
## beta.site[9]  -0.07802 0.474797 0.0033573      1.512e-01
## sd.site        0.32552 0.501937 0.0035492      1.286e-01
## sd.y           0.16454 0.001923 0.0000136      1.407e-05
## 
## 2. Quantiles for each variable:
## 
##                   2.5%      25%       50%       75%     97.5%
## beta.ghs[2]   -0.05434 -0.03551 -0.025634 -0.015662  0.002965
## beta.o         0.19684  0.24212  0.266185  0.369437  1.750741
## beta.site[10] -1.56162 -0.17868 -0.075636 -0.048776 -0.001177
## beta.site[1]  -1.51043 -0.12783 -0.024227  0.001892  0.050065
## beta.site[2]  -1.48774 -0.10923 -0.005172  0.023544  0.074124
## beta.site[3]  -1.54646 -0.16479 -0.061868 -0.036039  0.010611
## beta.site[4]  -1.45997 -0.07829  0.023831  0.050004  0.096899
## beta.site[5]  -1.55384 -0.17149 -0.066822 -0.038637  0.010396
## beta.site[6]  -1.47869 -0.09681  0.005665  0.031646  0.077549
## beta.site[7]  -1.41478 -0.03610  0.065690  0.093824  0.145028
## beta.site[8]  -1.61530 -0.23308 -0.127209 -0.100299 -0.052060
## beta.site[9]  -1.33358  0.04685  0.147407  0.173253  0.220973
## sd.site        0.05595  0.07587  0.095051  0.177954  1.776041
## sd.y           0.16079  0.16326  0.164533  0.165835  0.168314
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed
## Warning in FUN(X[[i]], ...): start value not changed

This returns three things:

names(ma_result)
## [1] "trait.mcmc"  "post.distns" "jagged.data"
  1. The posterior distributions…
ma_result[["post.distns"]]
##                               distn    parama     paramb   n
## growth_resp_factor             beta   2.63000   6.520000   0
## leaf_turnover_rate          weibull   1.37000   1.430000 363
## root_respiration_rate          unif   0.00000 100.000000  NA
## root_turnover_rate             unif   0.00000  10.000000  NA
## Amax                           norm  11.79299   1.056999  NA
## leaf_respiration_rate_m2    weibull   2.00000   6.000000  NA
## SLA                           gamma 102.36676  10.914490 455
## leafC                          norm  50.60000   1.320000 291
## Vm_low_temp                    norm   0.00000   3.000000  NA
## AmaxFrac                       unif   0.60000   0.900000  NA
## psnTOpt                        unif   5.00000  40.000000  NA
## stem_respiration_rate          unif   0.00000 100.000000  NA
## extinction_coefficient         unif   0.38000   0.620000  NA
## half_saturation_PAR            unif   4.00000  27.000000  NA
## dVPDSlope                      unif   0.01000   0.250000  NA
## dVpdExp                        unif   1.00000   3.000000  NA
## veg_respiration_Q10            unif   1.40000   2.600000  NA
## fine_root_respiration_Q10      unif   1.40000   5.000000  NA
## coarse_root_respiration_Q10    unif   1.40000   5.000000  NA

…(2) the full MCMC samples (as a list)…

tail(ma_result[["trait.mcmc"]][["Amax"]][[1]])
## Markov Chain Monte Carlo (MCMC) output:
## Start = 11988 
## End = 12000 
## Thinning interval = 2 
##      beta.ghs[2]   beta.o beta.site[10] beta.site[1] beta.site[2] beta.site[3]
## [1,]  -1.1802323 12.61165     -5.689288     2.587135     5.857733    1.2479673
## [2,]  -1.4400180 12.74553     -5.590202     2.876645     6.269349    0.5856926
## [3,]  -2.1421645 13.18453     -4.368546     2.012452     5.437286    0.4562517
## [4,]  -0.7041074 12.97383     -4.975439     2.407349     5.520082    1.4910267
## [5,]  -1.5959022 13.27007     -5.539856     1.530881     5.863945    0.3437617
## [6,]  -2.0148808 13.29447     -5.339206     2.056501     4.851472    1.0102211
## [7,]  -1.8077722 12.86305     -4.758238     2.216045     6.036991    1.1292973
##      beta.site[4] beta.site[5] beta.site[6] beta.site[7] beta.site[8]
## [1,]    -1.721251    -3.480006    -1.460229    -2.230171    -2.632894
## [2,]    -1.354406    -2.073040    -1.609061    -2.251312    -2.913111
## [3,]    -2.559505    -2.303332    -1.500299    -2.620556    -3.271695
## [4,]    -2.263867    -3.238142    -1.569383    -2.005207    -3.185027
## [5,]    -2.475789    -2.696107    -1.372144    -2.655565    -3.175787
## [6,]    -2.814662    -3.190607    -1.677198    -2.224305    -3.208611
## [7,]    -2.954517    -2.833855    -1.477243    -2.988862    -3.468842
##      beta.site[9]  sd.site     sd.y
## [1,]    -2.015340 2.227176 6.729617
## [2,]    -2.804390 3.647173 6.860889
## [3,]    -3.177122 3.477774 6.806104
## [4,]    -3.005815 3.429386 6.815505
## [5,]    -3.702332 3.186195 6.893912
## [6,]    -3.984517 2.160705 6.755739
## [7,]    -4.080277 3.228638 6.717711

…(3) and the “JAGS-ified” trait data.

head(ma_result[["jagged.data"]][["Amax"]])
##           Y  n site     trt ghs    obs.prec        se cite greenhouse site_id
## 1 18.046454 74    1 control   1 0.001200041 3.3557231    6          1       1
## 2 18.480473 53    1 control   1 0.014015658 1.1602600    1          1       1
## 3  4.657720 39    1 control   1 0.001901748 3.6719033    4          1       1
## 4 13.236781 15    2 control   1 0.246123176 0.5204489    1          1       2
## 5 19.723438 92    2 control   1 0.003531625 1.7543603    4          1       2
## 6  9.803368  2    2 control   1 0.374649089 1.1552412    5          1       2
##   treatment_id trt_name trt_num
## 1            2      t01       1
## 2            1      t19       1
## 3            2      t25       1
## 4            2      t02       1
## 5            3      t15       1
## 6            2      t36       1