% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ild_msm_bootstrap.R
\name{ild_msm_bootstrap}
\alias{ild_msm_bootstrap}
\title{Cluster bootstrap inference for weighted \code{lmer} (MSM / IPW sensitivity)}
\usage{
ild_msm_bootstrap(
  fit = NULL,
  formula = NULL,
  data = NULL,
  weights_col = ".ipw",
  n_boot = 200L,
  weight_policy = c("fixed_weights", "reestimate_weights"),
  weights_fn = NULL,
  cluster = c("id", "data"),
  cluster_vec = NULL,
  coef_fun = function(f) lme4::fixef(f),
  seed = NULL,
  verbose = FALSE,
  ...
)
}
\arguments{
\item{fit}{A \code{lmerMod} from [ild_ipw_refit()] or [ild_lme()] with \code{attr(fit, "ild_data")}.
Ignored if \code{formula} and \code{data} are supplied.}

\item{formula}{Optional. If \code{fit} is \code{NULL}, mixed-model formula for refitting.}

\item{data}{Optional ILD object when \code{fit} is \code{NULL}; must contain \code{weights_col}.}

\item{weights_col}{Name of the row-level weight column in each bootstrap dataset (default \code{".ipw"}).}

\item{n_boot}{Number of bootstrap replicates.}

\item{weight_policy}{\code{"fixed_weights"} (subset rows; weights unchanged within replicated rows)
or \code{"reestimate_weights"} (apply \code{weights_fn} to each resampled dataset before fitting).}

\item{weights_fn}{Function \code{function(x)} taking ILD \code{x} and returning ILD with
\code{weights_col} present. Required when \code{weight_policy = "reestimate_weights"}.
Ignored for \code{"fixed_weights"}.}

\item{cluster}{\code{"id"} (default: cluster by \code{ild_meta(data)$ild_id}) or \code{"data"}.}

\item{cluster_vec}{When \code{cluster = "data"}, length-\code{nrow(data)} vector of cluster IDs
aligned to \code{ild_data} rows.}

\item{coef_fun}{\code{function(fit)} returning named numeric vector of estimands (default \code{lme4::fixef}).}

\item{seed}{Optional integer passed to \code{set.seed()} before resampling.}

\item{verbose}{Logical. If \code{TRUE}, warn on failed refits.}

\item{...}{Passed to \code{lme4::lmer()} on each successful replicate (e.g. \code{control}).
Do not pass \code{weights} here; use \code{weights_col} on the ILD data.}
}
\value{
An object of class \code{ild_msm_bootstrap}: \code{replicates} (matrix \code{n_boot} x \code{p}),
  \code{estimate} (point estimates from \code{fit}), \code{bootstrap_se}, \code{conf_low}, \code{conf_high},
  \code{term_names}, \code{n_boot}, \code{n_success}, \code{n_cluster}, metadata, and \code{fit} (original).
  Use [tidy_ild_msm_bootstrap()] for an \code{\link{ild_tidy_schema}} tibble.
}
\description{
Resamples **clusters** (persons) with replacement, subsets \code{attr(fit, "ild_data")}
(or \code{data}), optionally re-estimates weights, and refits \code{\link[lme4]{lmer}} with
the same formula. Collects fixed effects across replicates for bootstrap standard errors and
percentile confidence intervals.
}
\examples{
if (requireNamespace("lme4", quietly = TRUE)) {
  set.seed(5001)
  d <- ild_simulate(n_id = 12, n_obs_per = 6, seed = 5001)
  d$stress <- rnorm(nrow(d))
  d <- ild_prepare(d, id = "id", time = "time")
  d <- ild_center(d, y)
  d$.ipw <- runif(nrow(d), 0.8, 1.2)
  f0 <- ild_lme(y ~ y_bp + y_wp + stress + (1 | id), data = d, ar1 = FALSE,
    warn_no_ar1 = FALSE, warn_uncentered = FALSE)
  fw <- ild_ipw_refit(f0, data = d, weights = ".ipw")
  b <- ild_msm_bootstrap(fw, n_boot = 15L, weight_policy = "fixed_weights", seed = 2)
  print(b)
  tidy_ild_msm_bootstrap(b)
}
}
\seealso{
[ild_msm_inference], [ild_ipw_refit()], [tidy_ild_msm_bootstrap()]
}
