Calculates the expected total time spent in specified target state(s).
By default this sums state occupancy probabilities over visit-scale unit
steps. When time_map is supplied, it maps/interpolates to
real time and uses trapezoidal AUC. Already interpolated SOP output from
interpolate_sops() is integrated on its current real-time grid.
It automatically adapts to the input format:
Patient-Level: If input is an array from
soprob_markov, it returns time-in-state for each patient.Tidy SOP: If input is a
markov_sopsormarkov_avg_sopsobject, it returns grouped total time. Stored simulation, bootstrap, or posterior draws are reduced identically and used to recompute confidence intervals and standard errors.Legacy Wide Bootstrap-Level: If input is a data frame with
boot_id,time,tx, andstate_*columns, it returns mean time-in-state per treatment group and the difference for each bootstrap sample.
Usage
time_in_state(
x,
target_states = 1,
time_map = NULL,
baseline_time = 0,
target_times = NULL
)Arguments
- x
Input object.
Array:
[Patients x Time x States](Frequentist) or[Draws x Patients x Time x States](Bayes).Tidy SOP: A
markov_sops,markov_avg_sops, ormarkov_interpolated_sopsobject.Data Frame: Legacy wide bootstrap SOP data containing columns
boot_id,time,tx, andstate_*.
- target_states
Vector of target state(s) to include in the time calculation. Can be integer indices or character names (e.g.,
1orc("Home", "Rehab")). For bootstrap outputs, these must match the suffix of thestate_columns (e.g., if column isstate_1, use1).- time_map
Optional named numeric vector or data frame mapping visit labels to real elapsed times. Supplying this switches tidy SOP and array inputs to trapezoidal real-time AUC.
- baseline_time
Real time of the observed baseline state for tidy SOP outputs. The default
0supplies a fixed interpolation anchor tointerpolate_sops(). Set toNULLto disable baseline anchoring.- target_times
Optional numeric real-time grid for tidy SOP outputs when
time_mapis supplied. This controls the interpolation grid used for AUC, for exampletarget_times = 1:28withbaseline_time = 0uses day 0 as an anchor but starts the AUC at day 1. IfNULL, integration runs frombaseline_timethrough the mapped follow-up times, matchingavg_time()andavg_comparisons(). Withbaseline_time = NULL, it starts at the first mapped follow-up time.
Value
Input = Array (Frequentist): A named numeric vector of length
n_patients.Input = Array (Bayesian): A matrix of dimension
[n_draws x n_patients].Input = Tidy SOP: A data frame with
total_timeand grouping columns. When stored draws are available, draw-basedconf.low,conf.high, andstd.errorcolumns are recomputed and the reduced draws are retained in adrawsattribute. Any draw-specific baseline anchors have already been propagated byinterpolate_sops().Input = Bootstrap DF: A tibble with columns:
boot_id: Bootstrap iterationSOP_tx: Mean time in target state (Treatment)SOP_ctrl: Mean time in target state (Control)delta: Difference (Treatment - Control)
See also
avg_time() for model-level average time estimates with analytical,
simulation, bootstrap, or posterior uncertainty.
Examples
if (FALSE) { # \dontrun{
# --- Scenario 1: Patient-Level Estimates ---
sops_arr <- soprob_markov(model, data, times = 1:30, y_levels = 1:6)
days_home <- time_in_state(sops_arr, target_states = 1)
# Real-time AUC after fitting on factor visit indices
real_days_home <- time_in_state(
avg,
target_states = 1,
time_map = c("1" = 3, "2" = 7, "3" = 14, "4" = 28),
baseline_time = 0,
target_times = 1:28
)
# --- Scenario 2: Bootstrap Inference ---
avg_boot <- avg_sops(
model,
variables = list(tx = c(0, 1)),
times = 1:30,
y_levels = 1:6,
id_var = "id"
) |>
inferences(method = "bootstrap", n_draws = 100)
boot_effects <- time_in_state(avg_boot, target_states = 1)
} # }
