fit_stan_model: Fit a stan model using (modified) code and data generated by...

View source: R/fit_stan_model.R

fit_stan_modelR Documentation

Fit a stan model using (modified) code and data generated by brms

Description

Fit a stan model using (modified) code and data generated by brms

Usage

fit_stan_model(
  file,
  seed,
  bform,
  bdata,
  bpriors = NULL,
  car1 = TRUE,
  sample_prior = "no",
  knots = NULL,
  d_x = NULL,
  family = student(),
  backend = "rstan",
  overwrite = FALSE,
  var_car1 = NULL,
  var_xcens = NULL,
  cens_ind = NULL,
  lcl = NULL,
  lower_bound = NULL,
  stancode = NULL,
  ...
)

Arguments

file

Load/save csvs to this filepath and name.

seed

Random seed passed to rstan::stan().

bform

A brms formula. N.B., transformations of variables within the formula may not be picked up by post-processing functions; it's generally better to create new, transformed variables.

bdata

A data frame used to fit the model.

bpriors

Priors specified by brms functions.

car1

Logical. Generate CAR(1) errors?

sample_prior

Passed on to brms::brm().

knots

Passed on to brms::brm().

d_x

A vector representing the spacing in time of observations in each series, equal to zero at the first timestep. If NULL (the default), d_x is drawn from the dataframe bdata.

family

A brmsfamily object. Note that some post-processing functions assume a student-t likelihood.

backend

Run Stan's algorithms using rstan or cmdstanr.

overwrite

Overwrite an exising model stored as CSVs? Defaults to FALSE.

var_car1

For multivariate models, specify which variable to generate CAR(1) errors for.

var_xcens

A character vector containing the names of variables with left-censoring; the order should correspond to that of lcl and cens_ind.

cens_ind

A character vector containing the names of left-censoring indicator variables corresponding to the contents of var_xcens. (Indicator variables should take the form of character vectors with "left" for left-censored and "none" for observed.)

lcl

A numeric vector of censoring limits for left-censored variables. Alternatively, a list of numeric vectors with censoring limits for each variable and observation; the order should correspond to that of lcl and cens_ind.

lower_bound

An optional lower bound for left-censored parameters. The default is no lower bound.

stancode

A named character vector of length one, where the name is the model block to modify and the value is the additional Stan code. This has only been validated for the generated quantities block.

...

Passed on to rstan::stan() or cmdstanr::sample(), depending on the backend selected.

Value

A brms model object fitted with rstan or cmdstanr.

Examples

library("brms")
seed <- 1
data <- read.csv(paste0(system.file("extdata", package = "bgamcar1"), "/data.csv"))
fit <- fit_stan_model(
  paste0(system.file("extdata", package = "bgamcar1"), "/test"),
  seed,
  bf(y | cens(ycens, y2 = y2) ~ 1),
  data,
  prior(normal(0, 1), class = Intercept),
  car1 = FALSE,
  save_warmup = FALSE,
  chains = 3
)

bentrueman/bgamcar1 documentation built on July 6, 2024, 11:16 p.m.