conformalIte: Conformal inference for individual treatment effects

Description Usage Arguments Details Value Examples

View source: R/conformalIte.R

Description

conformalIte supports four algorithms: the nested approach with exact and inexact calibration for cases with both potential outcomes missing, the naive approach for cases with both potential outcomes missing and the counterfactual inference for cases with only one potential outcome missing. For each algorithm, it supports both split conformal inference and CV+, including weighted Jackknife+ as a special case. For each type, it supports both conformalized quantile regression (CQR) and standard conformal inference based on conditional mean regression.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
conformalIte(
  X,
  Y,
  T,
  alpha = 0.1,
  algo = c("nest", "naive", "counterfactual"),
  exact = FALSE,
  type = c("CQR", "mean"),
  side = c("two", "above", "below"),
  quantiles = NULL,
  outfun = NULL,
  outparams = list(),
  psfun = NULL,
  psparams = list(),
  cfprop = 0.5,
  citype = c("CQR", "mean"),
  lofun = NULL,
  loquantile = 0.4,
  loparams = list(),
  upfun = NULL,
  upquantile = 0.6,
  upparams = list(),
  useCV = FALSE,
  trainprop = 0.75,
  nfolds = 10,
  wthigh = 20,
  wtlow = 0.05
)

Arguments

X

covariates.

Y

observed outcome vector.

T

treatment indicator, a binary vector.

alpha

confidence level.

algo

a string that takes values in {"nest", "naive", "counterfactual"}. See Details.

exact

a logical indicating whether the exact calibration is used for nested approach. Used only when algo = "nest". See Details.

type

a string that takes values in {"CQR", "mean"}.

side

a string that takes values in {"two", "above", "below"}. See Details.

quantiles

for covariates in the training data. Used only when type = "CQR". See Details.

outfun

a function that models the conditional mean or quantiles, or a valid string. The default is random forest when type = "mean" and quantile random forest when type = "CQR". See Details.

outparams

a list of other parameters to be passed into outfun.

psfun

a function that models the missing mechanism (probability of missing given X), or a valid string. The default is "Boosting". See Details.

psparams

a list of other parameters to be passed into psfun.

cfprop

the proportion of units to be used to compute ITE intervals in nested approach. Used only when algo = "nest".

citype

the type of interval conformal inference used in the nested approach with exact calibration. Used only when algo = "nest" and exact = TRUE.

lofun

the function to fit the lower bound, or a valid string. Used only when algo = "nest". See Details.

loquantile

the quantile to fit for lofun; see Details. Used only when algo = "nest" and citype = "CQR". See Details.

loparams

a list of other parameters to be passed into lofun.

upfun

the function to fit the upper bound, or a valid string. Used only when algo = "nest". See Details.

upquantile

the quantile to fit for upfun. Used only when algo = "nest" and citype = "CQR". See Details.

upparams

a list of other parameters to be passed into upfun.

useCV

FALSE for split conformal inference and TRUE for CV+.

trainprop

proportion of units for training outfun. The default if 75%. Used only when useCV = FALSE.

nfolds

number of folds. The default is 10. Used only when useCV = TRUE.

wthigh

upper truncation level of weights. See predict.conformalSplit or predict.conformalCV.

wtlow

lower truncation level of weights. See predict.conformalSplit or predict.conformalCV.

Details

The algorithm to be used is controlled by algo and exact:

When side = "above", intervals are of form [-Inf, a(x)] and when side = "below" the intervals are of form [a(x), Inf].

When type = "CQR", quantiles must be a vector of 2, regardless of side. When side = "two", quantiles will be used in outfun for both Y(1) and Y(0); when side = "above" or "below", quantiles[1] will be used for Y(0) and quantiles[2] will be used for Y(1).

outfun is applied to both Y(1) and Y(0). outfun can be a valid string, including

or a function object whose input must include, but not limited to

When type = "CQR", outfun should also include an argument quantiles that is either a vector of length 2 or a scalar, depending on the argument side. The output of outfun must be a matrix with two columns giving the conditional quantile estimates when quantiles is a vector of length 2; otherwise, it must be a vector giving the conditional quantile estimate or conditional mean estimate. Other optional arguments can be passed into outfun through outparams.

lofun and upfun have the same forms as outfun except that the input quantiles must be scalar when citype = "CQR", instead of a vector of 2, because only one conditional quantile is fitted. The argument loquantile is used for lofun and the argument hiquantile is used for upfun. Moreover, the output must be a vector giving the conditional quantile estimate or conditional mean estimate. Other optional arguments can be passed into lofun through loparams and upfun through upparams.

psfun can be a valid string, including

or a function object whose input must include, but not limited to

The output of psfun must be a vector of predicted probabilities. Other optional arguments can be passed into psfun through psparams.

Value

a function that outputs the interval estimates on a given dataset. When algo = "nest" or "naive", it takes a single input X; when algo = "counterfactual", it takes three inputs X, Y and T.

#' @seealso conformal, conformalInt, conformalCf

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Generate potential outcomes from two linear models
set.seed(1)
n <- 1000
d <- 5
X <- matrix(rnorm(n * d), nrow = n)
beta <- rep(1, 5)
Y1 <- X %*% beta + rnorm(n)
Y0 <- rnorm(n)

# Generate treatment indicators
ps <- pnorm(X[, 1])
T <- as.numeric(ps < runif(n))
Y <- ifelse(T == 1, Y1, Y0)

# Generate testing data
ntest <- 5
Xtest <- matrix(rnorm(ntest * d), nrow = ntest)

# Inexact nested method
CIfun <- conformalIte(X, Y, T, alpha = 0.1, algo = "nest", exact = FALSE, type = "CQR",
                      quantiles = c(0.05, 0.95), outfun = "quantRF", useCV = FALSE)
CIfun(Xtest)

# Exact nested method
CIfun <- conformalIte(X, Y, T, alpha = 0.1, algo = "nest", exact = TRUE, type = "CQR",
                      quantiles = c(0.05, 0.95), outfun = "quantRF",  useCV = FALSE)
CIfun(Xtest)

# naive method
CIfun <- conformalIte(X, Y, T, alpha = 0.1, algo = "naive", type = "CQR",
                      quantiles = c(0.05, 0.95), outfun = "quantRF",  useCV = FALSE)
CIfun(Xtest)

# counterfactual method, Y and T needs to be observed
pstest <- pnorm(Xtest[, 1])
Ttest <- as.numeric(pstest < runif(ntest))
Y1test <- Xtest %*% beta + rnorm(ntest)
Y0test <- rnorm(ntest)
Ytest <- ifelse(Ttest == 1, Y1test, Y0test)
CIfun <- conformalIte(X, Y, T, alpha = 0.1, algo = "counterfactual", type = "CQR",
                      quantiles = c(0.05, 0.95), outfun = "quantRF",  useCV = FALSE)
CIfun(Xtest, Ytest, Ttest)

lihualei71/cfcausal documentation built on April 8, 2021, 3:55 a.m.