% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/interface_to_interface.R
\name{matrix_to_formula}
\alias{matrix_to_formula}
\title{Convert a matrix-based model to a formula interface}
\usage{
matrix_to_formula(
  fit_func,
  predict_func = function(model, newX, ...) stats::predict(model, newdata = newX, ...)
)
}
\arguments{
\item{fit_func}{A model-fitting function whose first two positional
arguments are \code{x} (numeric matrix) and \code{y} (response vector),
e.g. \code{glmnet::glmnet}.}

\item{predict_func}{A prediction function with signature
\code{function(model, newX, ...)} where \code{newX} is a numeric matrix.
Defaults to a thin wrapper around \code{stats::predict} that passes
\code{newdata} as \code{newx}.}
}
\value{
A named list with two elements:
\describe{
\item{\code{fit(formula, data, ...)}}{Fits the model. The formula is
expanded with \code{model.matrix}; the intercept column is dropped
before passing to \code{fit_func} (add it back via \code{...} if your
model needs it). Extra arguments are forwarded to \code{fit_func}.}
\item{\code{predict(model, newdata, ...)}}{Generates predictions.
\code{newdata} is expanded with the same \code{model.matrix} terms
captured at fit time. Extra arguments are forwarded to
\code{predict_func}.}
}
}
\description{
Wraps a model function that expects a numeric matrix \code{X} and a response
vector \code{y} (like \code{glmnet::glmnet}) so it can be called with the
familiar \code{formula + data} interface.  The formula is expanded via
\code{\link[stats]{model.matrix}}, which handles factor dummy-coding,
interactions, and inline transformations automatically.
}
\examples{
\dontrun{
glmnet_formula <- matrix_to_formula(
  fit_func = glmnet::glmnet,
  predict_func = function(model, newX, ...) {
    glmnet::predict.glmnet(model, newx = newX, s = 0.01, ...)
  }
)
model <- glmnet_formula$fit(mpg ~ wt + hp + factor(cyl), data = mtcars)
glmnet_formula$predict(model, newdata = mtcars[1:5, ])
}

}
