---
title: "Forecasting Longitudinal Networks with lame"
author: "Cassy Dorff, Tosin Salau, Shahryar Minhas"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Forecasting Longitudinal Networks with lame}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
# the models run when the tarball is built; R CMD check re-knits only the text
run_vignette <- !nzchar(Sys.getenv("_R_CHECK_PACKAGE_NAME_"))
if (!run_vignette) knitr::knit_hooks$set(evaluate.inline = function(code, envir) "")
knitr::opts_chunk$set(
	fig.retina = 1,
	dpi = 72,
	collapse = TRUE,
	comment = "#>",
	fig.width = 7,
	fig.height = 5,
	eval = run_vignette
)
```

## What this vignette covers

`lame()` with at least one dynamic component (`dynamic_beta`,
`dynamic_ab`, `dynamic_uv`) is a state-space model, and
`predict(fit, h = ...)` carries its posterior forward to future periods.
This vignette walks through:

1. **Fit a model with dynamic coefficients.**
2. **Forecast `h` periods ahead** on the link scale and the response
   scale.
3. **Compute counterfactual forecasts** by feeding in alternative
   future covariates.
4. **Visualise** the forecast and the per-coefficient ribbon plot.
5. **Check the forecast**: choose the transition model, score held-out
   periods and assess calibration.

## Step 1: Fit

We simulate 30 actors over 6 periods, a single dyadic covariate `trade`,
and a directed binary outcome `cooperation`. The effect of `trade` on
cooperation is time-varying and **mean-reverting** in the simulation: it
follows an AR(1) around 0.5 with persistence 0.7, starting high (0.9)
and settling toward its long-run level. The network is moderately dense
(~37%), more than 300 ties in each period. `dynamic_beta` estimates one
coefficient per period, and the ties of a period are what inform its
coefficient.

```{r fit}
library(lame)
set.seed(2026)

n_fc <- 30
T_fc <- 6
rho_true <- 0.7
beta_bar <- 0.5
beta_t_true <- numeric(T_fc)
beta_t_true[1] <- 0.9
for (t in 2:T_fc) {
	beta_t_true[t] <- beta_bar + rho_true * (beta_t_true[t - 1] - beta_bar) +
		rnorm(1, 0, 0.15)
}

Xdyad <- lapply(seq_len(T_fc), function(t) {
	x <- matrix(rnorm(n_fc * n_fc), n_fc, n_fc)
	array(
		x,
		dim = c(n_fc, n_fc, 1),
		dimnames = list(NULL, NULL, "trade")
	)
})
a_fc <- rnorm(n_fc, 0, 0.4)
b_fc <- rnorm(n_fc, 0, 0.4)
Y <- lapply(seq_len(T_fc), function(t) {
	eta <- -0.5 + beta_t_true[t] * Xdyad[[t]][, , 1] + outer(a_fc, b_fc, "+")
	Yt <- matrix(rbinom(n_fc * n_fc, 1, pnorm(eta)), n_fc, n_fc)
	diag(Yt) <- NA
	rownames(Yt) <- colnames(Yt) <- sprintf("a%02d", seq_len(n_fc))
	Yt
})
names(Y) <- paste0("t", seq_len(T_fc))

round(beta_t_true, 3) # simulated coefficient path
sapply(Y, function(y) round(mean(y, na.rm = TRUE), 3)) # per-period density

fit <- lame(
	Y,
	Xdyad = Xdyad,
	family = "binary",
	R = 0,
	dynamic_beta = "dyad", # the trade coefficient is AR(1)
	dynamic_beta_kind = "ar1", # mean-reverting; switch to "rw1" for drift
	nscan = 300,
	burn = 30,
	odens = 5,
	verbose = FALSE
)

