library("cobalt")
data("lalonde", package = "cobalt")
#A user-defined version of method = "ps"
my.ps <- function(treat, covs, estimand, focal = NULL) {
covs <- make_full_rank(covs)
d <- data.frame(treat, covs)
f <- formula(d)
ps <- glm(f, data = d, family = "binomial")$fitted
w <- get_w_from_ps(ps, treat = treat, estimand = estimand,
focal = focal)
return(list(w = w, ps = ps))
}
#Balancing covariates between treatment groups (binary)
(W1 <- weightit(treat ~ age + educ + married +
nodegree + re74, data = lalonde,
method = my.ps, estimand = "ATT"))
summary(W1)
bal.tab(W1)
data("msmdata")
(W2 <- weightitMSM(list(A_1 ~ X1_0 + X2_0,
A_2 ~ X1_1 + X2_1 +
A_1 + X1_0 + X2_0,
A_3 ~ X1_2 + X2_2 +
A_2 + X1_1 + X2_1 +
A_1 + X1_0 + X2_0),
data = msmdata,
method = my.ps))
summary(W2)
bal.tab(W2)
# Kernel balancing using the KBAL package, available
# using devtools::install_github("chadhazlett/KBAL").
# Only the ATT and ATC are available. Use 'kbal.method'
# instead of 'method' in weightit() to choose between
# "ebal" and "el".
if (FALSE) {
kbal.fun <- function(treat, covs, estimand, focal, ...) {
args <- list(...)
if (is_not_null(focal))
treat <- as.numeric(treat == focal)
else if (estimand != "ATT")
stop("estimand must be 'ATT' or 'ATC'.", call. = FALSE)
if ("kbal.method" %in% names(args)) {
names(args)[names(args) == "kbal.method"] <- "method"
}
args[!names(args) %in% setdiff(names(formals(KBAL::kbal)),
c("X", "D"))] <- NULL
k.out <- do.call(KBAL::kbal, c(list(X = covs, D = treat),
args))
w <- k.out$w
return(list(w = w))
}
(Wk <- weightit(treat ~ age + educ + married +
nodegree + re74, data = lalonde,
method = kbal.fun, estimand = "ATT",
kbal.method = "ebal"))
summary(Wk)
bal.tab(Wk, disp.ks = TRUE)
}
Run the code above in your browser using DataLab