R/calculate_treatment_effects.R
treatment.effects.RdCalculates covariate conditional treatment effects using estimated benefit scores
treatment.effects(x, ...)
# S3 method for default
treatment.effects(x, ...)
treat.effects(
benefit.scores,
loss = c("sq_loss_lasso", "logistic_loss_lasso", "poisson_loss_lasso",
"cox_loss_lasso", "owl_logistic_loss_lasso", "owl_logistic_flip_loss_lasso",
"owl_hinge_loss", "owl_hinge_flip_loss", "sq_loss_lasso_gam",
"poisson_loss_lasso_gam", "logistic_loss_lasso_gam", "sq_loss_gam",
"poisson_loss_gam", "logistic_loss_gam", "owl_logistic_loss_gam",
"owl_logistic_flip_loss_gam", "owl_logistic_loss_lasso_gam",
"owl_logistic_flip_loss_lasso_gam", "sq_loss_xgboost", "custom"),
method = c("weighting", "a_learning"),
pi.x = NULL,
...
)
# S3 method for subgroup_fitted
treatment.effects(x, ...)a fitted object from fit.subgroup()
not used
vector of estimated benefit scores
loss choice USED TO CALCULATE benefit.scores of both the M function from Chen, et al (2017) and
potentially the penalty used for variable selection. See fit.subgroup for more details.
method choice USED TO CALCULATE benefit.scores. Either the "weighting" method or
"a_learning" method. See fit.subgroup for more details
The propensity score for each observation
A List with elements delta (if the treatment effects are a difference/contrast,
i.e. \(E[Y|T=1, X] - E[Y|T=-1, X]\)) and gamma (if the treatment effects are a ratio,
i.e. \(E[Y|T=1, X] / E[Y|T=-1, X]\))
fit.subgroup for function which fits subgroup identification models.
print.individual_treatment_effects for printing of objects returned by
treat.effects or treatment.effects
library(personalized)
set.seed(123)
n.obs <- 500
n.vars <- 25
x <- matrix(rnorm(n.obs * n.vars, sd = 3), n.obs, n.vars)
# simulate non-randomized treatment
xbetat <- 0.5 + 0.5 * x[,21] - 0.5 * x[,11]
trt.prob <- exp(xbetat) / (1 + exp(xbetat))
trt01 <- rbinom(n.obs, 1, prob = trt.prob)
trt <- 2 * trt01 - 1
# simulate response
delta <- 2 * (0.5 + x[,2] - x[,3] - x[,11] + x[,1] * x[,12])
xbeta <- x[,1] + x[,11] - 2 * x[,12]^2 + x[,13]
xbeta <- xbeta + delta * trt
# continuous outcomes
y <- drop(xbeta) + rnorm(n.obs, sd = 2)
# time-to-event outcomes
surv.time <- exp(-20 - xbeta + rnorm(n.obs, sd = 1))
cens.time <- exp(rnorm(n.obs, sd = 3))
y.time.to.event <- pmin(surv.time, cens.time)
status <- 1 * (surv.time <= cens.time)
# create function for fitting propensity score model
prop.func <- function(x, trt)
{
# fit propensity score model
propens.model <- cv.glmnet(y = trt,
x = x, family = "binomial")
pi.x <- predict(propens.model, s = "lambda.min",
newx = x, type = "response")[,1]
pi.x
}
subgrp.model <- fit.subgroup(x = x, y = y,
trt = trt01,
propensity.func = prop.func,
loss = "sq_loss_lasso",
nfolds = 3) # option for cv.glmnet
trt_eff <- treatment.effects(subgrp.model)
str(trt_eff)
#> List of 2
#> $ delta: num [1:500] 0.533 15.571 1.365 10.705 20.995 ...
#> ..- attr(*, "comparison.trts")= int 1
#> ..- attr(*, "reference.trt")= int 0
#> ..- attr(*, "trts")= int [1:2] 0 1
#> $ gamma: logi NA
#> - attr(*, "class")= chr [1:2] "individual_treatment_effects" "list"
trt_eff
#> Summary of individual treatment effects:
#> E[Y|T=1, X] - E[Y|T=0, X]
#>
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -25.661 -3.245 3.926 4.070 10.995 34.932
library(survival)
subgrp.model.cox <- fit.subgroup(x = x, y = Surv(y.time.to.event, status),
trt = trt01,
propensity.func = prop.func,
loss = "cox_loss_lasso",
nfolds = 3) # option for cv.glmnet
trt_eff_c <- treatment.effects(subgrp.model.cox)
str(trt_eff_c)
#> List of 2
#> $ delta: logi NA
#> $ gamma: num [1:500] 1.362 0.973 1.527 0.897 1.236 ...
#> ..- attr(*, "comparison.trts")= int 1
#> ..- attr(*, "reference.trt")= int 0
#> ..- attr(*, "trts")= int [1:2] 0 1
#> - attr(*, "class")= chr [1:2] "individual_treatment_effects" "list"
trt_eff_c
#> Summary of individual treatment effects:
#> E[Y|T=1, X] / E[Y|T=0, X]
#>
#> Note: for survival outcomes, the above ratio is
#> E[g(Y)|T=1, X] / E[g(Y)|T=0, X],
#> where g() is a monotone increasing function of Y,
#> the survival time
#>
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.3656 0.8367 1.0695 1.1264 1.3357 2.6774