Mercurial > repos > deepakjadmin > caret_reg_tool3
comparison predict.R @ 0:749240e755e1 draft default tip
Uploaded
| author | deepakjadmin |
|---|---|
| date | Fri, 22 Jan 2016 14:18:58 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:749240e755e1 |
|---|---|
| 1 ########## | |
| 2 args <- commandArgs(T) | |
| 3 arg1 <- args[1] | |
| 4 arg2 <- args[2] | |
| 5 arg3 <- args[3] | |
| 6 #source("~/galaxy-dist/tools/mpdstoolsV2/tool3/Preold.R") | |
| 7 #pre(arg1,arg2,arg3) | |
| 8 set.seed(1) | |
| 9 pre <- function(args1,args2,args3){ | |
| 10 #args <- commandArgs(TRUE) | |
| 11 nTrain <- read.csv(args1,row.names= 1, header = T) # example nTrain.csv file of unknown activity | |
| 12 #save(nTrain,file = "nTrain.RData") | |
| 13 #load("nTrain.RData") | |
| 14 load(args2) # model generated from previous programn | |
| 15 newdata <- nTrain | |
| 16 modelFit <- Fit | |
| 17 ########### | |
| 18 # input csv file must contaion the exact same column as used in model building # | |
| 19 # Also do pre-proccessing by means of centering and scaling | |
| 20 ## problem in s4 object so first check that the given model has s4 object in | |
| 21 ## >isS4(Fit$finalmodel) if it is s4 than add in with elseif loop | |
| 22 ## eg . isS4(plsFit$finalModel) == TRUE | |
| 23 f=function(x){ | |
| 24 x<-as.numeric(as.character(x)) #first convert each column into numeric if it is from factor | |
| 25 x[is.na(x)] =median(x, na.rm=TRUE) #convert the item with NA to median value from the column | |
| 26 x #display the column | |
| 27 } | |
| 28 | |
| 29 f2=function(x){ | |
| 30 all(is.na(x)) | |
| 31 } | |
| 32 | |
| 33 fop <- apply(newdata,2,f2) | |
| 34 allcolumnmissing <- which(fop) | |
| 35 if (length(allcolumnmissing) > 0){ | |
| 36 newdata[,allcolumnmissing] <- 0 | |
| 37 newdata[,allcolumnmissing] <- newdata[,allcolumnmissing] + runif(3,0,0.000000000000000001) ### add noise} | |
| 38 } | |
| 39 | |
| 40 library(caret) | |
| 41 | |
| 42 #if(as.character(!isS4(Fit$finalModel == "TRUE"))) | |
| 43 if((Fit$method != "svmRadial") && (Fit$method != "svmLinear") ) | |
| 44 { | |
| 45 reqcol <- Fit$finalModel$xNames | |
| 46 newdata <- newdata[,reqcol] | |
| 47 newdata <- apply(newdata,2,f) | |
| 48 newdata <- newdata + runif(3,0,0.01) ### add noise to overcome from NZV error | |
| 49 newdata1 <- preProcess(newdata, method = c("center", "scale")) | |
| 50 newdata11 <- predict(newdata1,newdata) | |
| 51 ########### | |
| 52 library(stats) | |
| 53 testpredict <- predict(modelFit,newdata11) | |
| 54 names <- as.data.frame(rownames(nTrain)) | |
| 55 colnames(names) <- "COMPOUND" | |
| 56 activity <- as.data.frame(testpredict) | |
| 57 colnames(activity) <- "PREDICTED VALUE" | |
| 58 dw <- format(cbind(names,activity),justify="centre") | |
| 59 write.table(dw,file=args3,row.names=FALSE,sep="\t") | |
| 60 } | |
| 61 #else if(Fit$method == "svmRadial") | |
| 62 else if((Fit$method == "svmLinear") | (Fit$method == "svmRadial")) | |
| 63 { | |
| 64 reqcol <- colnames(Fit$trainingData) | |
| 65 reqcol <- reqcol[1:length(reqcol)-1] | |
| 66 newdata <- newdata[,reqcol] | |
| 67 newdata <- apply(newdata,2,f) | |
| 68 newdata <- newdata + runif(3,0,0.01) ### add little noise to overcome from NZV problem | |
| 69 newdata1 <- preProcess(newdata, method = c("center", "scale")) | |
| 70 newdata11 <- predict(newdata1,newdata) | |
| 71 testpredict <- predict(modelFit,newdata11) | |
| 72 names <- as.data.frame(rownames(nTrain)) | |
| 73 colnames(names) <- "COMPOUND" | |
| 74 activity <- as.data.frame(testpredict) | |
| 75 colnames(activity) <- "PREDICTED VALUE" | |
| 76 dw <- format(cbind(names,activity),justify="centre") | |
| 77 write.table(dw,file=args3,row.names=FALSE,sep="\t") | |
| 78 | |
| 79 } | |
| 80 else { | |
| 81 dw <- "There is something wrong in data or model" | |
| 82 write.csv(dw,file=args3,row.names=FALSE) | |
| 83 | |
| 84 } | |
| 85 | |
| 86 } | |
| 87 pre(arg1,arg2,arg3) |
