## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dpi = 96, out.width = "100%",
                      message = FALSE, warning = FALSE)
set.seed(2026)

## -----------------------------------------------------------------------------
library(hsemx)

## ----fit----------------------------------------------------------------------
fit <- hsem(list(m ~ x + (1 + x | cluster),
                 y ~ m + x + (1 + m + x | cluster)),
            data = mlmed)
summary(fit)

## ----effects------------------------------------------------------------------
eff <- hsem_effects(fit, from = "x", to = "y", seed = 1)
eff

## ----effects-plot, fig.height = 6.5-------------------------------------------
plot(eff)

## ----path-classic, fig.height = 4.6-------------------------------------------
hsem_plot_path(fit)

## ----path-twolevel, fig.height = 6--------------------------------------------
hsem_plot_path(fit, style = "twolevel")

## ----path-clusters, fig.height = 5--------------------------------------------
hsem_plot_path(fit, style = "clusters")

## ----ridge, fig.height = 7----------------------------------------------------
hsem_plot_ridge(fit)

## ----forest, fig.height = 6---------------------------------------------------
hsem_plot_forest(fit)

## ----recor, fig.width = 7, fig.height = 6-------------------------------------
hsem_plot_recor(fit)

## ----check--------------------------------------------------------------------
chk <- hsem_check(fit, nsim = 200, seed = 1)
chk

## ----check-plot, fig.height = 13----------------------------------------------
plot(chk, equation = "y")

## ----lineup, eval = FALSE-----------------------------------------------------
#  lu <- hsem_lineup(fit, equation = "y", type = "resid", seed = 7)
#  lu
#  lineup_answer(lu)

## ----influence, eval = FALSE--------------------------------------------------
#  infl <- hsem_influence(fit)
#  plot(infl)

## ----dsem---------------------------------------------------------------------
ema <- hsem_center(subset(smoke_ema, time > 1), c("urge_lag1", "dep_lag1"), "id")
dsem <- hsem(list(urge ~ urge_lag1_cw + dep_lag1_cw + (1 + urge_lag1_cw | id),
                  dep  ~ urge_lag1_cw + dep_lag1_cw + (1 + dep_lag1_cw  | id)),
             data = ema)
summary(dsem)

## ----dsem-plot, fig.height = 4.8----------------------------------------------
hsem_plot_path(dsem, show_cor = "all")

## ----binary-------------------------------------------------------------------
db <- hsem_sim_mediation(J = 60, nj = 25, family_y = "binomial", seed = 11)
fb <- hsem(list(m ~ x + (1 + x | cluster), y ~ m + x + (1 | cluster)),
           data = db, family = list(m = "gaussian", y = binomial()), laplace = TRUE)
summary(fb)$coefficients

## ----nn-----------------------------------------------------------------------
nnfit <- hsem_nn(list(m ~ x + (1 + x | cluster), y ~ m + x + (1 + m | cluster)),
                 data = mlmed, nn = list(y = ~ z1 + z2), architecture = "resnet",
                 hidden = c(32, 32, 32), epochs = 80, seed = 1)
round(fixef(nnfit$fit), 3)
cor(nn_component(nnfit), nnfit$data$g_true)

## ----nn-pdp, fig.height = 4---------------------------------------------------
hsem_nn_pdp(nnfit)

## ----cv, eval = FALSE---------------------------------------------------------
#  cv <- hsem_cv(list(m ~ x + (1 + x | cluster), y ~ m + x + (1 + m | cluster)),
#                data = mlmed, nn = list(y = ~ z1 + z2), cluster = "cluster",
#                architectures = c("linear", "mlp", "resnet", "glu", "ensemble"),
#                folds = 5, seed = 1)
#  cv
#  plot(cv)

## ----sim----------------------------------------------------------------------
ss <- hsem_simstudy(
  nrep = 30,
  generate = function(r) hsem_sim_mediation(J = 30, nj = 10),
  fit = function(d) hsem(list(m ~ x + (1 + x | cluster),
                              y ~ m + x + (1 + m | cluster)), d),
  truth = c("m~x" = 0.5, "y~m" = 0.4, "y~x" = 0.2), seed = 1)
ss

## ----sim-plot, fig.height = 5.5-----------------------------------------------
plot(ss)

## ----zip, fig.height = 4.5----------------------------------------------------
plot(ss, type = "zip", parameter = "y~m")

## ----power, eval = FALSE------------------------------------------------------
#  pw <- hsem_power(J = c(10, 20, 40, 80), nrep = 200,
#                   generate = function(J) hsem_sim_mediation(J = J, nj = 10, b = 0.2),
#                   fit = function(d) hsem(list(m ~ x + (1 + x | cluster),
#                                               y ~ m + x + (1 + m | cluster)), d),
#                   parameter = "y~m", truth = c("y~m" = 0.2), seed = 1)
#  plot(pw)

