% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/onramp.R
\name{f_hat_from_models}
\alias{f_hat_from_models}
\title{Build the \code{F_hat} matrix from a list of fitted study-level models}
\usage{
f_hat_from_models(models, grid, predict_fn = NULL)
}
\arguments{
\item{models}{A non-empty list of fitted model objects, one per study.}

\item{grid}{A data frame (or matrix) of grid points at which to evaluate
each model. Same columns as the data each model was fitted on.}

\item{predict_fn}{Optional \verb{function(model, grid)} returning a numeric
vector; defaults to the class-aware dispatcher above.}
}
\value{
An \code{length(models)}-by-\code{nrow(grid)} numeric matrix; row \code{i} is
model \code{i} evaluated at every row of \code{grid}.
}
\description{
Many users arrive at MetaHunt with one fitted model per study (e.g. a
\code{ranger::ranger()} random forest or a \code{grf::causal_forest()}) and a
chosen evaluation grid. \code{f_hat_from_models()} evaluates each model on
the shared grid and stacks the predictions into the \code{m}-by-\code{G_grid}
matrix the rest of the package expects.
}
\details{
By default the function dispatches on the model class:
\itemize{
\item \code{ranger} objects are evaluated as \code{predict(model, data = grid)$predictions}.
\item Objects inheriting from \code{causal_forest} or \code{grf} are evaluated as
\code{predict(model, newdata = grid)$predictions}.
\item All other classes fall through to
\code{as.numeric(predict(model, newdata = grid))}, which works for \code{lm},
\code{glm}, \code{randomForest}, and most other R model objects.
}

Override the dispatch with \code{predict_fn = function(model, grid) ...} if
your models need bespoke handling. The function must return a length-\code{G_grid}
numeric vector for each model.

All rows of the returned matrix must have the same length (\code{G_grid})
and contain no \code{NA} values; the function errors otherwise.
}
\examples{
# Toy example: each "centre" fits a polynomial regression
set.seed(1)
make_centre_data <- function(slope) {
  x <- runif(60)
  data.frame(x = x, y = slope * x + rnorm(60, sd = 0.1))
}
models <- lapply(c(-1, 0, 1, 0.5, -0.5), function(s)
  stats::lm(y ~ poly(x, 2), data = make_centre_data(s)))

grid  <- data.frame(x = seq(0, 1, length.out = 30))
F_hat <- f_hat_from_models(models, grid)
dim(F_hat)   # 5 x 30

}
\seealso{
\code{\link[=build_grid]{build_grid()}} to construct \code{grid} from a reference dataset.
}
