#------------------------------------------------------------------------------#
#This is simulation study #1 described in Stat 2012; 1: 103-114.
#------------------------------------------------------------------------------#
library(MASS)
library(rpart)
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
#Generate data
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
expit <- function(x) { exp(x)/(1+exp(x)) }
n <- 200
means <- rep(0,5)
vmat <- matrix(0,nrow=5,ncol=5) + diag(5)
X <- data.frame(mvrnorm(n,means,vmat))
p1 <- expit(-0.1 + 0.5*X$X1 + 0.5*X$X2)
a <- rbinom(n=n, size=1, prob=p1)
tg <- I(X$X1 > -0.545) * I( X$X2 < 0.545)
means <- exp( 2 + 0.25*X$X1 + 0.25*X$X2 - 0.25*X$X5 - 0.3*(a-tg)^2)
y <- sapply(means,function(x){rnorm(n=1, mean=x, sd=1)})
df <- data.frame(X,a)
colnames(df) <- c("x1", "x2", "x3", "x4", "x5", "a1")
#--------------------------------------------------------------------------#
# Define the propensity for treatment model and methods.
#--------------------------------------------------------------------------#
propen <- buildModelObj(model = ~ x1 + x2,
solver.method = 'glm',
solver.args = list('family'='binomial'),
predict.method = 'predict.glm',
predict.args = list(type='response'))
#--------------------------------------------------------------------------#
# Define the conditional expectation model.
#--------------------------------------------------------------------------#
expec.main <- buildModelObj(model = ~x1+x2+x3+x4+x5,
solver.method = 'lm')
expec.cont <- buildModelObj(model = ~x1+x2+x3+x4+x5,
solver.method = 'lm')
#--------------------------------------------------------------------------#
# Define the classification model.
#--------------------------------------------------------------------------#
class <- buildModelObj(model = ~x1 + x2 + x3 + x4 + x5,
solver.method = 'rpart',
solver.args = list(method="class"),
predict.args = list(type='class'))
#--------------------------------------------------------------------------#
# Specify the column index of df corresponding to the treatment covariate
#--------------------------------------------------------------------------#
tx.vars <- "a1"
estAIPWE <- optimalClass(moPropen = propen,
moMain = expec.main,
moCont = expec.cont,
moClass = class,
data = df,
response = y,
txName = tx.vars,
iter=0)
estimator(estAIPWE)
classif(estAIPWE)
outcome(estAIPWE)
propen(estAIPWE)
head(optTx(estAIPWE))
estIPWE <- optimalClass(moPropen = propen,
moMain = NULL,
moCont = NULL,
moClass = class,
data = df,
response = y,
txName = tx.vars,
iter=0)
estimator(estIPWE)
classif(estIPWE)
propen(estIPWE)
head(optTx(estIPWE))
Run the code above in your browser using DataLab