Simulating random intercepts

simulate_iid() generates a number of iid random intercepts from a zero mean Gaussian distribution with variance \(\sigma^2\). The variance is either specified through the standard deviance \(\sigma\) or the precision \(\tau = 1 /\sigma ^2\). Specifying both leads to an error, even if both arguments have compatible arguments.

library(inlatools)
str(x <- simulate_iid(sigma = 1))
#>  'sim_iid' num [1:1000] 0.228 -0.491 -0.998 -0.641 -0.87 ...
#>  - attr(*, "sigma")= num 1
str(y <- simulate_iid(tau = 100))
#>  'sim_iid' num [1:1000] 9.05e-06 -6.23e-02 2.33e-02 -6.30e-02 1.31e-02 ...
#>  - attr(*, "sigma")= num 0.1
simulate_iid(sigma = 0.1, tau = 100)
#> Error: either 'sigma' or 'tau' must be NULL

Inspecting simulated random intercepts

The default plot() on the simulated random intercepts yields a density with indication of the baseline and quantiles. The plot title displays the standard deviation, variance and precision used to simulate the random intercepts. This makes it easy to simulate the random intercept based on a standard deviation and get the precision required for the prior.

plot(x)
Default plot of simulated random intercepts.

Default plot of simulated random intercepts.

Centering and quantiles

By default the random intercepts are center so that their mean matches the baseline. The alternative are center = "bottom" and center = "top". In those cases, the random effects are centered to that the baseline matches the lowest (center = "bottom") or the highest (center = "top") quantile. This is useful in case you want to get an idea of the difference between the lowest quantile and some other point (e.g. the largest quantile). The plot below illustrates that ratio of more that 50 between extreme random intercepts are not uncommon when \(\sigma = 1\) with a log link.

plot(x, link = "log", center = "bottom")
Density of the random effects after centering to the lowest quantile.

Density of the random effects after centering to the lowest quantile.

The user has the option to specify custom quantiles.

plot(
  y, center = "top", link = "logit", baseline = 0.9, quantiles = c(0.01, 0.99)
)
Density of the random effects after centering to the highest quantile.

Density of the random effects after centering to the highest quantile.

Priors for fixed effects

INLA assumes Gaussian priors for the fixed effects. Hence we can use simulate_iid() to get a feeling of these priors too. Below we give two examples. The first shows the default prior for fixed effects. The second one shows an informative prior for a fixed effect.

fixed <- simulate_iid(tau = 0.001)
plot(fixed)
Simulated density of the default fixed effect prior with mean = 0 and precision = 0.001

Simulated density of the default fixed effect prior with mean = 0 and precision = 0.001

fixed <- simulate_iid(tau = 0.1)
plot(fixed, baseline = 14)
Simulated density of an informative fixed effect prior with mean = 14 and precision = 0.1

Simulated density of an informative fixed effect prior with mean = 14 and precision = 0.1