source('../R/data_gen.R')
source('../R/caretImp.R')
source('../R/utils.R')
source('../R/rf.R') # random forest
source('../R/ranger.R') # ranger
source('../R/rpart.R') # rpart
source('../R/gbm.R') # gbm
source('../R/cforest.R')  # cforest
source('../R/ctree.R') # ctree
source('../R/guide.R') # guide
source('../R/rfSRC.R') # rfsrc
source('../R/glmnet.R') # glmnet
source("../R/rlt.R") # rlt
source("../R/bart.R") # bartm


## Design for parallel running in slurm
index <- Sys.getenv('SLURM_ARRAY_TASK_ID')
run <- as.numeric(index)
set.seed(run+123456)

library(pacman)

N <- 400

dm <- Sys.getenv('DM') # D1
om <- Sys.getenv('OM') # E0, E1, E2, E3, E4, E5

cat('Data Model:',  dm, '\n', 'Outcome model: ', om, '\n', 'Sample size: ', N)

SIGMA <- as.numeric(Sys.getenv('SIGMA'))

dirpath <- paste0('data/', om)

dir.create(dirpath)

data <- data_generate(N, sigma = SIGMA, dm = dm, om = om)


x <- data$x
xvars <- colnames(x)
y <- data$y

filepath <- paste0(dirpath, '/res', index, '.rds')

res <- list()
res[['bart']] <- bart_imp(x, y)
res[['rf']] <- rf_imp(x, y)
res[['rpart']] <- rpart_imp(x, y)
res[['ranger']] <- ranger_imp(x, y)
res[['rfSRC']] <- rfSRC_imp(x, y)
res[['gbm']] <- gbm_imp(x, y)
res[['cforest1']] <- cforest_imp(x, y, FALSE)
res[['cforest2']] <- cforest_imp(x, y, TRUE)
res[['ctree']] <- ctree_imp(x, y)

if (base::version$os == 'linux-gnu') {
  res[['guide']] <- guide_imp(x, y, index, method='new')
}
res[['glmnet']] <- glmnet_imp(x, y, inter_x=FALSE)

saveRDS(res, file=filepath)
