cs_sampling_brms: cs_sampling_brms

View source: R/cs_sampling_brms.R

cs_sampling_brmsR Documentation

cs_sampling_brms

Description

cs_sampling_brms is a wrapper function that takes inputs in the form of model statements in familiar brms syntax. Then brms helper functions build Stan models and call cs_sampling.

Usage

cs_sampling_brms(
  svydes,
  brmsmod,
  data,
  family,
  par_brms = NA,
  prior = NULL,
  stanvars = NULL,
  knots = NULL,
  ctrl_stan = list(chains = 1, iter = 2000, warmup = 1000, thin = 1),
  rep_design = FALSE,
  ctrl_rep = list(replicates = 100, type = "mrbbootstrap"),
  stancode_args = list(),
  standata_args = list(),
  H_estimate = "MCMC",
  matrix_sqrt = "eigen",
  sampling_args = list()
)

Arguments

svydes
  • a svydesign object or a svrepdesign object. This contains cluster ID, strata, and weight information (svydesign) or replicate weight information (svrepdesign)

brmsmod
  • brmsformula object, as input to make_stancode. The brmsformula must specify a weight variable via weights().

data
  • a data frame, as input to make_stancode

family
  • brmsfamily as input to make_stancode specifying distribution and link function

par_brms
  • a list of a subset of parameters to output after adjustment. All parameters are adjusted including the derived parameters, so users may want to only compare subsets. The default, NA, will return all parameters.

prior
  • optional input to make_stancode

stanvars
  • optional input to make_stancode

knots
  • optional input to make_stancode

ctrl_stan
  • a list of control parameters to pass to sampling. Currently includes the number of chains, iter, warmpup, and thin with defaults.

rep_design
  • logical indicating if the svydes object is a svrepdesign. If FALSE, the design will be converted to a svrepdesign using ctrl_rep settings

ctrl_rep
  • a list of settings when converting svydes from a svydesign object to a svrepdesign object. replicates - number of replicate weights. type - the type of replicate method to use, the default is mrbbootstrap which sample half of the clusters in each strata to make each replicate (see as.svrepdesign).

stancode_args
  • a list of extra arguments to be passed to make_stancode.

standata_args
  • a list of extra arguments to be passed to make_standata.

H_estimate
  • a string indicating the method to use to estimate H. The default "MCMC" is Monte Carlo averaging over posterior draws. Otherwise, a plug-in using the posterior mean.

matrix_sqrt
  • a string indicating the method to use to take the "square root" of the R1 and R2 matrices. The default "eigen" uses the eigenvalue decomposition. Otherwise, the Cholesky decomposition is used.

sampling_args
  • a list of extra arguments to be passed to sampling.

Value

The output of cs_sampling.

Examples


#continuous dependent variable
# Survey Design Information
library(survey)
data(api)
apistrat$wt <- apistrat$pw /mean(apistrat$pw)

dstrat <-
svydesign(id=~1,strata=~stype, weights=~wt, data=apistrat, fpc=~fpc)

#Define and Run the Stan Model Via BRMS Wrapper
library(brms)
set.seed(12345)
model_formula <- formula("api00|weights(wt) ~
                            ell + meals + mobility")
mod.brms <- cs_sampling_brms(svydes = dstrat,
                             brmsmod = brmsformula(model_formula, center = FALSE),
                             data = apistrat, family = gaussian())

#Plot the results

plot(mod.brms)
plot(mod.brms, varnames = paste("b", 1:4, sep =""))


RyanHornby/csSampling documentation built on Jan. 5, 2023, 12:37 p.m.