stan_add: Build/add STAN code/data for the correlation's PC-prior.

View source: R/stan.R

stan_addR Documentation

Build/add STAN code/data for the correlation's PC-prior.

Description

Build/add STAN code/data for the correlation's PC-prior.

Usage

stan_add(x, model, lambda, name)

stan_add_pc_correl(x, model, lambda, name)

stan_add_graphpcor(x, model, lambda, name)

stan_add_code(x, to_add)

Arguments

x

either a STAN code or list with the data used to fit a STAN model.

model

either a character ("pc_correl" or "graphcor") or a basecor or a basepcor object to define the base correlation model. See basecor() or basepcor().

lambda

the parameter for the exponential prior on the radius of the sphere, see details in the PC-multivariate vignette.

name

character to provide the name for the Cholesky of a correlation matrix or the correlation matrix. See details.

to_add

named list with the code to be added

Details

The parametrization is set as in basecor() or basepcor(). If a basecor is provided, the prior would be considered for the Cholesky factor of a correlation matrix. If a basepcor is provided, the prior would be considered for a correlation matrix (parametrized from it's inverse). The base is used to define an informative prior, as derived in the pcmultivariate vignette.

Value

a list of two elements, one as a list of three additional code to be added into a STAN code and the other with the required additional data.

Functions

  • stan_add_pc_correl(): method for basecor

  • stan_add_graphpcor(): method for basepcor

  • stan_add_code(): add code at the end of each section

References

Daniel Simpson, H\aa vard Rue, Andrea Riebler, Thiago G. Martins and Sigrunn H. S\o rbye (2017). Penalising Model Component Complexity: A Principled, Practical Approach to Constructing Priors. Statistical Science 2017, Vol. 32, No. 1, 1–28. <doi 10.1214/16-STS576>

Examples


## STAN model code without the prior for L, as LCorr
Scode0 <- "
data {
  int<lower=1> p;
  int<lower=1> n;
  vector[p] y[n];
  vector[p] mu;
}
model {
  y ~ multi_normal_cholesky(mu, LCorr);
}
generated quantities {
  corr_matrix[p] Corr = tcrossprod(LCorr);
}
"

## add the pc_correl code
Scode <- stan_add(Scode0, 'pc_correl', lambda = 1, name = "LCorr")
Scode

Sdata0 <- list(
  p = as.integer(3), 
  n = as.integer(1), 
  y = matrix(0,1,3), 
  mu = rep(0.0, 3)
)

baseC <- basecor(rep(0,3), 3)

Sdata <- stan_add(Sdata0, baseC, lambda = 1, name = "LCorr")
str(Sdata)


graphpcor documentation built on March 23, 2026, 9:07 a.m.