3
|
1 args <- commandArgs(T)
|
|
2
|
|
3 arg1 <- args[1]
|
|
4 arg2 <- args[2]
|
|
5 arg3 <- args[3]
|
|
6 arg4 <- args[4]
|
|
7 arg5 <- args[5]
|
|
8 arg6 <- args[6]
|
|
9 arg7 <- args[7]
|
|
10 arg8 <- args[8]
|
|
11
|
|
12 library(caret)
|
|
13 load(arg1)
|
|
14
|
|
15 RAWDATA <- dataX
|
|
16 RAWDATA$outcome <- dataY
|
|
17 rawData <- dataX
|
|
18 predictorNames <- names(rawData)
|
|
19
|
|
20 isNum <- apply(rawData[,predictorNames, drop = FALSE], 2, is.numeric)
|
|
21 if(any(!isNum)) stop("all predictors in rawData should be numeric")
|
|
22
|
|
23 colRate <- apply(rawData[, predictorNames, drop = FALSE],
|
|
24 2, function(x) mean(is.na(x)))
|
|
25 colExclude <- colRate > 0.01
|
|
26 if(any(colExclude)){
|
|
27 predictorNames <- predictorNames[-which(colExclude)]
|
|
28 rawData <- RAWDATA[, c(predictorNames,"outcome")]
|
|
29 } else {
|
|
30 rawData <- RAWDATA
|
|
31 }
|
|
32 rowRate <- apply(rawData[, predictorNames, drop = FALSE],
|
|
33 1, function(x) mean(is.na(x)))
|
|
34
|
|
35 rowno <- dim(rawData)[1]
|
|
36 if (rowno <= 1000){
|
|
37 cutoff <- rowno / (rowno * 100)
|
|
38 } else if (rowno > 1000 & rowno <= 5000) {
|
|
39 cutoff <- rowno / (rowno * 100 * 0.5 )
|
|
40 } else {
|
|
41 cutoff <- rowno / (rowno * 100 * 0.5 * 0.5)
|
|
42 }
|
|
43 rowExclude <- rowRate > cutoff
|
|
44 if(any(rowExclude)){
|
|
45 rawData <- rawData[!rowExclude, ]
|
|
46 ##hasMissing <- apply(rawData[, predictorNames, drop = FALSE],
|
|
47 ##1, function(x) mean(is.na(x)))
|
|
48
|
|
49 ############################################################################
|
|
50
|
|
51
|
|
52 ###############################################################################
|
|
53 } else {
|
|
54 rawData <- rawData[complete.cases(rawData),]
|
|
55
|
|
56 }
|
|
57
|
|
58 set.seed(1234)
|
|
59
|
|
60 #print(dim(dataX))
|
|
61 #print(dim(rawData))
|
|
62 #print(length(dataY))
|
|
63
|
|
64 nzv <- nearZeroVar(rawData[,1:(length(rawData) - 1)])
|
|
65 if(length(nzv) > 0) {
|
|
66 #nzvVars <- names(rawData)[nzv]
|
|
67 rawData <- rawData[,-nzv]
|
|
68 #rawData$outcome <- dataY
|
|
69 }
|
|
70
|
|
71 predictorNames <- names(rawData)[names(rawData) != "outcome"]
|
|
72
|
|
73 dx <- rawData[,1:length(rawData)-1]
|
|
74 dy <- rawData[,length(rawData)]
|
|
75 corrThresh <- as.numeric(arg8)
|
|
76 highCorr <- findCorrelation(cor(dx, use = "pairwise.complete.obs"),corrThresh)
|
|
77 dx <- dx[, -highCorr]
|
|
78 subsets <- seq(1,length(dx),by=5)
|
|
79 normalization <- preProcess(dx)
|
|
80 dx <- predict(normalization, dx)
|
|
81 dx <- as.data.frame(dx)
|
|
82
|
|
83 if (arg4 == "lmFuncs"){
|
|
84 ctrl1 <- rfeControl(functions = lmFuncs,
|
|
85 method = arg5 ,
|
|
86 repeats = as.numeric(arg6),
|
|
87 number = as.numeric(arg7),
|
|
88 verbose = FALSE)
|
|
89 } else if(arg4 == "rfFuncs"){
|
|
90 ctrl1 <- rfeControl(functions = rfFuncs,
|
|
91 method = arg5 ,
|
|
92 repeats = as.numeric(arg6),
|
|
93 number = as.numeric(arg7),
|
|
94 verbose = FALSE)
|
|
95 }else if (arg4 == "treebagFuncs"){
|
|
96 ctrl1 <- rfeControl(functions = treebagFuncs,
|
|
97 method = arg5 ,
|
|
98 repeats = as.numeric(arg6),
|
|
99 number = as.numeric(arg7),
|
|
100 verbose = FALSE)
|
|
101 }else {
|
|
102
|
|
103 ctrl1 <- rfeControl(functions = nbFuncs,
|
|
104 method = arg5 ,
|
|
105 repeats = as.numeric(arg6),
|
|
106 number = as.numeric(arg7),
|
|
107 verbose = FALSE)
|
|
108 }
|
|
109
|
|
110
|
|
111
|
|
112
|
|
113 Profile <- rfe(dx, dy,sizes = subsets,rfeControl = ctrl1)
|
|
114
|
|
115 pred11 <- predictors(Profile)
|
|
116 save(Profile,file=arg2)
|
|
117 dataX <- rawData[,pred11]
|
|
118 dataY <- rawData$outcome
|
|
119
|
|
120 save(dataX,dataY,file=arg3)
|
|
121 rm(dataX)
|
|
122 rm(dataY)
|
|
123
|