R/mformula.R

Defines functions mformula

Documented in mformula

#' model formula
#'
#' Model formula based on discrete variables and continuous variables

#' @param y The response variable
#' @param fe.disc The vector of discrete variables. Default is NULL.
#' @param fe.cont The vector of continuous variables. Default is NULL.
#' @return Model formula
#' @export
#'
#' @examples
#' # Using discrete and continuous variables
#' y="resp"
#' fe.disc=c("x1","x2","x3","x4")
#' fe.cont="x"
#' mformula(y,fe.disc, fe.cont)
#' as.formula(mformula(y,fe.disc, fe.cont))
#'
#'
#' mformula("resp,fe.disc,fe.cont)
#'
#'
#' # Only using discrete variables
#' y="resp"
#' fe.disc=c("x1","x2","x3","x4")
#'
#' mformula(y,fe.disc)
#' as.formula(mformula(y,fe.disc))
#'
#'
#'
#' # None of the variables are defined, this only output the response variable.
#' y="resp"
#' mformula(y)
#'


mformula <-  function(y="y",fe.disc=NULL,fe.cont=NULL){  #,model.type="hglmbc"
  fac_fomular <- paste0(y,"~")

  if(length(fe.cont)==0&length(fe.disc)==0){
    print("None of the variables are defined!!!!!, only the response variable")
    cont_fomular <- paste0(y)

  }else{

    if(length(fe.cont)!=0){
      for(i in 1:length(fe.disc)){
        temp <- paste0("as.factor(",fe.disc[i],")+")
        fac_fomular <- paste0(fac_fomular,temp)
      }

      cont_fomular <- fac_fomular

      for(j in 1:length(fe.cont)){
        temp <- paste0(fe.cont[j])
        cont_fomular <- paste0(cont_fomular,temp)
      }

    }else{

      for(i in 1:(length(fe.disc)-1)){
        temp <- paste0("as.factor(",fe.disc[i],")+")
        fac_fomular <- paste0(fac_fomular,temp)
      }
      cont_fomular <- paste(fac_fomular,fe.disc[length(fe.disc)])
    }

  }
  return(cont_fomular)
}
niroshar/hglmbc documentation built on June 15, 2020, 12:40 a.m.