View source: R/sensitivity_functions.R
contour_plot | R Documentation |
Generates bias contour plots to aid with sensitivity analysis
contour_plot(
varW,
sigma2,
killer_confounder,
df_benchmark,
benchmark = TRUE,
shade = FALSE,
shade_var = NULL,
shade_fill = "#35a4bf",
shade_alpha = 0.25,
contour_width = 1,
binwidth = NULL,
label_size = 0.25,
point_size = 1,
nudge = 0.05,
axis_text_size = 12,
axis_title_size = 14,
axis_line_width = 1,
print = FALSE
)
varW |
Variance of the estimated weights |
sigma2 |
Estimated variance of the outcome (i.e., stats::var(Y) for obervational setting; stats::var(tau) for generalization setting)#' |
killer_confounder |
Threshold for bias considered large enough to be a killer confounder. For example, if researchers are concerned about the bias large enough to reduce an estimated treatment effect to zero or change directional sign, set |
df_benchmark |
Data frame containing formal benchmarking results. The data.frame must contain the columns |
benchmark |
Flag for whether or not to display benchmarking results ( |
shade |
Flag for whether or not a specific benchmarking covariate (or set of benchmarked covariates) should be shaded a different color ( |
shade_var |
If |
shade_fill |
Color to fill the highlighted variables. Default is set to |
shade_alpha |
Alpha value for the fill color. Default is set to |
contour_width |
Width of the contour lines. Default is set to |
binwidth |
If set to a numeric value, the function will generate a contour plot with the specified binwidth. Default is set to |
label_size |
Size of the labels. Default is set to |
point_size |
Size of the points. Default is set to |
nudge |
Nudge value for the labels. Default is set to |
axis_text_size |
Size of the axis text. Default is set to |
axis_title_size |
Size of the axis title. Default is set to |
axis_line_width |
Width of the axis lines. Default is set to |
print |
If set to |
A ggplot2 object, containing the bias contour plot
# For the external validity setting:
data(jtpa_women)
site_name <- "NE"
df_site <- jtpa_women[which(jtpa_women$site == site_name), ]
df_else <- jtpa_women[which(jtpa_women$site != site_name), ]
# Estimate unweighted estimator:
model_dim <- estimatr::lm_robust(Y ~ T, data = df_site)
PATE <- coef(lm(Y ~ T, data = df_else))[2]
DiM <- coef(model_dim)[2]
# Generate weights using observed covariates:
df_all <- jtpa_women
df_all$S <- ifelse(jtpa_women$site == "NE", 1, 0)
model_ps <- WeightIt::weightit(
(1 - S) ~ . - site - T - Y,
data = df_all, method = "ebal", estimand = "ATT"
)
weights <- model_ps$weights[df_all$S == 1]
# Estimate IPW model:
model_ipw <- estimatr::lm_robust(Y ~ T, data = df_site, weights = weights)
ipw <- coef(model_ipw)[2]
# Estimate bound for var(tau):
vartau <- var(df_site$Y[df_site$T == 1]) - var(df_site$Y[df_site$T == 0])
RV <- robustness_value(estimate = ipw, b_star = 0, sigma2 = vartau, weights = weights)
# Select weighting variables:
weighting_vars <- names(df_all)[which(!names(df_all) %in% c("site", "S", "Y", "T"))]
# Run benchmarking:
df_benchmark <- run_benchmarking(
weighting_vars = weighting_vars,
data = df_all[, -1],
treatment = "T", outcome = "Y", selection = "S",
estimate = ipw,
RV = RV, sigma2 = vartau,
estimand = "PATE"
)
# Generate bias contour plot:
contour_plot(
var(weights), vartau, ipw, df_benchmark,
benchmark = TRUE, shade = TRUE,
shade_var = c("age", "prevearn"),
label_size = 4
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.