Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Created June 6, 2019 17:09
Show Gist options
  • Save apoorvalal/06a79d3ce546d325fcd72e4113061f6e to your computer and use it in GitHub Desktop.
Save apoorvalal/06a79d3ce546d325fcd72e4113061f6e to your computer and use it in GitHub Desktop.
#%%
library(LalRUtils)
load_or_install(c('tidyverse','magrittr', 'rio', 'data.table',
'stargazer', 'mgcv', 'glmnet', 'caret', 'ranger', 'doMC', 'e1071',
'parallel', 'tictoc', 'pushoverr'))
set.seed(42)
#registerDoMC(cores=4)
####################################################
rootdir = '/home/users/apoorval/tmp/FINAL/'
setwd(rootdir)
load('TrainData.RData')
colnames(train_cov) = paste0('pred', 1:ncol(train_cov))
traindat = cbind(train_dep, train_cov)
###############################################################
## setup
###############################################################
TrainingDataIndex <- createDataPartition(train_dep,
p=0.75, list = FALSE)
# Create Training Data
trainingData <- traindat[TrainingDataIndex,]
testData <- traindat[-TrainingDataIndex,]
# 10 fold CV
TrainingParameters <- trainControl(method = "repeatedcv",
number = 10, repeats=10)
###############################################################
### RANDOM FOREST
###############################################################
Rand_Tuning <- trainControl(method = "repeatedcv",
number = 10, repeats=10,
# randomise tuning parameter search
search = 'random')
tic()
rfModel <- train(train_dep ~ ., data = trainingData,
method = "ranger",
# ranger parallelises on all cores by default
# set to reasonable number
num.threads = 4,
trControl= Rand_Tuning,
metric = "RMSE",
tuneLength = 1e3,
#preProcess = c("scale","center"),
na.action = na.omit
)
toc()
RFPredictions <- predict(rfModel, testData)
RF_Diag = postResample(pred = RFPredictions,
obs = testData[, 1])
rfModel
RF_Diag
pushover_quiet('RF done')
###############################################################
#%% Produce Predictions
#load('TestCovs.Rdata')
# save.image('PredProb.Rdata')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment