Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,111 changes: 1,524 additions & 587 deletions R/bart.R

Large diffs are not rendered by default.

3,153 changes: 2,249 additions & 904 deletions R/bcf.R

Large diffs are not rendered by default.

28 changes: 20 additions & 8 deletions R/calibration.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Calibrate the scale parameter on an inverse gamma prior for the global error variance as in Chipman et al (2022)
#'
#'
#' Chipman, H., George, E., Hahn, R., McCulloch, R., Pratola, M. and Sparapani, R. (2022). Bayesian Additive Regression Trees, Computational Approaches. In Wiley StatsRef: Statistics Reference Online (eds N. Balakrishnan, T. Colton, B. Everitt, W. Piegorsch, F. Ruggeri and J.L. Teugels). https://doi.org/10.1002/9781118445112.stat08288
#'
#' @param y Outcome to be modeled using BART, BCF or another nonparametric ensemble method.
Expand All @@ -10,7 +10,7 @@
#' @param standardize (Optional) Whether or not outcome should be standardized (`(y-mean(y))/sd(y)`) before calibration of `lambda`. Default: `TRUE`.
#'
#' @return Value of `lambda` which determines the scale parameter of the global error variance prior (`sigma^2 ~ IG(nu,nu*lambda)`)
#' @export
#' @export
#'
#' @examples
#' n <- 100
Expand All @@ -21,14 +21,26 @@
#' lambda <- calibrateInverseGammaErrorVariance(y, X, nu = nu)
#' sigma2hat <- mean(resid(lm(y~X))^2)
#' mean(var(y)/rgamma(100000, nu, rate = nu*lambda) < sigma2hat)
calibrateInverseGammaErrorVariance <- function(y, X, W = NULL, nu = 3, quant = 0.9, standardize = TRUE) {
calibrateInverseGammaErrorVariance <- function(
y,
X,
W = NULL,
nu = 3,
quant = 0.9,
standardize = TRUE
) {
# Compute regression basis
if (!is.null(W)) basis <- cbind(X, W)
else basis <- X
if (!is.null(W)) {
basis <- cbind(X, W)
} else {
basis <- X
}
# Standardize outcome if requested
if (standardize) y <- (y-mean(y))/sd(y)
if (standardize) {
y <- (y - mean(y)) / sd(y)
}
# Compute the "regression-based" overestimate of sigma^2
sigma2hat <- mean(resid(lm(y~basis))^2)
sigma2hat <- mean(resid(lm(y ~ basis))^2)
# Calibrate lambda based on the implied quantile of sigma2hat
return((sigma2hat*qgamma(1-quant,nu))/nu)
return((sigma2hat * qgamma(1 - quant, nu)) / nu)
}
Loading
Loading