Learn R Programming

INLAtools (version 0.0.8)

rgeneric_get: rgeneric_get is an internal function used by graph, pred, initial, mu or prior methods for rgeneric.

Description

The generic_get retrieve a model property specified by cmd on an rgeneric object. The functions listed below are for each cmd case.

Usage

rgeneric_get(
  model,
  cmd = c("graph", "Q", "initial", "mu", "log_prior"),
  theta,
  optimize = TRUE
)

# S3 method for rgeneric initial(model)

# S3 method for rgeneric mu(model, theta)

# S3 method for rgeneric graph(model, optimize)

# S3 method for rgeneric prec(model, theta, optimize)

# S3 method for rgeneric prior(model, theta)

Value

depends on cmd

numeric scalar (if numeric vector is provided for theta) or vector (if numeric matrix is provided for theta).

Arguments

model

a rgeneric object.

cmd

an string to specify which model element to get

theta

numeric vector with the model parameters. If missing, the initial() will be used.

optimize

logical indicating if it is to be returned only the elements and not as a sparse matrix.

Functions

  • initial(rgeneric): Retrive the initial parameter(s) of an rgeneric model.

  • mu(rgeneric): Evaluate the mean for an rgeneric model.

  • graph(rgeneric): Retrieve the graph of an rgeneric object

  • prec(rgeneric): Retrieve the precision of an rgeneric object

  • prior(rgeneric): Evaluate the prior for an rgeneric model

Examples

Run this code
library(INLAtools)
old.par <- par(no.readonly = TRUE)

## Setting the prior parameters
prior.par <- c(1, 0.5) # P(sigma > 1) = 0.5
cmodel <- cgeneric(
  model = "iid", n = 10,
  param = prior.par)

## prior summaries: sigma and log-precision
(lamb <- -log(prior.par[2])/prior.par[1])
(smedian <- qexp(0.5, lamb))
(smean <- 1/lamb)

## mode: at the minimum of - log-prior
(lpmode <- optimize(function(x)
  -prior(cmodel, theta = x),
  c(-10, 30))$minimum)
## mean: integral of x*f(x)dx
(lpmean <- integrate(function(x)
  exp(prior(cmodel, theta = matrix(x, 1)))*x,
  -10, 30)$value)

## prior visualization: log(precision) and sigma
par(mfrow = c(1, 2))
plot(function(x)
 exp(prior(cmodel, theta = matrix(x, nrow=1))),
  -3, 3, n = 601, xlab = "log-precision",
  ylab = "density")
abline(v = lpmode, lwd = 3, col = 2)
rug(-2*log(smedian), lwd = 3, col = 3)
rug(lpmean, lwd = 3, col = 4)
plot(function(x)
 exp(prior(cmodel,
  theta = matrix(
    -2*log(x),
    nrow = 1))+log(2)-log(x)),
  1/100, 10, n = 1000,
  xlab = expression(sigma),
  ylab = "density")
plot(function(x) dexp(x, lamb),
   1/100, 10, n = 1000,
   add = TRUE, lty = 2, col = 2)
rug(smedian, lwd = 3, col = 3)
rug(smean, lwd = 3, col = 4)

par(old.par)

Run the code above in your browser using DataLab