Long-format draws of the linear predictor for marginaleffects-style use
Source:R/tidy_lame.R
prediction_draws_long.RdReturns a long-format data frame with one row per
(draw, i, j, period) combination, giving the per-draw linear
predictor (or response-scale prediction) at each dyad and period.
Only the regression coefficients vary across .draw: the
additive (a, b) and multiplicative (U, V)
effects are held at their posterior means, so the spread across draws
reflects coefficient uncertainty only, not the full posterior of the
linear predictor.
Intended for marginaleffects- / tidybayes-style
downstream summarisation: column names follow the
.draw / .chain / .iteration / .value
convention so that tidybayes::spread_draws() and
marginaleffects::posterior_draws() auto-dispatch on the
returned data frame. Actor and period names from fit$Y's
dimnames are carried forward into the actor_i,
actor_j, period_label columns.
Usage
prediction_draws_long(
object,
newdata = NULL,
type = c("link", "response"),
n_draws = 100L,
seed = NULL
)Arguments
- object
A fitted
lameobject.- newdata
Optional list of
Tdyadic covariate arrays for counterfactual evaluation. DefaultNULLuses in-sample covariates.- type
One of
"link"(default) or"response". For a Poisson fit the response of a draw is its mean count, \(e_t \exp(\eta + \sigma^2 / 2)\), with the draw's own residual variance and the exposure of the period.- n_draws
Number of posterior draws to use. Default 100.
- seed
Optional RNG seed.
Value
A long-format data frame with columns .chain,
.iteration, .draw, period, period_label,
i, j, actor_i, actor_j, .value.
Returned as a tibble when the tibble package is available;
otherwise a plain data frame. The .iteration column identifies
the retained coefficient iteration; .draw numbers the requested
draws, including repeated selections.
Examples
set.seed(1)
n <- 12; Tt <- 3
Y <- lapply(seq_len(Tt), function(t) {
m <- matrix(rnorm(n * n), n, n); diag(m) <- NA; m
})
# chains = 1 keeps the example short; the default runs two chains and compares them
fit <- lame(Y, R = 1, nscan = 20, burn = 5, odens = 1, verbose = FALSE, chains = 1)
draws <- prediction_draws_long(fit, n_draws = 10, seed = 1)
head(draws)
#> # A tibble: 6 × 10
#> .chain .iteration .draw period period_label i j actor_i actor_j
#> <int> <int> <int> <int> <chr> <int> <int> <chr> <chr>
#> 1 1 4 1 1 t1 1 1 a1 a1
#> 2 1 4 1 1 t1 2 1 a10 a1
#> 3 1 4 1 1 t1 3 1 a11 a1
#> 4 1 4 1 1 t1 4 1 a12 a1
#> 5 1 4 1 1 t1 5 1 a2 a1
#> 6 1 4 1 1 t1 6 1 a3 a1
#> # ℹ 1 more variable: .value <dbl>