# estimated path of the trade coefficient
coef_path <- coef(fit)
trade_row <- grep("^trade[._]dyad$|trade", rownames(coef_path), value = TRUE)[1]
round(coef_path[trade_row, ], 3)
```

The estimated path follows the simulated one: it starts high and settles
toward the long-run level, and the largest gap between the two is
`r round(max(abs(coef_path[trade_row, ] - beta_t_true)), 2)`. `lame()` ran
two independently seeded chains and compared them before returning the
fit; the largest R-hat among the monitored quantities is
`r round(max(fit$rhat$rhat, na.rm = TRUE), 2)`. `summary(fit)` reports the
posterior of the persistence $\rho_\beta$, which Step 5 uses to choose the
transition model.

## Step 2: Forecast `h` periods ahead

```{r forecast}
# forecasts draw the AR(1) state at random, so seed them
set.seed(1)
# 3-step-ahead forecast on the link scale
fc_link <- predict(fit, h = 3, type = "link")
length(fc_link) # 3, one matrix per future period
dim(fc_link[[1]]) # n x n (30 x 30 here)

# response scale: the inverse link is applied to each draw
fc_resp <- predict(fit, h = 3, type = "response")
# posterior-mean predicted probabilities
range(fc_resp[[1]], na.rm = TRUE)

# by_draw = TRUE returns the full [n, n, h, n_draws] array
fc_full <- predict(fit, h = 3, type = "response", by_draw = TRUE)
dim(fc_full) # n x n x 3 x n_draws

# lower, median and upper matrices per period (95% by default)
fc_ci <- predict(fit, h = 3, type = "response", interval = "credible")
str(fc_ci[[1]]) # lower, median and upper, each n x n
```

Each element of `fc_ci` holds `lower`, `median` and `upper` matrices of
tie probabilities for one future period. The 95% interval of a dyad is on
average `r round(mean(fc_ci[[1]]$upper - fc_ci[[1]]$lower, na.rm = TRUE), 3)`
wide on the probability scale at `h = 1` and
`r round(mean(fc_ci[[3]]$upper - fc_ci[[3]]$lower, na.rm = TRUE), 3)` at
`h = 3`. Uncertainty about the coefficient path accumulates with the
horizon; because the path is mean-reverting (the posterior mean of
`rho_beta` is `r round(mean(fit$RHO_BETA), 2)`), it levels off at the rate
given in Step 5.

### Visualising the forecast

Two views make the forecast concrete. The left panel is the predicted
tie-probability matrix for the first future period (`fc_resp[[1]]`) --
the actual network the model expects next. The right panel collapses
each horizon to its across-dyad average predicted probability with the
average 95% credible band, so you can watch the band widen as the
horizon grows.

```{r forecast-plot, fig.width = 7, fig.height = 3.4, fig.alt="Left: heatmap of predicted tie probabilities among the 30 actors for the first forecast period, darker cells indicating higher predicted probability. Right: the across-dyad average predicted probability at horizons h = 1, 2, 3 with a shaded 95 percent credible band that widens with the horizon."}
library(ggplot2)
library(patchwork)

# left: forecast heatmap for the first future period
P1 <- fc_resp[[1]]
rn <- rownames(P1); if (is.null(rn)) rn <- sprintf("a%02d", seq_len(nrow(P1)))
cn <- colnames(P1); if (is.null(cn)) cn <- sprintf("a%02d", seq_len(ncol(P1)))
heat_df <- data.frame(
	sender = factor(rn[row(P1)], levels = rev(rn)),
	receiver = factor(cn[col(P1)], levels = cn),
	prob = as.vector(P1)
)
p_heat <- ggplot(heat_df, aes(receiver, sender, fill = prob)) +
	geom_tile() +
	scale_fill_distiller(palette = "Blues", direction = 1, limits = c(0, 1), name = "P(tie)") +
	labs(title = "Forecast for t7", x = "Receiver", y = "Sender") +
	theme_bw(base_size = 9) +
	theme(
		panel.border = element_blank(),
		axis.line = element_blank(),
		axis.text = element_blank(),
		axis.ticks = element_blank(),
		legend.position = "top",
		plot.title = element_text(hjust = 0),
		plot.subtitle = element_text(hjust = 0),
		plot.caption = element_text(hjust = 0)
	)

