knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) options(rmarkdown.html_vignette.check_title = FALSE)
library(makemyprior)
We consider a latin square experiment, with a 9x9 latin square design, following the procedure of @fuglstad2020. We assume we have the following model: $$ y_{i,j} = \alpha + \beta \cdot k[i,j] + a_i + b_j + c_{k[i,j]} + \varepsilon_{i,j}, \quad i,j = 1, \dots, 9, $$ where
We remove implicit intercept and linear effect by requiring $\sum_{i=1}^9 c_i^{(1)} = 0$ and $\sum_{i=1}^9 i c_i^{(1)} = 0$.
The model is specified as:
formula <- y ~ lin + mc(row) + mc(col) + mc(iid, constr = TRUE) + mc(rw2, model = "rw2", constr = TRUE, lin_constr = TRUE)
We use the dataset latin_square
in makemyprior
, and present three
priors. We do not carry out inference, as it takes time and will slow
down the compilation of the vignettes by a lot, but include code so the
user can carry out the inference themselves.
We want to avoid overfitting of the model, and use a prior with shrinkage towards the residuals in the top split with median of $0.25$. We do not have any preference for the attribution of the row, column and treatment effects, and use an ignorant Dirichlet prior for the middle split. In the bottom split we again we want to avoid overfitting, and use a prior with shrinkage towards the unstructured treatment effect and a median corresponding to 75% unstructured treatment effect. We do not want to say anything about the scale of the total variance, and use the default Jeffreys' prior.
prior1 <- make_prior( formula, latin_data, prior = list(tree = "s1 = (iid, rw2); s2 = (row, col, s1); s3 = (s2, eps)", w = list(s1 = list(prior = "pc1", param = 0.75), s2 = list(prior = "dirichlet"), s3 = list(prior = "pc0", param = 0.25)))) prior1
plot_prior(prior1) # or plot(prior)
plot_tree_structure(prior1)
Inference can be carried out by running:
posterior1 <- inference_stan(prior1, iter = 15000, warmup = 5000, seed = 1, init = "0", chains = 1) plot_posterior_stan(posterior1, param = "prior", prior = TRUE)
For inference with INLA we need to include the linear constraint in a different way:
formula_inla <- y ~ lin + mc(row) + mc(col) + mc(iid, constr = TRUE) + mc(rw2, model = "rw2", constr = TRUE, extraconstr = list(A = matrix(1:9, 1, 9), e = matrix(0, 1, 1))) prior1_inla <- make_prior( formula_inla, latin_data, prior = list(tree = "s1 = (iid, rw2); s2 = (row, col, s1); s3 = (s2, eps)", w = list(s1 = list(prior = "pc1", param = 0.75), s2 = list(prior = "dirichlet"), s3 = list(prior = "pc0", param = 0.25)))) posterior1_inla <- inference_inla(prior1_inla) plot_posterior_stdev(posterior1_inla)
We can imagine we do not not want to include the residual effect in the tree with the row, column and treatment effects, and assume we have prior knowledge that the latent variance $\sigma_{a}^2 + \sigma_{b}^2 + \sigma_{c^{(1)}}^2 + \sigma_{c^{(2)}}^2$ is not not larger than $0.25$, and use a PC prior for variance that fulfills $\text{P}(\text{total st.dev.} > sqrt(0.2)) = 0.05$. We assume we have knowledge saying that the same is true for the residual variance. We assume the latent variance is distributed in the same way as in Prior 1.
prior2 <- make_prior( formula, latin_data, prior = list(tree = "s1 = (iid, rw2); s2 = (row, col, s1); (eps)", w = list(s1 = list(prior = "pc1", param = 0.75), s2 = list(prior = "dirichlet")), V = list(s2 = list(prior = "pc", param = c(sqrt(0.2), 0.05)), eps = list(prior = "pc", param = c(sqrt(0.2), 0.05))))) prior2
plot_prior(prior2) # or plot(prior2)
plot_tree_structure(prior2)
Inference can be carried out by running:
posterior2 <- inference_stan(prior2, iter = 15000, warmup = 5000, seed = 1, init = "0", chains = 1) plot_posterior_stan(posterior2)
In the last prior, we use component-wise priors on row, column, treatment and residual variance, but use a PC prior with shrinkage towards the unstructured treatment effect to avoid overfitting. We use a strong prior, and say that that the all variances are not much larger than $0.1$.
prior3 <- make_prior( formula, latin_data, prior = list(tree = "(row); (col); (iid); (rw2); (eps)", V = list( row = list(prior = "pc", param = c(sqrt(0.1), 0.05)), col = list(prior = "pc", param = c(sqrt(0.1), 0.05)), iid = list(prior = "pc", param = c(sqrt(0.1), 0.05)), rw2 = list(prior = "pc", param = c(sqrt(0.1), 0.05)), eps = list(prior = "pc", param = c(sqrt(0.1), 0.05)) ))) prior3
plot_prior(prior3) # or plot(prior3)
plot_tree_structure(prior3)
Inference can be carried out by running:
posterior3 <- inference_stan(prior3, iter = 15000, warmup = 5000, seed = 1, init = "0", chains = 1) plot_posterior_stan(posterior3)
sessionInfo()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.