R/makeModel.R

Defines functions makeModel.mlogit makeModel.WrappedModel makeModel.train

Documented in makeModel.mlogit makeModel.train makeModel.WrappedModel

#' @title A convenient function to create a [dymiumCore::Model] object using a supported model object.
#'
#' @description
#'
#' Make a light weight model object that can be used by dymiumCore's transition functions.
#' It currently supports:
#' * caret's train and
#' * mlogit's
#'
#' @param model a model object. See the description section for the supported models.
#' @param preprocessing_fn a preprocessing function.
#' @param ... dots.
#'
#' @return a `Model` object.
#' @export
"makeModel" <- function(model, ...) {
  UseMethod("makeModel")
}

#' @rdname makeModel
makeModel.train <- function(model, preprocessing_fn = NULL, ...) {
  compatible_methods = c("glm")

  if (!checkmate::test_subset(x = model$method,
                              choices = compatible_methods,
                              empty.ok = FALSE)) {
    stop(
      "ModelCaretTrain only works with: ",
      paste(compatible_methods,  collapse = ", "),
      ".",
      " Consider using `Model` instead if the given model is not currently supported."
    )
  }

  if (model$method == "glm") {
    return(
      ModelBinaryChoice$new(
        params = model$finalModel$coefficients,
        formula = model$terms,
        preprocessing_fn = preprocessing_fn
      )
    )
  }

  stop("something went wrong.")
}

#' @rdname makeModel
makeModel.WrappedModel <-
  function(model, preprocessing_fn = NULL, ...) {
    stop("Not implemented yet.")
  }

#' @rdname makeModel
makeModel.mlogit <-
  function(model, preprocessing_fn = NULL, ...) {
    ModelMultinomialLogit$new(params = model[['coefficients']],
                              formula = model[['formula']],
                              preprocessing_fn = preprocessing_fn)
  }
dymium-org/dymiumModel documentation built on June 23, 2020, 11:01 a.m.