# right: across-dyad mean probability with the mean 95% band
horizon_df <- data.frame(
	period = factor(
		paste0("t", T_fc + seq_along(fc_ci)),
		levels = paste0("t", T_fc + seq_along(fc_ci))
	),
	median = sapply(fc_ci, function(m) mean(m$median, na.rm = TRUE)),
	lower = sapply(fc_ci, function(m) mean(m$lower, na.rm = TRUE)),
	upper = sapply(fc_ci, function(m) mean(m$upper, na.rm = TRUE))
)
p_horizon <- ggplot(horizon_df, aes(period, median, group = 1)) +
	geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey85") +
	geom_line() + geom_point(size = 2) +
	labs(
		title = "Average forecast by horizon",
		x = "Forecast period", y = "Mean P(tie)"
	) +
	theme_bw(base_size = 9) +
	theme(
		panel.border = element_blank(),
		axis.line = element_blank(),
		axis.ticks = element_blank(),
		legend.position = "top",
		plot.title = element_text(hjust = 0),
		plot.subtitle = element_text(hjust = 0),
		plot.caption = element_text(hjust = 0)
	)

p_heat + p_horizon
```

The heatmap shows the model does not forecast a uniform network: some
sender/receiver pairs carry a much higher predicted tie probability than
others, inherited from their estimated additive effects and the recovered
trade coefficient. The horizon panel shows the average predicted
probability staying roughly level (the coefficient is mean-reverting) while
its credible band widens with `h` -- the visual signature of accumulating
forecast uncertainty.

The forecast propagates `(β_t, a_t, b_t, U_t, V_t)` forward by drawing
from the joint posterior of the dynamic state-space hyperparameters
`(ρ_β, σ_β, ρ_ab, σ_ab, ρ_uv, σ_uv)` for each posterior draw. The same
forecasting machinery covers the supported outcome families; the binary
example above is illustrative. For `family = "normal"`, `type = "response"`
returns the forecasted conditional mean. For `family = "poisson"`, each
matched parameter draw has conditional mean `e * exp(η + ve / 2)`, where
`e` is the future-period exposure (set with `newexposure`). With
`by_draw = FALSE` the point forecast is the mean count at the draw-averaged
predictor and variance, `e * exp(mean(η) + mean(ve) / 2)`, the form of the
in-sample `fitted()` value; `by_draw = TRUE` returns each draw's own
conditional mean. Use `interval = "prediction"` for predictive count
quantiles, or `interval = "credible"` for quantiles of the per-draw
conditional means. These are distinct summaries.

### Poisson exposure offsets via `period_exposure`

For `family = "poisson"`, pass `period_exposure = e` to `lame()` (a
non-negative numeric vector of length `T`) when the per-period rate
should be scaled by a known exposure (e.g. weeks observed, population
size, number of edge-formation opportunities). The sampler treats
`log(period_exposure[t])` as a fixed offset on the linear predictor:
conditional on the latent log-rate `Z`, the Poisson mean is
`period_exposure[t] * exp(Z_t)`. Integrating the Gaussian observation
noise within a parameter draw gives
`period_exposure[t] * exp(η_t + ve / 2)`; at the posterior means of `η_t`
and `ve` this is the mean count `fitted()` reports. Zero exposure
gives a structural zero count. `period_exposure` belongs to the Poisson
family.
When you call `predict(fit, h = K)` later without `newexposure`, the
response-scale forecast reuses the last observed exposure for every
future period; pass `newexposure = rep(e_new, K)` to forecast under a
different exposure path.

## Step 3: Counterfactual forecasts

Pass `newdata = list_of_h_future_X_arrays` to combine alternative
future covariates with the forecast. Each array is `n x n x p` and the
slice order along the 3rd dimension must match the original
`dimnames(Xdyad[[1]])[[3]]` from the fit (here a single slice,
`trade`).

```{r counterfactual}
# seed the forecast draws
set.seed(1)
# baseline: hold trade at its last observed value
last_X <- Xdyad[[length(Xdyad)]]
X_future <- list(last_X, last_X, last_X)
fc_cf <- predict(fit, h = 3, type = "response", newdata = X_future)

# counterfactual: one more unit (one standard deviation) of trade for every dyad
X_future_up <- lapply(X_future, function(x) {
	x[, , 1] <- x[, , 1] + 1
	x
})
fc_up <- predict(fit, h = 3, type = "response", newdata = X_future_up)

