Compute bias relative to term-specific true values within grouped simulation results
Source:R/model_evaluation.R
eval_bias.Rd
Computes the mean bias (difference between estimated values and true values)
within each group, typically inside evaluate_model_results()
for simulation evaluation pipelines.
Arguments
- x
A numeric vector of estimates (e.g., from a model term).
- term
A named numeric vector providing the true value for each term. For example,
c("(Intercept)" = 0, x = 2)
to specify the true values for each term. IfNULL
(default), bias is computed relative to zero.- na.rm
Logical; whether to remove missing values when computing the mean bias. Defaults to
FALSE
.
Details
This function is designed to be used inside dplyr::summarise()
within a grouped
tidyverse pipeline, typically after grouping by term
. It computes the mean of
x
minus the true value for the corresponding term.
If term
is provided, the current grouping must include a term
variable matching
the names in term
. If a term in the group is not found in the provided term
mapping,
the function will return NA
with a warning.
Examples
library(dplyr)
#> Error in library(dplyr): there is no package called ‘dplyr’
library(purrr)
library(broom.mixed)
#> Error in library(broom.mixed): there is no package called ‘broom.mixed’
# Simulate and fit models
sim_models <- tibble(
id = 1:50,
model = map(1:50, ~ lm(mpg ~ wt, data = mtcars))
) |>
extract_model_results()
#> Error in loadNamespace(x): there is no package called ‘tidyr’
# Compute bias relative to true value (hypothetical slope = -5)
sim_models |>
filter(term == "wt") |>
evaluate_model_results(
bias = eval_bias(
estimate,
term = c("wt" = -5)
)
)
#> Error in loadNamespace(x): there is no package called ‘dplyr’
# Compute bias relative to zero for all terms
sim_models |>
group_by(term) |>
evaluate_model_results(
bias = eval_bias(estimate)
)
#> Error in loadNamespace(x): there is no package called ‘dplyr’