-
-
Notifications
You must be signed in to change notification settings - Fork 40
Model-averaged estimates/intervals/distributions #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1715b38
ba161b6
f8244c5
191aaf9
d44c8c7
626b9ea
0cfa280
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||||||||||||||||||||||||
| 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
|
||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||
| )$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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||
| )$root | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| c(CI_low, CI_high) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| .tailarea <- function(theta, theta_hats, se_theta_hats, model_weights, alpha, residual_dfs) { | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [air] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||
| t_quantiles <- (theta - theta_hats) / se_theta_hats | ||||||||||||||||||||||||
| sum(model_weights * stats::pt(t_quantiles, df = residual_dfs)) - alpha | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[air] reported by reviewdog 🐶