## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = requireNamespace("MatchIt", quietly = TRUE) &&
    requireNamespace("WeightIt", quietly = TRUE)
)

## ----eval = FALSE-------------------------------------------------------------
# # install.packages("remotes")
# remotes::install_github("kabajiro/oalasso")

## ----message = FALSE----------------------------------------------------------
library(oalasso)

set.seed(20260702)
n <- 500
X <- matrix(rnorm(n * 6), n, 6, dimnames = list(NULL, paste0("x", 1:6)))
A <- rbinom(n, 1, plogis(X %*% c(1, 1, 0, 0, 1, 1)))
Y <- as.numeric(2 * A + X %*% c(0.6, 0.6, 0.6, 0.6, 0, 0) + rnorm(n))
sim <- data.frame(A, Y, X)

fit.sim <- oal(A ~ x1 + x2 + x3 + x4 + x5 + x6, data = sim, outcome = ~ Y)
fit.sim

## ----message = FALSE----------------------------------------------------------
data("lalonde", package = "MatchIt")

fit <- oal(treat ~ age + educ + race + married + nodegree + re74 + re75,
           data = lalonde, outcome = ~ re78)
fit

## ----eval = FALSE-------------------------------------------------------------
# m <- MatchIt::matchit(treat ~ age + educ + race + married + nodegree + re74 + re75,
#                       data = lalonde, distance = fit$ps, method = "nearest")

## -----------------------------------------------------------------------------
m <- oal_match(fit, method = "nearest", caliper = 0.2)
summary(m)$nn

## ----eval = FALSE-------------------------------------------------------------
# fit.att <- oal(treat ~ age + educ + race + married + nodegree + re74 + re75,
#                data = lalonde, outcome = ~ re78, estimand = "ATT")
# m.att <- oal_match(fit.att, method = "nearest")

## -----------------------------------------------------------------------------
w <- oal_weight(fit)          # = weightit(..., ps = fit$ps, estimand = "ATE")
summary(w)

## -----------------------------------------------------------------------------
head(weights(fit))

## ----fig.width = 6, fig.height = 4--------------------------------------------
plot(fit, type = "wamd")

## -----------------------------------------------------------------------------
summary(fit)

## ----fig.width = 6, fig.height = 4--------------------------------------------
cobalt::bal.tab(fit)
plot(fit, type = "balance")

## ----eval = FALSE-------------------------------------------------------------
# fit.goal <- oal(treat ~ age + educ + race + married + nodegree + re74 + re75,
#                 data = lalonde, outcome = ~ re78, method = "goal")

