b_linear: b_linear

View source: R/b_linear.R

b_linearR Documentation

b_linear

Description

Bayesian model for fitting a linear normal model to data.

Usage

b_linear(
  x,
  y,
  s,
  priors = NULL,
  warmup = 1000,
  iter = 2000,
  chains = 4,
  seed = NULL,
  refresh = NULL,
  control = NULL,
  suppress_warnings = TRUE
)

Arguments

x

a vector containing sequence indexes (time).

y

a vector containing responses of subjects.

s

a vector containing subject indexes. Starting index should be 1 and the largest subject index should equal the number of subjects.

priors

List of parameters and their priors - b_prior objects. You can put a prior on the mu_a (mean intercept), sigma_a (variance of mu_a), mu_b (mean slope), sigma_s (variance of mu_b), mu_s (variance) and sigma_s (variance of mu_s) parameters (default = NULL).

warmup

Integer specifying the number of warmup iterations per chain (default = 1000).

iter

Integer specifying the number of iterations (including warmup, default = 2000).

chains

Integer specifying the number of parallel chains (default = 4).

seed

Random number generator seed (default = NULL).

refresh

Frequency of output (default = NULL).

control

A named list of parameters to control the sampler's behavior (default = NULL).

suppress_warnings

Suppress warnings returned by Stan (default = TRUE).

Value

An object of class 'linear_class'.

Examples


# priors
mu_prior <- b_prior(family="normal", pars=c(0, 100))
sigma_prior <- b_prior(family="uniform", pars=c(0, 500))

# attach priors to relevant parameters
priors <- list(c("mu_a", mu_prior),
               c("sigma_a", sigma_prior),
               c("mu_b", mu_prior),
               c("sigma_b", sigma_prior),
               c("mu_s", sigma_prior),
               c("sigma_s", sigma_prior))

# generate data
x <- vector()
y <- vector()
s <- vector()
for (i in 1:5) {
  x <- c(x, rep(1:10, 2))
  y <- c(y, rnorm(20, mean=1:10, sd=2))
  s <- c(s, rep(i, 20))
}

fit <- b_linear(x=x, y=y, s=s, priors=priors, chains=1)



bayes4psy documentation built on Sept. 29, 2023, 5:08 p.m.