### Install rstanarm Development Version from GitHub Source: https://github.com/stan-dev/rstanarm/blob/master/README.md Installs the latest development version of rstanarm from GitHub. Ensure rstan and a C++ toolchain are installed first. Adjust '-j2' based on available CPU cores. ```r # Change 2 to however many cores you can/want to use to parallelize install # If you experience crashes or run out RAM during installation, try changing this to 1 Sys.setenv(MAKEFLAGS = "-j2") Sys.setenv("R_REMOTES_NO_ERRORS_FROM_WARNINGS" = "true") remotes::install_github("stan-dev/rstanarm", INSTALL_opts = "--no-multiarch", force = TRUE) ``` -------------------------------- ### Example Model Fitting with stan_glmer Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.md Demonstrates fitting a generalized linear mixed-effects model using `stan_glmer` from the rstanarm package. This example is kept small for demonstration purposes by limiting chains and iterations. ```R ### script to run all rstanarm tests locally library(rstanarm) # library(rstantools) # library(bayesplot) remove(list=ls()) ### run prerequisite functions # example_model <- stan_glmer(cbind(incidence, size - incidence) ~ size + period + (1|herd), data = lme4::cbpp, family = binomial, # this next line is only to keep the example small in size! chains = 2, cores = 1, seed = 12345, iter = 500) # last_dimnames <- function(x) { ndim <- length(dim(x)) dimnames(x)[[ndim]] } ### run tests devtools::test() ``` -------------------------------- ### Install rstanarm from CRAN Source: https://github.com/stan-dev/rstanarm/blob/master/README.md Installs the latest stable release of the rstanarm package from the Comprehensive R Archive Network (CRAN). ```r install.packages("rstanarm") ``` -------------------------------- ### Install Survival Analysis Version of rstanarm Source: https://github.com/stan-dev/rstanarm/blob/master/README.md Installs a binary version of the survival branch of rstanarm from the Stan R packages repository. This is useful for accessing experimental survival analysis functionality without compiling from source. ```r install.packages("rstanarm", repos = c('https://stan-dev.r-universe.dev', getOption("repos"))) ``` -------------------------------- ### List S3 Methods for 'ivreg' Class Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm This command lists the available S3 methods for objects of class 'ivreg'. It helps identify which post-estimation functions might need to be reimplemented for compatibility with rstanarm. ```r methods(class = "ivreg") ``` -------------------------------- ### RStanArm Model Fit Workflow Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.md Illustrates the typical workflow for fitting models using rstanarm, showing the interaction between user-defined R functions, the .fit workhorse function, and the Stan model file. ```text ┏━━━━━━ USER ━━━━━━┓ model_A.R model_B.R ┗━━━━ model.fit ━━━━┛ ┃ model.stan ``` -------------------------------- ### Run rstanarm Tests Locally Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.html A script to execute all rstanarm tests locally. It loads the rstanarm library and runs prerequisite functions before initiating the tests. ```R library(rstanarm) # library(rstantools) # library(bayesplot) remove(list=ls()) ### run prerequisite functions # example_model <- stan_glmer(cbind(incidence, size - incidence) ~ size + period + (1|herd), data = lme4::cbpp, family = binomial, # this next line is only to keep the example small in size! chains = 2, cores = 1, seed = 12345, iter = 500) # last_dimnames <- function(x) { ndim <- length(dim(x)) dimnames(x)[[ndim]] } ### run tests devtools::test() ``` -------------------------------- ### Create Git Branch for New Model Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm Use git to create a new branch for your development work. The branch name should reflect the functionality being added. ```git git checkout master git checkout -b feature/ivreg ``` -------------------------------- ### Inspect Function Arguments with args() Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm Use the args() function to inspect the arguments of an existing R function, which helps in emulating its signature in a new rstanarm function. ```R > args(AER::ivreg) function (formula, instruments, data, subset, na.action, weights, offset, contrasts = NULL, model = TRUE, y = TRUE, x = FALSE, ...) ``` -------------------------------- ### Include Vignette Children Snippets Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.html Demonstrates how to include reusable code snippets from other files into vignettes using the child directive. ```R {r, child="children/*.txt"} ``` -------------------------------- ### Emulate AER::ivreg Data Parsing in R Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm Use this code to parse data in the same way as AER::ivreg. It constructs a call to AER::ivreg, evaluates it, and captures the resulting model frame. ```r mc <- match.call(expand.dots = FALSE) mc$model <- mc$y <- mc$x <- TRUE # NULLify any Stan specific arguments in mc now mc[[1L]] <- quote(AER::ivreg) if (!requireNamespace("AER")) stop("the AER package is needed by 'stan_ivreg'") mf <- eval(mc, parent.frame()) ``` -------------------------------- ### Emulate R Function Arguments in stan_foo Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm Define the stan_foo function by adopting arguments from an existing R function. The '...' argument is used to separate arguments from the emulated function and those added for rstanarm, and to pass additional arguments to the underlying sampling function. ```R stan_ivreg <- function (formula, instruments, data, subset, na.action, weights, offset, contrasts = NULL, model = TRUE, y = TRUE, x = FALSE, ...) # there will be more arguments ``` -------------------------------- ### Set knitr Chunk Options Source: https://github.com/stan-dev/rstanarm/blob/master/vignettes/children/SETTINGS-knitr.txt Configures default knitr chunk options for RStanarm documentation. This includes suppressing messages and warnings, controlling evaluation based on the NOT_CRAN environment variable, and setting default figure properties like device, resolution, aspect ratio, width, output width, and alignment. ```R stopifnot(require(knitr)) opts_chunk$set( comment=NA, message = FALSE, warning = FALSE, eval = identical(Sys.getenv("NOT_CRAN"), "true"), dev = "png", dpi = 150, fig.asp = 0.618, fig.width = 5, out.width = "60%", fig.align = "center" ) ``` -------------------------------- ### Set Default ggplot2 Theme Source: https://github.com/stan-dev/rstanarm/blob/master/vignettes/children/SETTINGS-gg.txt Loads necessary libraries and sets the default ggplot2 theme to a bayesplot theme. This is useful for ensuring consistent plot styles across multiple visualizations. ```r library(ggplot2) library(bayesplot) theme_set(bayesplot::theme_default()) ``` -------------------------------- ### View Compiled Stan Model in R Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.md Access and view the compiled Stan model object named 'continuous' within the rstanarm package. ```R rstanarm:::stanmodels$continuous ``` -------------------------------- ### Stan Generated Quantities for Intercept Transformation Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.md Shows how to transform the intercept parameter in the generated quantities block when covariates have been centered. This is necessary to report the correct intercept value. ```stan generated quantities { array[has_intercept] real alpha; if (has_intercept) { alpha[1] = gamma[1] - dot_product(beta, xbar) } } ``` -------------------------------- ### Set Normal Prior for Intercept and Coefficients Source: https://github.com/stan-dev/rstanarm/blob/master/vignettes/children/stan_glm_priors.txt Use `normal(0,1)` for both `prior_intercept` and `prior` to specify a normal distribution with mean 0 and scale 1 for the intercept and all regression coefficients. This represents a belief that parameters are equally likely to be positive or negative but unlikely to be far from zero. ```R stan_glm(formula, data = NULL, prior_intercept = normal(0, 1), prior = normal(0, 1)) ``` -------------------------------- ### Define stan_ivreg.fit function in rstanarm Source: https://github.com/stan-dev/rstanarm/wiki/Adding-a-new-model-to-rstanarm This function serves as the low-level interface for fitting instrumental variable regression models. It includes argument validation to ensure data integrity before passing to the Stan fitting function. ```R stan_ivreg.fit <- function (x, y, z, weights, offset, ...) { # there will be more arguments stopifnot(is.matrix(X), is.vector(y), is.matrix(z), length(y) == nrow(X), nrow(X) == nrow(Z)) } ``` -------------------------------- ### Stan Intercept Transformation Source: https://github.com/stan-dev/rstanarm/blob/master/dev-notes/rstanarm_dev_notes.html Transform the intercept parameter when using centered predictors in Stan. This is necessary to correctly report the intercept after fitting with centered covariates. ```stan generated quantities { real alpha[has_intercept]; { alpha[1] = gamma[1] - dot_product(beta, xbar) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.