##########################################################
# Load and process data set
##########################################################
data(bmiData)
#----------------------------------------------------#
# Recast treatment variables to (-1,1) --- required.
#----------------------------------------------------#
bmiData$A1[which (bmiData$A1=="MR")] <- 1L
bmiData$A1[which (bmiData$A1=="CD")] <- -1L
bmiData$A2[which (bmiData$A2=="MR")] <- 1L
bmiData$A2[which (bmiData$A2=="CD")] <- -1L
bmiData$A1 <- as.integer(bmiData$A1)
bmiData$A2 <- as.integer(bmiData$A2)
#----------------------------------------------------#
# define response y to be the negative 12 month
# change in BMI from baseline
#----------------------------------------------------#
bmiData$y <- -100*(bmiData[,6] - bmiData[,4])/bmiData[,4]
##########################################################
# Second-stage regression
##########################################################
#----------------------------------------------------#
# Create modeling object for main effect component
#----------------------------------------------------#
moMain <- buildModelObj(model = ~ gender + parentBMI + month4BMI,
solver.method = 'lm')
#----------------------------------------------------#
# Create modeling object for contrast component
#----------------------------------------------------#
moCont <- buildModelObj(model = ~ parentBMI + month4BMI,
solver.method='lm')
fitIQ2 <- iqLearnSS(moMain = moMain,
moCont = moCont,
data = bmiData,
response = bmiData$y,
txName = "A2",
iter = 0)
##########################################################
# Model conditional expected value of main effect term
##########################################################
#----------------------------------------------------#
# Create modeling object for main effect component
#----------------------------------------------------#
moMain <- buildModelObj(model = ~ gender + race + parentBMI + baselineBMI,
solver.method = 'lm')
#----------------------------------------------------#
# Create modeling object for contrast component
#----------------------------------------------------#
form <-
moCont <- buildModelObj(model = ~ gender + parentBMI,
solver.method = 'lm')
fitIQ1main <- iqLearnFSM(moMain = moMain,
moCont = moCont,
response = fitIQ2,
data = bmiData,
txName = "A1",
iter = 100)
##########################################################
# Model conditional mean of contrast function
##########################################################
#----------------------------------------------------#
# Create modeling object for main effect component
#----------------------------------------------------#
form <-
moMain <- buildModelObj(model = ~ gender + race + parentBMI + baselineBMI,
solver.method = 'lm')
#----------------------------------------------------#
# Create modeling object for contrast component
#----------------------------------------------------#
moCont <- buildModelObj(model = ~ gender + parentBMI + baselineBMI,
solver.method = 'lm')
fitIQ1cm <- iqLearnFSC(moMain = moMain,
moCont = moCont,
response = fitIQ2,
data = bmiData,
txName = "A1",
iter = 0)
##########################################################
# Variance Modeling
##########################################################
#----------------------------------------------------#
# Create modeling object for main effect component
#----------------------------------------------------#
moMain <- buildModelObj(model = ~ gender + race + parentBMI + baselineBMI,
solver.method = 'lm')
#----------------------------------------------------#
# Create modeling object for contrast component #
#----------------------------------------------------#
moCont <- buildModelObj(model = ~ parentBMI + baselineBMI,
solver.method='lm')
fitIQ1var <- iqLearnFSV(object = fitIQ1cm,
moMain = moMain,
moCont = moCont,
data = bmiData,
txName = "A1",
iter = 100)
##########################################################
# Optimal Treatment for first stage
##########################################################
optimalTx <- optTx(x = fitIQ1main,
y = fitIQ1cm,
z = fitIQ1var,
dens = "nonpar")
##########################################################
# Plug-in Values
##########################################################
plugInValue(optTx1 = optimalTx$optimalTx,
optTx2 = optTx(fitIQ2)$optimalTx,
response = bmiData$y,
tx1 = bmiData$A1,
tx2 = bmiData$A2)
Run the code above in your browser using DataLab