| Title: | Tidy Intensive Longitudinal Data Analysis |
|---|---|
| Description: | A reproducible, tidyverse-style framework for intensive longitudinal data analysis in R, with built-in methodological safeguards, provenance tracking, and reporting tools. Encodes time structure, enforces within-between decomposition, provides spacing-aware lags, and integrates diagnostics and visualization. Use ild_prepare(), ild_center(), ild_lag(), and related functions for a unified pipeline from raw EMA/diary data to interpretable models. |
| Authors: | Alex Litovchenko [aut, cre] |
| Maintainer: | Alex Litovchenko <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-05-19 09:58:05 UTC |
| Source: | https://github.com/cran/tidyILD |
tidyILD is a reproducible, tidyverse-style framework for intensive longitudinal data (ILD) analysis in R, with built-in methodological safeguards, provenance tracking, and reporting tools. It supports ecological momentary assessment (EMA) and diary studies with a tidy pipeline from raw data to mixed-effects models: explicit time structure, within-between decomposition, spacing-aware lags, and diagnostics. Use it when you have repeated measures per person over time and want consistent handling of time, gaps, centering, and residual correlation (AR1/CAR1).
All ILD structure ('.ild_*' columns and 'ild_*' metadata) is created only
by ild_prepare (via the internal constructor). Downstream
functions expect data prepared with ild_prepare(). For the full
workflow and applications, see the vignettes. Estimands that require
joint multivariate dynamics, penalized high-dimensional
longitudinal models, or full latent-variable DSEM are better handled
by specialist packages after preprocessing; see
vignette("ild-specialist-backends", package = "tidyILD").
A minimal workflow: simulate or load data, prepare with
ild_prepare, inspect with ild_summary, apply
ild_center and ild_lag, fit with
ild_lme or ild_fit (same engines plus optional
backend = "brms"), then ild_diagnostics or
ild_plot. State-space: ild_kfas. See the examples below.
ild_diagnostics_bundle, guardrail_registry,
ild_tidy_schema, ild_augment_schema
ild_prepare, ild_as_tsibble,
as_ild,
is_ild, validate_ild, ild_meta
ild_summary,
ild_spacing_class, ild_spacing,
ild_design_check, ild_missing_pattern,
ild_missing_compliance, ild_missing_cohort,
ild_missing_hazard_first, ild_missingness_report,
ild_missing_bias, ild_missing_model,
ild_ipw_weights, ild_ipw_refit,
ild_plot (types: trajectory, gaps, missingness, predicted_trajectory).
These overlap with ild_diagnostics_utilities (bundle section providers).
ild_center, ild_center_plot,
ild_decomposition, ild_lag, ild_check_lags,
ild_panel_lag_prepare, ild_crosslag, ild_align
ild_fit (unified: lme4, nlme, brms), ild_lme,
ild_brms (Bayesian), ild_kfas (KFAS state-space; not via ild_fit),
ild_person_model, ild_heterogeneity, ild_heterogeneity_stratified,
ild_tvem (time-varying effects)
ild_diagnose (ild_diagnostics_bundle),
ild_diagnostics_utilities, ild_acf, ild_diagnostics,
ild_plot (types: fitted, predicted_trajectory, residual_acf; optional facet_by),
ild_plot_predicted_trajectory, ild_heatmap,
ild_spaghetti, ild_circadian, ild_tvem_plot
ild_provenance, ild_history,
ild_methods, ild_report, ild_compare_pipelines,
ild_compare_fits, ild_export_provenance
vignette("developer-contracts", package = "tidyILD");
normative spec: system.file("dev", "DEVELOPER_CONTRACTS.md", package = "tidyILD")
ild_prior_ild, ild_tidy, ild_augment,
ild_diagnose, ild_autoplot,
augment_ild_model, tidy_ild_model
(model or robust SE via se = "robust"), ild_robust_se;
tidy.ild_lme, augment.ild_lme (broom.mixed, see broom_ild_lme)
ild_plot, ild_spaghetti, and ild_heatmap cover trajectories,
heatmaps, gaps, and missingness; optional facet_by adds panels (e.g. by cluster).
For observed and fitted lines over time, use ild_plot_predicted_trajectory or
ild_plot(..., type = "predicted_trajectory"). After ild_diagnose, use
ild_autoplot on the bundle. Backend-specific plots include ild_tvem_plot,
ild_plot_states, ild_plot_forecast, and ild_plot_filtered_vs_smoothed
(KFAS). See vignette("visualization-in-tidyILD", package = "tidyILD") for a question-to-function map
and recipes (including partial effects via marginaleffects / ggeffects on _wp / _bp columns).
browseVignettes("tidyILD") lists all vignettes. Key entries:
From raw data to model with tidyILD — Full pipeline: prepare, inspect, center, lag, fit, diagnose.
Visualization in tidyILD — Index of plots, bundle sections, facet_by,
predicted trajectories, and partial-effects templates.
Short analysis report — Fit, tidy fixed effects, fitted vs observed, residual ACF and Q-Q.
Within-between decomposition and irregular spacing — Centering (BP/WP), gap-aware lags, spacing classification.
Temporal dynamics: choosing a model for ILD —
Maps estimands to lags, residual AR (ild_lme), time-varying effects (ild_tvem),
and state-space backends (ild_kfas, ild_ctsem).
Specialist backends: when to move beyond the default stack — Handoffs to dynamite, PGEE, DSEM, and multivariate workflows; export patterns after prepare/center/lag.
Reproducible ILD workflows with tidyILD provenance — Inspect history, generate methods text, ild_report(), export and compare pipelines.
Glossary and quick-start checklist — Table of main functions and a short checklist.
Heterogeneity and person-specific effects — BLUPs vs ild_person_model,
ild_heterogeneity(), bundle plots.
Missingness in ILD: diagnostics and sensitivity routes —
ild_missingness_report(), MAR/MNAR context, IPW templates.
ILD: Intensive longitudinal data; many repeated measurements per person over time (e.g. EMA).
Within-between decomposition: ild_center
adds _bp (person mean) and _wp (within-person deviation);
use WP for within-person effects and BP for between-person or
cross-level terms.
Spacing-aware lags: ild_lag supports
index, gap_aware (NA when gap > max_gap), and
time_window; avoids misalignment from assuming equal spacing.
Residual correlation: ild_lme can fit nlme
with AR1 or CAR1 for residual autocorrelation;
ild_spacing_class helps choose regular-ish vs
irregular-ish spacing.
Person-level: ild_person_model fits models separately
per participant; ild_person_distribution plots the distribution of
estimates across persons (N-of-1 / idiographic). ild_heterogeneity summarizes
partial-pooling person-specific effects from mixed models (contrasts with no pooling).
Alex Litovchenko [email protected]
browseVignettes and vignette(package = "tidyILD") for
vignettes. Core entry points: ild_prepare, ild_lme.
vignette("ild-specialist-backends", package = "tidyILD") for using
tidyILD as a preprocessing layer with external multivariate or high-dimensional estimators.
Related packages: lme4, nlme (model backends),
broom.mixed (tidiers).
library(tidyILD) d <- ild_simulate(n_id = 10, n_obs_per = 12, irregular = TRUE, seed = 42) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_summary(x) x <- ild_center(x, y) x <- ild_lag(x, y, mode = "gap_aware", max_gap = 7200) fit <- ild_lme(y ~ 1, data = x, ar1 = TRUE, correlation_class = "CAR1") ild_diagnostics(fit, data = x) ild_plot(fit, type = "fitted")library(tidyILD) d <- ild_simulate(n_id = 10, n_obs_per = 12, irregular = TRUE, seed = 42) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_summary(x) x <- ild_center(x, y) x <- ild_lag(x, y, mode = "gap_aware", max_gap = 7200) fit <- ild_lme(y ~ 1, data = x, ar1 = TRUE, correlation_class = "CAR1") ild_diagnostics(fit, data = x) ild_plot(fit, type = "fitted")
If the object already has the required '.ild_*' columns and
attributes, validates and returns it (with tidyild_df and ild_tbl class if missing).
Otherwise errors.
as_ild(x)as_ild(x)
x |
A data frame or tibble that may already be ILD-shaped. |
An ILD tibble with class tidyild_df and ild_tbl.
Returns a tibble conforming to ild_augment_schema: .ild_id,
.ild_time, .outcome, .fitted, .resid, .resid_std,
engine, model_class, plus optional columns (see schema).
.resid_std is Pearson-type when residuals(fit, type = "pearson") is
available and length-matched; otherwise NA (principled but sparse).
Used internally by [ild_diagnostics()] and [ild_plot()]. Requires
attr(fit, "ild_data"); refit with [ild_lme()] if missing.
augment_ild_model(fit, ...)augment_ild_model(fit, ...)
fit |
A fitted model from [ild_lme()] (must have |
... |
Unused. |
A tibble; see ild_augment_schema.
These S3 methods delegate to [broom.mixed::tidy()] and [broom.mixed::augment()]
on the underlying model object so that ild_lme fits work in tidy workflows.
Package broom.mixed must be attached (e.g. library(broom.mixed)).
tidy.ild_lme(x, ...) augment.ild_lme(x, ...)tidy.ild_lme(x, ...) augment.ild_lme(x, ...)
x |
A fitted model from [ild_lme()]. |
... |
Passed to |
Same as the corresponding broom.mixed method.
A small simulated dataset with 10 persons and 14 observations per person, irregular timing, and two variables (mood, stress). For use in examples and vignettes. Use [ild_prepare()] to convert to an ILD object.
A data frame with 140 rows and 4 columns:
Person identifier (1–10).
POSIXct timestamp (irregular within person).
Simulated mood score.
Simulated stress score.
Simulated with a fixed seed (12345) for reproducibility.
Returns the canonical catalog of guardrail rules tidyILD may use. Each rule has a
stable rule_id, section (bundle slot), severity, and default
default_message / default_recommendation text. When you call
ild_diagnose, triggered rules appear as rows in
ild_diagnostics_bundle$guardrails with message and
recommendation possibly customized for the run.
guardrail_registry()guardrail_registry()
A tibble with columns rule_id, section, severity,
default_message, default_recommendation.
ild_diagnose, ild_diagnostics_bundle
Computes ACF on a variable in ILD data or on residuals from an [ild_lme()] fit. Use this to check whether AR1 is appropriate before fitting models. ACF is computed over the ordered observation sequence (pooled or within person); it does not adjust for irregular time gaps.
ild_acf(x, ..., by_id = FALSE)ild_acf(x, ..., by_id = FALSE)
x |
Either an ILD object (see [is_ild()]) or a fitted model from [ild_lme()]. |
... |
When |
by_id |
Logical. If |
A list with acf: a tibble with columns lag and acf (pooled). If by_id = TRUE, acf_by_id is a named list of tibbles (one per person).
d <- ild_simulate(n_id = 5, n_obs_per = 10, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_acf(x, "y") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) ild_acf(fit)d <- ild_simulate(n_id = 5, n_obs_per = 10, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_acf(x, "y") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) ild_acf(fit)
For each row in the primary ILD, finds observations in the secondary
data set (same id, time within window before the primary time)
and attaches an aggregated value (e.g. mean, median, or closest).
Use when combining self-report with wearables or other streams that
have different timestamps.
ild_align( primary, secondary, value_var, window, time_secondary = "time", fun = c("mean", "median", "closest") )ild_align( primary, secondary, value_var, window, time_secondary = "time", fun = c("mean", "median", "closest") )
primary |
An ILD object (see [is_ild()]); the stream to keep as rows. |
secondary |
A data frame with id and time columns and the value variable(s) to align. |
value_var |
Character. Name of the column in |
window |
Numeric or lubridate duration. Time window (same units as |
time_secondary |
Character. Name of the time column in |
fun |
Character. Aggregation for values in window: |
The primary data with a new column <value_var>_aligned (numeric; NA where no secondary obs in window).
prim <- ild_prepare( data.frame( id = rep(1:2, each = 3), time = as.POSIXct(rep(c(0, 3600, 7200), 2), origin = "1970-01-01"), y = rnorm(6) ), id = "id", time = "time" ) sec <- data.frame( id = rep(1:2, each = 4), time = as.POSIXct(rep(c(0, 1800, 3600, 5400), 2), origin = "1970-01-01"), heart_rate = 60 + rnorm(8, 0, 5) ) ild_align(prim, sec, "heart_rate", window = 3600, fun = "mean")prim <- ild_prepare( data.frame( id = rep(1:2, each = 3), time = as.POSIXct(rep(c(0, 3600, 7200), 2), origin = "1970-01-01"), y = rnorm(6) ), id = "id", time = "time" ) sec <- data.frame( id = rep(1:2, each = 4), time = as.POSIXct(rep(c(0, 1800, 3600, 5400), 2), origin = "1970-01-01"), heart_rate = 60 + rnorm(8, 0, 5) ) ild_align(prim, sec, "heart_rate", window = 3600, fun = "mean")
Wraps [tsibble::as_tsibble()] using the subject and time column names from
[ild_meta()] as key and index. When [ild_prepare()] recorded tsibble
provenance (ild_tsibble_meta() is non-NULL), the regular
argument is set from the stored is_regular flag so the reconstructed
interval often matches the original for unchanged data. Otherwise
defaults to regular = TRUE.
ild_as_tsibble(x, ...)ild_as_tsibble(x, ...)
x |
An object that passes [validate_ild()]. |
... |
Optional arguments passed to [tsibble::as_tsibble()] (e.g.
override |
A tbl_ts object (see tsibble).
vignette("tsibble-interoperability", package = "tidyILD")
d <- ild_simulate(n_id = 4, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") if (requireNamespace("tsibble", quietly = TRUE)) { ts <- ild_as_tsibble(x) class(ts) }d <- ild_simulate(n_id = 4, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") if (requireNamespace("tsibble", quietly = TRUE)) { ts <- ild_as_tsibble(x) class(ts) }
Dispatches to [augment_ild_model()] for lmerMod and lme objects from [ild_lme()],
or the brmsfit method for [ild_brms()]. All methods return tables conforming to
ild_augment_schema.
ild_augment(x, ...) ## S3 method for class 'lmerMod' ild_augment(x, ...) ## S3 method for class 'lme' ild_augment(x, ...) ## Default S3 method: ild_augment(x, ...) ## S3 method for class 'brmsfit' ild_augment(x, summary = TRUE, ...) ## S3 method for class 'ild_fit_ctsem' ild_augment(x, ...) ## S3 method for class 'ild_fit_kfas' ild_augment(x, ...)ild_augment(x, ...) ## S3 method for class 'lmerMod' ild_augment(x, ...) ## S3 method for class 'lme' ild_augment(x, ...) ## Default S3 method: ild_augment(x, ...) ## S3 method for class 'brmsfit' ild_augment(x, summary = TRUE, ...) ## S3 method for class 'ild_fit_ctsem' ild_augment(x, ...) ## S3 method for class 'ild_fit_kfas' ild_augment(x, ...)
x |
A fitted model from [ild_lme()]. |
... |
Passed to [augment_ild_model()]. |
summary |
Logical. For |
ild_augment() outputsDefines the minimum stable contract for augmented observation-level tables.
augment_ild_model and ild_augment methods emit all required
columns; see augment_ild_model for .resid_std semantics.
ild_augment_schema()ild_augment_schema()
**Required columns**
.ild_id |
Person identifier (from ILD) |
.ild_time |
Time variable |
.outcome |
Observed outcome (numeric vector aligned to rows) |
.fitted |
Fitted / posterior mean fitted value |
.resid |
Raw residual |
.resid_std |
Pearson-type residual when residuals(fit, type = "pearson") is available and length-matched; otherwise NA (not ad hoc z-scores) |
engine |
Character engine id |
model_class |
Model class string |
**Optional:** .fitted_lower, .fitted_upper, .influence,
.state, .state_lower, .state_upper.
A list with required and optional character vectors.
ild_tidy_schema, ild_diagnostics_bundle.
Reserved prefixes and migration notes: vignette("developer-contracts", package = "tidyILD").
For local_level only, use [ild_augment()] which already provides
.state for the level. This function is reserved for future multi-state
models.
ild_augment_states(x, ...)ild_augment_states(x, ...)
x |
An object from [ild_kfas()]. |
... |
Reserved. |
A tibble.
For ild_diagnostics_bundle from [ild_diagnose()], dispatches by section
and type (section-first; see ?ild_autoplot method for
ild_diagnostics_bundle). For [ild_diagnostics()], calls [plot_ild_diagnostics()]
directly. For lmerMod or lme fits from [ild_lme()], calls [ild_plot()]
(default type = "fitted_vs_actual").
ild_autoplot(x, ...) ## S3 method for class 'ild_diagnostics' ild_autoplot(x, ...) ## S3 method for class 'lmerMod' ild_autoplot(x, type = "fitted_vs_actual", ...) ## S3 method for class 'lme' ild_autoplot(x, type = "fitted_vs_actual", ...) ## Default S3 method: ild_autoplot(x, ...) ## S3 method for class 'brmsfit' ild_autoplot(x, type = "pp_check", ...) ## S3 method for class 'ild_fit_ctsem' ild_autoplot(x, type = c("fitted_vs_actual", "residual_time", "qq"), ...) ## S3 method for class 'ild_fit_kfas' ild_autoplot(x, type = c("states", "innovations", "signal"), ...) ## S3 method for class 'ild_diagnostics_bundle' ild_autoplot(x, section = "residual", type = NULL, ...)ild_autoplot(x, ...) ## S3 method for class 'ild_diagnostics' ild_autoplot(x, ...) ## S3 method for class 'lmerMod' ild_autoplot(x, type = "fitted_vs_actual", ...) ## S3 method for class 'lme' ild_autoplot(x, type = "fitted_vs_actual", ...) ## Default S3 method: ild_autoplot(x, ...) ## S3 method for class 'brmsfit' ild_autoplot(x, type = "pp_check", ...) ## S3 method for class 'ild_fit_ctsem' ild_autoplot(x, type = c("fitted_vs_actual", "residual_time", "qq"), ...) ## S3 method for class 'ild_fit_kfas' ild_autoplot(x, type = c("states", "innovations", "signal"), ...) ## S3 method for class 'ild_diagnostics_bundle' ild_autoplot(x, section = "residual", type = NULL, ...)
x |
A bundle, |
... |
Passed to [plot_ild_diagnostics()] ( |
type |
Character: |
section |
For |
Wraps [brms::brm()] with ILD validation, default priors for common longitudinal
mixed models, standardized posterior metadata in attr(fit, "ild_posterior"),
and analysis provenance (priors, chains, iterations). Attach ild_data for
use with [ild_tidy()], [ild_augment()], [ild_diagnose()], and [ild_methods()].
ild_brms( formula, data, prior = NULL, prior_template = c("default", "weakly_informative", "minimal_shrinkage"), warn_uncentered = TRUE, ... )ild_brms( formula, data, prior = NULL, prior_template = c("default", "weakly_informative", "minimal_shrinkage"), warn_uncentered = TRUE, ... )
formula |
A brms / lme4-style formula (e.g. |
data |
An ILD object from [ild_prepare()]. |
prior |
Optional |
prior_template |
Passed to [ild_prior_ild()] when |
warn_uncentered |
If |
... |
Arguments passed to [brms::brm()] (e.g. |
Requires packages brms and a Stan backend (rstan or cmdstanr).
A brmsfit object with attributes:
ild_dataThe ILD data frame.
ild_posteriorList: sampler settings, prior summary, divergences, etc.
ild_provenanceStandard tidyILD analysis provenance.
[ild_fit()] with backend = "brms" is equivalent to ild_brms().
For IPW/MSM workflows, full Bayesian joint MSM is not in tidyILD; see [ild_msm_inference].
Returns a suggested formula and notes for fitting with [ild_brms()].
This does **not** run the model. Identification, priors, and convergence
depend on N, T, and scaling; see vignette("brms-dynamics-recipes", package = "tidyILD").
ild_brms_dynamics_formula(outcome, lag_var, id_var = "id")ild_brms_dynamics_formula(outcome, lag_var, id_var = "id")
outcome |
Character name of the response. |
lag_var |
Character name of an existing lag column (e.g. from [ild_lag()]). |
id_var |
Character name of the grouping column in |
A list: formula (formula object), notes (character).
[ild_brms()], [ild_panel_lag_prepare()].
Applies [ild_lag()] repeatedly according to an [ild_msm_history_spec()] and
records a manifest under attr(x, "ild_msm_history_manifest").
ild_build_msm_history(data, spec, mode = NULL, max_gap = NULL)ild_build_msm_history(data, spec, mode = NULL, max_gap = NULL)
data |
An ILD object. |
spec |
Object from [ild_msm_history_spec()]. |
mode |
Optional override for |
max_gap |
Optional override for |
ILD object with lagged columns added; attributes include
ild_msm_history_manifest (tibble with variable, lag,
column).
d <- ild_simulate(n_id = 6, n_obs_per = 5, seed = 1) d$stress <- rnorm(nrow(d)) d$trt <- rbinom(nrow(d), 1, 0.4) d <- ild_prepare(d, id = "id", time = "time") hs <- ild_msm_history_spec(c("stress", "trt"), lags = 1:2) d2 <- ild_build_msm_history(d, hs) attr(d2, "ild_msm_history_manifest")d <- ild_simulate(n_id = 6, n_obs_per = 5, seed = 1) d$stress <- rnorm(nrow(d)) d$trt <- rbinom(nrow(d), 1, 0.4) d <- ild_prepare(d, id = "id", time = "time") hs <- ild_msm_history_spec(c("stress", "trt"), lags = 1:2) d2 <- ild_build_msm_history(d, hs) attr(d2, "ild_msm_history_manifest")
Combines a result (e.g. a fit from [ild_lme()] or output from
[ild_diagnostics()]) with a manifest and optional label for one-shot
saving. Typical use: saveRDS(ild_bundle(fit, label = "model_ar1"), "run.rds").
You can build a manifest with [ild_manifest()] and pass scenario
(e.g. from [ild_summary()]) and seed before bundling.
ild_bundle(result, manifest = NULL, label = NULL)ild_bundle(result, manifest = NULL, label = NULL)
result |
Any object (e.g. fitted model, diagnostics list). |
manifest |
List. Reproducibility manifest from [ild_manifest()].
If |
label |
Optional character. Short label for the run (e.g.
|
A list with elements result, manifest, label,
suitable for [saveRDS()].
dat <- ild_prepare(ild_simulate(seed = 1), "id", "time") fit <- ild_lme(y ~ 1 + (1 | id), dat, ar1 = FALSE, warn_no_ar1 = FALSE) b <- ild_bundle(fit, label = "ar1") names(b) b <- ild_bundle(fit, manifest = ild_manifest(seed = 1, scenario = list(n_obs = 50)), label = "run1")dat <- ild_prepare(ild_simulate(seed = 1), "id", "time") fit <- ild_lme(y ~ 1 + (1 | id), dat, ar1 = FALSE, warn_no_ar1 = FALSE) b <- ild_bundle(fit, label = "ar1") names(b) b <- ild_bundle(fit, manifest = ild_manifest(seed = 1, scenario = list(n_obs = 50)), label = "run1")
For each selected variable, computes the person mean (between-person component) and the within-person deviation (variable minus person mean). Use '*_wp' at level-1 and '*_bp' at level-2 or in cross-level interactions to avoid ecological fallacy and conflation bias. Selected variables must be numeric.
ild_center( x, ..., type = c("person_mean", "grand_mean"), naming = c("suffix", "prefix") )ild_center( x, ..., type = c("person_mean", "grand_mean"), naming = c("suffix", "prefix") )
x |
An ILD object (see [is_ild()]). |
... |
Variables to center (tidy-select). Unquoted names or a single character vector of column names. Must be numeric. |
type |
Character. '"person_mean"' (default) for person-mean centering (x_bp, x_wp); '"grand_mean"' for grand-mean centering (x_gm, x_wp_gm). |
naming |
Character. '"suffix"' (default): new columns |
The same ILD tibble with additional columns. ILD attributes are preserved.
Shows within-person deviation and between-person (person mean) distribution for selected variable(s). Uses the same plot as [ild_decomposition(..., plot = TRUE)]. Useful when you only want the visualization without the variance table.
ild_center_plot(x, ...)ild_center_plot(x, ...)
x |
An ILD object (see [is_ild()]). |
... |
Variables to plot (tidy-select). Must be numeric. Only the first is plotted. |
A ggplot object (WP vs BP density overlay for the first selected variable).
d <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_center_plot(x, y)d <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_center_plot(x, y)
Given an ILD object and lag variable names, reports how many lagged
values are valid vs invalid (NA because the time distance to the
lagged row exceeded a threshold). Useful to audit lag columns before
modeling without re-specifying max_gap.
ild_check_lags(x, lag_vars = NULL, max_gap = NULL)ild_check_lags(x, lag_vars = NULL, max_gap = NULL)
x |
An ILD object (see [is_ild()]) that contains lag columns
(e.g. from [ild_lag()] with |
lag_vars |
Character vector of lag column names (e.g. |
max_gap |
Numeric. Threshold used to define invalid (same units as
|
A data frame with one row per lag variable: var, lag (parsed lag order or "window"),
n_valid, n_invalid, n_first, n_total, pct_valid, pct_invalid.
Plots a variable by hour of day (or time-of-day) when .ild_time is
POSIXct. Useful for EMA (e.g. mood or activity by hour). Does not add
columns to the ILD object; hour is derived internally for plotting.
ild_circadian(x, var, type = c("boxplot", "line"))ild_circadian(x, var, type = c("boxplot", "line"))
x |
An ILD object (see [is_ild()]) with |
var |
Character or symbol. Variable to plot (e.g. mood, activity). |
type |
Character. |
A ggplot object (variable by hour of day).
d <- ild_simulate(n_id = 5, n_obs_per = 12, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_circadian(x, y)d <- ild_simulate(n_id = 5, n_obs_per = 12, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_circadian(x, y)
Builds a compact comparison table for a named list of fitted objects. Uses [stats::AIC()], [stats::BIC()], and [stats::nobs()] where those methods exist. This is **not** a likelihood-ratio test and does not establish that models are nested; interpret only when model comparison is scientifically appropriate for your estimand.
ild_compare_fits(fits, guardrail_bundles = NULL)ild_compare_fits(fits, guardrail_bundles = NULL)
fits |
A named (or unnamed) list of fitted model objects. |
guardrail_bundles |
Optional named list of [ild_diagnostics_bundle] objects
aligned with |
A tibble with columns model, engine (from [ild_methods()]
truncated to one line when long), aic, bic, n_obs,
converged (best-effort), n_guardrails (if bundles given),
and notes (fit-specific caveats).
vignette("temporal-dynamics-model-choice", package = "tidyILD").
Flattens provenance for each object (data steps plus analysis steps), then compares step sequences and key arguments. Useful to compare preprocessing pipelines, model settings, or to see what changed between two analyses.
ild_compare_pipelines(x1, x2)ild_compare_pipelines(x1, x2)
x1 |
First object (ILD data, model fit, or diagnostics with provenance). |
x2 |
Second object (same types as x1). |
A list of class ild_compare_pipelines with only_in_first
(step names only in x1), only_in_second, differing (list of
per-step arg differences), and summary (character vector of
human-readable differences). If either object has no provenance, returns
a list with message and empty comparison components.
set.seed(1) d <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) x1 <- ild_prepare(d, id = "id", time = "time") x1 <- ild_center(x1, y) x2 <- ild_prepare(d, id = "id", time = "time") x2 <- ild_center(x2, y) ild_compare_pipelines(x1, x2)set.seed(1) d <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) x1 <- ild_prepare(d, id = "id", time = "time") x1 <- ild_center(x1, y) x2 <- ild_prepare(d, id = "id", time = "time") x2 <- ild_center(x2, y) ild_compare_pipelines(x1, x2)
One-call pipeline: [ild_lag()] the predictor, [ild_check_lags()] to audit, then [ild_lme()] to fit outcome on the lagged predictor. Returns the fit, the lag-term coefficient (estimate, CI, p), and lag validity check.
ild_crosslag( data, outcome, predictor, lag = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = NULL, ar1 = FALSE, include_diagnostics = FALSE, ... )ild_crosslag( data, outcome, predictor, lag = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = NULL, ar1 = FALSE, include_diagnostics = FALSE, ... )
data |
An ILD object (see [is_ild()]). |
outcome |
Character or symbol. Name of the outcome variable (e.g. |
predictor |
Character or symbol. Name of the predictor to lag (e.g. |
lag |
Integer. Lag order (default 1). |
mode |
Character. Passed to [ild_lag()]: |
max_gap |
Numeric or NULL. Passed to [ild_lag()] when |
ar1 |
Logical. If |
include_diagnostics |
Logical. If |
... |
Passed to [ild_lme()]. |
A list: fit (fitted model), lag_term (one-row tibble from [tidy_ild_model()] for the lag variable),
lag_check (tibble from [ild_check_lags()]), data (ILD with lag column), and optionally diagnostics.
d <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") out <- ild_crosslag(x, "y", "y", lag = 1, ar1 = FALSE, warn_no_ar1 = FALSE) out$lag_term out$lag_checkd <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") out <- ild_crosslag(x, "y", "y", lag = 1, ar1 = FALSE, warn_no_ar1 = FALSE) out$lag_term out$lag_check
First-class tidyILD wrapper for ctsem. This backend is designed for irregularly spaced ILD where continuous-time dynamics are preferable to discrete-time approximations.
ild_ctsem( data, outcome, ct_model = NULL, model_type = c("stanct", "ctfit"), id_col = ".ild_id", time_col = ".ild_time_num", time_scale = 1, ... )ild_ctsem( data, outcome, ct_model = NULL, model_type = c("stanct", "ctfit"), id_col = ".ild_id", time_col = ".ild_time_num", time_scale = 1, ... )
data |
An object that passes [validate_ild()] (typically after [ild_prepare()]). |
outcome |
Character name of numeric outcome column. |
ct_model |
Optional ctsem model object. If |
model_type |
Character. |
id_col |
Person-id column in |
time_col |
Continuous-time index column in |
time_scale |
Optional positive scalar to rescale |
... |
Passed to |
v1 focuses on a conservative single-outcome pathway with explicit class, provenance, and diagnostics compatibility. Users may supply a custom ctsem model object; otherwise a simple 1-manifest local-level style template is used.
An object of class c("ild_fit_ctsem", "ild_fit_model", "ild_analysis").
Attributes ild_data and ild_provenance are attached.
[ild_tidy()], [ild_augment()], [ild_diagnose()], [ild_autoplot()].
Reports WP and BP variance and their ratio for selected variables. Use as a diagnostic and teaching tool: large ratio suggests within-person variance dominates; small ratio suggests between-person differences dominate. Helps avoid conflating WP and BP effects in modeling.
ild_decomposition(x, ..., plot = FALSE)ild_decomposition(x, ..., plot = FALSE)
x |
An ILD object (see [is_ild()]). |
... |
Variables to decompose (tidy-select). Must be numeric. |
plot |
Logical. If |
A tibble with columns variable, wp_var, bp_var, ratio (wp_var / bp_var; Inf if bp_var is 0). If plot = TRUE, a list with table and plot (ggplot).
d <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_decomposition(x, y)d <- ild_simulate(n_id = 10, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") ild_decomposition(x, y)
Aggregates [ild_summary()], [ild_spacing()], [ild_spacing_class()], and optionally [ild_decomposition()] and [ild_missing_pattern()] into one design summary. Use before modeling to see spacing class, correlation recommendation, within- vs between-person variance, and missingness.
ild_design_check(x, vars = NULL)ild_design_check(x, vars = NULL)
x |
An ILD object (see [is_ild()]). |
vars |
Optional character vector of variable names for decomposition
and missingness. If |
Also a **section provider** for [ild_diagnose()] (see [ild_diagnostics_utilities]).
A list of class ild_design_check: summary (from ild_summary),
spacing_class (regular-ish / irregular-ish), spacing (from ild_spacing),
recommendation (AR1/CAR1 text), wp_bp (decomposition tibble or NULL),
missingness (list with summary tibble and pct_na overall, or NULL).
Use print() for a human-readable summary.
[ild_diagnose()] embeds this object in design$ild_design_check on the
ild_diagnostics_bundle.
[ild_diagnose()], [ild_diagnostics_bundle()]
Other ild_diagnostics_utilities:
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
d <- ild_simulate(n_id = 10, n_obs_per = 8, irregular = TRUE, seed = 1) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_design_check(x, vars = "y")d <- ild_simulate(n_id = 10, n_obs_per = 8, irregular = TRUE, seed = 1) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_design_check(x, vars = "y")
Returns a single ild_diagnostics_bundle for lmerMod, lme,
brmsfit, ild_fit_kfas, and ild_fit_ctsem.
Sections are parallel across engines;
see ?ild_diagnostics_bundle. Residual ACF/Q-Q for frequentist models are stored in
residual$legacy_ild_diagnostics for [plot_ild_diagnostics()]. For raw residual-only
computation without the bundle, call [ild_diagnostics()] directly.
ild_diagnose(object, ...) ## S3 method for class 'ild_fit_kfas' ild_diagnose(object, data = NULL, type = NULL, by_id = NULL, ...) ## S3 method for class 'lmerMod' ild_diagnose( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'lme' ild_diagnose( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'brmsfit' ild_diagnose( object, data = NULL, type = c("all", "convergence", "sampler", "ppc"), by_id = TRUE, ppc_ndraws = 500L, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'ild_fit_ctsem' ild_diagnose( object, data = NULL, type = NULL, by_id = NULL, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## Default S3 method: ild_diagnose(object, ...)ild_diagnose(object, ...) ## S3 method for class 'ild_fit_kfas' ild_diagnose(object, data = NULL, type = NULL, by_id = NULL, ...) ## S3 method for class 'lmerMod' ild_diagnose( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'lme' ild_diagnose( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'brmsfit' ild_diagnose( object, data = NULL, type = c("all", "convergence", "sampler", "ppc"), by_id = TRUE, ppc_ndraws = 500L, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## S3 method for class 'ild_fit_ctsem' ild_diagnose( object, data = NULL, type = NULL, by_id = NULL, missing_model = FALSE, missing_model_predictors = NULL, causal_detail = FALSE, balance = FALSE, balance_treatment = NULL, balance_covariates = NULL, balance_weights_col = ".ipw_treat", balance_by_occasion = FALSE, ... ) ## Default S3 method: ild_diagnose(object, ...)
object |
A fitted model. |
... |
Reserved. |
data |
Optional ILD data (defaults to |
type |
Residual diagnostic types for |
by_id |
For |
missing_model |
If |
missing_model_predictors |
Character vector for |
causal_detail |
If |
balance |
If |
balance_treatment |
Character. Binary treatment column for balance (required if |
balance_covariates |
Character vector of covariate columns for SMDs (required if |
balance_weights_col |
Weights column for balance (default |
balance_by_occasion |
If |
ppc_ndraws |
For |
**Building blocks:** the same analyses are available as standalone functions ([ild_design_check()], [ild_missing_pattern()], etc.); see [ild_diagnostics_utilities].
ild_autoplot, ild_diagnostics_bundle,
guardrail_registry, ild_diagnostics_utilities,
ild_diagnostics
Computes residual ACF (by person and/or pooled), residual vs fitted, residual vs time, and optional Q-Q. Use 'type' to request only specific diagnostics. For 'ild_lme' models with 'ar1 = TRUE', the estimated AR/CAR parameter is reported when 'type' includes '"residual_acf"'.
ild_diagnostics( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, ... )ild_diagnostics( object, data = NULL, type = c("residual_acf", "residual_time", "qq"), by_id = TRUE, ... )
object |
A fitted model from [ild_lme()] (or an object with 'residuals()', and optional 'fitted()'; if not 'ild_lme', pass 'data' with '.ild_id' and '.ild_time_num' or '.ild_seq'). |
data |
Optional. ILD data (required if 'object' is not from [ild_lme()]). |
type |
Character vector. Which diagnostics to compute: '"residual_acf"', '"residual_time"' (residuals vs time and vs fitted), '"qq"'. Default is all three. |
by_id |
Logical. If 'TRUE', compute ACF within each person (default 'TRUE'). |
... |
Unused. |
The return value follows a stable schema: 'meta' (engine, ar1, id/time columns,
n_obs, n_id), 'data$residuals' (tibble with '.ild_id', '.ild_time', response, '.resid', '.fitted'),
and 'stats' (e.g. 'acf', 'ar1_param'). Plots are not stored in the object; use
[plot_ild_diagnostics()] to generate them from a diagnostics object. The column
.resid is always filled; .fitted is filled when it can be computed
without refitting, otherwise it is NA (same for both engines and all type values).
Residual ACF is computed over the ordered observation sequence within person; it does not adjust for irregular time gaps.
A list of class 'ild_diagnostics' with: 'meta' (engine, ar1, id_col, time_col,
n_obs, n_id, type, by_id), 'data' (list with 'residuals' = tibble of .ild_id, .ild_time,
response (name from formula), .resid, .fitted; data$residuals always exists, .resid is always filled,
.fitted is returned when it can be computed without refitting, otherwise NA),
'stats' (list with 'acf' = list(pooled = tibble, by_id = list) when requested,
'ar1_param' = numeric or NULL for lme). Use [plot_ild_diagnostics()] for plots.
x <- ild_prepare(ild_simulate(n_id = 3, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) diag <- ild_diagnostics(fit, type = c("residual_acf", "qq")) plot_ild_diagnostics(diag)x <- ild_prepare(ild_simulate(n_id = 3, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) diag <- ild_diagnostics(fit, type = c("residual_acf", "qq")) plot_ild_diagnostics(diag)
The bundle has identical top-level slot names for every estimation backend.
Content differs by engine; unavailable sections are NULL.
warnings and guardrails are always tibbles (possibly 0 rows).
Guardrails use the canonical schema documented in guardrail_registry:
rule_id, section, severity, triggered, message,
recommendation (only triggered rules are stored as rows).
ild_diagnostics_bundle( meta = NULL, data = NULL, design = NULL, fit = NULL, residual = NULL, predictive = NULL, missingness = NULL, causal = NULL, warnings = NULL, guardrails = NULL, summary_text = character() ) new_ild_diagnostics_bundle( meta = NULL, data = NULL, design = NULL, fit = NULL, residual = NULL, predictive = NULL, missingness = NULL, causal = NULL, warnings = NULL, guardrails = NULL, summary_text = character() ) is_ild_diagnostics_bundle(x)ild_diagnostics_bundle( meta = NULL, data = NULL, design = NULL, fit = NULL, residual = NULL, predictive = NULL, missingness = NULL, causal = NULL, warnings = NULL, guardrails = NULL, summary_text = character() ) new_ild_diagnostics_bundle( meta = NULL, data = NULL, design = NULL, fit = NULL, residual = NULL, predictive = NULL, missingness = NULL, causal = NULL, warnings = NULL, guardrails = NULL, summary_text = character() ) is_ild_diagnostics_bundle(x)
meta, data, design, fit, residual, predictive, missingness, causal
|
Optional sections
(list or structured object, or |
warnings, guardrails
|
Tibbles (default empty). |
summary_text |
Character vector (default empty). |
x |
Object to test. |
Several standalone functions **feed** these slots when you use ild_diagnose;
see ild_diagnostics_utilities (ild_design_check,
ild_missing_pattern, ild_missing_model, ild_ipw_weights).
Bundles returned by ild_diagnose also set attr(bundle, "ild_fit") and
attr(bundle, "ild_data") for ild_autoplot; re-run ild_diagnose if
these are missing (e.g. after loading a saved bundle without the fitted object).
The print method lists each slot and prints a short summary line:
number of warning rows, number of guardrail rows, highest guardrail severity
(info < warning), and up to five triggered rule_id values.
An object of class ild_diagnostics_bundle.
is_ild_diagnostics_bundle: logical.
metaList or NULL: run ids, engine, dimensions.
dataList or NULL: spacing, gaps, compliance, missingness, distributions.
designList or NULL: within/between variation, time coverage, occasions, imbalance.
fitList or NULL: convergence, singularity, optimizer / MCMC diagnostics.
residualList or NULL: ACF, Q-Q, fitted vs observed, heteroskedasticity.
predictiveList or NULL: CV, PPC, forecast error.
missingnessList or NULL: IPW / imputation summaries when applicable.
causalList or NULL: weights, positivity, causal diagnostics when applicable.
warningsTibble: structured warnings.
guardrailsTibble: methodological guardrails (see guardrail_registry).
summary_textCharacter: short narrative summary.
ild_diagnose, guardrail_registry,
ild_diagnostics_utilities,
ild_tidy_schema, ild_augment_schema.
Per-section field semantics: vignette("developer-contracts", package = "tidyILD").
These functions are **standalone utilities** you can run on prepared ILD data at any time
(typically before or alongside modeling). The same logic also **feeds**
ild_diagnostics_bundle sections when you call ild_diagnose on a
fitted model, so the package presents one coherent diagnostics story: a top-level façade
(ild_diagnose) plus reusable building blocks below.
ild_diagnose
ild_design_checkPopulates the bundle design slot (embedded as
design$ild_design_check) and contributes WP/BP and design-time missingness views.
ild_missing_patternDrives data (data$missing_pattern,
global columns) and missingness (variable-specific summaries when model predictors
are known).
ild_missing_modelNot called automatically by ild_diagnose.
Use when you fit a missingness model for IPW or interpretation; it is the usual
prerequisite for ild_ipw_weights.
ild_ipw_weightsAdds .ipw (and related steps in the IPW workflow).
When those columns are present on the ILD data attached to the fit, ild_diagnose
fills the bundle causal slot with weight summaries.
ild_iptw_weights, ild_iptw_msm_weights, ild_ipcw_weights, ild_joint_msm_weights
MSM-style
inverse-probability weights for treatment (pooled or sequential ) and monotone censoring, combined into .ipw.
Distinct from outcome missingness IPW above.
ild_msm_bootstrap, tidy_ild_msm_bootstrap
Person-level bootstrap for weighted lmer after ild_ipw_refit; see ild_msm_inference.
ild_msm_balance, ild_ipw_ess, ild_msm_overlap_plot
Weighted covariate balance (SMD), effective sample size, and propensity overlap plots for MSM / IPW workflows.
ild_msm_estimand, ild_msm_fit, ild_msm_diagnose, ild_msm_contrast_over_time, ild_msm_history_spec, ild_build_msm_history, ild_msm_recovery
Estimand-first MSM workflow, one-call diagnostics bridge, time-indexed contrasts, history construction, and simulation-based recovery checks.
ild_diagnose, ild_diagnostics_bundle, ild_diagnostics
Writes the full provenance structure (data or analysis) to a file for reproducibility supplements, preregistration appendices, or lab archiving. Requires the jsonlite package for JSON or yaml for YAML.
ild_export_provenance(x, path, format = c("auto", "json", "yaml"))ild_export_provenance(x, path, format = c("auto", "json", "yaml"))
x |
An ILD object or an analysis object with [ild_provenance()]. |
path |
Character. File path to write (e.g. |
format |
Character. |
The path invisibly, after writing the file.
Façade that selects the estimation backend explicitly. Equivalent to calling
[ild_lme()] for "lme4" / "nlme", or [ild_brms()] for "brms".
Named functions remain the primary APIs for full documentation and examples.
ild_fit( formula, data, backend = c("lme4", "nlme", "brms"), correlation_class = c("auto", "AR1", "CAR1"), random = ~1 | .ild_id, warn_no_ar1 = TRUE, warn_uncentered = TRUE, prior = NULL, prior_template = c("default", "weakly_informative", "minimal_shrinkage"), ... )ild_fit( formula, data, backend = c("lme4", "nlme", "brms"), correlation_class = c("auto", "AR1", "CAR1"), random = ~1 | .ild_id, warn_no_ar1 = TRUE, warn_uncentered = TRUE, prior = NULL, prior_template = c("default", "weakly_informative", "minimal_shrinkage"), ... )
formula |
Passed to [ild_lme()] or [ild_brms()]. |
data |
An ILD object (see [is_ild()]). |
backend |
Character. |
correlation_class, random, warn_no_ar1
|
Passed to [ild_lme()] when
|
warn_uncentered |
Passed to [ild_lme()] or [ild_brms()]. |
prior, prior_template
|
Passed to [ild_brms()] when |
... |
Passed to [ild_lme()] / [lme4::lmer()] / [nlme::lme()] or [ild_brms()]. |
**State-space / latent-dynamics models** are not mixed-model formulas:
use [ild_kfas()] or [ild_ctsem()] directly;
see vignette("kfas-choosing-backend", package = "tidyILD").
To pass backend to brms::brm() (e.g. "rstan" vs "cmdstanr"), use
[ild_brms()] directly: ild_fit() reserves backend for the tidyILD engine
("lme4", "nlme", "brms").
A fitted model: lmerMod / lme from [ild_lme()], or brmsfit
from [ild_brms()], each with the same attributes as those functions document.
[ild_kfas()] and [ild_ctsem()] for state-space / latent-dynamics
backends (not available via ild_fit()).
Person x time heatmap of a variable. See [ild_plot()].
ild_heatmap(x, var = NULL, facet_by = NULL, ...)ild_heatmap(x, var = NULL, facet_by = NULL, ...)
x |
ILD object or fitted model (for heatmap, data are taken from ild_data if model). |
var |
Variable to plot. If NULL, single data column is used. |
facet_by |
Optional column in ILD for [ggplot2::facet_wrap()]. |
... |
Passed to [ild_plot()] (e.g. id_var, time_var). |
A ggplot object.
Summarizes **conditional modes** (empirical Bayes / BLUP deviations) and
**person-specific coefficients** (fixef + ranef) for hierarchical models
fit with [ild_lme()] or [ild_brms()]. Outputs are labeled as **partial-pooling**
estimates; they are not the same as no-pooling separate regressions (see
[ild_person_model()]) or population-average fixed effects alone.
ild_heterogeneity(fit, ...) ## S3 method for class 'lmerMod' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), conf_level = 0.95, ... ) ## S3 method for class 'lme' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), conf_level = 0.95, ... ) ild_heterogeneity_stratified(formula, data, subgroup, min_n_id = 5L, ...) ## S3 method for class 'ild_heterogeneity' ild_tidy(x, ...) ## S3 method for class 'ild_heterogeneity' ild_autoplot(x, type = c("caterpillar", "histogram"), term = NULL, ...) ## S3 method for class 'brmsfit' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), n_draws = 500L, ... )ild_heterogeneity(fit, ...) ## S3 method for class 'lmerMod' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), conf_level = 0.95, ... ) ## S3 method for class 'lme' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), conf_level = 0.95, ... ) ild_heterogeneity_stratified(formula, data, subgroup, min_n_id = 5L, ...) ## S3 method for class 'ild_heterogeneity' ild_tidy(x, ...) ## S3 method for class 'ild_heterogeneity' ild_autoplot(x, type = c("caterpillar", "histogram"), term = NULL, ...) ## S3 method for class 'brmsfit' ild_heterogeneity( fit, term = NULL, group = NULL, threshold = NULL, scale = c("raw", "sd_x", "sd_y"), n_draws = 500L, ... )
fit |
A fitted |
... |
Passed to [ild_lme()]. |
term |
Which random-effect term to plot (required if multiple). |
group |
Name of the grouping factor in |
threshold |
Optional numeric; with |
scale |
If |
conf_level |
For |
formula, data
|
Passed to [ild_lme()] within each subgroup. |
subgroup |
Character name of a column in |
min_n_id |
Minimum number of distinct persons required in a subgroup. |
x |
An |
type |
|
n_draws |
For |
An object of class ild_heterogeneity: a list with meta, random_effects
(long tibble), varcorr (tibble), summary (per-term metrics), and fit.
A tibble with one row per successful subgroup fit, including key columns from
ild_heterogeneity()$summary flattened (subgroup_level, term, etc.).
This is a **descriptive** comparison across refits, not a formal test of variance heterogeneity.
ild_tidy(ild_heterogeneity): Tidy long table of person-level random effects (for ild_heterogeneity objects).
ild_autoplot(ild_heterogeneity): Caterpillar or histogram of person-specific effects.
[ild_tidy.ild_heterogeneity()], [ild_autoplot.ild_heterogeneity()], [ild_heterogeneity_stratified()], [ild_person_model()]
For ILD data, displays data provenance steps. For analysis objects (e.g. from [ild_lme()], [ild_diagnostics()]), displays source data provenance (if any) and analysis steps. Use [ild_provenance()] to get the raw structured object.
ild_history(x)ild_history(x)
x |
An ILD object (see [is_ild()]) or an analysis object with |
The provenance object (from [ild_provenance()]) invisibly, or a message if none.
Builds **discrete-time** IPCW weights for **monotone loss-to-follow-up**: each person contributes a sequence of observed visits; after the last visit the person is censored. This differs from [ild_missing_model()] + [ild_ipw_weights()], which model **sporadic item missingness** on an outcome.
ild_ipcw_weights(x, predictors, stabilize = TRUE, trim = c(0.01, 0.99), ...)ild_ipcw_weights(x, predictors, stabilize = TRUE, trim = c(0.01, 0.99), ...)
x |
An ILD object (see [is_ild()]). |
predictors |
Character vector of covariate names (time-varying or baseline
carried forward in |
stabilize |
Logical. If |
trim |
Numeric of length 2. Quantiles applied to **final** cumulative
|
... |
Passed to |
Internally, each person-occasion row is labeled with an indicator
drop_next: whether the person drops before the next scheduled occasion
(including the last observed row). A pooled logistic regression models
drop_next given covariates; stabilized or unstabilized inverse
probabilities are accumulated within person (product over follow-up) to yield
.ipw_censor.
**Assumptions:** Monotone censoring, positivity, correct censoring model.
x with added column .ipw_censor. Attribute
ild_ipcw_fit holds the fitted glm.
Adds .ipw_censor. Combine with [ild_iptw_weights()] or
[ild_iptw_msm_weights()] via [ild_joint_msm_weights()].
[ild_iptw_weights()], [ild_joint_msm_weights()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(3) d <- ild_simulate(n_id = 18, n_obs_per = 7, seed = 3) d$stress <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") x <- ild_ipcw_weights(x, predictors = "stress") summary(x$.ipw_censor)set.seed(3) d <- ild_simulate(n_id = 18, n_obs_per = 7, seed = 3) d$stress <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") x <- ild_ipcw_weights(x, predictors = "stress") summary(x$.ipw_censor)
For **time-varying binary treatment** , fits a separate treatment
propensity model at each occasion (pooling persons at that time):
where is supplied by the user
(e.g. lags from [ild_lag()], prior treatment, time-varying confounders). The
stabilized factor at each person-occasion is multiplied **within person**
over time to yield a **cumulative** MSM IPTW in .ipw_treat.
ild_iptw_msm_weights( x, treatment, history, time_var = ".ild_seq", stabilize = c("marginal", "prior_treatment", "none"), prior_treatment = NULL, trim = c(0.01, 0.99), ... )ild_iptw_msm_weights( x, treatment, history, time_var = ".ild_seq", stabilize = c("marginal", "prior_treatment", "none"), prior_treatment = NULL, trim = c(0.01, 0.99), ... )
x |
An ILD object (see [is_ild()]). |
treatment |
Character. Binary |
history |
Predictors |
time_var |
Character. Occasion index column (default |
stabilize |
Character: |
prior_treatment |
Character. Required when |
trim |
Numeric of length 2. Quantiles applied to the **final** cumulative
|
... |
Passed to |
This differs from [ild_iptw_weights()], which fits **one pooled** logistic
over all rows (appropriate for a single treatment decision or descriptive
pooling, not the standard sequential MSM estimand for ).
**Assumptions:** Sequential exchangeability given , positivity
at each , and correct models. Variance (e.g. bootstrap by person) is
not computed here.
x with .ipw_treat set to the cumulative sequential weight.
Attributes: ild_iptw_msm_fits (named list of denominator glm fits
by occasion), ild_iptw_msm_numerator_fits (same for numerator when
prior_treatment).
marginalNumerator at each occasion uses the sample marginal
among rows at that time (same pattern as pooled stabilized
IPTW).
prior_treatmentNumerator uses from
a logistic model A_t ~ prior_treatment fit at each occasion (pass
prior_treatment column name). Denominator remains .
noneUnstabilized for the observed .
Sets .ipw_treat for use with [ild_ipcw_weights()] and
[ild_joint_msm_weights()].
[ild_iptw_weights()], [ild_ipcw_weights()], [ild_joint_msm_weights()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(11) d <- ild_simulate(n_id = 20, n_obs_per = 6, seed = 11) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.4)) d <- ild_prepare(d, id = "id", time = "time") d <- ild_lag(d, stress, mode = "gap_aware", max_gap = Inf) d <- ild_lag(d, trt, mode = "gap_aware", max_gap = Inf) x <- ild_iptw_msm_weights(d, treatment = "trt", history = ~ stress_lag1 + trt_lag1) summary(x$.ipw_treat)set.seed(11) d <- ild_simulate(n_id = 20, n_obs_per = 6, seed = 11) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.4)) d <- ild_prepare(d, id = "id", time = "time") d <- ild_lag(d, stress, mode = "gap_aware", max_gap = Inf) d <- ild_lag(d, trt, mode = "gap_aware", max_gap = Inf) x <- ild_iptw_msm_weights(d, treatment = "trt", history = ~ stress_lag1 + trt_lag1) summary(x$.ipw_treat)
Fits a **single pooled** treatment propensity model (logistic regression)
over all person-occasions and computes stabilized or unstabilized IPTW weights
(one factor per row). Use this for a **time-invariant** treatment or descriptive
pooling. For **time-varying** with a sequential MSM estimand, use
[ild_iptw_msm_weights()] instead. Outcome missingness IPW is
[ild_missing_model()] + [ild_ipw_weights()].
ild_iptw_weights( x, treatment, predictors, stabilize = TRUE, trim = c(0.01, 0.99), ... )ild_iptw_weights( x, treatment, predictors, stabilize = TRUE, trim = c(0.01, 0.99), ... )
x |
An ILD object (see [is_ild()]). |
treatment |
Character. Name of a **binary** treatment column (numeric
|
predictors |
Character vector of covariate names in |
stabilize |
Logical. If |
trim |
Numeric of length 2. Quantiles for trimming (default |
... |
Passed to |
**Assumptions:** Positivity (), correct specification of
the treatment model. This is sensitivity / MSM-style tooling, not a substitute
for careful causal design.
x with added column .ipw_treat. Attributes include
ild_iptw_fit (the fitted glm object).
Adds .ipw_treat. Use [ild_joint_msm_weights()] to combine with
.ipw_censor into .ipw for [ild_ipw_refit()] and [ild_diagnose()].
[ild_iptw_msm_weights()], [ild_ipcw_weights()], [ild_joint_msm_weights()], [ild_ipw_refit()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(2) d <- ild_simulate(n_id = 20, n_obs_per = 6, seed = 2) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(d$stress > 0) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, treatment = "trt", predictors = "stress") summary(x$.ipw_treat)set.seed(2) d <- ild_simulate(n_id = 20, n_obs_per = 6, seed = 2) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(d$stress > 0) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, treatment = "trt", predictors = "stress") summary(x$.ipw_treat)
For nonnegative weights , .
ild_ipw_ess( data, weights_col = ".ipw", by_occasion = FALSE, time_var = ".ild_seq" )ild_ipw_ess( data, weights_col = ".ipw", by_occasion = FALSE, time_var = ".ild_seq" )
data |
An ILD object. |
weights_col |
Name of the nonnegative weight column (e.g. |
by_occasion |
If |
time_var |
Column for occasion index (default |
A single numeric (pooled) or a tibble with occasion and ess when
by_occasion = TRUE.
set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") x$.ipw <- runif(nrow(x), 0.5, 1.5) ild_ipw_ess(x, ".ipw") ild_ipw_ess(x, ".ipw", by_occasion = TRUE)set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") x$.ipw <- runif(nrow(x), 0.5, 1.5) ild_ipw_ess(x, ".ipw") ild_ipw_ess(x, ".ipw", by_occasion = TRUE)
Takes a fit from [ild_lme()] (or a formula and data) and refits the model using observation weights from [ild_ipw_weights()]. Only the lme4 (lmer) path is supported; nlme (ar1 = TRUE) is not supported for weighted refits. This is a sensitivity tool, not a full MNAR solution.
ild_ipw_refit(fit_or_formula, data, weights = ".ipw", ar1 = FALSE, ...)ild_ipw_refit(fit_or_formula, data, weights = ".ipw", ar1 = FALSE, ...)
fit_or_formula |
Either a fitted model from [ild_lme()] (lmerMod), or a
formula. If a formula, |
data |
An ILD object (see [is_ild()]) containing a weight column (default |
weights |
Character. Name of the weight column in |
ar1 |
Logical. Must be |
... |
Passed to [ild_lme()] or |
A fitted model (lmerMod) with attr(..., "ild_data") set so that
[tidy_ild_model()] and [ild_diagnostics()] work. For inference under estimated
weights, see [ild_msm_bootstrap()] and [ild_msm_inference].
set.seed(1) d <- ild_simulate(n_id = 12, n_obs_per = 10, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 25)] <- NA x <- ild_prepare(d, id = "id", time = "time") x <- ild_center(x, mood) mm <- ild_missing_model(x, "mood", "stress") xw <- ild_ipw_weights(x, mm, stabilize = TRUE) fit0 <- ild_lme(mood ~ mood_bp + mood_wp + stress + (1 | id), data = xw, ar1 = FALSE, warn_no_ar1 = FALSE) fitw <- ild_ipw_refit(fit0, data = xw) tidy_ild_model(fitw)set.seed(1) d <- ild_simulate(n_id = 12, n_obs_per = 10, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 25)] <- NA x <- ild_prepare(d, id = "id", time = "time") x <- ild_center(x, mood) mm <- ild_missing_model(x, "mood", "stress") xw <- ild_ipw_weights(x, mm, stabilize = TRUE) fit0 <- ild_lme(mood ~ mood_bp + mood_wp + stress + (1 | id), data = xw, ar1 = FALSE, warn_no_ar1 = FALSE) fitw <- ild_ipw_refit(fit0, data = xw) tidy_ild_model(fitw)
Uses the fitted missingness model from [ild_missing_model()] to compute
weights: unstabilized w = 1 / p_obs or stabilized
w = mean(p_obs) / p_obs. Weights are trimmed to avoid extremes.
Use with [ild_ipw_refit()] for sensitivity analysis. This is diagnostic
and sensitivity tooling, not a full MNAR solution.
ild_ipw_weights(x, miss_fit, stabilize = TRUE, trim = c(0.01, 0.99))ild_ipw_weights(x, miss_fit, stabilize = TRUE, trim = c(0.01, 0.99))
x |
An ILD object (see [is_ild()]); rows must align to the data used to fit |
miss_fit |
The return value of [ild_missing_model()] (list with |
stabilize |
Logical. If |
trim |
Numeric of length 2. Quantiles to trim weights (default |
Also a **section provider** for [ild_diagnose()] when .ipw is present on the
analysis data (see [ild_diagnostics_utilities]).
x with an added column .ipw (numeric). ILD attributes are preserved.
Adds .ipw to x. When that column is on the ILD data attached to the fit,
[ild_diagnose()] fills causal (weight summaries) on ild_diagnostics_bundle.
[ild_diagnose()], [ild_diagnostics_bundle()], [ild_missing_model()], [ild_iptw_weights()], [ild_ipcw_weights()], [ild_joint_msm_weights()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(1) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 20)] <- NA x <- ild_prepare(d, id = "id", time = "time") mm <- ild_missing_model(x, "mood", "stress") xw <- ild_ipw_weights(x, mm, stabilize = TRUE) summary(xw$.ipw)set.seed(1) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 20)] <- NA x <- ild_prepare(d, id = "id", time = "time") mm <- ild_missing_model(x, "mood", "stress") xw <- ild_ipw_weights(x, mm, stabilize = TRUE) summary(xw$.ipw)
Multiplies .ipw_treat and .ipw_censor into a single analysis
weight column (default .ipw) for use with [ild_ipw_refit()] and
[ild_diagnose()]. Optional scaling sets the mean joint weight to 1.
ild_joint_msm_weights( x, stabilize = c("mean1", "none"), trim = c(0.01, 0.99), joint_name = ".ipw" )ild_joint_msm_weights( x, stabilize = c("mean1", "none"), trim = c(0.01, 0.99), joint_name = ".ipw" )
x |
An ILD object containing |
stabilize |
Character. |
trim |
Numeric of length 2. Quantiles for trimming the **joint** vector
(default |
joint_name |
Character. Name of the combined weight column (default |
x with joint_name set; component columns unchanged.
Sets .ipw (or joint_name) so existing causal diagnostics and
guardrails apply to the **joint** weight.
[ild_iptw_weights()], [ild_iptw_msm_weights()], [ild_ipcw_weights()], [ild_ipw_refit()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(4) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 4) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(d$stress > 0) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, "trt", "stress") x <- ild_ipcw_weights(x, "stress") x <- ild_joint_msm_weights(x) summary(x$.ipw)set.seed(4) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 4) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(d$stress > 0) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, "trt", "stress") x <- ild_ipcw_weights(x, "stress") x <- ild_joint_msm_weights(x) summary(x$.ipw)
Opinionated entry point for **local level** models in v1. Requires the
KFAS package. One distinct .ild_id per fit; subset your data first.
ild_kfas( data, outcome, state_spec = "local_level", observation_family = "gaussian", time_units, irregular_time = FALSE, smoother = TRUE, forecast_horizon = 0L, fit_context = c("single_series", "independent_series_per_id"), ... )ild_kfas( data, outcome, state_spec = "local_level", observation_family = "gaussian", time_units, irregular_time = FALSE, smoother = TRUE, forecast_horizon = 0L, fit_context = c("single_series", "independent_series_per_id"), ... )
data |
An object that passes [validate_ild()] (e.g. after [ild_prepare()]). |
outcome |
Character name of the numeric outcome column. |
state_spec |
Character; use |
observation_family |
Currently only |
time_units |
Character describing the time column (e.g. |
irregular_time |
Logical; if |
smoother |
Logical; passed to |
forecast_horizon |
Integer; when |
fit_context |
|
... |
Passed to |
An object of class c("ild_fit_kfas", "ild_fit_model", "ild_analysis")
with list slots kfas_model, kfs, spec, state_labels,
mapping, schema_version, preprocessing. Attributes ild_data and
ild_provenance are set.
inst/dev/KFAS_V1_BACKEND.md, [ild_tidy()], [ild_augment()], [ild_diagnose()].
Conceptual vignettes:
Computes lagged values within each person. Use this instead of [dplyr::lag()], which assumes equal spacing and no gaps and is unsafe for irregular ILD.
ild_lag( x, ..., n = 1L, mode = c("index", "gap_aware", "time_window"), max_gap = NULL, window = NULL, resolution = c("closest_prior", "last_in_window", "mean_in_window") )ild_lag( x, ..., n = 1L, mode = c("index", "gap_aware", "time_window"), max_gap = NULL, window = NULL, resolution = c("closest_prior", "last_in_window", "mean_in_window") )
x |
An ILD object (see [is_ild()]). |
... |
Variables to lag (tidy-select). Unquoted names or selection. |
n |
Integer. Lag order (default 1 = previous observation). |
mode |
Character. |
max_gap |
Numeric. For |
window |
Numeric or lubridate duration. For |
resolution |
Character. For |
The same ILD tibble with new lag columns. ILD attributes preserved.
When 'ar1 = FALSE', fits with [lme4::lmer()] (no residual correlation). When 'ar1 = TRUE', fits with [nlme::lme()] using a residual correlation structure: CAR1 (continuous-time) by default for irregular spacing, or AR1 when spacing is regular-ish. Use [ild_spacing_class()] to inform the choice; override with 'correlation_class'.
ild_lme( formula, data, ar1 = FALSE, correlation_class = c("auto", "AR1", "CAR1"), random = ~1 | .ild_id, warn_no_ar1 = TRUE, warn_uncentered = TRUE, ... )ild_lme( formula, data, ar1 = FALSE, correlation_class = c("auto", "AR1", "CAR1"), random = ~1 | .ild_id, warn_no_ar1 = TRUE, warn_uncentered = TRUE, ... )
formula |
Fixed-effects formula. For 'ar1 = TRUE', must be fixed-only (e.g. 'y ~ x'); random structure is set to '~ 1 | .ild_id' internally. For 'ar1 = FALSE', formula may include random effects (e.g. 'y ~ x + (1|id)'). |
data |
An ILD object (see [is_ild()]). |
ar1 |
Logical. If 'TRUE', fit with nlme and residual AR1/CAR1 correlation; if 'FALSE', fit with lme4 (no residual correlation). |
correlation_class |
Character. '"auto"' (default) uses [ild_spacing_class()] to choose CAR1 (irregular-ish) or AR1 (regular-ish). Use '"CAR1"' or '"AR1"' to override. |
random |
For 'ar1 = TRUE', the random effects formula (default '~ 1 | .ild_id'). Must use '.ild_id' as grouping for correlation to match. |
warn_no_ar1 |
If 'TRUE' (default), warn when 'ar1 = FALSE' that temporal autocorrelation is not modeled. |
warn_uncentered |
If 'TRUE' (default), warn when a predictor in the
formula varies both within and between persons but is not decomposed
(no |
... |
Passed to [lme4::lmer()] or [nlme::lme()]. |
A fitted model object (class 'lmerMod' or 'lme') with attribute 'ild_data' (the ILD data) and 'ild_ar1' (logical). When 'ar1 = TRUE', the returned object has class 'ild_lme' prepended and attribute 'ild_random_resolved' (the formula actually passed to nlme, e.g. '~ 1 | M2ID'). See [ild_diagnostics()] and [ild_plot()].
# lme4 path: formula includes random effects set.seed(1) dat <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) dat <- ild_prepare(dat, id = "id", time = "time") dat <- ild_center(dat, y) fit_lmer <- ild_lme(y ~ y_bp + y_wp + (1 | id), data = dat, ar1 = FALSE, warn_no_ar1 = FALSE) # nlme path (may not converge on all platforms; see ?nlme::lme) ## Not run: fit_lme <- ild_lme(y ~ y_bp + y_wp, data = dat, random = ~ 1 | id, ar1 = TRUE) ## End(Not run)# lme4 path: formula includes random effects set.seed(1) dat <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) dat <- ild_prepare(dat, id = "id", time = "time") dat <- ild_center(dat, y) fit_lmer <- ild_lme(y ~ y_bp + y_wp + (1 | id), data = dat, ar1 = FALSE, warn_no_ar1 = FALSE) # nlme path (may not converge on all platforms; see ?nlme::lme) ## Not run: fit_lme <- ild_lme(y ~ y_bp + y_wp, data = dat, random = ~ 1 | id, ar1 = TRUE) ## End(Not run)
Captures timestamp, optional seed, optional scenario fingerprint, session info, and optional git SHA for use when saving or serializing results (e.g. after [ild_lme()] or [ild_diagnostics()]). The return value is a serializable list suitable for [saveRDS()] or [ild_bundle()].
ild_manifest( seed = NULL, scenario = NULL, include_session = TRUE, include_git = FALSE, git_path = "." )ild_manifest( seed = NULL, scenario = NULL, include_session = TRUE, include_git = FALSE, git_path = "." )
seed |
Optional integer. Seed used for the run (e.g. from [ild_simulate()] or set before fitting). Not captured automatically; pass explicitly if you want it in the manifest. |
scenario |
Optional. Named list or character string describing the run (e.g. formula, n_obs, n_id, ar1). Build from [ild_summary()] or a short list when calling after [ild_lme()] / [ild_diagnostics()]. |
include_session |
Logical. If 'TRUE' (default), include [utils::sessionInfo()] in the manifest. Set to 'FALSE' to reduce size. |
include_git |
Logical. If 'TRUE', attempt to record the current
git commit SHA from |
git_path |
Character. Path to the repository root (default
|
A list with elements timestamp (POSIXct), seed
(integer or NULL), scenario (as provided or NULL),
session_info (list from sessionInfo() or NULL),
git_sha (length-1 character or NA). All elements are
serializable.
m <- ild_manifest() names(m) m <- ild_manifest(seed = 42, scenario = list(n_obs = 100, formula = "y ~ x")) m$seed m$scenariom <- ild_manifest() names(m) m <- ild_manifest(seed = 42, scenario = list(n_obs = 100, formula = "y ~ x")) m$seed m$scenario
Returns the metadata attributes set by [ild_prepare()]: user-facing id/time column names, gap threshold, n_units, n_obs, and spacing (descriptive stats only).
ild_meta(x)ild_meta(x)
x |
An ILD object (see [is_ild()]). |
A named list of metadata (ild_id, ild_time, ild_gap_threshold,
ild_n_units, ild_n_obs, ild_spacing). ild_spacing includes overall
stats and may contain by_id, a tibble of per-person spacing stats.
Takes an ILD data object, a model fit, or a diagnostics object and produces a concise methods-style paragraph based on the recorded provenance (data preparation, centering, lagging, modeling, etc.).
ild_methods(x, robust_se = NULL, bundle = NULL, ...)ild_methods(x, robust_se = NULL, bundle = NULL, ...)
x |
An ILD object (see [is_ild()]), a model from [ild_lme()] or [ild_tvem()], a diagnostics object from [ild_diagnostics()], or another object with [ild_provenance()] (e.g. [ild_power()] result, [ild_missing_model()] result). |
robust_se |
Optional. If you reported fixed effects with cluster-robust SEs
via [tidy_ild_model()] with |
bundle |
Optional. An object from [ild_diagnose()] (class
|
... |
Unused. |
After fitting, call diag <- ild_diagnose(fit) and pass
bundle = diag to surface triggered guardrails in the methods narrative
without duplicating [ild_report()]'s structured diagnostics_summary.
A single character string (one or more sentences) suitable for a
methods section. Use cat() or print() to display.
set.seed(1) d <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) x <- ild_prepare(d, id = "id", time = "time") x <- ild_center(x, y) ild_methods(x)set.seed(1) d <- ild_simulate(n_id = 5, n_obs_per = 6, seed = 1) x <- ild_prepare(d, id = "id", time = "time") x <- ild_center(x, y) ild_methods(x)
Fits a logistic model of missingness (binary: is the outcome NA?) on a predictor variable. Use as a diagnostic: if the predictor is significant, missingness may be informative and results could be biased. This function does not correct for missingness; it flags the assumption for sensitivity analyses.
ild_missing_bias(x, outcome_var, predictor_var, random = FALSE)ild_missing_bias(x, outcome_var, predictor_var, random = FALSE)
x |
An ILD object (see [is_ild()]). |
outcome_var |
Character. Name of the variable with missingness (e.g. |
predictor_var |
Character. Name of the suspected predictor of missingness (e.g. |
random |
Logical. If |
A list with predictor (name), estimate, std_error,
p_value, and message (short note about informative missingness).
set.seed(1) d <- ild_simulate(n_id = 20, n_obs_per = 10, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 30)] <- NA # some missing x <- ild_prepare(d, id = "id", time = "time") ild_missing_bias(x, "mood", "stress")set.seed(1) d <- ild_simulate(n_id = 20, n_obs_per = 10, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 30)] <- NA # some missing x <- ild_prepare(d, id = "id", time = "time") ild_missing_bias(x, "mood", "stress")
For each distinct '.ild_seq' value, computes how many rows exist and what fraction has a non-missing outcome. Optional line plot.
ild_missing_cohort(x, outcome, plot = TRUE)ild_missing_cohort(x, outcome, plot = TRUE)
x |
An ILD object. |
outcome |
Character; outcome column. |
plot |
Logical; if 'TRUE', return a 'ggplot2' object in '$plot'. |
Uses **within-study sequence** ('.ild_seq'), not calendar time. For irregular timing, interpret as ordinal wave position rather than equal time spacing.
A list with 'by_occasion' (tibble: '.ild_seq', 'n_rows', 'n_obs', 'pct_observed') and if 'plot = TRUE', 'plot'.
[ild_missing_pattern()], [ild_missing_compliance()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
Per person (after sorting by '.ild_seq'), returns coverage on the outcome, longest streak of observed values, whether missingness is monotone (dropout pattern: once missing, stays missing), and optional comparison to an expected number of occasions.
ild_missing_compliance(x, outcome, expected_occasions = NULL)ild_missing_compliance(x, outcome, expected_occasions = NULL)
x |
An ILD object (see [is_ild()]). |
outcome |
Character; column with possible 'NA' (e.g. EMA outcome). |
expected_occasions |
Optional positive integer: planned waves per person for adherence ('pct_of_expected'). If persons have different row counts, this is only a rough benchmark; see Details. |
**Monotone missing** is defined only when the person has at least one missing outcome: all observations after the first missing are missing. Intermittent missing yields 'FALSE'. **Discrete hazard** for first missing is provided by [ild_missing_hazard_first()] (cohort-level by '.ild_seq').
A tibble with columns '.ild_id', 'n_rows', 'n_obs_outcome', 'pct_nonmissing_outcome', 'longest_run_observed', 'monotone_missing' ('NA' if no missing values on the outcome for that person), and if 'expected_occasions' is set, 'pct_of_expected' and 'meets_expected_rows'.
[ild_missing_pattern()], [ild_missingness_report()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
For each '.ild_seq', estimates the fraction of person-occasions **at risk** (previous occasion observed, or first occasion) that are **missing** on the outcome. This is a coarse discrete-time hazard for **first** missing spell when missingness is intermittent; under **monotone dropout** it matches the usual discrete hazard of dropout.
ild_missing_hazard_first(x, outcome)ild_missing_hazard_first(x, outcome)
x |
An ILD object. |
outcome |
Character; outcome column. |
**Assumptions:** Rows are ordered within person by '.ild_seq'. The first row per person is always at risk. Later rows are at risk only if the previous row (same person) was non-missing—so a person who is already missing is not counted again. This targets a **first event** narrative; it is not a full repeated-events model.
A tibble with '.ild_seq', 'n_at_risk', 'n_missing', 'hazard'.
[ild_missing_cohort()], [ild_missing_compliance()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_model(),
ild_missing_pattern(),
ild_missingness_report()
Fits a logistic model predicting whether the outcome is missing from covariates. Use as a diagnostic; then [ild_ipw_weights()] to compute inverse-probability weights and [ild_ipw_refit()] for a sensitivity analysis. This is not a full MNAR solution—treat as diagnostic and sensitivity tooling.
ild_missing_model( x, outcome, predictors, random = FALSE, family = stats::binomial(), ... )ild_missing_model( x, outcome, predictors, random = FALSE, family = stats::binomial(), ... )
x |
An ILD object (see [is_ild()]). |
outcome |
Character. Name of the variable with missingness (e.g. |
predictors |
Character vector. Names of covariates to predict missingness. |
random |
Logical. If |
family |
Passed to |
... |
Passed to |
**Not** run automatically by [ild_diagnose()]; it is the usual prerequisite for [ild_ipw_weights()] when you want IPW summaries in the bundle (see [ild_diagnostics_utilities]).
A list with fit (the fitted model or NULL if no/all missing),
tidy (tibble: term, estimate, std_error, p_value), outcome,
predictors, and message. If fit is not NULL,
p_missing is a numeric vector of predicted P(missing) per row (aligned to x).
There is no separate “missingness model” slot on the bundle. This function
supports the IPW workflow; after [ild_ipw_weights()], [ild_diagnose()] can summarize
weights in causal on ild_diagnostics_bundle.
[ild_diagnose()], [ild_diagnostics_bundle()], [ild_ipw_weights()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_pattern(),
ild_missingness_report()
set.seed(1) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 20)] <- NA x <- ild_prepare(d, id = "id", time = "time") mm <- ild_missing_model(x, "mood", "stress") mm$tidyset.seed(1) d <- ild_simulate(n_id = 15, n_obs_per = 8, seed = 1) d$stress <- rnorm(nrow(d)) d$mood <- d$y d$mood[sample(nrow(d), 20)] <- NA x <- ild_prepare(d, id = "id", time = "time") mm <- ild_missing_model(x, "mood", "stress") mm$tidy
Returns a tabular summary of missingness by person and/or by variable,
plus an optional heatmap plot. Complements [ild_summary()] and supports
checking data before modeling. When vars = NULL, all non-internal
data columns are used (observation presence across variables).
ild_missing_pattern( x, vars = NULL, max_ids = NULL, seed = NULL, outcome = NULL, expected_occasions = NULL )ild_missing_pattern( x, vars = NULL, max_ids = NULL, seed = NULL, outcome = NULL, expected_occasions = NULL )
x |
An ILD object (see [is_ild()]). |
vars |
Optional character vector of variable names to summarize.
If |
max_ids |
Optional integer. If set, subset to this many persons (sampled)
before computing |
seed |
Optional integer. Seed for sampling when |
outcome |
Optional character; if set, |
expected_occasions |
Passed to [ild_missing_compliance()] when |
Also a **section provider** for [ild_diagnose()] (see [ild_diagnostics_utilities]).
A list with: summary (tibble: one row per var, columns var, n_obs, n_na, pct_na),
plot (ggplot2 object for missingness heatmap), by_id, overall,
n_complete, vars. When outcome is set, by_id includes
compliance columns (see [ild_missing_compliance()]).
[ild_diagnose()] passes summaries into data$missing_pattern (global) and
missingness (model variables) on the ild_diagnostics_bundle.
[ild_diagnose()], [ild_diagnostics_bundle()]
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missingness_report()
Bundles person-level compliance, pattern summaries, cohort and hazard tables, optional [ild_missing_model()], heuristic flags, and short text snippets for methods sections. This is **diagnostic and reporting** tooling, not a substitute for formal sensitivity analysis or MNAR models.
ild_missingness_report( x, outcome, predictors = NULL, fit_missing_model = TRUE, random = FALSE, expected_occasions = NULL, max_ids = NULL, seed = NULL, cohort_plot = TRUE )ild_missingness_report( x, outcome, predictors = NULL, fit_missing_model = TRUE, random = FALSE, expected_occasions = NULL, max_ids = NULL, seed = NULL, cohort_plot = TRUE )
x |
An ILD object (see [is_ild()]). |
outcome |
Character; outcome column with possible 'NA'. |
predictors |
Optional character vector passed to [ild_missing_model()] when 'fit_missing_model' is 'TRUE'. If empty, no missingness model is fit. |
fit_missing_model |
Logical; fit [ild_missing_model()] when predictors are non-empty. |
random |
Logical; passed to [ild_missing_model()] ('glmer' vs 'glm'). |
expected_occasions |
Optional integer for [ild_missing_compliance()]. |
max_ids, seed
|
Passed to [ild_missing_pattern()] for large cohorts. |
cohort_plot |
Logical; include ggplot for [ild_missing_cohort()]. |
A list (class 'ild_missingness_report') with:
Tibble from [ild_missing_compliance()].
Output of [ild_missing_pattern()] (includes compliance columns on 'by_id' if 'outcome' was passed through).
List from [ild_missing_cohort()].
Tibble from [ild_missing_hazard_first()].
Named list: 'dropout_late_pooled' (logical from same heuristic as guardrail 'GR_DROPOUT_LATE_CONCENTRATION').
Result of [ild_missing_model()] or 'NULL'.
Character vector of short paragraphs for copy-paste.
[ild_missing_pattern()], [ild_missing_bias()], [ild_ipw_weights()], 'vignette("ild-missingness-workflow", package = "tidyILD")'
Other ild_diagnostics_utilities:
ild_design_check(),
ild_ipcw_weights(),
ild_iptw_msm_weights(),
ild_iptw_weights(),
ild_ipw_weights(),
ild_joint_msm_weights(),
ild_missing_cohort(),
ild_missing_compliance(),
ild_missing_hazard_first(),
ild_missing_model(),
ild_missing_pattern()
Computes **standardized mean differences** between weighted treated and control groups for
each named covariate. When by_occasion = TRUE, balance is computed **within** each
occasion (stratum); when FALSE, all rows are pooled into one pseudo-population.
ild_msm_balance( data, treatment, covariates, weights_col = ".ipw_treat", by_occasion = FALSE, time_var = ".ild_seq" )ild_msm_balance( data, treatment, covariates, weights_col = ".ipw_treat", by_occasion = FALSE, time_var = ".ild_seq" )
data |
An ILD object with |
treatment |
Character. Binary treatment column ( |
covariates |
Character vector of numeric or binary covariate columns. |
weights_col |
Analysis weights (e.g. |
by_occasion |
If |
time_var |
Occasion index column (default |
A tibble with columns stratum, covariate, smd,
mean_treated, mean_control, ess_stratum, weight_col.
set.seed(2) d <- ild_simulate(n_id = 12, n_obs_per = 6, seed = 2) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) x <- ild_prepare(d, id = "id", time = "time") x$.ipw_treat <- runif(nrow(x), 0.8, 1.2) ild_msm_balance(x, treatment = "trt", covariates = "stress", weights_col = ".ipw_treat")set.seed(2) d <- ild_simulate(n_id = 12, n_obs_per = 6, seed = 2) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) x <- ild_prepare(d, id = "id", time = "time") x$.ipw_treat <- runif(nrow(x), 0.8, 1.2) ild_msm_balance(x, treatment = "trt", covariates = "stress", weights_col = ".ipw_treat")
lmer (MSM / IPW sensitivity)Resamples **clusters** (persons) with replacement, subsets attr(fit, "ild_data")
(or data), optionally re-estimates weights, and refits lmer with
the same formula. Collects fixed effects across replicates for bootstrap standard errors and
percentile confidence intervals.
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, ... )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, ... )
fit |
A |
formula |
Optional. If |
data |
Optional ILD object when |
weights_col |
Name of the row-level weight column in each bootstrap dataset (default |
n_boot |
Number of bootstrap replicates. |
weight_policy |
|
weights_fn |
Function |
cluster |
|
cluster_vec |
When |
coef_fun |
|
seed |
Optional integer passed to |
verbose |
Logical. If |
... |
Passed to |
An object of class ild_msm_bootstrap: replicates (matrix n_boot x p),
estimate (point estimates from fit), bootstrap_se, conf_low, conf_high,
term_names, n_boot, n_success, n_cluster, metadata, and fit (original).
Use [tidy_ild_msm_bootstrap()] for an ild_tidy_schema tibble.
[ild_msm_inference], [ild_ipw_refit()], [tidy_ild_msm_bootstrap()]
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) }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) }
Computes marginal treatment contrasts by occasion using model fixed effects.
This helper is intentionally standalone: it accepts either an ild_msm_fit
object or a weighted lmerMod with attached ild_data.
ild_msm_contrast_over_time( object, treatment = NULL, time_var = ".ild_seq", target_time = "all", conf_level = 0.95 )ild_msm_contrast_over_time( object, treatment = NULL, time_var = ".ild_seq", target_time = "all", conf_level = 0.95 )
object |
Output from [ild_msm_fit()] or a |
treatment |
Optional treatment column name. Defaults to
|
time_var |
Occasion column (default |
target_time |
Optional target subset: |
conf_level |
Confidence level for Wald intervals (default |
Tibble with 'ild_tidy_schema' columns plus method,
terms, and target_time.
ild_msm_fit result in one callConvenience bridge to [ild_diagnose()] using object$fit and
object$weights_data.
ild_msm_diagnose(object, ...)ild_msm_diagnose(object, ...)
object |
Output from [ild_msm_fit()]. |
... |
Passed to [ild_diagnose()]. |
Creates a lightweight estimand object used by [ild_msm_fit()]. v1.1 preserves backward compatibility with v1 static ATE calls while adding explicit slots for regime specification, time targeting, and contrasts.
ild_msm_estimand( type = c("ate", "att"), regime = "static", treatment, time_var = ".ild_seq", contrast = NULL, target_time = "all", regime_value = 1, dynamic_rule = NULL )ild_msm_estimand( type = c("ate", "att"), regime = "static", treatment, time_var = ".ild_seq", contrast = NULL, target_time = "all", regime_value = 1, dynamic_rule = NULL )
type |
Estimand type. Supports |
regime |
Regime class. Supports |
treatment |
Binary treatment column name. |
time_var |
Occasion index column (default |
contrast |
Optional contrast definition. Character labels are accepted.
Lists may include |
target_time |
Optional target occasion:
|
regime_value |
For |
dynamic_rule |
For |
Object of class ild_msm_estimand.
e <- ild_msm_estimand(treatment = "trt") ee <- ild_msm_estimand(treatment = "trt") e
High-level runner that takes an [ild_msm_estimand()] plus ILD data, builds
treatment (and optional censoring) weights, refits a weighted lmer,
and optionally computes inference via robust SEs or cluster bootstrap.
ild_msm_fit( estimand, data, outcome_formula, history, history_spec = NULL, treatment_engine = c("sequential_msm", "pooled"), predictors_censor = NULL, weights_col = ".ipw", stabilize_treat = c("marginal", "prior_treatment", "none"), prior_treatment = NULL, stabilize_joint = c("mean1", "none"), trim = c(0.01, 0.99), inference = c("none", "robust", "bootstrap"), robust_type = c("CR2", "CR3", "CR0"), n_boot = 200L, weight_policy = c("fixed_weights", "reestimate_weights"), weights_fn = NULL, seed = NULL, strict_inference = FALSE, ... )ild_msm_fit( estimand, data, outcome_formula, history, history_spec = NULL, treatment_engine = c("sequential_msm", "pooled"), predictors_censor = NULL, weights_col = ".ipw", stabilize_treat = c("marginal", "prior_treatment", "none"), prior_treatment = NULL, stabilize_joint = c("mean1", "none"), trim = c(0.01, 0.99), inference = c("none", "robust", "bootstrap"), robust_type = c("CR2", "CR3", "CR0"), n_boot = 200L, weight_policy = c("fixed_weights", "reestimate_weights"), weights_fn = NULL, seed = NULL, strict_inference = FALSE, ... )
estimand |
Object from [ild_msm_estimand()]. |
data |
ILD data. |
outcome_formula |
Outcome model formula for [ild_lme()] (typically includes treatment term). |
history |
Predictors for treatment process weights:
one-sided formula (e.g. |
history_spec |
Optional [ild_msm_history_spec()] to build lagged history before weighting. |
treatment_engine |
|
predictors_censor |
Optional character vector for IPCW model. If supplied, joint weights
are built with [ild_joint_msm_weights()] and stored in |
weights_col |
Column used by [ild_ipw_refit()] (default |
stabilize_treat |
Treatment-weight stabilization mode. For sequential MSM:
|
prior_treatment |
Required when |
stabilize_joint |
Joint-weight stabilization for [ild_joint_msm_weights()]. |
trim |
Quantile trimming applied during weight construction. |
inference |
|
robust_type |
Passed to [ild_robust_se()] and [tidy_ild_model()] when |
n_boot |
Passed to [ild_msm_bootstrap()] when |
weight_policy |
Passed to [ild_msm_bootstrap()] when |
weights_fn |
Passed to [ild_msm_bootstrap()] when |
seed |
Optional seed for bootstrap inference. |
strict_inference |
Logical. If |
... |
Passed to [ild_lme()]. |
v1.1 keeps the v1 workflow and adds explicit capability signaling for inference/regime paths that are degraded or unsupported.
Object of class ild_msm_fit with elements:
estimand, history_spec, weights_data, fit, inference,
treatment_engine, weights_col. Analysis provenance is attached.
set.seed(31) d <- ild_simulate(n_id = 12, n_obs_per = 6, seed = 31) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) d <- ild_prepare(d, id = "id", time = "time") d <- ild_center(d, y) d <- ild_lag(d, stress, max_gap = Inf) d <- ild_lag(d, trt, max_gap = Inf) est <- ild_msm_estimand(treatment = "trt") res <- ild_msm_fit( estimand = est, data = d, outcome_formula = y ~ y_bp + y_wp + stress + trt + (1 | id), history = ~ stress_lag1 + trt_lag1, predictors_censor = "stress", inference = "none", warn_no_ar1 = FALSE, warn_uncentered = FALSE ) res$fitset.seed(31) d <- ild_simulate(n_id = 12, n_obs_per = 6, seed = 31) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) d <- ild_prepare(d, id = "id", time = "time") d <- ild_center(d, y) d <- ild_lag(d, stress, max_gap = Inf) d <- ild_lag(d, trt, max_gap = Inf) est <- ild_msm_estimand(treatment = "trt") res <- ild_msm_fit( estimand = est, data = d, outcome_formula = y ~ y_bp + y_wp + stress + trt + (1 | id), history = ~ stress_lag1 + trt_lag1, predictors_censor = "stress", inference = "none", warn_no_ar1 = FALSE, warn_uncentered = FALSE ) res$fit
Creates a lightweight spec object used by [ild_build_msm_history()] to generate
lagged confounder/treatment history columns with deterministic names (e.g.
stress_lag1, trt_lag2).
ild_msm_history_spec( vars, lags = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = Inf )ild_msm_history_spec( vars, lags = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = Inf )
vars |
Character vector of variable names to lag. |
lags |
Integer vector of lag orders. Recycled across |
mode |
Passed to [ild_lag()] (default |
max_gap |
Passed to [ild_lag()] for |
Object of class ild_msm_history_spec.
s <- ild_msm_history_spec(vars = c("stress", "trt"), lags = 1:2) ss <- ild_msm_history_spec(vars = c("stress", "trt"), lags = 1:2) s
For **pooled** [ild_iptw_weights()], uses attr(data, "ild_iptw_fit") and plots the
distribution of fitted by treatment level.
ild_msm_overlap_plot(data, treatment, source = c("auto", "pooled", "msm"))ild_msm_overlap_plot(data, treatment, source = c("auto", "pooled", "msm"))
data |
ILD with treatment column and relevant |
treatment |
Binary treatment column name. |
source |
Character: |
For **sequential** [ild_iptw_msm_weights()], uses attr(data, "ild_iptw_msm_fits") and
facets by occasion: fitted denominator propensity at each .
A ggplot object.
if (requireNamespace("ggplot2", quietly = TRUE)) { set.seed(3) d <- ild_simulate(n_id = 15, n_obs_per = 5, seed = 3) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, treatment = "trt", predictors = "stress") ild_msm_overlap_plot(x, treatment = "trt", source = "pooled") }if (requireNamespace("ggplot2", quietly = TRUE)) { set.seed(3) d <- ild_simulate(n_id = 15, n_obs_per = 5, seed = 3) d$stress <- rnorm(nrow(d)) d$trt <- as.integer(stats::rbinom(nrow(d), 1L, 0.45)) x <- ild_prepare(d, id = "id", time = "time") x <- ild_iptw_weights(x, treatment = "trt", predictors = "stress") ild_msm_overlap_plot(x, treatment = "trt", source = "pooled") }
Repeats an MSM analysis over simulated datasets with known true_ate and
summarizes estimation bias, RMSE, CI coverage, and positivity stress summaries.
ild_msm_recovery( n_sim = 100L, n_id = 80L, n_obs_per = 10L, true_ate = 0.5, n_boot = 200L, inference = c("bootstrap", "robust", "none"), seed = 1001L, censoring = TRUE, scenario_grid = NULL )ild_msm_recovery( n_sim = 100L, n_id = 80L, n_obs_per = 10L, true_ate = 0.5, n_boot = 200L, inference = c("bootstrap", "robust", "none"), seed = 1001L, censoring = TRUE, scenario_grid = NULL )
n_sim |
Number of simulations. |
n_id |
Number of persons per simulation. |
n_obs_per |
Planned observations per person. |
true_ate |
True additive treatment effect. |
n_boot |
Bootstrap replicates when |
inference |
|
seed |
Base seed. |
censoring |
If |
scenario_grid |
Optional data frame/list of scenario settings. Supported columns:
|
List with summary, summary_by_scenario, and sim_results.
## Not run: out <- ild_msm_recovery(n_sim = 10, n_id = 40, n_obs_per = 10, true_ate = 0.5, n_boot = 50) out$summary ## End(Not run)## Not run: out <- ild_msm_recovery(n_sim = 10, n_id = 40, n_obs_per = 10, true_ate = 0.5, n_boot = 50) out$summary ## End(Not run)
Generates ILD with time-varying treatment, confounding, and optional monotone
censoring/dropout under a known treatment effect (true_ate).
ild_msm_simulate_scenario( n_id = 60L, n_obs_per = 10L, true_ate = 0.5, censoring = TRUE, positivity_stress = 1, treatment_strength = 0.8, censoring_strength = 0.6, seed = 42L )ild_msm_simulate_scenario( n_id = 60L, n_obs_per = 10L, true_ate = 0.5, censoring = TRUE, positivity_stress = 1, treatment_strength = 0.8, censoring_strength = 0.6, seed = 42L )
n_id |
Number of persons. |
n_obs_per |
Planned observations per person. |
true_ate |
True additive treatment effect on outcome. |
censoring |
If |
positivity_stress |
Multiplier for treatment/censoring logits (>1 worsens overlap; <1 improves overlap). |
treatment_strength |
Base strength of treatment dependence on confounders/history. |
censoring_strength |
Base strength of dropout dependence on stress/treatment. |
seed |
Integer seed. |
ILD object with columns stress and binary trt; attributes include
true_ate.
d <- ild_msm_simulate_scenario(n_id = 20, n_obs_per = 8, true_ate = 0.5, seed = 12) ild_summary(d)$summaryd <- ild_msm_simulate_scenario(n_id = 20, n_obs_per = 8, true_ate = 0.5, seed = 12) ild_summary(d)$summary
Sequentially applies [ild_lag()] to each named variable with the same
mode, max_gap, window, and resolution, then runs
[ild_check_lags()] once on all created lag columns. Use this when building
multivariate lag specifications (e.g. several predictors or lag orders) with
consistent gap or time-window semantics.
ild_panel_lag_prepare( data, variables, n = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = NULL, window = NULL, resolution = c("closest_prior", "last_in_window", "mean_in_window") )ild_panel_lag_prepare( data, variables, n = 1L, mode = c("gap_aware", "index", "time_window"), max_gap = NULL, window = NULL, resolution = c("closest_prior", "last_in_window", "mean_in_window") )
data |
An ILD object (see [is_ild()]). |
variables |
Character vector of column names to lag. |
n |
Integer vector of lag orders, recycled to |
mode, max_gap, window, resolution
|
Passed to each [ild_lag()] call. |
A list with data (ILD with lag columns), lag_vars
(names of created columns), check (tibble from [ild_check_lags()]),
and spec (list of arguments for provenance).
vignette("temporal-dynamics-model-choice", package = "tidyILD"),
[ild_crosslag()], [ild_lag()].
Draws a histogram or density of the selected term's estimates across persons. Useful to visualize heterogeneity (e.g. distribution of slopes or intercepts).
ild_person_distribution( person_fit, term = NULL, type = c("histogram", "density") )ild_person_distribution( person_fit, term = NULL, type = c("histogram", "density") )
person_fit |
Tibble returned by [ild_person_model()] (columns |
term |
Character. Which term to plot (e.g. |
type |
Character. |
A ggplot object.
Splits the ILD by person and fits the same formula (e.g. lm) within each.
Returns a tibble of person-level estimates for teaching, N-of-1 analysis, or
inspecting heterogeneity. Use [ild_person_distribution()] to visualize the
distribution of estimates across persons.
ild_person_model(formula, data, method = c("lm"), min_obs = 2L)ild_person_model(formula, data, method = c("lm"), min_obs = 2L)
formula |
A formula (e.g. |
data |
An ILD object (see [is_ild()]). |
method |
Character. Currently only |
min_obs |
Integer. Minimum observations per person to fit (default 2). Persons with fewer are omitted or get NA rows. |
A tibble with columns .ild_id (or the id column name from metadata),
term, estimate, std_error, p_value, and optionally
sigma, n_obs. One row per person per term (long format).
d <- ild_simulate(n_id = 5, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") pm <- ild_person_model(y ~ 1, x) ild_person_distribution(pm, term = "(Intercept)")d <- ild_simulate(n_id = 5, n_obs_per = 8, seed = 1) x <- ild_prepare(d, id = "id", time = "time") pm <- ild_person_model(y ~ 1, x) ild_person_distribution(pm, term = "(Intercept)")
Produces trajectory (spaghetti), heatmap, gaps, and (if a fitted model is provided) fitted vs actual and residual ACF. Works for both lmerMod and lme (ild_lme with ar1 = TRUE).
ild_plot( x, type = c("trajectory", "heatmap", "gaps", "missingness", "fitted", "fitted_vs_actual", "predicted_trajectory", "residual_acf"), var = NULL, id_var = ".ild_id", time_var = c(".ild_time_num", ".ild_seq"), max_ids = 20L, seed = 42L, facet_by = NULL, ... )ild_plot( x, type = c("trajectory", "heatmap", "gaps", "missingness", "fitted", "fitted_vs_actual", "predicted_trajectory", "residual_acf"), var = NULL, id_var = ".ild_id", time_var = c(".ild_time_num", ".ild_seq"), max_ids = 20L, seed = 42L, facet_by = NULL, ... )
x |
An ILD tibble or a fitted [ild_lme()] model. |
type |
Character (or vector). One or more of: '"trajectory"', '"heatmap"', '"gaps"', '"missingness"', '"fitted"' or '"fitted_vs_actual"' (requires fitted model), '"predicted_trajectory"' (observed and fitted lines vs time; requires fitted model), '"residual_acf"' (requires fitted model; ACF is over observation sequence, not adjusted for irregular time gaps). If length > 1, returns a named list of ggplots. |
var |
For 'trajectory' or 'heatmap', the variable to plot (optional; if missing and only one non-.ild_* column exists, it is used). |
id_var |
For trajectory, variable used for grouping (default '.ild_id'). |
time_var |
For trajectory/gaps/'predicted_trajectory', x-axis: '.ild_time_num' or '.ild_seq'. |
max_ids |
For trajectory and 'predicted_trajectory', max number of persons to plot (sampled if larger; default 20). Set to 'Inf' to plot all. |
seed |
Integer. Seed for sampling ids when 'max_ids' is set (default 42). |
facet_by |
Optional character: name of a column in the ILD (e.g. cluster or site) to pass to [ggplot2::facet_wrap()] for 'trajectory', 'heatmap', 'gaps', and 'predicted_trajectory'. |
... |
Unused. |
A single ggplot when 'length(type) == 1', or a named list of ggplots when 'length(type) > 1'.
x <- ild_prepare(ild_simulate(n_id = 3, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) ild_plot(fit, type = "fitted_vs_actual") ild_plot(fit, type = c("fitted_vs_actual", "residual_acf"))x <- ild_prepare(ild_simulate(n_id = 3, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) ild_plot(fit, type = "fitted_vs_actual") ild_plot(fit, type = c("fitted_vs_actual", "residual_acf"))
Compares one-step filtered state (att) with smoothed (alphahat) when available.
ild_plot_filtered_vs_smoothed(x)ild_plot_filtered_vs_smoothed(x)
x |
An object from [ild_kfas()]. |
A ggplot object.
When forecast_horizon > 0 was used in [ild_kfas()], attempts a short
ahead forecast via KFAS; otherwise returns an informative empty panel.
ild_plot_forecast(x, ...)ild_plot_forecast(x, ...)
x |
An object from [ild_kfas()]. |
... |
Passed to |
A ggplot object.
Uses [augment_ild_model()] or [ild_augment()] on the fitted object and plots two lines per person (observed vs fitted) against 'time_var'. For a scatter of observed vs fitted, use [ild_plot()] with 'type = "fitted"'.
ild_plot_predicted_trajectory( fit, time_var = c(".ild_time_num", ".ild_seq"), max_ids = 20L, seed = 42L, facet_by = NULL )ild_plot_predicted_trajectory( fit, time_var = c(".ild_time_num", ".ild_seq"), max_ids = 20L, seed = 42L, facet_by = NULL )
fit |
Model from [ild_lme()] or [ild_brms()] (must carry 'ild_data'). |
time_var |
'.ild_time_num' or '.ild_seq' (default first of these in [match.arg()]). |
max_ids, seed
|
Passed through for subsampling persons. |
facet_by |
Optional column name in 'ild_data' for [ggplot2::facet_wrap()]. |
A 'ggplot' object.
Plot smoothed latent states (first state by default)
ild_plot_states(x, state_index = 1L, ...)ild_plot_states(x, state_index = 1L, ...)
x |
An object from [ild_kfas()]. |
state_index |
Integer; which column of |
... |
Passed to [ggplot2::labs()]. |
A ggplot object.
Estimates empirical power by repeatedly simulating data with a known effect
(via [ild_simulate()] plus one added predictor), fitting with [ild_lme()],
and counting the proportion of runs where the target term is significant
(Wald p < alpha). The workflow (simulate, fit, reject/retain) mirrors
simulation-based power in packages like mixpower; ild_power() is
focused on ILD and ild_lme(). For multi-parameter grids, LRT, or
general LMMs, consider mixpower.
ild_power( formula, n_sim = 500L, n_id, n_obs_per, effect_size, test_term = NULL, alpha = 0.05, ar1 = FALSE, seed = 42L, return_sims = FALSE, verbose = TRUE, ... )ild_power( formula, n_sim = 500L, n_id, n_obs_per, effect_size, test_term = NULL, alpha = 0.05, ar1 = FALSE, seed = 42L, return_sims = FALSE, verbose = TRUE, ... )
formula |
Fixed-effects formula including the predictor to power for
and random effects, e.g. |
n_sim |
Integer. Number of simulation replications (default 500). |
n_id |
Integer. Number of persons per replication. |
n_obs_per |
Integer. Observations per person per replication. |
effect_size |
Numeric. True coefficient for |
test_term |
Character or |
alpha |
Numeric. Significance level for rejection (default 0.05). |
ar1 |
Logical. If |
seed |
Integer. Base random seed; replication |
return_sims |
Logical. If |
verbose |
Logical. If |
... |
Passed to [ild_simulate()] (e.g. |
The data-generating process adds one predictor (name from test_term)
as standard normal and adds effect_size * predictor to the outcome
on top of the base [ild_simulate()] DGP (id, time, y). No change to
ild_simulate() is required.
For ar1 = FALSE (lmer), the lme4 backend does not report p-values;
inference for the test term uses a Wald z-approximation (estimate / SE)
so that power is still computed. For ar1 = TRUE (nlme), p-values
come from the model summary.
A list: power (proportion of converged runs with p < alpha),
n_sim, n_reject, n_converged, n_failed,
alpha, test_term. If return_sims = TRUE, also
sim_results (tibble of per-run results).
[ild_recovery_metrics()] to summarize bias, RMSE, and coverage from
return_sims. vignette("benchmark-simulation-recovery", package = "tidyILD")
(worked example).
set.seed(42) res <- ild_power( formula = y ~ x + (1 | id), n_sim = 25L, n_id = 15L, n_obs_per = 10L, effect_size = 0.3, seed = 42L, verbose = FALSE ) res$power res$n_rejectset.seed(42) res <- ild_power( formula = y ~ x + (1 | id), n_sim = 25L, n_id = 15L, n_obs_per = 10L, effect_size = 0.3, seed = 42L, verbose = FALSE ) res$power res$n_reject
Validates and encodes longitudinal structure: parses time, sorts by id and time, handles duplicate timestamps, and adds internal columns ('.ild_*') and metadata. All downstream functions assume the result of 'ild_prepare()'.
ild_prepare( data, id = NULL, time = NULL, gap_threshold = Inf, duplicate_handling = c("first", "last", "error", "collapse"), collapse_fn = NULL )ild_prepare( data, id = NULL, time = NULL, gap_threshold = Inf, duplicate_handling = c("first", "last", "error", "collapse"), collapse_fn = NULL )
data |
A data frame or tibble with at least an id and a time column.
If Tsibble interoperability: Accepting a |
id |
Character. Name of the subject/unit identifier column. Omit both
|
time |
Character. Name of the time column (Date, POSIXct, or numeric). |
gap_threshold |
Numeric. Time distance above which an interval is flagged as a gap ('.ild_gap' TRUE). Same units as the numeric time (e.g. seconds if time is POSIXct). Use 'Inf' to disable gap flagging. |
duplicate_handling |
Character. How to handle duplicate timestamps
within the same id: '"first"' (keep first), '"last"' (keep last),
'"error"' (stop with an error), '"collapse"' (aggregate with |
collapse_fn |
Named list of functions, one per variable to collapse.
Used only when |
An ILD tibble with '.ild_*' columns and metadata attributes.
Spacing metadata (see [ild_meta()]) includes overall stats and a
by_id tibble of per-person spacing stats (median_dt, iqr_dt,
n_intervals, pct_gap). Use [ild_summary()] to inspect and check gap
flags before modeling.
vignette("tsibble-interoperability", package = "tidyILD")
(tsibble ingestion, provenance, ild_as_tsibble()).
Returns a brmsprior object suitable for the prior argument of
[ild_brms()] / [brms::brm()]. Templates follow weakly informative defaults:
Student-t on intercepts and group SDs, normal on regression coefficients, and
exponential on residual SD where applicable.
ild_prior_ild( template = c("default", "weakly_informative", "minimal_shrinkage") )ild_prior_ild( template = c("default", "weakly_informative", "minimal_shrinkage") )
template |
Character selecting the template:
|
A brmsprior data frame (class brmsprior).
[ild_brms()]
For ILD data: returns attr(x, "tidyILD")$provenance (version + steps from
preprocessing). For analysis objects (e.g. fits from [ild_lme()], [ild_diagnostics()],
[ild_tvem()], [ild_power()], [ild_missing_model()]): returns attr(x, "ild_provenance"),
which has source_data_provenance (snapshot of data provenance) and
analysis_steps (list of analysis step records).
ild_provenance(x)ild_provenance(x)
x |
An ILD object (see [is_ild()]) or an analysis object with |
For data: list with version and steps. For analysis: list with
version, source_data_provenance, analysis_steps. NULL if none.
Summarizes **bias**, **RMSE**, and **nominal Wald interval coverage** for a known
coefficient using the per-replication table from [ild_power()] when
return_sims = TRUE. Purely descriptive; does not run new simulations.
ild_recovery_metrics(sim_results, truth, level = 0.95, converged_only = TRUE)ild_recovery_metrics(sim_results, truth, level = 0.95, converged_only = TRUE)
sim_results |
A data frame or tibble with columns |
truth |
Known value of the estimand (e.g. |
level |
Nominal coverage level for Wald intervals (default |
converged_only |
If |
A one-row tibble with truth, n (rows used),
n_total (rows in sim_results if converged_only), mean_estimate,
bias, rmse, coverage, level.
[ild_power()]
Builds a structured list with methods narrative (from [ild_methods()]), fixed-effects table (from [tidy_ild_model()]), a short diagnostics summary, and the raw provenance. Optionally exports provenance to a file.
ild_report(fit, export_provenance_path = NULL, robust_se = NULL, ...)ild_report(fit, export_provenance_path = NULL, robust_se = NULL, ...)
fit |
A fitted model from [ild_lme()] (or a list with a |
export_provenance_path |
Optional. If provided, [ild_export_provenance()] is called to write provenance to this path; the path is included in the returned list. |
robust_se |
Optional. Passed to [ild_methods()] when building the methods text (e.g. |
... |
Unused. |
A list with a stable schema: meta (list with n_obs, n_id, engine when available), methods, model_table, diagnostics_summary (includes guardrails_narrative and guardrails when [ild_diagnose()] succeeds), provenance, provenance_export_path (character or NULL). When guardrails are triggered, methods_with_guardrails repeats the methods paragraph with guardrails appended (same pattern as ild_methods(fit, bundle = d)).
set.seed(1) x <- ild_prepare(ild_simulate(n_id = 5, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) r <- ild_report(fit) r$methods r$model_tableset.seed(1) x <- ild_prepare(ild_simulate(n_id = 5, n_obs_per = 6, seed = 1), id = "id", time = "time") fit <- ild_lme(y ~ 1 + (1 | id), data = x, ar1 = FALSE, warn_no_ar1 = FALSE) r <- ild_report(fit) r$methods r$model_table
Computes cluster-robust (sandwich) variance estimators with small-sample
corrections via the clubSandwich package. Use with [tidy_ild_model()]
via se = "robust" for fixed-effect inference. Requires
attr(fit, "ild_data"); refit with [ild_lme()] if missing.
ild_robust_se( fit, type = c("CR2", "CR3", "CR0"), cluster = c("id", "data"), cluster_vec = NULL, ... )ild_robust_se( fit, type = c("CR2", "CR3", "CR0"), cluster = c("id", "data"), cluster_vec = NULL, ... )
fit |
A fitted model from [ild_lme()] (lmerMod or lme). |
type |
Character. Correction type: |
cluster |
Either |
cluster_vec |
When |
... |
Passed to |
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 weight_policy; see [ild_msm_inference].
A list with vcov (matrix), type, cluster_name,
engine ("lmer" or "lme"), and optionally message
if a fallback was used (e.g. lme not fully supported on this build).
[tidy_ild_model()] with se = "robust", clubSandwich::vcovCR,
[ild_msm_bootstrap()], [ild_msm_inference].
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) }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) }
Generates a tibble with id, time, and outcome y. Optionally uses
AR(1) within-person correlation and configurable WP/BP variance. Use
[ild_prepare()] after to get a proper ILD object.
ild_simulate( n_id = 5L, n_obs_per = 10L, n_time = NULL, irregular = FALSE, ar1 = NULL, wp_effect = 0.5, bp_effect = 1, seed = 42L )ild_simulate( n_id = 5L, n_obs_per = 10L, n_time = NULL, irregular = FALSE, ar1 = NULL, wp_effect = 0.5, bp_effect = 1, seed = 42L )
n_id |
Integer. Number of persons (default 5). |
n_obs_per |
Integer. Observations per person (default 10). |
n_time |
Integer. Alias for |
irregular |
Logical. If |
ar1 |
Numeric or |
wp_effect |
Numeric. Scale (SD) of within-person innovation (default 0.5). |
bp_effect |
Numeric. Scale (SD) of between-person random intercept (default 1). |
seed |
Integer. Random seed for reproducibility (default 42). |
A data frame with columns id, time (POSIXct), and y.
vignette("benchmark-simulation-recovery", package = "tidyILD")
(simulation benchmarks and DGP description).
d <- ild_simulate(n_id = 3, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") d2 <- ild_simulate(n_id = 100, n_time = 50, ar1 = 0.4, wp_effect = 0.6, bp_effect = 0.3, irregular = TRUE, seed = 1)d <- ild_simulate(n_id = 3, n_obs_per = 5, seed = 1) x <- ild_prepare(d, id = "id", time = "time") d2 <- ild_simulate(n_id = 100, n_time = 50, ar1 = 0.4, wp_effect = 0.6, bp_effect = 0.3, irregular = TRUE, seed = 1)
Reports observation intervals in human-friendly units (e.g. hours) and
recommends AR1 vs CAR1 for use in [ild_lme()]. Surfaces the same logic
that ild_lme(..., ar1 = TRUE) uses internally so users can see
why a correlation structure was chosen.
ild_spacing(x, gap_large_hours = 12)ild_spacing(x, gap_large_hours = 12)
x |
An ILD object (see [is_ild()]). |
gap_large_hours |
Numeric. Intervals (in hours) above which to count
as "large gaps" for |
A list with median_interval (hours), iqr (hours),
large_gaps_pct (percent of intervals > gap_large_hours),
coefficient_of_variation, recommendation (character: use CAR1 or AR1),
and spacing_class (regular-ish or irregular-ish).
d <- ild_simulate(n_id = 5, n_obs_per = 10, irregular = TRUE, seed = 1) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_spacing(x)d <- ild_simulate(n_id = 5, n_obs_per = 10, irregular = TRUE, seed = 1) x <- ild_prepare(d, id = "id", time = "time", gap_threshold = 7200) ild_spacing(x)
Returns a simple classification for use in documentation or when choosing correlation structure (e.g. AR1 vs CAR1 in [ild_lme()]). The rule is documented and overridable via arguments. Does not change core ILD behavior.
ild_spacing_class(x, cv_threshold = 0.2, pct_gap_threshold = 10)ild_spacing_class(x, cv_threshold = 0.2, pct_gap_threshold = 10)
x |
An ILD object (see [is_ild()]). |
cv_threshold |
Numeric. Coefficient of variation of within-person intervals above which spacing is "irregular-ish" (default 0.2). |
pct_gap_threshold |
Numeric. Percent of intervals flagged as gaps above which spacing is "irregular-ish" (default 10). |
Character: '"regular-ish"' or '"irregular-ish"'.
vignette("kfas-irregular-timing-spacing", package = "tidyILD")
(KFAS and irregular timing). vignette("ild-decomposition-and-spacing", package = "tidyILD")
(within-between and spacing).
Line plot of variable over time, one line per person. See [ild_plot()].
ild_spaghetti(x, var = NULL, facet_by = NULL, ...)ild_spaghetti(x, var = NULL, facet_by = NULL, ...)
x |
ILD object or fitted model. |
var |
Variable to plot. If NULL, single data column is used. |
facet_by |
Optional column in ILD for [ggplot2::facet_wrap()] (e.g. cluster). |
... |
Passed to [ild_plot()] (e.g. max_ids, seed, id_var, time_var). |
A ggplot object.
Reports number of persons, number of observations, time range, descriptive spacing (median/IQR of intervals, percent gaps), and duplicate info. Uses [ild_meta()] and '.ild_*' columns only. No hard "regular"/"irregular" label; use [ild_spacing_class()] for that.
ild_summary(x)ild_summary(x)
x |
An ILD object (see [is_ild()]). |
A list with elements: summary (one-row tibble with n_id, n_obs,
time_min, time_max, prop_gap, median_dt_sec, iqr_dt_sec), n_units,
n_obs, time_range, spacing, n_gaps, pct_gap.
If ild_prepare() was run on a tbl_ts, tsibble is also
present (same as [ild_tsibble_meta()]). The summary tibble is the
primary contract for programmatic use.
Dispatches to [tidy_ild_model()] for lmerMod and lme objects from [ild_lme()],
or the brmsfit method for [ild_brms()]. All methods return tables conforming to
ild_tidy_schema.
ild_tidy(x, ...) ## S3 method for class 'lmerMod' ild_tidy(x, ...) ## S3 method for class 'lme' ild_tidy(x, ...) ## Default S3 method: ild_tidy(x, ...) ## S3 method for class 'brmsfit' ild_tidy(x, intervals = TRUE, object = FALSE, ...) ## S3 method for class 'ild_fit_ctsem' ild_tidy(x, ...) ## S3 method for class 'ild_fit_kfas' ild_tidy(x, ...)ild_tidy(x, ...) ## S3 method for class 'lmerMod' ild_tidy(x, ...) ## S3 method for class 'lme' ild_tidy(x, ...) ## Default S3 method: ild_tidy(x, ...) ## S3 method for class 'brmsfit' ild_tidy(x, intervals = TRUE, object = FALSE, ...) ## S3 method for class 'ild_fit_ctsem' ild_tidy(x, ...) ## S3 method for class 'ild_fit_kfas' ild_tidy(x, ...)
x |
A fitted model from [ild_lme()]. |
... |
Passed to [tidy_ild_model()]. |
intervals |
Logical. For |
object |
Logical. Passed through for non-brms fits; ignored for |
Minimum stable contract for parameter-level tables returned by ild_tidy.
Implementations (tidy_ild_model, ild_tidy.brmsfit) emit all
required columns; conf_low / conf_high replace legacy ci_low / ci_high.
ild_tidy_schema()ild_tidy_schema()
**Required columns:** term, component, effect_level, estimate,
std_error, conf_low, conf_high, statistic, p_value,
interval_type, engine, model_class.
**Optional:** rhat, ess_bulk, ess_tail, pd, rope_low, rope_high.
**interval_type:** Frequentist rows are typically Wald (model or robust; see tidy_ild_model).
Bootstrap output from tidy_ild_msm_bootstrap uses bootstrap_percentile (equal-tailed over replicate coefficients).
A list with required and optional character vectors.
ild_augment_schema, ild_diagnostics_bundle.
Full semantics for component, effect_level, and interval_type:
vignette("developer-contracts", package = "tidyILD").
Returns a tibble with one row per time index and columns for the smoothed level (and optional variance). Separate from [ild_tidy()] which tidies **parameters**.
ild_tidy_states(x, ...)ild_tidy_states(x, ...)
x |
An object from [ild_kfas()]. |
... |
Reserved. |
A tibble.
If [ild_prepare()] was called on a tbl_ts, metadata from the source
tsibble (key, index, interval summary, regularity) is stored in
attr(x, "tidyILD")$tsibble. Returns NULL if the object was
not prepared from a tsibble.
ild_tsibble_meta(x)ild_tsibble_meta(x)
x |
An object that passes [validate_ild()]. |
A named list or NULL.
[ild_prepare()], [ild_as_tsibble()],
vignette("tsibble-interoperability", package = "tidyILD")
Fits a GAM with a smooth in time and a time-varying coefficient for a
predictor using [mgcv::gam()]. Use [ild_tvem_plot()] to plot the
time-varying effect. Requires .ild_time_num (or a numeric time column).
ild_tvem( data, outcome, predictor, time_var = ".ild_time_num", k = 10L, re_id = TRUE, ... )ild_tvem( data, outcome, predictor, time_var = ".ild_time_num", k = 10L, re_id = TRUE, ... )
data |
An ILD object (see [is_ild()]). |
outcome |
Character. Name of the outcome variable. |
predictor |
Character. Name of the predictor with a time-varying effect. |
time_var |
Character. Name of the time variable (default |
k |
Integer. Basis dimension for smooth terms (default 10). |
re_id |
Logical. If |
... |
Passed to [mgcv::gam()]. |
A fitted gam object with class c("tidyild_tvem", "gam", ...)
and attribute ild_tvem_meta (list with outcome, predictor,
time_var, k, re_id).
set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 15, seed = 1) d$x <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") tv <- ild_tvem(x, "y", "x", k = 5, re_id = TRUE) ild_tvem_plot(tv)set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 15, seed = 1) d$x <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") tv <- ild_tvem(x, "y", "x", k = 5, re_id = TRUE) ild_tvem_plot(tv)
Builds a line plot of the smooth term for the time-varying effect of the
predictor (with optional confidence band). Uses a grid over the time variable
and [mgcv::predict.gam()] with type = "terms".
ild_tvem_plot(tvem_fit, n_grid = 100L, level = 0.95)ild_tvem_plot(tvem_fit, n_grid = 100L, level = 0.95)
tvem_fit |
A fitted object from [ild_tvem()] (class |
n_grid |
Integer. Number of points over the time range for the curve (default 100). |
level |
Numeric. Confidence level for the band (default 0.95). |
A ggplot object (time on x-axis, estimated effect on y-axis).
set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 15, seed = 1) d$x <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") tv <- ild_tvem(x, "y", "x", k = 5, re_id = TRUE) ild_tvem_plot(tv)set.seed(1) d <- ild_simulate(n_id = 10, n_obs_per = 15, seed = 1) d$x <- rnorm(nrow(d)) x <- ild_prepare(d, id = "id", time = "time") tv <- ild_tvem(x, "y", "x", k = 5, re_id = TRUE) ild_tvem_plot(tv)
Returns TRUE if the object has all required '.ild_*' columns and 'ild_*' metadata attributes (as set by [ild_prepare()]).
is_ild(x)is_ild(x)
x |
Any object. |
Logical.
Generates ggplot objects for the requested diagnostic types. Plots are not stored in the diagnostics object; call this function to create them.
plot_ild_diagnostics(diag, type = NULL)plot_ild_diagnostics(diag, type = NULL)
diag |
An object returned by [ild_diagnostics()]. |
type |
Character vector. Which plots to build (default: the types stored in |
A named list of ggplot objects (e.g. residual_acf, residuals_vs_fitted, residuals_vs_time, qq).
Returns a tibble conforming to ild_tidy_schema: required columns include
term, component, effect_level, estimate, std_error,
conf_low, conf_high, statistic, p_value, interval_type,
engine, model_class; optional Bayesian columns are NA for these engines.
component is "fixed" for coefficient rows; effect_level is inferred
conservatively from term names (e.g. _wp/_bp suffixes) or "population"
for the intercept, "unknown" when ambiguous.
tidy_ild_model( fit, conf_level = 0.95, object = FALSE, se = c("model", "robust"), robust_type = c("CR2", "CR3", "CR0"), ... )tidy_ild_model( fit, conf_level = 0.95, object = FALSE, se = c("model", "robust"), robust_type = c("CR2", "CR3", "CR0"), ... )
fit |
A fitted model from [ild_lme()] (lmerMod or lme). |
conf_level |
Numeric. Confidence level for intervals (default 0.95). |
object |
Logical. If |
se |
Character. |
robust_type |
Character. When |
... |
Passed to [ild_robust_se()] when |
With object = TRUE, returns an object of class tidyild_model (meta + table) for use with
print.tidyild_model.
A tibble, or when object = TRUE a list of class tidyild_model.
With se = "model" (default), standard errors and CIs come from the fitted
model. With se = "robust", cluster-robust (sandwich) SEs are used;
CIs and p-values are based on a Wald normal approximation. Install the
clubSandwich package to use robust SEs. For **IPW / MSM** weighted lmer
fits ([ild_ipw_refit()]), robust SEs do not account for estimated weights; use
[ild_msm_bootstrap()] and [tidy_ild_msm_bootstrap()] when you need bootstrap CIs;
see [ild_msm_inference].
ild_msm_bootstrap
Returns a tibble matching ild_tidy_schema with interval_type
bootstrap_percentile (equal-tailed over replicate coefficients).
tidy_ild_msm_bootstrap(x, conf_level = 0.95, ...)tidy_ild_msm_bootstrap(x, conf_level = 0.95, ...)
x |
Object from [ild_msm_bootstrap()]. |
conf_level |
Numeric. Used for implied tail quantiles (default 0.95). |
... |
Unused. |
A tibble; see ild_tidy_schema.
[ild_msm_bootstrap()], [ild_msm_inference]
Checks presence and types of '.ild_*' columns and 'ild_*' attributes.
Errors with a clear message if anything is missing or invalid.
Calls ild_normalize_internal() so legacy objects get attr(x, "tidyILD") and class tidyild_df.
validate_ild(x)validate_ild(x)
x |
Object to validate (expected to be an ILD tibble). |
Invisibly returns x if valid.