# change in each dyad's predicted probability at h = 3
delta_h3 <- fc_up[[3]] - fc_cf[[3]]
summary(as.vector(delta_h3))
```

The trade coefficient is positive, so one more unit of `trade` raises the
forecast cooperation probability at `h = 3`: by
`r round(mean(delta_h3, na.rm = TRUE), 2)` on average, and by between
`r round(min(delta_h3, na.rm = TRUE), 2)` and
`r round(max(delta_h3, na.rm = TRUE), 2)` across dyads. The marginal
probit effect $\phi(\eta)\,\beta$ is largest for dyads whose baseline
linear predictor $\eta = a_i + b_j + \beta x_{ij}$ is near zero (a tie
probability near one half) and smallest for dyads whose probability is
already close to 0 or 1. For substantive interpretation, summarise
`delta_h3` for the dyads that matter rather than reporting the aggregate
`summary()`.

Two properties of counterfactual covariates. (1) Because `dynamic_beta`
draws the coefficient path forward from its posterior, the same
covariate change produces different magnitudes at different horizons:
the multiplier on `trade` moves with the horizon. (2) For a covariate
centred at zero, such as `trade`, an additive shift (`+ 1`) is the
meaningful counterfactual; a multiplicative one (`* 2`) suits strictly
positive covariates.

## Step 4: Visualise the coefficient path

```{r autoplot, fig.width = 7, fig.height = 4, fig.alt="In-sample posterior coefficient paths from autoplot.lame: each panel shows the posterior median line and a 95 percent credible interval ribbon across the fitted training periods (t1-t6) for one regression coefficient. This plots the estimated in-sample path, not a forecast."}
library(ggplot2)
autoplot(fit, probs = c(0.025, 0.5, 0.975)) +
	labs(
		title = "Posterior coefficient paths",
		subtitle = "median + 95% interval",
		x = "Time period", y = "Coefficient value"
	)
```

The ribbon shows the posterior 95% credible interval around the
posterior median per coefficient and period; faceted by coefficient.

## Step 5: Forecast diagnostics

The transition model decides how forecast uncertainty grows with the
horizon:

- **AR(1), `dynamic_beta_kind = "ar1"`** with $|\rho| < 1$: the
  conditional $h$-step variance of $\beta_{T+h}$ given the training
  window is $\sigma_\beta^2 \cdot (1 - \rho^{2h}) / (1 - \rho^2)$. It
  *saturates* (the increment from $h$ to $h+1$ shrinks geometrically
  to zero) at the stationary variance $\sigma_\beta^2 / (1 - \rho^2)$,
  which grows as $\rho \to 1$.
- **RW1, `dynamic_beta_kind = "rw1"`**: $\rho = 1$ by construction and
  the $h$-step variance is exactly $\sigma_\beta^2 \cdot h$, *linear*
  growth in $h$.

In words: an AR(1) coefficient returns to its long-run mean and its
forecast settles into a stationary distribution; an RW1 coefficient
keeps its last level and adds innovation variance every period.

`lame` checks which of the two the data support. After an AR(1) fit,
`summary(fit)` recommends `dynamic_beta_kind = "rw1"` when the posterior
of $\rho_\beta$ is concentrated at or above 0.97, and `predict()` says
so once per fit when the upper 97.5% bound of $\rho_\beta$ reaches 0.99.
For the Step 1 fit that bound is
`r round(quantile(fit$RHO_BETA, 0.975), 3)`,
`r if (quantile(fit$RHO_BETA, 0.975) >= 0.99) "at the threshold: RW1 is the model to refit with" else "below 0.99: the mean-reverting forecast stands"`.

### A drifting coefficient: AR(1) or RW1

On a short panel the choice rests on the substantive question: does the
effect return to a long-run level, or keep the level it has reached? Fit
both transition models and compare their forecasts. Here a covariate
effect drifts upward over four periods of a 20-actor network. The cells
are simulated independently, so the fits set `dcor = FALSE`:

```{r drift-fit}
set.seed(2026)
n_sp <- 20
T_sp <- 4
beta_drift <- c(0.4, 0.9, 1.4, 1.9) # drifts upward without mean reversion
X_sp <- lapply(seq_len(T_sp), function(t) {
	x <- matrix(rnorm(n_sp * n_sp), n_sp, n_sp)
	array(
		x,
		dim = c(n_sp, n_sp, 1),
		dimnames = list(NULL, NULL, "sanction_risk")
	)
})
Y_sp <- lapply(seq_len(T_sp), function(t) {
	eta <- -2 + beta_drift[t] * X_sp[[t]][, , 1]
	Yt <- matrix(rbinom(n_sp * n_sp, 1, pnorm(eta)), n_sp, n_sp)
	diag(Yt) <- NA
	rownames(Yt) <- colnames(Yt) <- sprintf("s%02d", seq_len(n_sp))
	Yt
})
names(Y_sp) <- paste0("t", seq_len(T_sp))
sapply(Y_sp, function(y) round(mean(y, na.rm = TRUE), 3)) # sparse early on

