fit: Fit a Model Specification to a Data Set

fit.cluster_specR Documentation

Fit a Model Specification to a Data Set

Description

fit() and fit_xy() take a model specification, translate_tidyclust the required code by substituting arguments, and execute the model fit routine.

Usage

## S3 method for class 'cluster_spec'
fit(object, formula, data, control = control_cluster(), ...)

## S3 method for class 'cluster_spec'
fit_xy(object, x, case_weights = NULL, control = control_cluster(), ...)

Arguments

object

An object of class cluster_spec that has a chosen engine (via set_engine()).

formula

An object of class formula (or one that can be coerced to that class): a symbolic description of the model to be fitted.

data

Optional, depending on the interface (see Details below). A data frame containing all relevant variables (e.g. predictors, case weights, etc). Note: when needed, a named argument should be used.

control

A named list with elements verbosity and catch. See control_cluster().

...

Not currently used; values passed here will be ignored. Other options required to fit the model should be passed using set_engine().

x

A matrix, sparse matrix, or data frame of predictors. Only some models have support for sparse matrix input. See modelenv::get_encoding() for details. x should have column names.

case_weights

An optional classed vector of numeric case weights. This must return TRUE when hardhat::is_case_weights() is run on it. See hardhat::frequency_weights() and hardhat::importance_weights() for examples.

Details

fit() and fit_xy() substitute the current arguments in the model specification into the computational engine's code, check them for validity, then fit the model using the data and the engine-specific code. Different model functions have different interfaces (e.g. formula or x/y) and these functions translate_tidyclust between the interface used when fit() or fit_xy() was invoked and the one required by the underlying model.

When possible, these functions attempt to avoid making copies of the data. For example, if the underlying model uses a formula and fit() is invoked, the original data are references when the model is fit. However, if the underlying model uses something else, such as x/y, the formula is evaluated and the data are converted to the required format. In this case, any calls in the resulting model objects reference the temporary objects used to fit the model.

If the model engine has not been set, the model's default engine will be used (as discussed on each model page). If the verbosity option of control_cluster() is greater than zero, a warning will be produced.

If you would like to use an alternative method for generating contrasts when supplying a formula to fit(), set the global option contrasts to your preferred method. For example, you might set it to: options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly")). See the help page for stats::contr.treatment() for more possible contrast types.

Value

A cluster_fit object that contains several elements:

  • spec: The model specification object (object in the call to fit)

  • fit: when the model is executed without error, this is the model object. Otherwise, it is a try-error object with the error message.

  • preproc: any objects needed to convert between a formula and non-formula interface (such as the terms object)

The return value will also have a class related to the fitted model (e.g. "_kmeans") before the base class of "cluster_fit".

A fitted cluster_fit object.

See Also

set_engine(), control_cluster(), cluster_spec, cluster_fit

Examples

library(dplyr)

kmeans_mod <- k_means(num_clusters = 5)

using_formula <-
  kmeans_mod %>%
  set_engine("stats") %>%
  fit(~., data = mtcars)

using_x <-
  kmeans_mod %>%
  set_engine("stats") %>%
  fit_xy(x = mtcars)

using_formula
using_x

tidyclust documentation built on Sept. 26, 2023, 1:08 a.m.