Learn R Programming

GHRmodel

Overview

The GHRmodel package supports modeling health outcomes using Bayesian hierarchical spatio-temporal models with complex covariate effects (e.g., linear, non-linear, interactions, distributed lag linear and non-linear models) in the R-INLA framework. It is designed to help users identify key drivers and predictors of disease risk by enabling streamlined model exploration, comparison, and visualization of complex covariate effects.

The image below shows the structure of the GHRmodel package, outlining its functions (in blue), GHRmodel-specific output objects (in purple), generic output objects (in grey), and general functionality. Generic output objects can be provided directly by the user or can be generated using GHRmodel helper functions.

The GHRmodel package is designed to work in tandem with other packages of the GHRtools suite: GHRexplore, which facilitates data exploration and visualization, and GHRpredict, which computes out-of-sample probabilistic predictions of models developed in GHRmodel and enables predictive performance evaluation.

More information about the toolkit, with tutorials and published examples can be found at this website.

Installation

The latest version of the GHRmodel package is hosted on CRAN and can by installed using the following commands:

# Install from CRAN
install.packages("GHRmodel") 

# Get the development version from Gitlab
library(devtools)
devtools::install_git('https://earth.bsc.es/gitlab/ghr/ghrmodel.git')

GHRmodel depends on the INLA package, which is not available on CRAN. Instead, it must be installed from its own repository as follows:

 install.packages("INLA",repos=c(getOption("repos"),
                  INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE) 

In case you run into any issues, please check the installation instructions on the R-INLA website.

Usage

To fit a model with R-INLA, formulas need to follow its required syntax and structural conventions. INLA-compatible model formulas can be developed using either:

  • User-defined INLA-compatible input, which may consist of either user-defined

covariate lists or user-defined formula lists.

  • GHRmodel helper functions that allow the user to pre-process and transform covariates

and streamline writing INLA-compatible formulas.

In this example the user provides a list of INLA-compatible model formulas. These are passed to the as_GHRformulas() function to be converted into a standardized GHRformulas object. This ensures consistent output structure and random effect specification across models that can be interpreted by the function fit_models() for model fitting.

library("GHRmodel")

# Convert list of user-defined INLA formulas into a GHRformulas object 
formulas_user_ghr <- as_GHRformulas(c(
  
  # Model 1: random effects only, where monthly random effect is replicated by meso region and the spatial random effect is replicated by year
    "dengue_cases ~ 1 +
     f(month_id, model = 'rw1', replicate = spat_meso_id, cyclic = TRUE, constr = TRUE, hyper = prior_t) +
     f(year_id, model = 'iid', constr = TRUE, hyper = prior_t) +
     f(spat_id, model = 'bym2', graph = g, constr = TRUE, hyper = prior_sp, replicate = year_id2)",
    
  # Model 2: random effects and a varying effect for pdsi lag 1 by climate zone
  "dengue_cases ~ 1 + f(main_climate_f, pdsi.l1, model = 'iid') +
     f(month_id, model = 'rw1', replicate = spat_meso_id, cyclic = TRUE, constr = TRUE, hyper = prior_t) +
     f(year_id, model = 'iid', constr = TRUE, hyper = prior_t) +
     f(spat_id, model = 'bym2', graph = g, constr = TRUE, hyper = prior_sp, replicate = year_id2)",

  # Model 3: random effects and a 3-way interaction between different pdsi and tmin lags
  "dengue_cases ~ 1 + pdsi.l1 + tmin.l3 + pdsi.l6 + pdsi.l1:tmin.l3:pdsi.l6 +
     f(month_id, model = 'rw1', replicate = spat_meso_id, cyclic = TRUE, constr = TRUE, hyper = prior_t) +
     f(year_id, model = 'iid', constr = TRUE, hyper = prior_t) +
     f(spat_id, model = 'bym2', graph = g, constr = TRUE, hyper = prior_sp, replicate = year_id2)"
))

The fit_models() function allows users to fit a set of INLA-compatible model formulas defined by a GHRformulas object to a provided data set. It automates model fitting, extraction of outputs, and computation of a wide range of goodness-of-fit (GoF) metrics. The output is a GHRmodels object.

# User-defined INLA-compatible formulas can be passed into fit_models() as a GHRformulas object
model_user <- fit_models(
  formulas = formulas_user_ghr,
  data = data,
  family = "nbinomial",           # Negative binomial likelihood
  name = "mod",                   # Label prefix for each model
  offset = "population",          # Offset variable to account for population size
  control_compute = list(
    config = FALSE,               # Do not posterior predictive distribution
    vcov = FALSE                  # Do not return variance-covariance matrix
  ),
  pb = TRUE,                      # Display progress bar
  nthreads = 8                    # Use 8 threads for parallel computation
)

For model evaluation, GHRmodel provides a range of functions for model diagnostics, interpretation and evaluation. Functions with the prefix plot_* return graphical ggplot2 or cowplot objects, allowing users to easily customize visual outputs.

# Plot any linear coefficients found in the fitted model results. 
plot_coef_lin(
  model = model_user,              # Provide fitted model GHRmodels object
  exp = TRUE,                      # Exponentiate coefficients to relative risk scale
  title = "Relative Risk (RR)"     # Plot title
)

Developers

Giovenale Moirano, PhD
Barcelona Supercomputing Center
Global Health Resilience

Carles Milà, PhD
Barcelona Supercomputing Center
Global Health Resilience

Anna B. Kawiecki, PhD
Barcelona Supercomputing Center
Global Health Resilience

Rachel Lowe, PhD
Barcelona Supercomputing Center
Global Health Resilience (Group leader)

Copy Link

Version

Install

install.packages('GHRmodel')

Monthly Downloads

172

Version

0.1.1

License

GPL (>= 2)

Maintainer

Carles Milà

Last Published

November 7th, 2025

Functions in GHRmodel (0.1.1)

extract_names

Extract Covariate Names
plot_coef_crosspred

Plot crosspred Objects: Overall, Slices, or Heatmap
plot_coef_lin

Produce a Forest Plot of Linear Covariates from a GHRmodels Object
lag_cov

Generate lagged variables for one or more lags
map_MS

Administrative Map for Municipalities in the Mato Grosso do Sul
onebasis_inla

Create a One-Dimensional Basis for INLA
get_covariates

Retrieve Covariates from a GHRmodels Object as a List of Character Vectors
fit_models

Fit Multiple INLA Models
dengue_SP

Dengue cases from the "São Paulo" state of Brazil
dengue_MS

Dengue cases from the "Mato Grosso do Sul" state of Brazil
plot_fit

Plot Observed vs. Fitted Cases
rank_models

Rank Models by Goodness-of-Fit
subset_models

Subset GHRmodels Objects
plot_gof

Plot Models by Goodness-of-Fit
plot_ppd

Plot Posterior Predictive Densities Versus Observed Data
sample_ppd

Sample from the Posterior Predictive Distribution
plot_re

Plot Random Effects
stack_models

Merge GHRmodels
plot_coef_nl

Plot Nonlinear Effects from a GHRmodels Object
plot_coef_varying

Produce a Forest Plot for a Spatially or Temporally Varying Effects from a GHRmodels object.
write_inla_formulas

Generate INLA-compatible Model Formulas
crosspred_inla

Generate DLNM Predictions from GHRmodels Objects
GHRmodel-package

GHRmodel: Bayesian Hierarchical Modelling of Spatio-Temporal Health Data
cov_add

Add Covariates to All Combinations
as_GHRformulas

Convert R-INLA Model Formulas into a GHRformulas Object
cov_interact

Generate Interaction Terms Between Covariates
cov_multi

Create Covariate Combinations Across Groups
cov_nl

Create Non-Linear Effects for INLA
crossbasis_inla

Create a Two-Dimensional INLA-compatible Cross-basis Matrix
cov_varying

Create Spatially or Temporally Varying Effects for INLA
cov_uni

Build Univariable Covariate Sets