% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/method-residuals.R
\name{residuals}
\alias{residuals}
\alias{residuals,INLAvaan-method}
\alias{resid,INLAvaan-method}
\title{Model Residuals for INLAvaan Models}
\usage{
\S4method{residuals}{INLAvaan}(object, type = "raw", labels = TRUE, ...)

\S4method{resid}{INLAvaan}(object, type = "raw", ...)
}
\arguments{
\item{object}{An object of class \link{INLAvaan}.}

\item{type}{Character. \code{"raw"} (default) returns the unscaled
difference between the observed and model-implied covariance matrix (and
mean vector, when a mean structure is present). \code{"cor"} (or
\code{"cor.bollen"}) first rescales both matrices to a correlation
matrix. \code{"cor.bentler"} rescales both by the observed variances
(the basis of the SRMR). \code{"normalized"} and \code{"standardized"}
divide the raw residuals by their asymptotic standard errors.
\code{"casewise"} (aliases \code{"case"}, \code{"obs"},
\code{"observations"}, \code{"ov"}) returns observed-minus-fitted values
for each observation.}

\item{labels}{Logical. Attach variable names to the output. Default
\code{TRUE}.}

\item{...}{Currently unused.}
}
\value{
For moment-based \code{type}s, a list with elements
\code{type}, \code{cov}, and (when relevant) \code{mean}. For
\code{type = "casewise"}, a numeric matrix of observed-minus-fitted
values.
}
\description{
Extract the difference between the observed and model-implied (fitted)
sample statistics from a fitted \code{INLAvaan} model. As in \pkg{lavaan}
and \pkg{blavaan}, residuals are computed at the parameter estimates --
here the posterior means -- not as a posterior distribution over residuals.
}
\details{
This delegates to \pkg{lavaan}'s own \code{residuals()} machinery, so the
return structure matches lavaan exactly. Because INLAvaan stores the
posterior means as the point estimates of the fitted object, the residuals
are the observed statistics minus the posterior-mean model-implied
statistics (mirroring \pkg{blavaan}, which likewise inherits lavaan's
\code{residuals()} without overriding it).
}
\examples{
\donttest{
HS.model <- "
  visual  =~ x1 + x2 + x3
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9
"
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- acfa(HS.model, HolzingerSwineford1939, std.lv = TRUE, nsamp = 100,
            test = "none", verbose = FALSE)

# Raw residual covariance matrix (posterior means)
residuals(fit)

# SRMR-basis residuals
residuals(fit, type = "cor.bentler")

# Casewise observed-minus-fitted values
head(residuals(fit, type = "casewise"))
}

}
\seealso{
\code{\link[=fitted]{fitted()}}, \code{\link[=predict]{predict()}}, \link[lavaan:fitMeasures]{fitMeasures()}
}
