% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ctEmpiricalBayesFit.R
\name{ctEmpiricalBayesFit}
\alias{ctEmpiricalBayesFit}
\title{Empirical Bayes subject-wise ctsem fits}
\usage{
ctEmpiricalBayesFit(
  datalong,
  model,
  subjects = "all",
  priors = TRUE,
  optimize = TRUE,
  cores = 2,
  subjectFitArgs = list(),
  Npasses = 2,
  ebUse = c("rawest", "rawposterior"),
  ebRobust = TRUE,
  ebOutlierMAD = 6,
  ebOutlierQuantiles = c(0.025, 0.975),
  ebWinsorize = TRUE,
  minsd = 1e-06,
  verbose = 0,
  progress = TRUE,
  ...
)
}
\arguments{
\item{datalong}{Long format data containing multiple subjects.}

\item{model}{Model object from \code{\link{ctModel}}. Time independent
predictors are not supported. A random-effect-free copy of this model is used
for the per-subject fits.}

\item{subjects}{Vector of subject identifiers to fit, or \code{'all'}.}

\item{priors}{Logical. Passed to \code{\link{ctFit}}; defaults to \code{TRUE}.}

\item{optimize}{Logical. Passed to \code{\link{ctFit}}; defaults to
\code{TRUE}.}

\item{cores}{Number of subjects to fit in parallel. Each individual
subject-level \code{\link{ctFit}} call uses one core.}

\item{subjectFitArgs}{Named list of additional arguments passed to each
\code{\link{ctFit}} call. For optimized fits, \code{optimcontrol$stochastic}
defaults to \code{FALSE} for all EB passes unless supplied here or in
\code{...}. First-pass fits force \code{optimcontrol$estonly=TRUE}.}

\item{Npasses}{Total number of subject-wise fitting passes. The default
\code{2} fits once with the model prior, builds a marginal empirical Bayes
prior, then fits once with that prior. Values above \code{2} repeatedly map
the previous pass estimates back to the original raw scale, rebuild the
marginal EB prior, and refit.}

\item{ebUse}{\code{'rawest'} to build the empirical Bayes prior from first
pass point estimates, or \code{'rawposterior'} to pool raw posterior samples.}

\item{ebRobust}{Logical. If TRUE, the empirical Bayes prior is built from
robust raw summaries after outlier handling.}

\item{ebOutlierMAD}{Positive numeric. Raw values farther than this
many MAD-scaled deviations from the median are treated as outliers. Use
\code{Inf} or \code{NULL} to disable this rule.}

\item{ebOutlierQuantiles}{Length two numeric vector of lower and upper
quantiles used to bound first-pass raw values, or \code{NULL} to disable.}

\item{ebWinsorize}{Logical. If TRUE, outlying first-pass raw values are
clamped to the outlier bounds before computing the EB prior. If FALSE, they
are set to missing for EB prior construction.}

\item{minsd}{Lower bound used for empirical raw SDs before model adjustment.}

\item{verbose}{Integer from 0 to 2. Passed to \code{\link{ctFit}}.}

\item{progress}{Logical. If TRUE, report the current EB fitting stage and
overwrite a single console line with the subject fitting percentage.}

\item{...}{Additional arguments passed to each \code{\link{ctFit}} call.}
}
\value{
Object of class \code{ctEmpiricalBayesFit}, containing the subject
fit lists and metadata. \code{$initialfits} contains the first-pass model
prior fits, \code{$fits} contains the final empirical Bayes prior fits, and
\code{$passfits} contains every pass. Use \code{summary()} to compute final
transformed-parameter means, SDs, covariances, correlations, and outlier
diagnostics.
}
\description{
Fits one ctsem model per subject using the model prior, estimates the
empirical marginal distribution of the raw parameters, then fits each subject
again using the resulting empirical Bayes prior.
}
\examples{
\donttest{
model <- ctModel(type='ct', manifestNames='Y1', LAMBDA=matrix(1))
eb <- ctEmpiricalBayesFit(ctstantestdat, model, cores=2,
  subjectFitArgs=list(optimcontrol=list(finishsamples=20)))
summary(eb)
}
}
