Understanding AR1 through the glmmTMB package I've been working through a reproducible example to better understand AR1 covariance matrix using the glmmTMB package. I have a couple of questions, even if only one can be answered I would greatly appreciate it.
Here is some synthetic data, many thanks to Ben Bolker for this code:
library(glmmTMB)
library(MASS)    
simCor1 <- function(phi=0.8,sdgrp=2,sdres=1,
                    npergrp=20,ngrp=20,
                    seed=NULL,
                    ## set linkinv/simfun for GLMM sims
                    linkinv=identity,
                    simfun=identity) {
  if (!is.null(seed)) set.seed(seed)
  cmat <- sdres*phi^abs(outer(0:(npergrp-1),0:(npergrp-1),"-"))
  errs <- MASS::mvrnorm(ngrp,mu=rep(0,npergrp),Sigma=cmat)
  ranef <- rnorm(ngrp,mean=0,sd=sdgrp)
  d <- data.frame(f=rep(1:ngrp,each=npergrp))
  eta <- ranef[as.numeric(d$f)] + c(t(errs)) ## unpack errors by row
  mu <- linkinv(eta)
  d$y <- simfun(mu)
  d$tt <- factor(rep(1:npergrp,ngrp))
  return(d)
}
d <- simCor1(phi = 0.8, seed = 100)

Here is a basic AR1 model in glmmTMB:
mod <- glmmTMB(y~1 + (1|f) + ar1(tt-1|f), data=d,family=gaussian)

On analysing the model fit, it does a good job of identifying the phi parameter with an estimate of 0.85:
summary(mod)
...    
Conditional model:
 Groups   Name        Variance Std.Dev. Corr      
 f        (Intercept) 4.15645  2.0387             
 f.1      tt1         1.06157  1.0303   0.85 (ar1)
 Residual             0.04183  0.2045             
Number of obs: 400, groups:  f, 20
...

Here are my questions:
1) Why does the package notation require time tt as a random slope for ar1() to work? One consequence of this seems to be when I use the predict() function it seems to have the retrospective benefit of knowing how each group performed at each time interval, leading to predictions very close to the actual value y. Using a reproducible example where phi=0:
d <- simCor1(phi = 0, npergrp = 100, seed = 50)
ar1mod <- glmmTMB(y~1 + (1|f) + ar1(tt-1|f), data=d, family=gaussian)
simpmod <- glmmTMB(y~1 + (1|f), data=d, family=gaussian)

Even though phi is estimated to be effectively 0 by ar1mod and therefore concluding that the ar1 component of the model is effectively useless, the predictions between the two models seem wildly different:
d$errar1 <- d$y - predict(ar1mod)
d$errsimp <- d$y - predict(simpmod)
sqrt(mean(d$errar1 ^ 2))
## 0.0005218347
sqrt(mean(d$errsimp ^ 2))
## 0.9983432

2) In seeking to understand why tt is used as a random slope, I tried to recreate the calculation that glmmTMB must be doing to determine its estimate of phi. I thought that, perhaps, by taking the mean coefficient of tt at each time interval, and then determining the autocorrelation between the lagged tt values, we'd arrive at the glmmTMB estimate of the phi parameter. Using mod from earlier to try to recreate the estimated .85 parameter:
MeanTT <- colMeans(ranef(mod)$cond$f)[-1]
MeanTTlag1 <- MeanTT[-1]
MeanTTlag0 <- MeanTT[-length(MeanTT)]
cor(MeanTTlag1, MeanTTlag0)
## 0.430239

Well short of the 0.85. Why does this calculation not arrive at the 0.85 value and what is glmmTMB instead doing to calculate phi?
 A: So I realized this was asked years ago, but as there still isn't an answer, I'll try and explain the structure and the logic of AR-1 at bit of a higher level, and explain why the effect of time (defined here tt) is inherently a random effect. I'm not very familiar with the calculations of glmmTMB
The AR-1 accounts for auto-correlation which means, for a variable that is repeatedly measured over time, the values of the individual time points are correlated with the other values of that same variable, and observations closer in time are more correlated than observations farther in time. Further, the variance $\sigma^2$ is assumed to be homogenous within a time-series, and that the time measurements are evenly spaced. An example could be a measure of blood cell counts taken from a patient at the same time every morning for a span of days.
Lets assume we have 4 time points, or $t = 4$. The AR-1 correlation is a square matrix and is written:
$$
\mathbb{R} = \begin{bmatrix} 1 & \rho & \rho^2 & \rho^3 \\
\rho & 1 & \rho & \rho^2\\
\rho^2 & \rho & 1 & \rho \\
\rho^3 & \rho^2 & \rho & 1
\end{bmatrix}
$$
where $\rho$ is the correlation between time points. The correlation between time points exponentially declines as the distance between two time points $i-j$ increases. $\rho$ is also limited to $-1 < \rho < 1$.
Because we have the assumption that $\sigma^2$ is homogenous within a time series, the covariance is then:
$$
\sigma^2 \begin{bmatrix} 1 & \rho & \rho^2 & \rho^3 \\
\rho & 1 & \rho & \rho^2\\
\rho^2 & \rho & 1 & \rho \\
\rho^3 & \rho^2 & \rho & 1
\end{bmatrix} = \begin{bmatrix} \sigma^2 & \sigma^2\rho & \sigma^2\rho^2 & \sigma^2\rho^3 \\
\sigma^2\rho & \sigma^2 & \sigma^2\rho & \sigma^2\rho^2\\
\sigma^2\rho^2 & \sigma^2\rho & \sigma^2 & \sigma^2\rho \\
\sigma^2\rho^3 & \sigma^2\rho^2 & \sigma^2\rho & \sigma^2
\end{bmatrix}
$$
So, in the example of the blood cell counts, within an individual person the change in variance over time is an inherently random process.
