Skip to content
Draft
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ S3method(visualisation_recipe,n_clusters_dbscan)
S3method(visualisation_recipe,n_clusters_elbow)
S3method(visualisation_recipe,n_clusters_gap)
S3method(visualisation_recipe,n_clusters_silhouette)
export(averaged_parameters)
export(bootstrap_model)
export(bootstrap_parameters)
export(ci)
Expand Down
55 changes: 55 additions & 0 deletions R/averaged_parameters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' @export
averaged_parameters <- function(..., ci = .95, verbose = TRUE) {

Check warning on line 2 in R/averaged_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/averaged_parameters.R,line=2,col=43,[numeric_leading_zero_linter] Include the leading zero for fractional numeric constants.

Check warning on line 2 in R/averaged_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/averaged_parameters.R,line=2,col=43,[numeric_leading_zero_linter] Include the leading zero for fractional numeric constants.
insight::check_if_installed("performance")
models <- list(...)

# compute model weights
aic_values <- sapply(models, performance::performance_aic)
delta_aic <- aic_values - min(aic_values)
model_weights <- exp(-0.5 * delta_aic) / sum(exp(-0.5 * delta_aic))

# residual df's
residual_dfs <- sapply(models, degrees_of_freedom, method = "residual")

# data grid for average predictions
predictions <- lapply(models, function(m) {
d <- insight::get_datagrid(m)
new_data <- as.data.frame(lapply(d, function(i) {
if (is.factor(i)) {
as.factor(levels(i)[1])
} else if (is.numeric(i)) {
mean(i, na.rm = TRUE)
} else {
unique(i)[1]
}
}))
insight::get_predicted(m, data = new_data, ci = .95, predict = "link")

Check warning on line 26 in R/averaged_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/averaged_parameters.R,line=26,col=53,[numeric_leading_zero_linter] Include the leading zero for fractional numeric constants.

Check warning on line 26 in R/averaged_parameters.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/averaged_parameters.R,line=26,col=53,[numeric_leading_zero_linter] Include the leading zero for fractional numeric constants.
})

theta_hats <- unlist(predictions)
se_theta_hats <- sapply(predictions, function(p) {
attributes(p)$ci_data$SE
})

alpha <- (1 - ci) / 2

CI_low <- stats::uniroot(
f = .tailarea, interval = c(-1e+10, 1e+10), theta_hats = theta_hats,
se_theta_hats = se_theta_hats, model_weights = model_weights, alpha = alpha,
residual_dfs = residual_dfs, tol = 1e-10
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
f = .tailarea, interval = c(-1e+10, 1e+10), theta_hats = theta_hats,
se_theta_hats = se_theta_hats, model_weights = model_weights, alpha = alpha,
residual_dfs = residual_dfs, tol = 1e-10
f = .tailarea,
interval = c(-1e+10, 1e+10),
theta_hats = theta_hats,
se_theta_hats = se_theta_hats,
model_weights = model_weights,
alpha = alpha,
residual_dfs = residual_dfs,
tol = 1e-10

)$root

CI_high <- stats::uniroot(
f = .tailarea, interval = c(-1e+10, 1e+10), theta_hats = theta_hats,
se_theta_hats = se_theta_hats, model_weights = model_weights, alpha = 1 - alpha,
residual_dfs = residual_dfs, tol = 1e-10
Comment on lines +43 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
f = .tailarea, interval = c(-1e+10, 1e+10), theta_hats = theta_hats,
se_theta_hats = se_theta_hats, model_weights = model_weights, alpha = 1 - alpha,
residual_dfs = residual_dfs, tol = 1e-10
f = .tailarea,
interval = c(-1e+10, 1e+10),
theta_hats = theta_hats,
se_theta_hats = se_theta_hats,
model_weights = model_weights,
alpha = 1 - alpha,
residual_dfs = residual_dfs,
tol = 1e-10

)$root

c(CI_low, CI_high)
}


.tailarea <- function(theta, theta_hats, se_theta_hats, model_weights, alpha, residual_dfs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
.tailarea <- function(theta, theta_hats, se_theta_hats, model_weights, alpha, residual_dfs) {
.tailarea <- function(
theta,
theta_hats,
se_theta_hats,
model_weights,
alpha,
residual_dfs
) {

t_quantiles <- (theta - theta_hats) / se_theta_hats
sum(model_weights * stats::pt(t_quantiles, df = residual_dfs)) - alpha
}
Loading