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)))
|
4
|
25 colExclude <- colRate > 0.1
|
3
|
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
|
4
|
35
|
|
36 rowExclude <- rowRate > 0
|
3
|
37 if(any(rowExclude)){
|
|
38 rawData <- rawData[!rowExclude, ]
|
|
39 ##hasMissing <- apply(rawData[, predictorNames, drop = FALSE],
|
|
40 ##1, function(x) mean(is.na(x)))
|
|
41
|
|
42 ############################################################################
|
|
43
|
|
44
|
|
45 ###############################################################################
|
|
46 } else {
|
|
47 rawData <- rawData[complete.cases(rawData),]
|
|
48
|
|
49 }
|
|
50
|
4
|
51 set.seed(2)
|
3
|
52
|
|
53 #print(dim(dataX))
|
|
54 #print(dim(rawData))
|
|
55 #print(length(dataY))
|
|
56
|
|
57 nzv <- nearZeroVar(rawData[,1:(length(rawData) - 1)])
|
|
58 if(length(nzv) > 0) {
|
|
59 #nzvVars <- names(rawData)[nzv]
|
|
60 rawData <- rawData[,-nzv]
|
|
61 #rawData$outcome <- dataY
|
|
62 }
|
|
63
|
|
64 predictorNames <- names(rawData)[names(rawData) != "outcome"]
|
|
65
|
|
66 dx <- rawData[,1:length(rawData)-1]
|
|
67 dy <- rawData[,length(rawData)]
|
|
68 corrThresh <- as.numeric(arg8)
|
|
69 highCorr <- findCorrelation(cor(dx, use = "pairwise.complete.obs"),corrThresh)
|
|
70 dx <- dx[, -highCorr]
|
|
71 subsets <- seq(1,length(dx),by=5)
|
|
72 normalization <- preProcess(dx)
|
|
73 dx <- predict(normalization, dx)
|
|
74 dx <- as.data.frame(dx)
|
|
75
|
|
76 if (arg4 == "lmFuncs"){
|
|
77 ctrl1 <- rfeControl(functions = lmFuncs,
|
|
78 method = arg5 ,
|
|
79 repeats = as.numeric(arg6),
|
|
80 number = as.numeric(arg7),
|
|
81 verbose = FALSE)
|
|
82 } else if(arg4 == "rfFuncs"){
|
|
83 ctrl1 <- rfeControl(functions = rfFuncs,
|
|
84 method = arg5 ,
|
|
85 repeats = as.numeric(arg6),
|
|
86 number = as.numeric(arg7),
|
|
87 verbose = FALSE)
|
|
88 }else if (arg4 == "treebagFuncs"){
|
|
89 ctrl1 <- rfeControl(functions = treebagFuncs,
|
|
90 method = arg5 ,
|
|
91 repeats = as.numeric(arg6),
|
|
92 number = as.numeric(arg7),
|
|
93 verbose = FALSE)
|
|
94 }else {
|
|
95
|
|
96 ctrl1 <- rfeControl(functions = nbFuncs,
|
|
97 method = arg5 ,
|
|
98 repeats = as.numeric(arg6),
|
|
99 number = as.numeric(arg7),
|
|
100 verbose = FALSE)
|
|
101 }
|
|
102
|
|
103
|
|
104
|
|
105
|
|
106 Profile <- rfe(dx, dy,sizes = subsets,rfeControl = ctrl1)
|
|
107
|
|
108 pred11 <- predictors(Profile)
|
|
109 save(Profile,file=arg2)
|
|
110 dataX <- rawData[,pred11]
|
|
111 dataY <- rawData$outcome
|
|
112
|
|
113 save(dataX,dataY,file=arg3)
|
|
114 rm(dataX)
|
|
115 rm(dataY)
|
|
116
|