-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMODELRFPRED
40 lines (31 loc) · 1.71 KB
/
MODELRFPRED
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
BEGIN
sys.rqScriptDrop('MODELRFPRED');
sys.rqScriptCreate('MODELRFPRED','function(dat,srcdat, datastore_name){
library(randomForest)
# Loading Trained Model from existing datastore
ore.load(name = datastore_name)
# Pulling into R Dataframe
testdat <- ore.pull(dat)
# Factors automatically converted to Character.
# The primary key if converted to factor will be returned from testOriginal
testOriginal <- ore.pull(dat)
ore.sync(table = srcdat)
traindat <- ore.pull(ore.get(srcdat))
# Equalizing labels for categorical variable between train and Test Dataset
## Find which columns are factors
factor_cols <- names(testdat)[sapply(testdat,is.character)]
## Converting training character columns to factor
traindat[, factor_cols] <- lapply(traindat[, factor_cols], as.factor)
testdat[, factor_cols] <- lapply(testdat[, factor_cols], as.factor)
## Changing the level
for(i in factor_cols){
levels(testdat[,i]) <- levels(traindat[,i])
}
# Predict
predRF <- predict(object = modRF,newdata = testdat,type = "class")
predRFprob <- predict(object = modRF,newdata = testdat,type = "prob")
# Creating predicted probability and actual label predicted dataframe
pred.df <- data.frame(id=testOriginal[,1] ,probY=as.double(predRFprob[,1]), probN=as.double(predRFprob[,2]),Ind = predRF)
pred.df
}');
END;