fit_drift <- lapply(c(ar1 = "ar1", rw1 = "rw1"), function(kind) {
	lame(
		Y_sp,
		Xdyad = X_sp,
		family = "binary",
		R = 0,
		dcor = FALSE,
		dynamic_beta = "dyad",
		dynamic_beta_kind = kind,
		nscan = 250,
		burn = 50,
		odens = 5,
		verbose = FALSE
	)
})
# estimated coefficient path under each transition model
round(t(sapply(fit_drift, function(f) coef(f)["sanction_risk_dyad", ])), 3)
```

Both fits recover the upward drift, and they agree on the fitted
periods. Four periods leave the posterior of $\rho_\beta$ close to its
prior (prior mean 0.8, posterior median
`r round(median(fit_drift$ar1$RHO_BETA), 2)` in the AR(1) fit), which is why the choice is made on substance. The two fits differ
in what they carry forward. The forecast coefficient
at horizon `h` is the link-scale difference between a forecast with
`sanction_risk = 1` for every dyad and one with `sanction_risk = 0`:

```{r drift-forecast, fig.width = 6, fig.height = 3.2, fig.alt="Line plot of the forecast coefficient on sanction_risk at horizons one through six under the AR(1) and RW1 transition models fitted to the drifting panel. The AR(1) line declines toward the long-run mean; the RW1 line stays at the last fitted level."}
x_at <- function(value) {
	lapply(seq_len(6), function(h) {
		array(
			value,
			dim = c(n_sp, n_sp, 1),
			dimnames = list(NULL, NULL, "sanction_risk")
		)
	})
}
# the same seed gives both forecasts the same coefficient draws
beta_forecast <- function(f) {
	set.seed(1)
	fc_one <- predict(f, h = 6, type = "link", newdata = x_at(1))
	set.seed(1)
	fc_zero <- predict(f, h = 6, type = "link", newdata = x_at(0))
	mapply(function(a, b) mean(a - b, na.rm = TRUE), fc_one, fc_zero)
}
beta_fc <- sapply(fit_drift, beta_forecast)
round(t(beta_fc), 3)

drift_df <- data.frame(
	h = rep(seq_len(6), 2),
	model = rep(c("AR(1)", "RW1"), each = 6),
	coefficient = c(beta_fc[, "ar1"], beta_fc[, "rw1"])
)
ggplot(drift_df, aes(h, coefficient, linetype = model)) +
	geom_line() + geom_point(size = 1.8) +
	labs(
		title = "Forecast coefficient by horizon",
		x = "Forecast horizon h", y = "Coefficient on sanction_risk"
	) +
	theme_bw(base_size = 9) +
	theme(
		panel.border = element_blank(),
		axis.line = element_blank(),
		axis.ticks = element_blank(),
		legend.position = "top",
		plot.title = element_text(hjust = 0),
		plot.subtitle = element_text(hjust = 0),
		plot.caption = element_text(hjust = 0),
		legend.title = element_blank()
	)
