View source: R/distribution_functions.R
dgmrf2 | R Documentation |
Density function of the multivariate Gaussian distribution reparametrised in terms of its precision matrix (inverse variance).
This implementation is particularly useful for defining the joint log-likelihood with penalised splines or i.i.d. random effects that have a multivariate Gaussian distribution with fixed precision/ penalty matrix \lambda S
.
As S
is fixed and only scaled by \lambda
, it is more efficient to precompute the determinant of S
(for the normalisation constant) and only scale the quadratic form by \lambda
when multiple spline parameters/ random effects with different \lambda
's but the same penalty matrix S
are evaluated.
dgmrf2(x, mu = 0, S, lambda, logdetS = NULL, log = FALSE)
x |
density evaluation point, either a vector or a matrix |
mu |
mean parameter. Either scalar or vector |
S |
unscaled precision matrix |
lambda |
precision scaling parameter Can be a vector if |
logdetS |
Optional precomputed log determinant of the precision matrix |
log |
logical; if |
This implementation allows for automatic differentiation with RTMB
.
vector of density values
x = matrix(runif(30), nrow = 3)
# iid random effects
S = diag(10)
sigma = c(1, 2, 3) # random effect standard deviations
lambda = 1 / sigma^2
d = dgmrf2(x, 0, S, lambda)
# P-splines
L = diff(diag(10), diff = 2) # second-order difference matrix
S = t(L) %*% L
lambda = c(1,2,3)
d = dgmrf2(x, 0, S, lambda, log = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.