Learn R Programming

hmde

The goal of hmde is to fit a model for the rate of change in some quantity based on a set of pre-defined functions arising from ecological applications. We estimate differential equation parameters from repeated observations of a process, such as growth rate parameters to data of sizes over time. In other language, hmde implements hierarchical Bayesian longitudinal models to solve the Bayesian inverse problem of estimating differential equation parameters based on repeat measurement surveys. Estimation is done using Markov Chain Monte Carlo, implemented through Stan via RStan, built under R 4.3.3. The inbuilt models are based on case studies in ecology.

A pre-print paper is available on bioarXiv, or as the hmde_paper.pdf file here.

The Maths

The general use case is to estimate a vector of parameters $\boldsymbol{\theta}$ for a chosen differential equation $$f\left( Y \left( t \right), \boldsymbol{\theta} \right) = \frac{dY}{dt}$$ based on the longitudinal structure $$Y \left( t_{j+1} \right) = Y\left( t_j \right) + \int_{t_j}^{t_{j+1}}f\left( Y \left( t \right), \boldsymbol{\theta} \right),dt. $$

The input data are observations of the form $y_{ij}$ for individual $i$ at time $t_j$, with repeated observations coming from the same individual. We parameterise $f$ at the individual level by estimating $\boldsymbol{\theta}i$ as the vector of parameters. We have hyper-parameters that determine the distribution of $\boldsymbol{\theta}i$ with typical prior distribution $$\boldsymbol{\theta}i \sim \log \mathcal{N}\left(\boldsymbol{\mu}{\log\left(\boldsymbol{\theta}\right)}, \boldsymbol{\sigma}{\log \left( \boldsymbol{\theta} \right)}\right), $$ where $\boldsymbol{\mu}{\log\left(\boldsymbol{\theta}\right)}$ and $\boldsymbol{\sigma}{\log\left(\boldsymbol{\theta}\right)}$ are vectors of means and standard deviations. In the case of a single individual, these are chosen prior values. In the case of a multi-individual model $\boldsymbol{\mu}{\log\left(\boldsymbol{\theta}\right)}$ and $\boldsymbol{\sigma}_{\log\left(\boldsymbol{\theta}\right)}$ have their own prior distributions and are fit to data.

Implemented Models

hmde comes with four DEs built and ready to go, each of which has a version for a single individual and multiple individuals.

Constant Model

The constant model is given by $$f \left( Y \left( t \right), \beta \right) = \frac{dY}{dt} = \beta,$$ and is best understood as describing the average rate of change over time.

von Bertalanffy

The von Bertalanffy mode is given by $$f \left( Y \left( t \right), \beta, Y_{max} \right) = \frac{dY}{dt} = \beta \left( Y_{max} - Y \left( t \right) \right),$$ where $\beta$ is the growth rate parameter and $Y_{max}$ is the maximum value that $Y$ takes.

Canham

The Canham (Canham et al. 2004) model is a hump-shaped function given by $$f \left( Y \left( t \right), f_{max}, Y_{max}, k \right) = \frac{dY}{dt} = f_{max} \exp \Bigg( -\frac{1}{2} \bigg( \frac{ \ln \left( Y \left( t \right) / Y_{max} \right) }{k} \bigg)^2 \Bigg), $$ where $f_{max}$ is the maximum growth rate, $Y_{max}$ is the $Y$-value at which that maximum occurs, and $k$ controls how narrow or wide the peak is.

Installation

‘hmde’ is under active development. You can install the current developmental version of ‘hmde’ from GitHub with:

# install.packages("remotes")
remotes::install_github("traitecoevo/hmde")

Quick demo

Create constant growth data with measurement error:

library(hmde)
y_obs <- seq(from=2, to=15, length.out=10) + rnorm(10, 0, 0.1)

Measurement error is necessary as otherwise the normal likelihood $$s_{ij} \sim \mathcal{N}\left( 0, \sigma_e \right)$$ blows up as $\sigma_e$ approaches 0.

Fit the model:

constant_fit <- hmde_model("constant_single_ind") |>
        hmde_assign_data(n_obs = 10,                  #Integer
                         y_obs = y_obs,               #vector length n_obs
                         obs_index = 1:10,            #vector length n_obs
                         time = 0:9,                  #Vector length n_obs
                         y_0_obs = y_obs[1]           #Real
        ) |>
        hmde_run(chains = 1, iter = 1000, verbose = FALSE, show_messages = FALSE)

Found a bug?

Please submit a GitHub issue with details of the bug. A reprex would be particularly helpful with the bug-proofing process!

Copy Link

Version

Install

install.packages('hmde')

Version

1.2.1

License

GPL (>= 3)

Maintainer

Tess O'Brien

Last Published

July 4th, 2025

Functions in hmde (1.2.1)

hmde_model_pars

Show parameter list for hmde supported model
hmde_model_des

Function to select DE given model name
hmde_plot_de_pieces

Plot pieces of chosen differential equation model for each individual. Structured to take the individual data tibble that is built by the hmde_extract_estimates function using the ind_par_name_mean estimates. Function piece will go from the first fitted size to the last. Accepted ggplot arguments will change the axis labels, title, line colour, alpha
hmde_plot_obs_est_inds

Plot estimated and observed values over time for a chosen number of individuals based on posterior estimates. Structured to take in the measurement_data tibble constructed by the hmde_extract_estimates function.
hmde_assign_data

Assign data to template for chosen model
hmde-package

The 'hmde' package.
hmde_affine_de

Differential equation for affine growth single individual model
Tree_Size_Data

Garcinia recondita - Barro Colorado Island data
Trout_Size_Data

SUSTAIN Salmo trutta data
hmde_const_de

Differential equation for constant growth single and multi- individual models
hmde_canham_de

Differential equation for Canham growth single and multi- individual models
hmde_extract_estimates

Extract samples and return measurement, individual, and population-level estimates
hmde_model_names

Returns names of available models.
hmde_model

Select data configuration template for hmde supported model
Tree_Size_Ests

Garcinia recondita model estimates - Barro Colorado Island data
Lizard_Size_Data

Skink size data - Lampropholis delicata
hmde_vb_de

Differential equation for von Bertalanffy growth single and multi- individual models
hmde_run

Run chosen pre-built model in Stan