```

The last fitted coefficient is
`r round(coef(fit_drift$ar1)["sanction_risk_dyad", T_sp], 2)` (AR(1)) and
`r round(coef(fit_drift$rw1)["sanction_risk_dyad", T_sp], 2)` (RW1). The
AR(1) forecast draws it back toward its long-run mean, to `r round(beta_fc[1, "ar1"], 2)` at `h = 1` and
`r round(beta_fc[6, "ar1"], 2)` at `h = 6`; the RW1 forecast holds the
last level, `r round(beta_fc[6, "rw1"], 2)` at `h = 6`. The same two
fits at the default chain length give 1.25 and 1.87 at `h = 6`, and their
95% forecast intervals on the link scale behave as the two variance
formulas say: under AR(1) the average width goes from 2.0 at `h = 1` to
2.9 at `h = 5` and stays there, under RW1 it goes from 1.9 to 3.4 at
`h = 6` and is still growing. Choose AR(1) when the effect is expected to
settle and RW1 when a shift is expected to persist; `lfo()` below scores
either choice on held-out periods.

To score forecasts on held-out periods, use the exact rolling-origin
leave-future-out CV:

```{r lfo}
# refit before each of the last two periods and score that period
# seed the forecast draws inside lfo()
set.seed(1)
T_fit <- length(fit$YPM)
lfo_periods <- tail(seq_len(T_fit), max(1L, min(2L, T_fit - 2L)))
lfo_res <- lfo(
	fit,
	periods = lfo_periods,
	refit = TRUE,
	nscan = 500,
	burn = 100,
	odens = 5,
	verbose = FALSE
)
print(lfo_res)
```

`lfo()` refits the model on the periods before each origin and scores
the held-out period with that period's own covariates. `elpd` is the log
predictive density summed over the `n_obs` observed dyads of the period;
higher is better. `nscan`, `burn` and `odens` set the chain of each refit
and, left out, the refits run at the length of `fit`. Period 5 scores
`r round(lfo_res$per_period$elpd[1], 1)` and period 6
`r round(lfo_res$per_period$elpd[2], 1)` here; refits at the default
length (`nscan = 10000`) score -481.8 and -473.1.

Compare the scores across origins. A sharp decline at the last origin
points to a late change in the data-generating process, and
`detect_change_point(fit)` then names the coefficient path and the period
to inspect.

### Probability-integral-transform (PIT) calibration

`forecast_pit()` complements `lfo()` by asking a sharper question:
*given the held-out outcomes, is the h-step posterior-predictive
distribution itself well calibrated?* The PIT uses the posterior predictive
CDF, averaging the conditional distributions over matched posterior draws.
For binary / Poisson / ordinal outcomes it randomises between the CDF
just below and at the observation, using the Czado-Gneiting-Held PIT
(Czado, Gneiting & Held 2009). A well-calibrated forecast produces
PIT values that are Uniform(0, 1).

```{r pit}
# fit on the first five periods, hold out the sixth
fit_train <- lame(
	Y[1:5],
	Xdyad = Xdyad[1:5],
	family = "binary",
	R = 0,
	dynamic_beta = "dyad",
	nscan = 100,
	burn = 30,
	odens = 5,
	verbose = FALSE
)
# seed the randomised PIT
set.seed(1)
pit <- forecast_pit(fit_train, y_future = list(Y[[6]]))
print(pit)
```

`plot(pit)` renders the PIT values against the Uniform(0, 1) reference
that a perfectly calibrated forecast would follow:

```{r pit-plot, fig.height = 3.6, fig.alt="PIT calibration plot for the held-out period: the empirical distribution of probability-integral-transform values compared with the Uniform(0,1) reference. Bars or a curve tracking the reference indicate calibration; a U-shape signals an over-confident (too-narrow) forecast and a central hump signals an under-confident (too-wide) one."}
plot(pit)
```

Bars that hug the uniform reference indicate a well-calibrated forecast.
A U-shape (mass piling up in the tails) is the signature of an
over-confident forecast whose intervals are too narrow; a central hump
is the opposite, an under-confident forecast with intervals that are too
wide. Read the plot for its overall shape; `cover_95` summarises it in
one number.

`pit$cover_95` is the fraction of PIT values in [0.025, 0.975]; here it
is `r round(pit$cover_95, 3)`, compared with 0.95 under marginal
calibration (0.929 when `fit_train` runs at the default chain length).
The KS statistic (`r round(pit$ks_stat, 3)` here) is the
largest distance between the empirical PIT distribution and the uniform
reference. Network cells are dependent, so it is reported as a
descriptive distance and `pit$ks_p` is `NA`. To pool evidence over
several held-out periods, pass one matrix per horizon in `y_future`.

## Comparing two fits with `loo_compare()`

When you have two candidate specifications, say a static-`beta`
baseline and a `dynamic_beta = "dyad"` variant, `loo::loo_compare()`
ranks them on out-of-sample predictive performance. Fit both with
`save_log_lik = TRUE` and independent residuals (`dcor = FALSE`), so that
each observed cell is one likelihood factor:

```{r loo-compare, eval = FALSE}
# two candidates with independent residuals and saved log-likelihoods
fit_static <- lame(
	Y,
	Xdyad = Xdyad,
	family = "binary",
	R = 0,
	dcor = FALSE,
	nscan = 5000,
	burn = 1000,
	odens = 25,
	save_log_lik = TRUE,
	verbose = FALSE
)
fit_dyn <- lame(
	Y,
	Xdyad = Xdyad,
	family = "binary",
	R = 0,
	dcor = FALSE,
	dynamic_beta = "dyad",
	nscan = 5000,
	burn = 1000,
	odens = 25,
	save_log_lik = TRUE,
	verbose = FALSE
)

