#------------------------------------------------------------------------------#
# This is simulation study#1 described in Biometrics \bold{68} 4 pp 1010-1018
#------------------------------------------------------------------------------#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
# Generate data
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
expit <- function(x) { exp(x)/(1+exp(x)) }
n <- 100
x1 <- runif(n, -1.5, 1.5)
x2 <- runif(n, -1.5, 1.5)
x12 <- x1*x1
x22 <- x2*x2
p1 <- expit(-1.0 + 0.8*x12 + 0.8*x22)
a1 <- rbinom(n=n, size=1, prob=p1)
mean <- exp(2.0 - 1.5*x12 - 1.5*x22 + 3.0*x1*x2 + a1*(-0.1 - x1 + x2))
y <- abs(rnorm(n,mean,sd=1))
data <- data.frame(x1,x12,x2,x22,a1,y)
#--------------------------------------------------------------------------#
# tx.var tells optimal which columns of data correspond to treatments.
#--------------------------------------------------------------------------#
tx.vars <- "a1"
#--------------------------------------------------------------------------#
# modeling.object for propensity of treatment.
#--------------------------------------------------------------------------#
moPropen <- buildModelObj(model = ~x12 + x22,
solver.method = 'glm',
solver.args = list('family'='binomial'),
predict.method = 'predict.glm',
predict.args = list(type='response'))
#--------------------------------------------------------------------------#
# modeling.object for conditional expectation models
#--------------------------------------------------------------------------#
expec.main <- buildModelObj(model = ~ x12 + x22 + x1:x2 + a1 + a1:(x1 + x2),
solver.method = 'lm')
expec.cont <- NULL
#--------------------------------------------------------------------------#
# treatment regime rules at each decision point.
#--------------------------------------------------------------------------#
tx.rules <- function(a,b,c,data){
as.numeric(a + b*data$x1 + c*data$x2 > 0 )}
#--------------------------------------------------------------------------#
# genoud requires some additional information
#--------------------------------------------------------------------------#
c1 <- c(-1,-1,-1)
c2 <- c( 1, 1, 1)
Domains <- cbind(c1,c2)
starts <- c(0,0,0)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
#!! A LARGER VALUE FOR POP.SIZE IS RECOMMENDED !!#
#!! THIS VALUE WAS CHOSEN TO MINIMIZE RUN TIME OF EXAMPLES !!#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
pop.size <- 50
ft <- optimalSeq(moPropen = moPropen,
moMain = expec.main,
moCont = expec.cont,
data = data,
response = y,
txName = tx.vars,
regimes = tx.rules,
pop.size = pop.size,
starting.values = starts,
Domains = Domains,
solution.tolerance = 0.0001,
iter = 0)
# The normalized estimated optimal treatment regime
# True Regime (-0.07, -0.71, 0.71)
est <- regimeCoef(ft)
est <- est/sqrt(est %*% est)
print(est)
# The estimated mean potential outcome for the treatment regime
# defined by the regime
estimator(ft)
# Access value objects of regression steps
genetic(ft)
outcome(ft)
propen(ft)
# Retrieve optimal treatment for training data
head(optTx(ft))
summary(ft)Run the code above in your browser using DataLab