All Projects → vdorie → stan4bart

vdorie / stan4bart

Licence: other
Uses Stan sampler and math library to semiparametrically fit linear and multilevel models with additive Bayesian Additive Regression Tree (BART) components.

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
Makefile
30231 projects
r
7636 projects
M4
1887 projects
Stan
76 projects

Projects that are alternatives of or similar to stan4bart

walker
Bayesian Generalized Linear Models with Time-Varying Coefficients
Stars: ✭ 38 (+192.31%)
Mutual labels:  bayesian, stan
Projpred
Projection predictive variable selection
Stars: ✭ 76 (+484.62%)
Mutual labels:  bayesian, stan
Bda r demos
Bayesian Data Analysis demos for R
Stars: ✭ 409 (+3046.15%)
Mutual labels:  bayesian, stan
Bayesplot
bayesplot R package for plotting Bayesian models
Stars: ✭ 276 (+2023.08%)
Mutual labels:  bayesian, stan
Stan
Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
Stars: ✭ 2,177 (+16646.15%)
Mutual labels:  bayesian, stan
Orbit
A Python package for Bayesian forecasting with object-oriented design and probabilistic models under the hood.
Stars: ✭ 346 (+2561.54%)
Mutual labels:  bayesian, stan
Matlabstan
Matlab interface to Stan, a package for Bayesian inference
Stars: ✭ 59 (+353.85%)
Mutual labels:  bayesian, stan
Rstanarm
rstanarm R package for Bayesian applied regression modeling
Stars: ✭ 285 (+2092.31%)
Mutual labels:  bayesian, stan
Shinystan
shinystan R package and ShinyStan GUI
Stars: ✭ 172 (+1223.08%)
Mutual labels:  bayesian, stan
Miscellaneous R Code
Code that might be useful to others for learning/demonstration purposes, specifically along the lines of modeling and various algorithms. Now almost entirely superseded by the models-by-example repo.
Stars: ✭ 146 (+1023.08%)
Mutual labels:  bayesian, stan
cmdstanr
CmdStanR: the R interface to CmdStan
Stars: ✭ 82 (+530.77%)
Mutual labels:  bayesian, stan
bayesian
Bindings for Bayesian TidyModels
Stars: ✭ 33 (+153.85%)
Mutual labels:  bayesian, stan
Bda py demos
Bayesian Data Analysis demos for Python
Stars: ✭ 781 (+5907.69%)
Mutual labels:  bayesian, stan
Modelselection
Tutorial on model assessment, model selection and inference after model selection
Stars: ✭ 139 (+969.23%)
Mutual labels:  bayesian, stan
geostan
Bayesian spatial analysis
Stars: ✭ 40 (+207.69%)
Mutual labels:  bayesian, stan
stantargets
Reproducible Bayesian data analysis pipelines with targets and cmdstanr
Stars: ✭ 31 (+138.46%)
Mutual labels:  bayesian, stan
MultiBUGS
Multi-core BUGS for fast Bayesian inference of large hierarchical models
Stars: ✭ 28 (+115.38%)
Mutual labels:  bayesian
awesome-agi-cocosci
An awesome & curated list for Artificial General Intelligence, an emerging inter-discipline field that combines artificial intelligence and computational cognitive sciences.
Stars: ✭ 81 (+523.08%)
Mutual labels:  bayesian
ArviZ.jl
Exploratory analysis of Bayesian models with Julia
Stars: ✭ 67 (+415.38%)
Mutual labels:  bayesian
stan-ja
Stanマニュアルの日本語への翻訳プロジェクト
Stars: ✭ 53 (+307.69%)
Mutual labels:  stan

This package is an implementation of a C++ sampler that uses BART for non-parametric mean components and Stan for multilevel/parametric ones.

Installation

  1. Install the developer tools for your platform (Mac OS, Windows). Mac OS users will need the (linked) gfortan for their respective platforms.
    • If on an ARM processor, install gfortran from here
    • If on an Intel processor, install gfortran from here
  2. Execute:
