R/ModelBinaryChoice.R

#' @title A light weight implementation of a logistic regression model.
#'
#' @description
#'
#' A binary logistic model.
#'
#' @export
#'
#' @examples
#' .data <- cars
#' .data$fast_car <- ifelse(.data$speed > 20, 1, 0)
#' glm_model <- glm(fast_car ~ dist, data = .data)
#' Mod <- ModelBinaryChoice$new(glm_model$coefficients, formula = glm_model$formula)
#' Mod
#' head(Mod$predict(.data))
ModelBinaryChoice <- R6::R6Class(
  classname = "ModelBinaryChoice",
  inherit = ModelBase,
  public = list(


    #' @description
    #'
    #' Initialisation function
    #'
    #' @param params a `data.frame` object.
    #' @param formula a `formula` object.
    #' @param preprocessing_fn a pre-processing function that gets applied to the
    #'  data given to the `predict` method before making the prediction.
    #'
    #' @return NULL
    initialize = function(params, formula, preprocessing_fn = NULL) {
      super$initialize(params = params,
                       formula = formula,
                       type = "binary_choice",
                       preprocessing_fn = preprocessing_fn)
      invisible(NULL)
    },

    #' @description
    #'
    #' This predict method returns probabilities generated from the parameters
    #' of this [Model] object.
    #'
    #' @param newdata a `data.frame` object.
    #' @param link_function :: `character(1)`\cr
    #'  default as 'logit' using `stats::binomial(link = "logit")`. Choice of
    #'  'logit' and 'probit'. TODO: implement 'probit' option.
    #'
    #' @return a numeric vector.
    #' @export
    predict = function(newdata, link_function = c("logit")) {
      link_function <- match.arg(link_function)
      linear_comb   <- private$.compute_linear_combination(newdata)
      1 / (1 + exp(-linear_comb))
    }
  )
)
dymium-org/dymiumModel documentation built on June 23, 2020, 11:01 a.m.