set.seed(126)
= 50
n <- 20 #intercept
a <- 0.2 #slope
b <- round(runif(n, 1, n), 1) #values of the year covariate
x <- 1:n
year <- 20
sigma <- 0.8
rho
library(nlme)
## define a constructor for a first-order
## correlation structure
<- corAR1(form = ~year, value = rho)
ar1 ## initialize this constructor against our data
<- Initialize(ar1, data = data.frame(year))
AR1 ## generate a correlation matrix
<- corMatrix(AR1)
V ## Cholesky factorization of V
<- chol(V)
Cv ## simulate AR1 errors
<- t(Cv) %*% rnorm(n, 0, sigma) # cov(e) = V * sig^2
e ## generate response
<- a + b * x + e
y = data.frame(y = y, x = x, year = year)
data.temporalCor write.table(data.temporalCor, file = "data.temporalCor.csv",
sep = ",", quote = F, row.names = FALSE)
pairs(data.temporalCor)
Temporal Autocorrelation (Stan)
This tutorial will focus on the use of Bayesian estimation to fit simple linear regression models …
Software, Statistics, Stan
This tutorial will focus on the use of Bayesian estimation to fit simple linear regression models. BUGS
(Bayesian inference Using Gibbs Sampling) is an algorithm and supporting language (resembling R
) dedicated to performing the Gibbs sampling implementation of Markov Chain Monte Carlo (MCMC) method. Dialects of the BUGS
language are implemented within three main projects:
OpenBUGS - written in component pascal.
JAGS - (Just Another Gibbs Sampler) - written in
C++
.Stan - a dedicated Bayesian modelling framework written in
C++
and implementing Hamiltonian MCMC samplers.
Whilst the above programs can be used stand-alone, they do offer the rich data pre-processing and graphical capabilities of R
, and thus, they are best accessed from within R
itself. As such there are multiple packages devoted to interfacing with the various software implementations:
R2OpenBUGS - interfaces with
OpenBUGS
R2jags - interfaces with
JAGS
rstan - interfaces with
Stan
This tutorial will demonstrate how to fit models in Stan
(Gelman, Lee, and Guo (2015)) using the package rstan
(Stan Development Team (2018)) as interface, which also requires to load some other packages.
Overview
Introduction
Up until now (in the proceeding tutorials), the focus has been on models that adhere to specific assumptions about the underlying populations (and data). Indeed, both before and immediately after fitting these models, I have stressed the importance of evaluating and validating the proposed and fitted models to ensure reliability of the models. It is now worth us revisiting those fundamental assumptions as well as exploring the options that are available when the populations (data) do not conform. Let’s explore a simple linear regression model to see how each of the assumptions relate to the model.
\[ y_i = \beta_0 + \beta_1x_i + \epsilon_i \;\;\; \text{with} \;\;\; \epsilon_i \sim \text{Normal}(0, \sigma^2). \]
The above simple statistical model models the linear relationship of \(y_i\) against \(x_i\). The residuals (\(\epsilon\)) are assumed to be normally distributed with a mean of zero and a constant (yet unknown) variance (\(\sigma\), homogeneity of variance). The residuals (and thus observations) are also assumed to all be independent.
Homogeneity of variance and independence are encapsulated within the single symbol for variance (\(\sigma^2\)). In assuming equal variances and independence, we are actually making an assumption about the variance-covariance structure of the populations (and thus residuals). Specifically, we assume that all populations are equally varied and thus can be represented well by a single variance term (all diagonal values in a \(N\times N\) covariance matrix are the same, \(\sigma^2\)) and the covariances between each population are zero (off diagonals). In simple regression, each observation (data point) represents a single observation drawn (sampled) from an entire population of possible observations. The above covariance structure thus assumes that the covariance between each population (observation) is zero - that is, each observation is completely independent of each other observation. Whilst it is mathematically convenient when data conform to these conditions (normality, homogeneity of variance, independence and linearity), data often violate one or more of these assumptions. In the following, I want to discuss and explore the causes and options for dealing with non-compliance to each of these conditions. By gaining a better understanding of how the various model fitting engines perform their task, we are better equipped to accommodate aspects of the data that don’t otherwise conform to the simple regression assumptions. In this tutorial we specifically focus on the topic of heterogeneity of the variance.
In order that the estimated parameters represent the underlying populations in an unbiased manner, the residuals (and thus each each observation) must be independent. However, what if we were sampling a population over time and we were interested in investigating how changes in a response relate to changes in a predictor (such as rainfall). For any response that does not “reset” itself on a regular basis, the state of the population (the value of its response) at a given time is likely to be at least partly dependent on the state of the population at the sampling time before. We can further generalise the above into:
\[ y_i \sim Dist(\mu_i), \]
where \(\mu_i=\boldsymbol X \boldsymbol \beta + \boldsymbol Z \boldsymbol \gamma\), with \(\boldsymbol X\) and \(\boldsymbol \beta\) representing the fixed data structure and fixed effects, respectively, while with \(\boldsymbol Z\) and \(\boldsymbol \gamma\) represent the varying data structure and varying effects, respectively. In simple regression, there are no “varying” effects, and thus:
\[ \boldsymbol \gamma \sim MVN(\boldsymbol 0, \boldsymbol \Sigma), \]
where \(\boldsymbol \Sigma\) is a variance-covariance matrix of the form
\[ \boldsymbol \Sigma = \frac{\sigma^2}{1-\rho^2} \begin{bmatrix} 1 & \rho^{\phi_{1,2}} & \ldots & \rho^{\phi_{1,n}} \\ \rho^{\phi_{2,1}} & 1 & \ldots & \vdots\\ \vdots & \ldots & 1 & \vdots\\ \rho^{\phi_{n,1}} & \ldots & \ldots & 1 \end{bmatrix}. \]
Notice that this introduces a very large number of additional parameters that require estimating: \(\sigma^2\) (error variance), \(\rho\) (base autocorrelation) and each of the individual covariances (\(\rho^{\phi_{n,n}}\)). Hence, there are always going to be more parameters to estimate than there are date avaiable to use to estimate these paramters. We typically make one of a number of alternative assumptions so as to make this task more manageable.
- When we assume that all residuals are independent (regular regression), i.e. \(\rho=0\), \(\boldsymbol \Sigma\) is essentially equal to \(\sigma^2 \boldsymbol I\) and we simply use:
\[ \boldsymbol \gamma \sim N( 0,\sigma^2). \]
- We could assume there is a reasonably simple pattern of correlation that declines over time. The simplest of these is a first order autoregressive (AR1) structure in which exponent on the correlation declines linearly according to the time lag (\(\mid t - s\mid\)).
\[ \boldsymbol \Sigma = \frac{\sigma^2}{1-\rho^2} \begin{bmatrix} 1 & \rho & \ldots & \rho^{\mid t-s \mid} \\ \rho & 1 & \ldots & \vdots\\ \vdots & \ldots & 1 & \vdots\\ \rho^{\mid t-s \mid } & \ldots & \ldots & 1 \end{bmatrix}. \]
Note, in making this assumption, we are also assuming that the degree of correlation is dependent only on the lag and not on when the lag occurs (stationarity). That is all lag 1 residual pairs will have the same degree of correlation, all the lag \(2\) pairs will have the same correlation and so on.
First order autocorrelation
Consider an example, in which the number of individuals at time \(2\) will be partly dependent on the number of individuals present at time \(1\). Clearly then, the observations (and thus residuals) are not fully independent - there is an auto-regressive correlation dependency structure. We could accommodate this lack of independence by fitting a model that incorporates a AR1 variance-covariance structure. Alternatively, we fit the following model:
\[ y_{it} \sim Dist(\mu_{it}), \]
where
\[ \mu_{it}=\boldsymbol X \boldsymbol \beta + \rho \epsilon_{i,t-1} + \gamma_{it}, \]
and where \(\gamma \sim N(0, \sigma^2)\). In this version of the model, we are stating that the expected value of an observation is equal to the regular linear predictor plus the autocorrelation parameter (\(\rho\)) multipled by the residual associated with the previous observation plus the regular independently distributed noise (\(\sigma^2\)). Such a model is substantially faster to fit, although along with stationarity assumes in estimating the autocorrelation parameter, only the smallest lags are used. To see this in action, we will first generate some temporally auto-correlated data.
We will now proceed to analyse these data via both of the above techniques for JAGS
:
incorporating AR1 residual autocorrelation structure
incorporating lagged residuals into the model
Incorporating lagged residuals
Model fitting
We proceed to code the model into JAGS
(remember that in this software normal distribution are parameterised in terms of precisions \(\tau\) rather than variances, where \(\tau=\frac{1}{\sigma^2}\)). Define the model.
= "
stanString data {
int<lower=1> n;
vector [n] y;
int<lower=1> nX;
matrix[n,nX] X;
}
transformed data {
}
parameters {
vector[nX] beta;
real<lower=0> sigma;
real<lower=-1,upper=1> phi;
}
transformed parameters {
vector[n] mu;
vector[n] epsilon;
mu = X*beta;
epsilon[1] = y[1] - mu[1];
for (i in 2:n) {
epsilon[i] = (y[i] - mu[i]);
mu[i] = mu[i] + phi*epsilon[i-1];
}
}
model {
phi ~ uniform(-1,1);
beta ~ normal(0,100);
sigma ~ cauchy(0,5);
y ~ normal(mu, sigma);
}
generated quantities {
}
"
## write the model to a text file
writeLines(stanString, con = "tempModel.stan")
Arrange the data as a list (as required by Stan
). As input, Stan
will need to be supplied with: the response variable, the predictor variable, the total number of observed items. This all needs to be contained within a list object. We will create two data lists, one for each of the hypotheses.
= model.matrix(~x, data.temporalCor)
Xmat <- with(data.temporalCor, list(y = y, X = Xmat,
data.temporalCor.list n = nrow(data.temporalCor), nX = ncol(Xmat)))
Define the nodes (parameters and derivatives) to monitor and chain parameters.
<- c("beta", "sigma", "phi")
params = 2
nChains = 500
burnInSteps = 1
thinSteps = 2000 #across all chains
numSavedSteps = ceiling(burnInSteps + (numSavedSteps * thinSteps)/nChains)
nIter nIter
NA [1] 1500
Now compile and run the Stan code via the rstan
interface.
library(rstan)
During the warmup stage, the No-U-Turn sampler (NUTS) attempts to determine the optimum stepsize - the stepsize that achieves the target acceptance rate (\(0.8\) or \(80\)% by default) without divergence (occurs when the stepsize is too large relative to the curvature of the log posterior and results in approximations that are likely to diverge and be biased) - and without hitting the maximum treedepth (\(10\)). At each iteration of the NUTS algorithm, the number of leapfrog steps doubles (as it increases the treedepth) and only terminates when either the NUTS criterion are satisfied or the tree depth reaches the maximum (\(10\) by default).
<- stan(data = data.temporalCor.list, file = "tempModel.stan", chains = nChains, pars = params, iter = nIter, warmup = burnInSteps, thin = thinSteps) data.temporalCor.rstan
NA
NA SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
NA Chain 1:
NA Chain 1: Gradient evaluation took 3.4e-05 seconds
NA Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.34 seconds.
NA Chain 1: Adjust your expectations accordingly!
NA Chain 1:
NA Chain 1:
NA Chain 1: Iteration: 1 / 1500 [ 0%] (Warmup)
NA Chain 1: Iteration: 150 / 1500 [ 10%] (Warmup)
NA Chain 1: Iteration: 300 / 1500 [ 20%] (Warmup)
NA Chain 1: Iteration: 450 / 1500 [ 30%] (Warmup)
NA Chain 1: Iteration: 501 / 1500 [ 33%] (Sampling)
NA Chain 1: Iteration: 650 / 1500 [ 43%] (Sampling)
NA Chain 1: Iteration: 800 / 1500 [ 53%] (Sampling)
NA Chain 1: Iteration: 950 / 1500 [ 63%] (Sampling)
NA Chain 1: Iteration: 1100 / 1500 [ 73%] (Sampling)
NA Chain 1: Iteration: 1250 / 1500 [ 83%] (Sampling)
NA Chain 1: Iteration: 1400 / 1500 [ 93%] (Sampling)
NA Chain 1: Iteration: 1500 / 1500 [100%] (Sampling)
NA Chain 1:
NA Chain 1: Elapsed Time: 0.038 seconds (Warm-up)
NA Chain 1: 0.038 seconds (Sampling)
NA Chain 1: 0.076 seconds (Total)
NA Chain 1:
NA
NA SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
NA Chain 2:
NA Chain 2: Gradient evaluation took 7e-06 seconds
NA Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds.
NA Chain 2: Adjust your expectations accordingly!
NA Chain 2:
NA Chain 2:
NA Chain 2: Iteration: 1 / 1500 [ 0%] (Warmup)
NA Chain 2: Iteration: 150 / 1500 [ 10%] (Warmup)
NA Chain 2: Iteration: 300 / 1500 [ 20%] (Warmup)
NA Chain 2: Iteration: 450 / 1500 [ 30%] (Warmup)
NA Chain 2: Iteration: 501 / 1500 [ 33%] (Sampling)
NA Chain 2: Iteration: 650 / 1500 [ 43%] (Sampling)
NA Chain 2: Iteration: 800 / 1500 [ 53%] (Sampling)
NA Chain 2: Iteration: 950 / 1500 [ 63%] (Sampling)
NA Chain 2: Iteration: 1100 / 1500 [ 73%] (Sampling)
NA Chain 2: Iteration: 1250 / 1500 [ 83%] (Sampling)
NA Chain 2: Iteration: 1400 / 1500 [ 93%] (Sampling)
NA Chain 2: Iteration: 1500 / 1500 [100%] (Sampling)
NA Chain 2:
NA Chain 2: Elapsed Time: 0.036 seconds (Warm-up)
NA Chain 2: 0.032 seconds (Sampling)
NA Chain 2: 0.068 seconds (Total)
NA Chain 2:
print(data.temporalCor.rstan, par = c("beta", "sigma", "phi"))
NA Inference for Stan model: anon_model.
NA 2 chains, each with iter=1500; warmup=500; thin=1;
NA post-warmup draws per chain=1000, total post-warmup draws=2000.
NA
NA mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
NA beta[1] 30.71 0.36 11.95 8.68 22.30 30.40 38.27 55.41 1082 1
NA beta[2] 0.22 0.00 0.10 0.02 0.16 0.22 0.30 0.43 1407 1
NA sigma 12.11 0.03 1.26 9.93 11.25 12.01 12.86 14.88 1359 1
NA phi 0.91 0.00 0.05 0.79 0.88 0.92 0.95 0.99 1117 1
NA
NA Samples were drawn using NUTS(diag_e) at Mon Jul 22 12:39:47 2024.
NA For each parameter, n_eff is a crude measure of effective sample size,
NA and Rhat is the potential scale reduction factor on split chains (at
NA convergence, Rhat=1).
MCMC diagnostics
library(mcmcplots)
= As.mcmc.list(data.temporalCor.rstan)
mcmc denplot(mcmc, parms = c("beta", "sigma", "phi"))
traplot(mcmc, parms = c("beta", "sigma", "phi"))
#Raftery diagnostic
raftery.diag(mcmc)
NA [[1]]
NA
NA Quantile (q) = 0.025
NA Accuracy (r) = +/- 0.005
NA Probability (s) = 0.95
NA
NA You need a sample size of at least 3746 with these values of q, r and s
NA
NA [[2]]
NA
NA Quantile (q) = 0.025
NA Accuracy (r) = +/- 0.005
NA Probability (s) = 0.95
NA
NA You need a sample size of at least 3746 with these values of q, r and s
#Autocorrelation diagnostic
autocorr.diag(mcmc)
NA beta[1] beta[2] sigma phi lp__
NA Lag 0 1.000000000 1.000000000 1.0000000000 1.000000000 1.000000000
NA Lag 1 0.244876071 0.131202988 0.1861700732 0.155207971 0.469635120
NA Lag 5 0.030363677 0.006018787 -0.0230078904 0.037502444 0.016158672
NA Lag 10 -0.003669667 -0.014831709 -0.0006623927 -0.009372815 0.022517151
NA Lag 50 0.014609742 0.005511481 -0.0400835388 0.018467269 -0.003215701
stan_ac(data.temporalCor.rstan)
stan_rhat(data.temporalCor.rstan)
stan_ess(data.temporalCor.rstan)
All diagnostics seem fine.
Model validation
Whenever we fit a model that incorporates changes to the variance-covariance structures, we need to explore modified standardized residuals. In this case, the raw residuals should be updated to reflect the autocorrelation (subtract residual from previous time weighted by the autocorrelation parameter) before standardising by sigma
.
\[ Res_i = Y_i - \mu_i \]
\[ Res_{i+1} = Res_{i+1} - \rho Res_i \]
\[ Res_i = \frac{Res_i}{\sigma} \]
= as.matrix(data.temporalCor.rstan)
mcmc = grep("beta", colnames(mcmc))
wch # generate a model matrix
= data.frame(x = data.temporalCor$x)
newdata = model.matrix(~x, newdata)
Xmat ## get median parameter estimates
= mcmc[, wch]
coefs = coefs %*% t(Xmat)
fit = -1 * sweep(fit, 2, data.temporalCor$y, "-")
resid = ncol(resid)
n -1] = resid[, -1] - (resid[, -n] * mcmc[, "phi"])
resid[, = apply(resid, 2, median)/median(mcmc[, "sigma"])
resid = apply(fit, 2, median)
fit
library(ggplot2)
ggplot() + geom_point(data = NULL, aes(y = resid, x = fit)) + theme_classic()
ggplot() + geom_point(data = NULL, aes(y = resid, x = data.temporalCor$x)) + theme_classic()
ggplot(data = NULL, aes(y = resid, x = data.temporalCor$year)) +
geom_point() + geom_line() + geom_hline(yintercept = 0, linetype = "dashed") + theme_classic()
plot(acf(resid, lag = 40))
= coefs %*% t(Xmat)
fit ## draw samples from this model
= sapply(1:nrow(mcmc), function(i) rnorm(nrow(data.temporalCor),
yRep "sigma"]))
fit[i, ], mcmc[i, ggplot() + geom_density(data = NULL, aes(x = as.vector(yRep),
fill = "Model"), alpha = 0.5) + geom_density(data = data.temporalCor,
aes(x = y, fill = "Obs"), alpha = 0.5) + theme_classic()
No obvious autocorrelation or other issues with residuals remaining.
Parameter estimates
Explore parameter estimates.
library(broom)
library(broom.mixed)
tidyMCMC(data.temporalCor.rstan, par = c("beta", "phi", "sigma"),
conf.int = TRUE, conf.method = "HPDinterval", rhat = TRUE,
ess = TRUE)
NA # A tibble: 4 × 7
NA term estimate std.error conf.low conf.high rhat ess
NA <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
NA 1 beta[1] 30.7 11.9 7.96 54.4 1.00 1082
NA 2 beta[2] 0.224 0.104 -0.0000273 0.414 1.00 1407
NA 3 phi 0.910 0.0541 0.810 0.998 1.00 1117
NA 4 sigma 12.1 1.26 9.88 14.7 1.00 1359
Incorporating AR1 residual autocorrelation structure
Model fitting
We proceed to code the model into JAGS
(remember that in this software normal distribution are parameterised in terms of precisions \(\tau\) rather than variances, where \(\tau=\frac{1}{\sigma^2}\)). Define the model.
= "
stanString functions {
matrix cov_matrix_ar1(real ar, real sigma, int nrows) {
matrix[nrows, nrows] mat;
vector[nrows - 1] gamma;
mat = diag_matrix(rep_vector(1, nrows));
for (i in 2:nrows) {
gamma[i - 1] = pow(ar, i - 1);
for (j in 1:(i - 1)) {
mat[i, j] = gamma[i - j];
mat[j, i] = gamma[i - j];
}
}
return sigma^2 / (1 - ar^2) * mat;
}
}
data {
int<lower=1> n; // total number of observations
vector[n] y; // response variable
int<lower=1> nX;
matrix[n,nX] X;
}
transformed data {
vector[n] se2 = rep_vector(0, n);
}
parameters {
vector[nX] beta;
real<lower=0> sigma; // residual SD
real <lower=-1,upper=1> phi; // autoregressive effects
}
transformed parameters {
}
model {
matrix[n, n] res_cov_matrix;
matrix[n, n] Sigma;
vector[n] mu = X*beta;
res_cov_matrix = cov_matrix_ar1(phi, sigma, n);
Sigma = res_cov_matrix + diag_matrix(se2);
Sigma = cholesky_decompose(Sigma);
// priors including all constants
beta ~ student_t(3,30,30);
sigma ~ cauchy(0,5);
y ~ multi_normal_cholesky(mu,Sigma);
}
generated quantities {
}
"
## write the model to a text file
writeLines(stanString, con = "tempModel2.stan")
Arrange the data as a list (as required by Stan
). As input, Stan
will need to be supplied with: the response variable, the predictor variable, the total number of observed items. This all needs to be contained within a list object. We will create two data lists, one for each of the hypotheses.
= model.matrix(~x, data.temporalCor)
Xmat <- with(data.temporalCor, list(y = y, X = Xmat,
data.temporalCor.list n = nrow(data.temporalCor), nX = ncol(Xmat)))
Define the nodes (parameters and derivatives) to monitor and chain parameters.
<- c("beta", "sigma", "phi")
params = 2
nChains = 500
burnInSteps = 1
thinSteps = 2000 #across all chains
numSavedSteps = ceiling(burnInSteps + (numSavedSteps * thinSteps)/nChains)
nIter nIter
NA [1] 1500
Now compile and run the Stan code via the rstan
interface.
<- stan(data = data.temporalCor.list, file = "tempModel2.stan", chains = nChains, pars = params, iter = nIter, warmup = burnInSteps, thin = thinSteps) data.temporalCor2.rstan
NA
NA SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
NA Chain 1:
NA Chain 1: Gradient evaluation took 0.00024 seconds
NA Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 2.4 seconds.
NA Chain 1: Adjust your expectations accordingly!
NA Chain 1:
NA Chain 1:
NA Chain 1: Iteration: 1 / 1500 [ 0%] (Warmup)
NA Chain 1: Iteration: 150 / 1500 [ 10%] (Warmup)
NA Chain 1: Iteration: 300 / 1500 [ 20%] (Warmup)
NA Chain 1: Iteration: 450 / 1500 [ 30%] (Warmup)
NA Chain 1: Iteration: 501 / 1500 [ 33%] (Sampling)
NA Chain 1: Iteration: 650 / 1500 [ 43%] (Sampling)
NA Chain 1: Iteration: 800 / 1500 [ 53%] (Sampling)
NA Chain 1: Iteration: 950 / 1500 [ 63%] (Sampling)
NA Chain 1: Iteration: 1100 / 1500 [ 73%] (Sampling)
NA Chain 1: Iteration: 1250 / 1500 [ 83%] (Sampling)
NA Chain 1: Iteration: 1400 / 1500 [ 93%] (Sampling)
NA Chain 1: Iteration: 1500 / 1500 [100%] (Sampling)
NA Chain 1:
NA Chain 1: Elapsed Time: 1.097 seconds (Warm-up)
NA Chain 1: 0.665 seconds (Sampling)
NA Chain 1: 1.762 seconds (Total)
NA Chain 1:
NA
NA SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2).
NA Chain 2:
NA Chain 2: Gradient evaluation took 0.000209 seconds
NA Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 2.09 seconds.
NA Chain 2: Adjust your expectations accordingly!
NA Chain 2:
NA Chain 2:
NA Chain 2: Iteration: 1 / 1500 [ 0%] (Warmup)
NA Chain 2: Iteration: 150 / 1500 [ 10%] (Warmup)
NA Chain 2: Iteration: 300 / 1500 [ 20%] (Warmup)
NA Chain 2: Iteration: 450 / 1500 [ 30%] (Warmup)
NA Chain 2: Iteration: 501 / 1500 [ 33%] (Sampling)
NA Chain 2: Iteration: 650 / 1500 [ 43%] (Sampling)
NA Chain 2: Iteration: 800 / 1500 [ 53%] (Sampling)
NA Chain 2: Iteration: 950 / 1500 [ 63%] (Sampling)
NA Chain 2: Iteration: 1100 / 1500 [ 73%] (Sampling)
NA Chain 2: Iteration: 1250 / 1500 [ 83%] (Sampling)
NA Chain 2: Iteration: 1400 / 1500 [ 93%] (Sampling)
NA Chain 2: Iteration: 1500 / 1500 [100%] (Sampling)
NA Chain 2:
NA Chain 2: Elapsed Time: 1.176 seconds (Warm-up)
NA Chain 2: 0.788 seconds (Sampling)
NA Chain 2: 1.964 seconds (Total)
NA Chain 2:
print(data.temporalCor2.rstan, par = c("beta", "sigma", "phi"))
NA Inference for Stan model: anon_model.
NA 2 chains, each with iter=1500; warmup=500; thin=1;
NA post-warmup draws per chain=1000, total post-warmup draws=2000.
NA
NA mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
NA beta[1] 21.61 0.46 16.08 -8.13 12.64 20.76 29.68 55.24 1230 1.00
NA beta[2] 0.22 0.00 0.10 0.01 0.15 0.22 0.29 0.42 1392 1.00
NA sigma 12.02 0.03 1.31 9.85 11.06 11.92 12.82 14.89 1462 1.00
NA phi 0.89 0.00 0.05 0.78 0.86 0.90 0.93 0.98 1131 1.01
NA
NA Samples were drawn using NUTS(diag_e) at Mon Jul 22 12:40:28 2024.
NA For each parameter, n_eff is a crude measure of effective sample size,
NA and Rhat is the potential scale reduction factor on split chains (at
NA convergence, Rhat=1).
MCMC diagnostics
= As.mcmc.list(data.temporalCor2.rstan)
mcmc denplot(mcmc, parms = c("beta", "sigma", "phi"))
traplot(mcmc, parms = c("beta", "sigma", "phi"))
#Raftery diagnostic
raftery.diag(mcmc)
NA [[1]]
NA
NA Quantile (q) = 0.025
NA Accuracy (r) = +/- 0.005
NA Probability (s) = 0.95
NA
NA You need a sample size of at least 3746 with these values of q, r and s
NA
NA [[2]]
NA
NA Quantile (q) = 0.025
NA Accuracy (r) = +/- 0.005
NA Probability (s) = 0.95
NA
NA You need a sample size of at least 3746 with these values of q, r and s
#Autocorrelation diagnostic
autocorr.diag(mcmc)
NA beta[1] beta[2] sigma phi lp__
NA Lag 0 1.00000000 1.000000000 1.000000000 1.000000000 1.000000000
NA Lag 1 0.21452456 0.112473437 0.181672833 0.124546428 0.527409040
NA Lag 5 -0.04090736 0.011068413 -0.001147177 -0.026057078 0.008368655
NA Lag 10 -0.02972948 -0.061470014 -0.027729823 -0.008138883 0.027081616
NA Lag 50 -0.01132752 -0.009636609 0.057698527 0.024742960 -0.018426417
stan_ac(data.temporalCor2.rstan)
stan_rhat(data.temporalCor2.rstan)
stan_ess(data.temporalCor2.rstan)
All diagnostics seem fine.
Model validation
Whenever we fit a model that incorporates changes to the variance-covariance structures, we need to explore modified standardized residuals. In this case, the raw residuals should be updated to reflect the autocorrelation (subtract residual from previous time weighted by the autocorrelation parameter) before standardising by sigma
.
\[ Res_i = Y_i - \mu_i \]
\[ Res_{i+1} = Res_{i+1} - \rho Res_i \]
\[ Res_i = \frac{Res_i}{\sigma} \]
= as.matrix(data.temporalCor2.rstan)
mcmc = grep("beta", colnames(mcmc))
wch # generate a model matrix
= data.frame(x = data.temporalCor$x)
newdata = model.matrix(~x, newdata)
Xmat ## get median parameter estimates
= mcmc[, wch]
coefs = coefs %*% t(Xmat)
fit = -1 * sweep(fit, 2, data.temporalCor$y, "-")
resid = ncol(resid)
n -1] = resid[, -1] - (resid[, -n] * mcmc[, "phi"])
resid[, = apply(resid, 2, median)/median(mcmc[, "sigma"])
resid = apply(fit, 2, median)
fit
ggplot() + geom_point(data = NULL, aes(y = resid, x = fit)) + theme_classic()
ggplot() + geom_point(data = NULL, aes(y = resid, x = data.temporalCor$x)) + theme_classic()
ggplot(data = NULL, aes(y = resid, x = data.temporalCor$year)) +
geom_point() + geom_line() + geom_hline(yintercept = 0, linetype = "dashed") + theme_classic()
plot(acf(resid, lag = 40))
= coefs %*% t(Xmat)
fit ## draw samples from this model
= sapply(1:nrow(mcmc), function(i) rnorm(nrow(data.temporalCor),
yRep "sigma"]))
fit[i, ], mcmc[i, ggplot() + geom_density(data = NULL, aes(x = as.vector(yRep),
fill = "Model"), alpha = 0.5) + geom_density(data = data.temporalCor,
aes(x = y, fill = "Obs"), alpha = 0.5) + theme_classic()
No obvious autocorrelation or other issues with residuals remaining.
Parameter estimates
Explore parameter estimates.
tidyMCMC(data.temporalCor2.rstan, par = c("beta", "phi", "sigma"),
conf.int = TRUE, conf.method = "HPDinterval", rhat = TRUE,
ess = TRUE)
NA # A tibble: 4 × 7
NA term estimate std.error conf.low conf.high rhat ess
NA <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
NA 1 beta[1] 21.6 16.1 -9.11 52.5 1.00 1230
NA 2 beta[2] 0.220 0.104 0.00806 0.410 0.999 1392
NA 3 phi 0.894 0.0541 0.795 0.990 1.01 1131
NA 4 sigma 12.0 1.31 9.63 14.5 1.00 1462