## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(dpi = 72, collapse = TRUE, comment = "#>")
# Frailty fits need lme4 (declared in Suggests). If it is unavailable - e.g. a
# CRAN check run with _R_CHECK_FORCE_SUGGESTS_=false - the modelling chunks
# render code-only instead of erroring the vignette build.
run_glmm <- requireNamespace("lme4", quietly = TRUE)

## ----load---------------------------------------------------------------------
library(remverse)

## ----data---------------------------------------------------------------------
data(randomREH3)
data(info3)

reh <- remify(edgelist = randomREH3, model = "tie", directed = TRUE)

stats <- remstats(
  reh = reh,
  tie_effects = ~ inertia(scaling = "std") + reciprocity(scaling = "std"),
  first = 700, memory = "decay", memory_value = 2000
)

## ----standard-----------------------------------------------------------------
fit_fixed <- remstimate(reh = reh, stats = stats)
summary(fit_fixed)

## ----sender-frailty, message=FALSE, warning=FALSE, eval=run_glmm--------------
fit_sender <- remstimate(
  reh    = reh,
  stats  = stats,
  random = ~ (1 | actor1)
)
summary(fit_sender)

## ----receiver-frailty, message=FALSE, warning=FALSE, eval=run_glmm------------
fit_receiver <- remstimate(
  reh    = reh,
  stats  = stats,
  random = ~ (1 | actor2)
)
summary(fit_receiver)

## ----crossed, message=FALSE, warning=FALSE, eval=run_glmm---------------------
fit_crossed <- remfrailty(reh = reh, stats = stats)
summary(fit_crossed)

## ----random-slope, message=FALSE, warning=FALSE, eval=run_glmm----------------
stats_inertia <- remstats(
  reh = reh,
  tie_effects = ~ inertia(scaling = "std") + reciprocity(scaling = "std"),
  first = 700, memory = "decay", memory_value = 2000
)

fit_slope <- remstimate(
  reh    = reh,
  stats  = stats_inertia,
  random = ~ (1 + inertia | actor1)
)
summary(fit_slope)

## ----comparison, eval=run_glmm------------------------------------------------
cat("Fixed-effects only: AIC =", AIC(fit_fixed),    "\n")
cat("Sender frailty:     AIC =", AIC(fit_sender$backend_fit),   "\n")
cat("Receiver frailty:   AIC =", AIC(fit_receiver$backend_fit), "\n")
cat("Crossed frailties:  AIC =", AIC(fit_crossed$backend_fit),  "\n")

## ----comparison-bic, eval=run_glmm--------------------------------------------
cat("Fixed-effects only: BIC =", BIC(fit_fixed),    "\n")
cat("Sender frailty:     BIC =", BIC(fit_sender$backend_fit),   "\n")
cat("Receiver frailty:   BIC =", BIC(fit_receiver$backend_fit), "\n")
cat("Crossed frailties:  BIC =", BIC(fit_crossed$backend_fit),  "\n")

## ----diagnostics, out.width="50%", dev=c("jpeg"), dev.args = list(bg = "white"), eval=run_glmm----
diag_crossed <- diagnostics(fit_crossed, reh, stats)
diag_crossed

plot(diag_crossed)

## ----diagnostics-fixed, eval=run_glmm-----------------------------------------
diag_fixed_only <- diagnostics(fit_crossed, reh, stats, use_ranef = FALSE)
diag_fixed_only

## ----ranef, eval=run_glmm-----------------------------------------------------
re <- lme4::ranef(fit_crossed$backend_fit)
re

