R/predict.R

#'Predictive Profiling
#'
#'This function indexes one dependent variable from a numeric variable
#'@import RForcecom
#'@import dplyr
#'@export predictor
predictor <- function(access_token, instance_url, object, numfield, catfield){

  instance_u <- paste0(instance_url,'/')
  api <- '36.0'
  myquery <- paste0('Select Id, ', numfield,', ',catfield,' FROM ', object)
  session <- c(sessionID = access_token,instanceURL = instance_u, apiVersion = api)
  data1 <- rforcecom.bulkQuery(session, myquery, object)
  data1 <- na.omit(data1)

  categorical <- data1[,3]
  numeric <- data1[,2]

  if(is.numeric(categorical) == TRUE){

    if(length(unique(categorical)) == 2 ){

      categorical<- as.factor(categorical) # If the numeric variable is discreet; ie.. has two levels, 1 and 0, convert them to factor.

    }else{

      categorical <- slider2(categorical, 5)
      categorical[1] <- NULL
      categorical <- categorical[,1]

    }

  }else{

    categorical <- as.factor(categorical)
  }
  
  data1 <- data.frame(Id = data1$Id, numeric, categorical)
  indexdata <- subset(data1, select = c("numeric", "categorical"))
  indexdata <- index(indexdata)
  indexdata$index<- round(indexdata$index, 0)
  indexdata <- subset(indexdata, select = c("Categorical", "index","avg","count")) #Writing the ranks to each individual record
  data2 <- merge(data1, indexdata, by.x = "categorical", by.y = "Categorical")
  data2 <- subset(data2, select = c("Id", "categorical", "index","avg","count"))
  colnames(data2) <- c("strId", "categorical", "dist","avg","count")
  return(data2)
}
keshav-valgen/segmentation documentation built on June 6, 2019, 1:11 a.m.