Skip to contents

The correlation matrix R of the ar(), ma(), arma(), cosy() or unstr() term of a fit, over the model's time levels. The residual covariance of a group is D R D restricted to the time points that group has, with D the diagonal matrix of that group's marginal residual sds: its sigma values (from sigma() or frm_linpred(dpar = "sigma")) under cosy() and unstr(), and those values times the square root of the process variance at unit innovation variance under ar(), ma() and arma(), whose sigma is the innovation sd (frmtmb-autocor, "The scale of sigma"). For fcor() and a term another package registers (frmtmb.spatial's sar()), it is the residual covariance divided by sigma^2, over the fitted rows: M for fcor(), \(((I - \rho W)'(I - \rho W))^{-1}\) for sar(). On a family with no residual it is the correlation of the latent residuals (frmtmb-autocor, "Latent residuals"), at the lag of each pair of positions in a group. Under cov = FALSE there the latents are a moving average of independent ones, so it is 0 beyond lag p.

Usage

autocor_matrix(fit, resp = NULL)

Arguments

fit

A frmtmb_fit.

resp

Response name, for a multivariate fit.

Value

A correlation matrix with the time levels as dimnames, or NULL when the fit has no residual correlation term.

See also

rescor_matrix() for the ACROSS-response residual correlation of a rescor = TRUE fit, which is a different structure and cannot be combined with this one.

Examples

set.seed(1)
d <- expand.grid(week = 1:5, subj = factor(1:25))
e <- as.vector(apply(matrix(rnorm(125), 5, 25), 2, function(z) {
  as.vector(stats::filter(z, 0.6, "recursive"))
}))
d$x <- rnorm(125)
d$y <- 1 + 0.5 * d$x + e
fit <- frm(bf(y ~ x + ar(week, subj, cov = TRUE)) + gaussian(),
           data = d)
autocor_matrix(fit)
#>            1         2         3         4          5
#> 1 1.00000000 0.5480376 0.3003452 0.1646004 0.09020721
#> 2 0.54803755 1.0000000 0.5480376 0.3003452 0.16460042
#> 3 0.30034516 0.5480376 1.0000000 0.5480376 0.30034516
#> 4 0.16460042 0.3003452 0.5480376 1.0000000 0.54803755
#> 5 0.09020721 0.1646004 0.3003452 0.5480376 1.00000000