View source: R/get_confidence_set.R
| get_confidence_set | R Documentation |
Calculates the confidence set for the recommended intervention. lago_optimization() calls it when include_confidence_set is TRUE, and it can also be called directly.
get_confidence_set(
predictors_data,
include_center_effects = FALSE,
center_weights_for_outcome_goal = 1,
include_time_effects = FALSE,
time_effect_optimization_value = NULL,
additional_covariates = NULL,
intervention_components,
include_interaction_terms = FALSE,
main_components = NULL,
outcome_data,
fitted_model,
link,
outcome_goal,
outcome_type,
intervention_lower_bounds,
intervention_upper_bounds,
confidence_set_grid_step_size,
center_characteristics = NULL,
center_characteristics_optimization_values = 0,
confidence_set_alpha = 0.05,
cluster_id = NULL,
cost_list_of_vectors,
rec_int
)
predictors_data |
A data.frame. The input data containing the intervention components and center characteristics. For a continuous outcome its rows must be the rows fitted_model was fitted on (drop the rows in stats::na.action(fitted_model)). |
include_center_effects |
A boolean. Specifies whether the fixed effects should be included in the outcome model. |
center_weights_for_outcome_goal |
A numeric vector. Specifies the
weights that will be used for calculating recommended interventions that
satisfy the outcome goal for an (weighted) average center.
There is one weight per center the fitted model has, in the order of its
center levels ( |
include_time_effects |
A boolean. Specifies whether the fixed time effects should be included in the outcome model. |
time_effect_optimization_value |
The period the confidence set is computed at, as the value of the "period" column that identifies it. The recommended intervention and the estimated outcome reported alongside the confidence set are computed at this period, so the interval is computed at it too. Required when include_time_effects is TRUE. |
additional_covariates |
A character vector. The names of the columns in the dataset that represent additional covariates that need to be included in the outcome model. This includes interaction terms or any other additional covariates. |
intervention_components |
A character vector. The names of the columns in the dataset that represent the intervention components. |
include_interaction_terms |
A boolean. Specifies whether there are interaction terms in the intervention components. |
main_components |
A character vector. Specifies the main intervention components in the presence of interaction terms. |
outcome_data |
A vector. The input data containing the outcome of interest, one value per fitted row for a continuous outcome. |
fitted_model |
A glm(). The fitted glm() outcome model. Continuous
covariance uses its actual prior.weights in fitted row order, not the
working weights or the original weights argument. A NULL prior.weights
field falls back to unit weights for direct calls. Otherwise it must be a
finite nonnegative numeric vector with one entry per fitted row.
Every factor, character and logical predictor must be treatment coded with
its first level as the reference and each coefficient named after its level,
as lago_optimization() fits it. A model
fitted with another coding, such as |
link |
A character string. The link function the interval is computed on, either "logit" or "identity". These are the only links the outcome machinery implements, see supported_outcome_links(). |
outcome_goal |
A numeric value. Specifies the outcome goal, a desired probability or mean value. |
outcome_type |
A character string. Specifies the type of the outcome. Must be either "continuous" for continuous outcomes or "binary" for binary outcomes. |
intervention_lower_bounds |
A numeric vector. Specifies the lower bounds of the intervention components. |
intervention_upper_bounds |
A numeric vector. Specifies the upper bounds of the intervention components. |
confidence_set_grid_step_size |
A numeric vector. Specifies the step size of the grid search algorithm used in the confidence set calculation. |
center_characteristics |
A character vector. The names of the columns in the dataset that represent the center characteristics. |
center_characteristics_optimization_values |
A numeric vector. The fixed values of the center characteristics at which the confidence set is computed, so the confidence set is specific to a center with these characteristic values. Must have the same length and order as center_characteristics. |
confidence_set_alpha |
A numeric value. The type I error considered in the confidence set calculations. |
cluster_id |
A list of one or two vectors, a single vector, or NULL.
The cluster vectors themselves, not column names, each with one entry per
fitted row, used for a clustered variance when the "outcome_type" is
continuous. An empty list is the same as NULL. |
cost_list_of_vectors |
A list of numeric vectors. The cost vectors used in the LAGO optimization. Each must hold at least one finite number. |
rec_int |
A numeric vector, the recommended interventions calculated from the optimization step. Named values of rec_int, the bounds, the grid steps, the cost list and the center characteristic values are matched to the components (or the center characteristics) by name, with the rule lago_optimization() uses. The names are read when one of them is an expected name, or when there are two or more values and each has a name, and then they must be the expected names, each once. Other names, such as the "50%" that quantile() gives one value, are ignored. |
Continuous covariance treats fitted prior weights as relative observation weights. Scaling all weights by the same positive constant leaves the covariance unchanged for a fixed fit. Zero weight rows contribute nothing, but fitted row and cluster validation still includes them. Singular weighted bread is refused without regularization.
For the identity link without clusters, the bread is the inverse of
X^T W X. The residual scale is \sum_i w_i (y_i - \mu_i)^2
divided by the positive weight row count minus the design column count.
This denominator must be positive. For a full rank Gaussian identity fit,
this is the usual estimated dispersion covariance. With clusters, the
score for each row is w_i x_i (y_i - \mu_i).
For the logit link, the existing Gauss Newton sandwich is retained. Set
d_i = \mu_i (1 - \mu_i) x_i. The bread is the inverse of
\sum_i w_i d_i d_i^T and the row score is
w_i d_i (y_i - \mu_i). Without clusters the meat is the sum of row
score outer products (HC0). With clusters, for either link, the meat is the
sum of cluster score outer products (CR0). Two clustering dimensions use
the first covariance plus the second minus the intersection covariance,
where the intersection is each pair of clusters that occurs in the data.
That sum need not be positive semidefinite. When it is not, its negative
eigenvalues are set to 0, as Cameron, Gelbach and Miller (2011) suggest,
and a warning says so. They are the eigenvalues of the covariance of the
fitted coefficients. No finite sample correction is added.
On the identity link with a family other than gaussian, such as
quasipoisson, the unclustered covariance is the model based one of
stats::vcov(fitted_model). With clusters, w_i above is then the IRLS
working weight of the fit (fitted_model$weights), the prior weight
divided by the family's variance function, so the clustered covariance is
the sandwich form of the same fit.
These formulas extend the existing continuous covariance definition. They are not a frequency weight or survey design variance. In particular, the existing continuous quasibinomial covariance on the logit link does not use that family's variance function: it is the Gauss-Newton sandwich of a least squares fit of the logit curve, for gaussian and quasibinomial fits alike, and not the sandwich of the quasibinomial estimating equation. This weight extension does not make it a general GLM covariance. Binary outcomes continue to use stats::vcov(fitted_model). Center weights for the outcome goal are separate from observation weights.
List( confidence_set_size_percentage = <number, the size of the confidence set as a fraction of the grid. Both the count of qualifying interventions and the size of the grid count grid interventions only, so rec_int is excluded from each>, rec_int_ci = <named numeric c(lower, upper), each bound rounded to 3 decimal places, or to 4 significant digits when it is below 0.1 in absolute value, the confidence interval at rec_int. Computed whether or not it covers the outcome goal, so callers never have to look for rec_int inside cs. For a binary outcome on the logit link both bounds are confined to [0, 1], where the estimate is a probability by construction and the interval around it therefore belongs in that range; the interval is still the delta-method one and a bound at exactly 0 or 1 is one that has been truncated to the range. Not confined on the identity link, where the estimate is the linear predictor and is itself unbounded, so confining the interval would report one that excludes its own estimate. That estimate leaving [0, 1] is a defect in its own right and not this one; lago_optimization() now warns when it does, so it is flagged rather than silent, and the interval is still reported as computed for the same reason as before. Not confined for a continuous outcome either, whose range is not knowable here. The exception, for both outcome types, is the weighted average center on the logit link, whose estimate is the weighted mean of the centers' probabilities: its interval is the delta method interval of that mean and is confined to [0, 1]. NULL when that interval is not computable>, cs = <data.frame of the grid interventions whose confidence interval covers the outcome goal, with their interval bounds and cost. rec_int is never one of its rows, and need not be a grid intervention at all. NULL when no grid intervention qualifies> )
lago_optimization()
Other LAGO functions:
lago_budget(),
lago_optimization(),
lago_report(),
lago_sensitivity(),
visualize_cost()
# Normally reached through lago_optimization(include_confidence_set = TRUE).
# Called directly it needs the fitted outcome model and the recommended
# intervention from the optimization step, so both are taken from a run of
# the optimizer rather than refitting the model by hand. get_confidence_set()
# binds its prediction matrix to the coefficient vector by name, so a
# hand-fitted model may list its terms in any order. It must be fitted on
# exactly the predictors passed here, though: the intercept, the fixed
# center effects, the fixed time effects, the intervention components, the
# additional covariates and the center characteristics. Any other set of
# coefficients is an error naming what did not match.
# The lower bounds start at 1 while the data also contains 0s, so the
# optimizer warns about that; the warning is expected here.
opt <- lago_optimization(
data = BB_data,
outcome_name = "pp3_oxytocin_mother",
outcome_type = "binary",
glm_family = "binomial",
intervention_components = c("coaching_updt", "launch_duration"),
center_characteristics = c("birth_volume_100"),
center_characteristics_optimization_values = 1.75,
intervention_lower_bounds = c(1, 1),
intervention_upper_bounds = c(40, 5),
cost_list_of_vectors = list(c(0, 1700), c(0, 8000)),
outcome_goal = 0.85,
outcome_goal_intention = "maximize",
include_confidence_set = FALSE,
quiet = TRUE
)
intervention_components <- c("coaching_updt", "launch_duration")
predictors <- c(intervention_components, "birth_volume_100")
cs <- get_confidence_set(
predictors_data = BB_data[, predictors, drop = FALSE],
intervention_components = intervention_components,
outcome_data = BB_data$pp3_oxytocin_mother,
fitted_model = opt$model,
link = "logit",
outcome_goal = 0.85,
outcome_type = "binary",
intervention_lower_bounds = c(1, 1),
intervention_upper_bounds = c(40, 5),
confidence_set_grid_step_size = c(1, 1),
center_characteristics = "birth_volume_100",
center_characteristics_optimization_values = 1.75,
cost_list_of_vectors = list(c(0, 1700), c(0, 8000)),
rec_int = opt$rec_int
)
# Fraction of the grid inside the 95% confidence set: 18 of the 200 grid
# interventions qualify here (40 coaching values by 5 launch durations), so
# 0.09. print() shows the same number as a percentage.
cs$confidence_set_size_percentage
# The confidence interval at the recommended intervention, reported in its
# own field as c(lower, upper). It is computed whether or not it covers the
# outcome goal, so it is available even when no grid intervention qualifies.
# lago_optimization() reports this interval as $est_outcome_ci.
cs$rec_int_ci
# rec_int need not be one of the grid interventions, and here it is not:
# its launch_duration is about 2.78 while the grid steps through whole
# days. It is never a row of cs either, which holds the 18 qualifying grid
# interventions and nothing else.
opt$rec_int
head(cs$cs)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.