# Formats an interval's endpoints for the prose
ci_text <- function(bounds, digits) {
sprintf("%.*f to %.*f", digits, bounds[[1]], digits, bounds[[2]])
}
# 95% percentile interval for R^2 from resampled cars.
# Resampling rows with replacement mimics
# drawing a new set of cars from the same population.
# The refits use lm.fit directly, because 2,000 workflow fits would be slow.
boot_r2_ci <- function(formula, data, n_boot = 2000) {
set.seed(571)
design <- model.matrix(formula, data)
y <- model.response(model.frame(formula, data))
r2 <- vapply(seq_len(n_boot), \(b) {
rows <- sample(nrow(design), replace = TRUE)
sse <- sum(lm.fit(design[rows, ], y[rows])$residuals^2)
1 - sse / sum((y[rows] - mean(y[rows]))^2)
}, numeric(1))
unname(quantile(r2, c(0.025, 0.975)))
}
# 95% percentile interval for the correlation of a and b
boot_cor_ci <- function(a, b, n_boot = 20000) {
set.seed(571)
corr <- vapply(seq_len(n_boot), \(i) {
rows <- sample(length(a), replace = TRUE)
cor(a[rows], b[rows])
}, numeric(1))
unname(quantile(corr, c(0.025, 0.975)))
}
# 95% percentile interval for each feature's VIF.
# The VIF of feature j is the jth diagonal entry
# of the inverse of the features' correlation matrix,
# which gives the same value as 1 / (1 - R^2) without a regression per feature.
boot_vif_ci <- function(features, n_boot = 20000) {
set.seed(571)
vifs <- vapply(seq_len(n_boot), \(b) {
resample <- features[sample(nrow(features), replace = TRUE), ]
diag(solve(cor(resample)))
}, numeric(ncol(features)))
apply(vifs, 1, quantile, probs = c(0.025, 0.975))
}
# The 95% interval for a linear combination w'beta
# of a fitted model's coefficients:
# the estimate plus or minus a t quantile times its standard error,
# from the coefficients' covariance matrix
lincomb_ci <- function(model, w) {
estimate <- sum(w * coef(model))
half <- qt(0.975, df.residual(model)) * sqrt(drop(t(w) %*% vcov(model) %*% w))
c(estimate = estimate, lower = estimate - half, upper = estimate + half)
}