Tidy a survatr_result into long format

Description

Stacks the per-intervention estimates and pairwise contrasts into a single long data.frame suitable for downstream plotting or further aggregation. The estimand column identifies which column the row came from: s_hat / risk_hat / rmst_hat for intervention rows, risk_difference / risk_ratio / rmst_difference for contrast rows.

Usage

## S3 method for class 'survatr_result'
tidy(x, which = c("all", "estimates", "contrasts"), conf.int = TRUE, ...)

Arguments

x A survatr_result from contrast().
which One of “estimates” (per-intervention rows only), “contrasts” (pairwise rows only), or “all” (both stacked). Default “all”.
conf.int Logical; when FALSE, drop ci_lower / ci_upper columns. Default TRUE.
Unused.

Value

A data.frame in long format with columns intervention | contrast | time | estimand | estimate | se | ci_lower | ci_upper (or a subset when which / conf.int restrict it).

See Also

Other survatr_result methods: contrast(), contrast.survatr_fit(), forrest(), forrest.survatr_result(), plot.survatr_result(), print.survatr_result()

Examples

library("survatr")

set.seed(2)
n_id <- 40L
K <- 5L
pp <- data.frame(
  id = rep(seq_len(n_id), each = K),
  t = rep(seq_len(K), times = n_id),
  A = rep(rbinom(n_id, 1L, 0.5), each = K),
  Y = rbinom(n_id * K, 1L, 0.1)
)
fit <- surv_fit(pp, "Y", "A", ~1, "id", "t", time_formula = ~ factor(t))
res <- contrast(
  fit,
  interventions = list(a1 = causatr::static(1), a0 = causatr::static(0)),
  times = 1:5,
  type = "risk_difference"
)
head(tidy(res))
  intervention contrast time estimand   estimate se ci_lower ci_upper
1           a1     <NA>    1 risk_hat 0.16460980 NA       NA       NA
2           a1     <NA>    2 risk_hat 0.35272325 NA       NA       NA
3           a1     <NA>    3 risk_hat 0.52766373 NA       NA       NA
4           a1     <NA>    4 risk_hat 0.61060590 NA       NA       NA
5           a1     <NA>    5 risk_hat 0.61060591 NA       NA       NA
6           a0     <NA>    1 risk_hat 0.08916256 NA       NA       NA