if (FALSE) {
# perform a meta analysis;
# load data:
data("CrinsEtAl2014")
# compute effect sizes (log odds ratios) from count data
# (using "metafor" package's "escalc()" function):
require("metafor")
crins.srr <- escalc(measure="OR",
ai=exp.SRR.events, n1i=exp.total,
ci=cont.SRR.events, n2i=cont.total,
slab=publication, data=CrinsEtAl2014, subset=c(1,4,6))
# analyze:
bma <- bayesmeta(crins.srr, mu.prior.mean=0, mu.prior.sd=4,
tau.prior=function(t){dhalfnormal(t, scale=0.5)})
# compute a 2-sided p-value for the effect (mu) parameter
# (note: this may take a while!):
p <- pppvalue(bma, parameter="mu", value=0, n=100)
# show result:
print(p)
# show the test statistic's distribution
# along with its actualized value:
plot(ecdf(p$replicates$statistic[,1]),
xlim=range(c(p$statistic, p$replicates$statistic[,1])))
abline(v=p$statistic, col="red")
# show the parameter values
# drawn from the (conditional) posterior distribution:
plot(bma, which=2)
abline(h=p$null.value) # (the null-hypothesized mu value)
points(p$replicates$tau, p$replicates$mu, col="cyan") # (the samples)
######################################################################
# Among the 3 studies, only the first (Heffron, 2003) was randomized.
# One might wonder about this particular study's effect (theta[1])
# in the light of the additional evidence and compute a one-sided
# p-value:
p <- pppvalue(bma, parameter="Heffron", value=0, n=100, alternative="less")
print(p)
######################################################################
# One may also define one's own 'test' statistic to be used.
# For example, one could utilize the Bayes factor to generate
# a p-value for the homogeneity (tau=0) hypothesis:
BF <- function(y, sigma)
{
bm <- bayesmeta(y=y, sigma=sigma,
mu.prior.mean=0, mu.prior.sd=4,
tau.prior=function(t){dhalfnormal(t, scale=0.5)},
interval.type="central")
# (central intervals are faster to compute;
# interval type otherwise is not relevant here)
return(bm$bayesfactor[1,"tau=0"])
}
# NOTE: the 'bayesmeta()' arguments above should probably match
# the specifications from the original analysis
p <- pppvalue(bma, parameter="tau", statistic=BF, value=0, n=100,
alternative="greater", rejection.region="lower.tail",
sigma=bma$sigma)
print(p)
######################################################################
# If one is only interested in generating samples (and not in test
# statistics or p-values), one may specify the 'statistic' argument
# as 'NA'.
# Note that different 'parameter', 'value' and 'alternative' settings
# imply different sampling schemes.
p <- pppvalue(bma, parameter="mu", statistic=NA, value=0,
alternative="less", n=100)
plot(bma, which=2)
abline(h=p$null.value)
points(p$replicates$tau, p$replicates$mu, col="cyan")
}
Run the code above in your browser using DataLab