Title: | This Package Creates a Set of Functions Useful for Bayesian modelling |
---|---|
Description: | A set of utility functions that can be used to post-process BUGS or JAGS objects as well as other to facilitate various Bayesian modelling activities (including in HTA). |
Authors: | Gianluca Baio |
Maintainer: | Gianluca Baio <[email protected]> |
License: | CC BY 4.0 |
Version: | 0.1.0 |
Built: | 2024-11-17 18:25:49 UTC |
Source: | https://github.com/giabaio/bmhe_utils |
Plots the ACF function
acfplot(x, col = "black", parameter = NULL, add_deviance = TRUE, ...)
acfplot(x, col = "black", parameter = NULL, add_deviance = TRUE, ...)
x |
A vector with simulations from a MCMC process (eg from a |
col |
The color with which to plot the ACF (default to |
parameter |
A text string to select a named parameter (eg if using
a |
add_deviance |
a logical argument to determine whether the |
... |
Extra arguments |
Gianluca Baio
## Not run: acfplot(m) ## End(Not run)
## Not run: acfplot(m) ## End(Not run)
Computes the parameters of a Beta distribution so that the mean and standard dev are the input (m,s)
betaPar(m, s)
betaPar(m, s)
m |
The implied mean for the underlying Beta distribution |
s |
The implied standard deviation for the underlying Beta distribution |
The list of relevant output including the values for the parameters of the Beta distribution (alpha and beta)
betaPar(.5,.15)
betaPar(.5,.15)
Compute the parameters of a Beta distribution, given a prior guess for key parameters. Based on "Bayesian ideas and data analysis", page 100. Optimisation method to identify the values of a,b that give required conditions on the Beta distribution
betaPar2(mode, upp, prob)
betaPar2(mode, upp, prob)
mode |
The implied mode of the distribution |
upp |
An upper bound value for the distribution |
prob |
The estimated probability that theta <= upp |
The list of relevant output including the values for the parameters of the Beta distribution and some underlying summary statistics of the resulting variable
res=betaPar2(.6,.7,.9)
res=betaPar2(.6,.7,.9)
Provides a quick and dirty, trial-and-error tool to identify suitable values for the the parameters of a Beta distribution to match set properties (eg mean, sd, 95% interval)
betaplot(a_max = 30, b_max = 30, step = 0.01)
betaplot(a_max = 30, b_max = 30, step = 0.01)
a_max |
The maximum value for the parameter |
b_max |
The maximum value for the parameter |
step |
The increment in the grid of values for |
Gianluca Baio
## Not run: betaplot() ## End(Not run)
## Not run: betaplot() ## End(Not run)
Creates a plot showing the mean and an interval estimate for the posterior distributions in a given model.
coefplot(x, low = 0.025, upp = 0.975, parameter = NULL, ...)
coefplot(x, low = 0.025, upp = 0.975, parameter = NULL, ...)
x |
an object of class ‘bugs’, see |
low |
the lower quantile to consider (default 2.5 percentile) |
upp |
the upper quantile to consider (default 97.5 percentile) |
parameter |
a vector of strings with the names of the parameters to be
included. Defaults to all those in the original model, but can be a
vector eg |
... |
Additional options |
Gianluca Baio
BUGS
, JAGS
## Not run: coefplot(m) ## End(Not run)
## Not run: coefplot(m) ## End(Not run)
Creates a plot showing the output of convergence indicators, such as the Potential Scale Reduction and the effective sample size
diagplot(x, what = "Rhat", label = FALSE, ...)
diagplot(x, what = "Rhat", label = FALSE, ...)
x |
an object of class ‘bugs’, see |
what |
A string indicating what diagnostic measure should be plotted. Options are 'Rhat' (default), indicating the PSR statistic, or 'n.eff', indicating the effective sample size |
label |
A logical input. If set to 'FALSE' (default), then does not include text labels next to each node |
... |
Additional options |
Gianluca Baio
BUGS
, JAGS
## Not run: diagplot(m) ## End(Not run)
## Not run: diagplot(m) ## End(Not run)
Computes the parameters of a Gamma distribution so that the mean and standard dev are the input (m,s)
gammaPar(m, s)
gammaPar(m, s)
m |
The implied mean for the underlying Beta distribution |
s |
The implied standard deviation for the underlying Beta distribution |
The list of relevant output including the values for the parameters of the Gamma distribution (shape and rate)
gammaPar(12,3)
gammaPar(12,3)
Provides a quick and dirty, trial-and-error tool to identify suitable values for the the parameters of a Gamma distribution to match set properties (eg mean, sd, 95% interval)
gammaplot(shape_max = 30, rate_max = 30, step = 0.01)
gammaplot(shape_max = 30, rate_max = 30, step = 0.01)
shape_max |
The maximum value for the parameter |
rate_max |
The maximum value for the parameter |
step |
The increment in the grid of values for |
Gianluca Baio
## Not run: gammaplot() ## End(Not run)
## Not run: gammaplot() ## End(Not run)
Computes the inverse logit of a number between -infinity and +infinity
ilogit(x)
ilogit(x)
x |
a real number |
inverse-logit(x) = exp(x)/(1+exp(x))
ilogit(2)
ilogit(2)
Computes the logit of a number
logit(x)
logit(x)
x |
a number between 0 and 1 |
logit(x)=log(x/(1-x))
logit(.2)
logit(.2)
Computes the parameters of a Normal distribution on the logit scale, so that, on the natural scale, the range where most of the mass is included is between the input 'low' and 'upp'
logitPar(low, upp)
logitPar(low, upp)
low |
The lower extreme of an implied range that is supposed to cover "most" of the mass under the natural scale of the distribution of the parameter (defined in the interval 0–1) |
upp |
The upper extreme of an implied range that is supposed to cover "most" of the mass under the natural scale of the distribution of the parameter (defined in the interval 0–1) |
The list of relevant output including the values for the parameters of the normal distribution (mulogit and sigmalogit), on the logit scale
logitPar(0.04,0.12)
logitPar(0.04,0.12)
Computes mean and variance of a logNormal distribution so that the parameters on the natural scale are mu and sigma
lognPar(m, s)
lognPar(m, s)
m |
The implied mean for the underlying Beta distribution |
s |
The implied standard deviation for the underlying Beta distribution |
The list of relevant output including the values for the parameters of the logNormal distribution in terms of the mean on the log scale (mulog) and the sd on the log scale (sigmalog)
lognPar(3,.15)
lognPar(3,.15)
Makes a traceplot (eg to visualise MCMC simulations from multiple chains)
mytraceplot(node, model = m, title = "", lab = "")
mytraceplot(node, model = m, title = "", lab = "")
node |
a string with the name of the node to be plotted, eg "theta" (in quotes) |
model |
the name of the object containing the MCMC simulations |
title |
the title of the graph (defaults to nothing) |
lab |
the label to write on the y-axis (defaults to nothing) |
the graph with the traceplot
## Not run: mytraceplot("x",m) ## End(Not run)
## Not run: mytraceplot("x",m) ## End(Not run)
Maps from odds to probabilities
odds2probs(odds)
odds2probs(odds)
odds |
the odds ratio against p: OR=(1-p)/p |
the value of the underlying probability, p
odds2probs(4)
odds2probs(4)
Computes the odds ratio between two probabilities
OR(p1, p2)
OR(p1, p2)
p1 |
a probability |
p2 |
another probability |
OR=(p1/(1-p1))/(p2/(1-p2))
OR(.5,.2)
OR(.5,.2)
Produces a plot of the values of the Gelman Rubin stats to determine visually convergence (and see clearly which node has reached it)
plotGR(m)
plotGR(m)
m |
is an object in the class jags or bugs (the output of the MCMC run) |
the graph with the Gelmn Rubin statistics plot
## Not run: plotGR(m) ## End(Not run)
## Not run: plotGR(m) ## End(Not run)
bugs
or jags
objectVarious plots for the posteriors in a bugs
or jags
object
posteriorplot(x, parameter = NULL, plot = "density", add_deviance = FALSE, ...)
posteriorplot(x, parameter = NULL, plot = "density", add_deviance = FALSE, ...)
x |
an object of class ‘bugs’, see |
parameter |
a string with the name of the parameter for which to show
the density plot. Can be a vector, eg |
plot |
the type of plot (options are 'density' (default) or 'bar' for a binned barplot of the posterior) or 'hist' for a histogram |
add_deviance |
a logical argument to determine whether the |
... |
further arguments |
Gianluca Baio
BUGS
, JAGS
Printing a bugs
object
## S3 method for class 'bugs' print(x, digits = 3, intervals = c(0.025, 0.25, 0.5, 0.75, 0.975), ...)
## S3 method for class 'bugs' print(x, digits = 3, intervals = c(0.025, 0.25, 0.5, 0.75, 0.975), ...)
x |
an object of class ‘bugs’, see |
digits |
rounding for tabular output on the console (default is to round to 1 decimal place) |
intervals |
the quantiles for the posterior distribution to be displayed in the summary statistics table |
... |
further arguments to |
Gianluca Baio
bugs
Computes and prints summary statistics for a vector or matrix of simulated values
stats(x, dim = 2, out = "table", ...)
stats(x, dim = 2, out = "table", ...)
x |
A vector or a matrix containing simulations from, eg, BUGS |
dim |
The dimension alongside which the summaries should be taken (by default is 2, which means the simulations are stored as a matrix, where the variables are columns) |
out |
A string indicating whether the output of the summary should be
formatted as a "normal" vector or matrix (default), or as a tibble.
Acceptable values are |
... |
Additional parameters that can be passed for the option
|
A table with some specific summary statistics
x=rnorm(1000) stats(x)
x=rnorm(1000) stats(x)
Computes and prints summary statistics for a vector or matrix of simulated values - tidyverse style
stats2(x, digits = 3, na.rm = TRUE)
stats2(x, digits = 3, na.rm = TRUE)
x |
A vector or a matrix containing simulations from, eg, BUGS |
digits |
The number of significant digits shown (default = 3) |
na.rm |
A logical value (default TRUE) to indicate whether NA should be removed |
Traceplot for a bugs
or jags
object
traceplot(x, parameter = NULL, ...)
traceplot(x, parameter = NULL, ...)
x |
an object of class ‘bugs’, see |
parameter |
a string with the name of the parameter for which to show
the traceplot. Can be a vector, eg |
... |
further arguments to |
Gianluca Baio
BUGS
, JAGS