Get list of mcmcglms run across values of slice sampling tuning parameters
Source:R/slice_utilities.R
mcmcglm_across_tuningparams.Rd
The function simply performs an lapply over mcmcglm for the values provided of tuning parameter values. See general usage in the vignette
Usage
mcmcglm_across_tuningparams(
...,
tuning_parameter_name = "w",
formula,
family,
data,
beta_prior = distributional::dist_normal(0, 1),
log_likelihood_extra_args = list(sd = 1),
sample_method = c("slice_sampling", "normal-normal"),
qslice_fun = qslice::slice_stepping_out,
n_samples = 500,
burnin = 100,
parallelise = FALSE,
n_cores = as.numeric(Sys.getenv("NUMBER_OF_PROCESSORS")) - 1
)
Arguments
- ...
A
list
orvector
with values of the tuning parameter- tuning_parameter_name
The name of the tuning parameter. Fx. for the default
qslice_fun
qslice::slice_stepping_out, the tuning parameter is called w- formula
an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. See more details at stats::glm
- family
a description of the error distribution and link function to be used in the model. This can be a
character
string naming a family function, a family function or the result of a call to a family function. (See family for details of family functions.)- data
an optional
data frame
,list
orenvironment
(or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken fromenvironment(formula)
, typically the environment from which the function is called.- beta_prior
a
distribution
object created by a function from thedistributional
package. Could fx. bedistributional::dist_normal(mean = 0, sd = 1)
.- log_likelihood_extra_args
a named
list
with arguments passed onto the log_density function. Fx. specification oflog_likelihood_extra_args = list(sd = x)
is needed for the case offamily = "gaussian"
- sample_method
a
character
specifying the method used for sampling. The default"slice_sampling"
is the intended value in most cases, as it works for any specification offamily
andbeta_prior
."normal-normal"
uses a conditional normal distribution to sample from in case of conjugate prior with gaussian response andbeta_prior
. Implemented for testing purposes but works for that niche case.- qslice_fun
a
function
from the qslice package. Default is qslice::slice_stepping_out which uses the slice sampler from Neal 2003, but all functions are available.- n_samples
a
numeric
with number of samples to draw of each parameter(/variable) in the model- burnin
a
numeric
with the number of samples to be marked as "burnin". Burnin samples are not included in thebeta_mean
calculation to increase finite sample performance of the LLN estimate- parallelise
a
logical
if the runs of the algorithm across values ofn_vars
should be parallelised using future.apply::future_lapply- n_cores
a
numeric
with the number of cores to use for parallelisation. Default is 1 less than the number of available cores
Examples
# Create test data for different scenarios
n <- 100
x1 <- rnorm (n)
x2 <- rbinom (n, 1, .5)
b0 <- 1
b1 <- 1.5
b2 <- 2
lin_pred <- b0+b1*x1+b2*x2
#############################################
# Different families and priors
# For family "gaussian" and iid normal prior
y_norm <- rnorm(n, mean = lin_pred, sd = 1)
dat_norm <- data.frame(Y = y_norm, X1 = x1, X2 = x2)
w05_mcmcglms <- mcmcglm_across_tuningparams(
seq(from = 0.5, by = 0.5, length.out = 9),
tuning_parameter_name = "w",
formula = Y ~ .,
family = "gaussian",
data = dat_norm,
n_samples = 10,
burnin = 0
)