Skip to content

Commit ab4063f

Browse files
committed
Format R code using air
1 parent 45a6eda commit ab4063f

File tree

14 files changed

+6093
-2657
lines changed

14 files changed

+6093
-2657
lines changed

R/bart.R

Lines changed: 1524 additions & 587 deletions
Large diffs are not rendered by default.

R/bcf.R

Lines changed: 2249 additions & 904 deletions
Large diffs are not rendered by default.

R/calibration.R

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' Calibrate the scale parameter on an inverse gamma prior for the global error variance as in Chipman et al (2022)
2-
#'
2+
#'
33
#' 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
44
#'
55
#' @param y Outcome to be modeled using BART, BCF or another nonparametric ensemble method.
@@ -10,7 +10,7 @@
1010
#' @param standardize (Optional) Whether or not outcome should be standardized (`(y-mean(y))/sd(y)`) before calibration of `lambda`. Default: `TRUE`.
1111
#'
1212
#' @return Value of `lambda` which determines the scale parameter of the global error variance prior (`sigma^2 ~ IG(nu,nu*lambda)`)
13-
#' @export
13+
#' @export
1414
#'
1515
#' @examples
1616
#' n <- 100
@@ -21,14 +21,26 @@
2121
#' lambda <- calibrateInverseGammaErrorVariance(y, X, nu = nu)
2222
#' sigma2hat <- mean(resid(lm(y~X))^2)
2323
#' mean(var(y)/rgamma(100000, nu, rate = nu*lambda) < sigma2hat)
24-
calibrateInverseGammaErrorVariance <- function(y, X, W = NULL, nu = 3, quant = 0.9, standardize = TRUE) {
24+
calibrateInverseGammaErrorVariance <- function(
25+
y,
26+
X,
27+
W = NULL,
28+
nu = 3,
29+
quant = 0.9,
30+
standardize = TRUE
31+
) {
2532
# Compute regression basis
26-
if (!is.null(W)) basis <- cbind(X, W)
27-
else basis <- X
33+
if (!is.null(W)) {
34+
basis <- cbind(X, W)
35+
} else {
36+
basis <- X
37+
}
2838
# Standardize outcome if requested
29-
if (standardize) y <- (y-mean(y))/sd(y)
39+
if (standardize) {
40+
y <- (y - mean(y)) / sd(y)
41+
}
3042
# Compute the "regression-based" overestimate of sigma^2
31-
sigma2hat <- mean(resid(lm(y~basis))^2)
43+
sigma2hat <- mean(resid(lm(y ~ basis))^2)
3244
# Calibrate lambda based on the implied quantile of sigma2hat
33-
return((sigma2hat*qgamma(1-quant,nu))/nu)
45+
return((sigma2hat * qgamma(1 - quant, nu)) / nu)
3446
}

0 commit comments

Comments
 (0)