% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ild_robust_se.R
\name{ild_robust_se}
\alias{ild_robust_se}
\title{Cluster-robust variance-covariance matrix for ILD model fits}
\usage{
ild_robust_se(
  fit,
  type = c("CR2", "CR3", "CR0"),
  cluster = c("id", "data"),
  cluster_vec = NULL,
  ...
)
}
\arguments{
\item{fit}{A fitted model from [ild_lme()] (lmerMod or lme).}

\item{type}{Character. Correction type: \code{"CR2"} (recommended), \code{"CR3"}, or \code{"CR0"}.}

\item{cluster}{Either \code{"id"} (default; cluster by the ILD id column from
\code{ild_data}) or \code{"data"} to use a user-supplied vector via \code{cluster_vec}.}

\item{cluster_vec}{When \code{cluster = "data"}, a vector of cluster IDs aligned to
the model rows (same length and order as \code{attr(fit, "ild_data")}).}

\item{...}{Passed to \code{clubSandwich::vcovCR()}.}
}
\value{
A list with \code{vcov} (matrix), \code{type}, \code{cluster_name},
  \code{engine} (\code{"lmer"} or \code{"lme"}), and optionally \code{message}
  if a fallback was used (e.g. lme not fully supported on this build).
}
\description{
Computes cluster-robust (sandwich) variance estimators with small-sample
corrections via the \pkg{clubSandwich} package. Use with [tidy_ild_model()]
via \code{se = "robust"} for fixed-effect inference. Requires
\code{attr(fit, "ild_data")}; refit with [ild_lme()] if missing.
}
\details{
For fits from [ild_ipw_refit()] with **estimated** IPW/IPCW weights, these SEs
apply to the **weighted outcome (mixed) model** only; they do **not** incorporate
first-stage uncertainty from the propensity/censoring models unless you treat
weights as fixed. For MSM-style inference, prefer [ild_msm_bootstrap()] with an
appropriate \code{weight_policy}; see [ild_msm_inference].
}
\examples{
if (requireNamespace("clubSandwich", quietly = TRUE)) {
  set.seed(1)
  dat <- ild_simulate(n_id = 8, n_obs_per = 6, seed = 1)
  dat <- ild_prepare(dat, id = "id", time = "time")
  dat <- ild_center(dat, y)
  fit <- ild_lme(y ~ y_bp + y_wp + (1 | id), data = dat, ar1 = FALSE, warn_no_ar1 = FALSE)
  rv <- ild_robust_se(fit, type = "CR2")
  rv$engine
  dim(rv$vcov)
}
}
\seealso{
[tidy_ild_model()] with \code{se = "robust"}, \code{clubSandwich::vcovCR},
  [ild_msm_bootstrap()], [ild_msm_inference].
}