# the best model has elpd_diff = 0; a named list labels the rows
cmp <- loo::loo_compare(list(
	fit_static = loo::loo(fit_static),
	fit_dyn = loo::loo(fit_dyn)
))
cmp
```

```{r loo-compare-read, eval = FALSE}
best <- rownames(cmp)[1]
runnerup <- rownames(cmp)[2]
diff_val <- abs(round(cmp[2, "elpd_diff"], 1))
se_val <- round(cmp[2, "se_diff"], 1)
ratio <- round(diff_val / se_val, 1)
```

**How to read this output.** The top row is the preferred model and has
`elpd_diff = 0`. Each subsequent row gives the difference in expected log
pointwise predictive density relative to that model and the standard error of
the difference. A difference that is large relative to its standard error
supports the preferred model; when the two are similar, the specifications are
not distinguishable on this predictive criterion. The Pareto-$k$ diagnostics
in the `loo` output report which pointwise estimates are reliable. Run as
written on this panel, the comparison prefers the dynamic model: the
static model has `elpd_diff` = -22.2 with standard error 6.6, and every
Pareto-$k$ is below 0.5.

`loo::loo()` and `loo::waic()` read the saved scores of `normal`,
`binary` and `poisson` fits, and of ordinal fits with
`ordinal_cutpoints = "explicit"`, whenever the observed cells are
independent given the parameters: directed fits with `dcor = FALSE`,
symmetric fits (each unordered dyad counted once) and bipartite fits.
Fits with residual dyad correlation are compared on whole held-out
periods with `lfo()` above; capped-binary, fixed-rank-nomination and
rank-likelihood ordinal fits are compared with the posterior predictive
checks of `gof()`.

### Memory-conscious long runs

If the in-memory log-likelihood matrix from `save_log_lik = TRUE`
would exceed RAM, pass `save_log_lik = "chunked"` instead. Each chain
writes its scores to disk as it samples. `loo()` reads the on-disk chunks
as it reads the in-memory matrix, and `read_log_lik(fit)` returns them as
a matrix:

```{r chunked, eval = FALSE}
fit_chk <- lame(
	Y,
	Xdyad = Xdyad,
	family = "binary",
	R = 0,
	dcor = FALSE,
	dynamic_beta = "dyad",
	nscan = 2000,
	burn = 500,
	odens = 5,
	save_log_lik = "chunked", # binary files, one per chunk of columns
	log_lik_chunk_size = 5000L,
	verbose = FALSE
)
loo::loo(fit_chk) # reads the chunks transparently
```

## See also

- The [dynamic effects article](https://netify-dev.github.io/lame/articles/dynamic_effects.html): the full reference on
  `dynamic_beta` / `dynamic_ab` / `dynamic_uv` and the decision tree
  for picking `dynamic_beta_kind`.
- `?predict.lame`: argument reference for `h`, `newdata`, `type`,
  `by_draw`.
- `?autoplot.lame`: ggplot ribbon plot for dynamic coefficients.
- `?lfo`: exact rolling-origin LFO CV.
