inference_stan {makemyprior}R Documentation

Run inference

Description

This function helps you run inference with rstan using a prior object from make_prior. Note that you must install Stan: install.packages("rstan"), see mc-stan.org.

Usage

inference_stan(
  prior_obj,
  use_likelihood = TRUE,
  print_prior = TRUE,
  path = NULL,
  ...
)

Arguments

prior_obj

An object from make_prior, from makemyprior_gui, from inference_stan, or from inference_inla (for refitting model)

use_likelihood

Whether to sample from the prior only (FALSE, can be used for e.g. debugging or to look at the priors on variance parameters when using an HD prior, see also Details), or to use the likelihood and data to get the posterior (TRUE, default).

print_prior

Whether to print a text with the chosen prior or not (default TRUE)

path

Path to folder. See compile_stan. Only necessary if compiled code is stored somewhere else than in tempdir() or the package directory (checking tempdir() first).

...

Other arguments to be sent to sampling. Useful arguments include:

iter

number of iterations for each chain (including burn-in, 2000 is the default)

warmup

number of iterations for the burn-in (default is iter/2)

chains

number of chains

init

initial value of the model parameters on internal parameterization (log-variances and covariate coefficients)

seed

seed value for random number generation

See sampling for more details. Note that for inference with stan, the Ntrials (the size parameter for binomial likelihood) and E (the mean E_i in E_i*exp(eta_i) for Poisson likelihood) argument must be included in the data object, and cannot be provided to this function.

Details

We cannot sample from a Jeffreys' prior since it is improper. If use_likelihood = FALSE and Jeffreys' prior is used for the total variance, the prior will be changed to a Gaussian(0,1) prior on the log total variance. This means that it does not make sense to look at the variances/standard deviations/precisions, but the variance proportions will be correct. Note that this is only an issue when sampling from the prior (i.e., not using the likelihood).

Value

A named list with a prior object (prior), a stan-object (stan) and some data stan requires (stan_data).

Examples

## Not run: 

vignette("make_prior", package = "makemyprior")

## End(Not run)

ex_prior <- makemyprior_example_model()
if (interactive() && requireNamespace("rstan")){
  posterior <- inference_stan(ex_prior, iter = 100)
  # Note: For reliable results, increase the number of iterations (e.g., 'iter = 2000')
  plot(posterior)
}

## Not run: 

posterior <- inference_stan(ex_prior, use_likelihood = TRUE, iter = 1e4, chains = 1, seed = 1)
plot(posterior)

## End(Not run)



[Package makemyprior version 1.2.1 Index]