if (length(find.package("remotes", quiet = TRUE)) == 0L)
  install.packages("remotes")
remotes::install_github("vdorie/dbarts")
remotes::install_github("vdorie/stan4bart")

Use

The package utilizes the flexible, expressive lme4 syntax for specifying group-level structures. See the package documentation ?stan4bart and ?stan4bart::stan4bart-generics for more information.

Formulas

  • y ~ bart(x_nuisance_1 + x_nuisance_1) + x_inference - wrap the formula for any variables that you want to be fit non-parametrically in a call to bart(); +s within bart() are interpretted figuratively as indicating the names of the variables to be included
  • y ~ bart(x_fixef) + (1 + x_ranef | g) - supports all lme4 varying intercept and slope formula constructs
  • y ~ x_1 + (1 + x_2 | g) - if there's no BART component, use rstanarm or lme4 instead

Main Event

The main function is stan4bart.

Results

Results are retrieved using the extract, fitted, and predict generics. See ?"stan4bart-generics" for more information.

Known Issues

The name and definition of extract conflict with rstan. The rstan package is not needed to use stan4bart and does not need to be loaded. If a name-collision occurs, the stan4bart extract can be referenced as in:

stan4bart:::extract.stan4bartFit(stan4bart_fit)

Example Code

library(stan4bart)

# Load a test-data function
source(system.file("common", "friedmanData.R", package = "stan4bart"), local = TRUE)

# Relatively low n for illustrative purposes
testData <- generateFriedmanData(n = 100, ranef = TRUE, causal = TRUE, binary = FALSE)

# First level model is:
#   y ~ f(x_1, x_2) + a * x_3^2 + b * x_4 + c * x_5 + z
# Random intercepts are added for g.1 and g.2, and a random slope is placed on x4
# x_6 through x_10 are pure noise
df <- with(testData, data.frame(x, g.1, g.2, y, z))

# Causal inference example
fit <- stan4bart(y ~ bart(. - g.1 - g.2 - X4 - z) + X4 + z + (1 + X4 | g.1) + (1 | g.2), df,
                 treatment = z,
                 cores = 1, seed = 0,
                 verbose = 1)

samples.mu.train <- extract(fit)
samples.mu.test  <- extract(fit, sample = "test")

# Individual conditional treatment effects
samples.icate <- (samples.mu.train - samples.mu.test) * (2 * testData$z - 1)
# Conditional average treatment effect
samples.cate <- apply(samples.icate, 2, mean)
cate <- mean(samples.cate)
cate.int <- c(cate - 1.96 * sd(samples.cate), cate + 1.96 * sd(samples.cate))

# Samples of the posterior predictive distribution are used in calculating
# the counterfactuals for SATE and for calculating the response under
# the observed treatment condition when estimating PATE.
samples.ppd.test <- extract(fit, type = "ppd", sample = "test")

# Individual sample treatment effects
samples.ite <- (testData$y - samples.ppd.test) * (2 * testData$z - 1)
# Sample average treatment effect
samples.sate <- apply(samples.ite, 2, mean)
sate <- mean(samples.sate)
sate.int <- c(sate - 1.96 * sd(samples.sate), sate + 1.96 * sd(samples.sate))

# Population average treatment effect
samples.ppd.train <- extract(fit, type = "ppd", sample = "train")
samples.pate <- apply((samples.ppd.train - samples.ppd.test) * (2 * testData$z - 1), 2, mean)
pate <- mean(samples.pate)
pate.int <- c(pate - 1.96 * sd(samples.pate), pate + 1.96 * sd(samples.pate))


fitted.mu.train <- fitted(fit)
# equal to: apply(samples.mu.train, 1, mean)
fitted.mu.test  <- fitted(fit, sample = "test")
# equal to: apply(samples.mu.test,  1, mean)

# Observed and conterfactual MSE
mse.train <- with(testData, mean((fitted.mu.train - mu.1 * z - mu.0 * (1 - z))^2))
mse.test  <- with(testData, mean((fitted.mu.test  - mu.1 * (1 - z) - mu.0 * z)^2))
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].