## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
set.seed(3)

## ----data---------------------------------------------------------------------
library(ctreeMI)
G <- 8; n_per <- 100; n <- G * n_per
cohort <- factor(rep(sprintf("c%02d", 1:G), each = n_per))
u  <- rnorm(G, 0, 0.6)[as.integer(cohort)]
x1 <- rnorm(n); x2 <- rnorm(n); x4 <- u + rnorm(n, 0, 0.3)
y  <- u + 1.2 * (x1 > 0) + rnorm(n)
d  <- data.frame(y, x1, x2, x4, cohort)
d$x1[sample(n, 120)] <- NA

## ----run----------------------------------------------------------------------
res <- discover_confirm(y ~ x1 + x2 + x4, data = d, m = 5,
                        cluster = "cohort", within = TRUE, seed = 7)
res$within

## ----report-------------------------------------------------------------------
report_confirm(res)

## ----hand, eval = FALSE-------------------------------------------------------
# parts <- split_holdout(d, strata = "cohort", seed = 42)
# imp_d <- mice::mice(parts$discover, m = 30, seed = 1)   # cohort stays in
# imp_c <- mice::mice(parts$confirm,  m = 30, seed = 2)   # the imputation model
# tree  <- ctree_stacked(y ~ x1 + x2 + x4, data = imp_d, center_by = "cohort")
# cw    <- confirm_within(tree, imp_c, cluster = "cohort")

