R/rfwrap.R

Defines functions rfwrap

#' Fit rf models using a loop give a set of responses
#'
#' @resp = list of responses
#' @pred= list of predictors
#' @modsave = path of folder where to save models
#' @indat = input dataset
#' 

rfwrap<-function(resp,pred,modsave,indat,class){
    if(class==TRUE){
    results<-foreach (x=1:length(resp),.errorhandling=c('remove'),.combine="rbind")%dopar% {
        rs<-paste(pred,collapse="+")
        form<-as.formula(paste(resp[x],"~",rs))
        indat[,resp[x]]<-as.factor(indat[,resp[x]])
        mod<-randomForest(form,data=indat,importance=TRUE)
        filep<-paste(modsave,"/",resp[x],sep="")
        save(mod,file=filep)
        df<-data.frame(predictor=row.names(mod$importance),mod$importance,variable=resp[x])
        row.names(df)<-1:dim(df)[1]
        df
    }
    } else {
        results<-foreach (x=1:length(resp),.errorhandling=c('remove'),.combine="rbind")%dopar% {
            rs<-paste(pred,collapse="+")
            form<-as.formula(paste(resp[x],"~",rs))
            mod<-randomForest(form,data=indat,importance=TRUE)
            filep<-paste(modsave,"/",resp[x],sep="")
            save(mod,file=filep)
            df<-data.frame(predictor=row.names(mod$importance),mod$importance,variable=resp[x])
            row.names(df)<-1:dim(df)[1]
            df
        }
    }
return(results)
}



        
drmarcogir/marcoUtils documentation built on Sept. 11, 2019, 12:08 a.m.