## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(seine)
data(elec_1968)

spec = ei_spec(
    elec_1968,
    predictors = vap_white:vap_other,
    outcome = pres_dem_hum:pres_abs,
    total = pres_total,
    covariates = c(state, pop_city:pop_rural, farm:educ_coll, inc_00_03k:inc_25_99k),
    preproc = function(x) {
        x = model.matrix(~ 0 + ., x) # convert factors to dummies
        bases::b_bart(x, trees = 250)
    }
)

## -----------------------------------------------------------------------------
m = ei_ridge(spec)
rr = ei_riesz(spec, penalty = m$penalty)

est = ei_est(m, rr, spec, contrast = list(predictor = c(1, -1, 0)), conf_level = FALSE)
print(est)

## -----------------------------------------------------------------------------
ei_sens(est, c_outcome = 0.5, c_predictor = 0.2)

## -----------------------------------------------------------------------------
ei_sens(est, c_outcome = 1, bias_bound = 0.05)

## -----------------------------------------------------------------------------
bench = ei_bench(spec, contrast = list(predictor = c(1, -1, 0)))

subset(bench, outcome == "pres_rep_nix")

## ----include=FALSE------------------------------------------------------------
fmt_pp = \(x) paste0(format(100*x, digits=2), "pp")
est_pt = subset(est, outcome=="pres_rep_nix")$estimate 
bb = subset(bench, covariate == "state" & outcome == "pres_rep_nix")
est_bias = subset(ei_sens(est, bb$c_outcome, bb$c_predictor), outcome == "pres_rep_nix")$bias_bound
est_max = max(abs(bench$est_chg))

## ----fig.height = 7, fig.alt = "Bias contour plot for the racially polarized Nixon vote"----
sens = ei_sens(est) # the default evaluates on a grid of parameters
plot(sens, "pres_rep_nix", bench = bench, bounds = c(-1, 1))

## -----------------------------------------------------------------------------
ei_sens_rv(est, bias_bound = estimate)

