inla.cgeneric.debugger is an R package designed to assist in debugging and working with INLA (Integrated Nested Laplace Approximation) CGeneric models. It provides utility functions for preparing data structures, calling dynamic CGeneric models, and building precision matrices.
You can install the package directly from GitHub:
devtools::install_github("vpnsctl/inla.cgeneric.debugger")
Load the package into your R session:
library(inla.cgeneric.debugger)
This package is particularly useful for debugging CGeneric models in INLA, allowing for flexible data preparation and dynamic library integration. It provides the following key functions:
- call_dynamic_cgeneric_model(): Dynamically calls a CGeneric model’s shared library with options for verbose output.
- build_Q_cgeneric(): Constructs the full precision matrix from a sparse representation.
The call_dynamic_cgeneric_model
function is used to dynamically call a CGeneric model’s shared library. It is especially helpful for debugging because it allows you to see the data passed to the C function and outputs the prints from the CGeneric functions.
result <- call_dynamic_cgeneric_model(
model,
cmd,
theta,
verbose = FALSE
)
- model: A model object returned by the
INLA::inla.cgeneric.define()
function. This object contains the CGeneric model definition, including the shared library path, model name, and data. - cmd: A command string specifying the operation ("void", "Q", "graph", "mu", "initial", "log_norm_const", "log_prior", "quit").
- theta: A numeric vector of parameters for the CGeneric model.
- verbose: A logical value. If
TRUE
, the function will display the data passed to the C function, which is useful for debugging. IfFALSE
, it will return the command results and show the prints from the CGeneric functions.
- Debugging Aid: By setting
verbose = TRUE
, you can inspect the data being passed to the C function, making it easier to identify issues. - Direct C Function Calls: The function directly calls the C function without invoking INLA, which makes it faster and more efficient for testing and debugging.
- Flexible Input: Accepts a model object from
INLA::inla.cgeneric.define()
, ensuring compatibility with INLA-defined models.
# Load required packages
library(fmesher)
library(inlabru)
library(rSPDE)
# Generate a 2D mesh
n_loc <- 20
loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2)
mesh_2d <- fm_mesh_2d(
loc = loc_2d_mesh,
cutoff = 0.01,
max.edge = c(0.1, 0.5)
)
# Define an anisotropic model
model_aniso <- rspde.anistropic2d(mesh = mesh_2d)
# Call the CGeneric model with verbose output to see data passed to C
Q_inla_format <- call_dynamic_cgeneric_model(
model_aniso,
cmd = "Q",
theta = c(-1, -1, 0, 0, 0),
verbose = TRUE
)
# Call the CGeneric model without verbose output to get function prints
graph <- call_dynamic_cgeneric_model(
model_aniso,
cmd = "graph",
theta = c(-1, -1, 0, 0, 0),
verbose = FALSE
)
Constructs the full precision matrix from a sparse representation returned by a CGeneric model.
Q_sparse <- build_Q_cgeneric(Q, graph)
- Q: Vector containing non-zero entries of the precision matrix (in INLA’s internal format), obtained from
call_dynamic_cgeneric_model()
withcmd = "Q"
. - graph: Graph representation (row, column indices, and counts), obtained from
call_dynamic_cgeneric_model()
withcmd = "graph"
.
Using the Q and graph computed previously:
# Build the sparse precision matrix from the sparse representation
Q_sparse <- build_Q_cgeneric(Q_inla_format, graph)
- The function returns a sparse matrix (
dgCMatrix
class) representing the precision matrix Q. call_dynamic_cgeneric_model()
withcmd = "Q"
returns Q in INLA’s internal format, whichbuild_Q_cgeneric()
converts into a standard sparse matrix.
Below is an illustrative example of how to use the package in combination with other packages like fmesher
, inlabru
, and rSPDE
.
library(fmesher)
n_loc <- 20
loc_2d_mesh <- matrix(runif(n_loc * 2), n_loc, 2)
mesh_2d <- fm_mesh_2d(
loc = loc_2d_mesh,
cutoff = 0.01,
max.edge = c(0.1, 0.5)
)
library(inlabru)
library(rSPDE)
model_aniso <- rspde.anistropic2d(mesh = mesh_2d)
library(inla.cgeneric.debugger)
# Call with verbose output to see data passed to C
Q_inla_format <- call_dynamic_cgeneric_model(
model_aniso,
cmd = "Q",
theta = c(-1, -1, 0, 0, 0),
verbose = TRUE
)
# Call without verbose output to get function prints
graph <- call_dynamic_cgeneric_model(
model_aniso,
cmd = "graph",
theta = c(-1, -1, 0, 0, 0),
verbose = FALSE
)
# Build the sparse precision matrix from the sparse representation
Q_sparse <- build_Q_cgeneric(Q_inla_format, graph)
The variable Q_sparse
now contains the sparse precision matrix that can be used for further computations or analyses.
- Compatibility: The package is designed to work seamlessly with INLA and other related packages.
- Efficiency: Directly calls the C function, making it faster by avoiding unnecessary overhead.
- Ease of Debugging: The
verbose
argument incall_dynamic_cgeneric_model
provides flexibility in debugging by controlling the level of output.
- Alexandre Simas
- David Bolin
This package is licensed under the GPL-3 license.
Feel free to explore the package and utilize its functions to enhance your debugging and modeling workflow with INLA CGeneric models.