Giter VIP home page Giter VIP logo

rmot's Introduction

rmot

R-CMD-check Codecov test coverage Lifecycle: experimental

The goal of rmot is to implement 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.

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

Rmot 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.

Power law

The power law model is given by $$f \left( Y \left( t \right), \beta_0, \beta_1, \bar{Y} \right) = \frac{dY}{dt} = \beta_0 \bigg( \frac{Y \left( t \right)}{\bar{Y}} \bigg)^{\beta_1},$$ where $\beta_0>0$ is the coefficient, $\beta_1$ is the power, and $\bar{Y}$ is a user-provided parameter that centres the model in order to avoid correlation between the $\beta$ parameters.

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

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

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

Quick demo

Create constant growth data with measurement error:

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 <- rmot_model("constant_single_ind") |>
        rmot_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
        ) |>
        rmot_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!

rmot's People

Contributors

fontikar avatar tess-lacoil avatar dfalster avatar

Stargazers

José R. Ferrer Paris avatar

Watchers

 avatar  avatar

Forkers

fontikar

rmot's Issues

vB model run failures

https://github.com/traitecoevo/rmot/actions/runs/8785643601/job/24106637311#step:6:451

═ Failed tests ════════════════════════════════════════════════════════════════
── Failure ('test-rmot_models_vb.R:47:3'): Execution: vb single individual ─────
`par_ests` (`actual`) not equal to as.numeric(data$single_true_data$DE_pars[c(1, 2)]) (`expected`).

  `actual`: 2.7 7.2
`expected`: 0.1 7.2

Problem parameter is the growth coefficient. Doesn't always fail to estimate properly but every so often will.

Unit testing: object in global environment not being seen in testing.

Problem in testing for constant single and multiple individual models, y_single not seen despite being in global environment. Works just fine when run in interactive mode.

── Error ('test-rmot_models.R:41:5'): Execution and output: Constant single individual ──
<purrr_error_indexed/rlang_error/error/condition>
Error in `.f(.x[[i]], ...)`: i In index: 4.
i With name: time.
Caused by error in `.f()`:
! object 'y_single' not found
Backtrace:
     ▆
  1. ├─rmot::rmot_run(...) at test-rmot_models.R:41:4
  2. ├─rmot::rmot_assign_data(...) at rmot/R/rmot_run.R:18:2
  3. │ └─purrr::map(user_code, eval) at rmot/R/rmot_assign_data.R:21:2
  4. │   └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
  5. │     ├─purrr:::with_indexed_errors(...)
  6. │     │ └─base::withCallingHandlers(...)
  7. │     ├─purrr:::call_with_cleanup(...)
  8. │     └─base (local) .f(.x[[i]], ...)
  9. │       └─base (local) .f(.x[[i]], ...)
 10. └─base::.handleSimpleError(...)
 11.   └─purrr (local) h(simpleError(msg, call))
 12.     └─cli::cli_abort(...)
 13.       └─rlang::abort(...)

Case studies

Get data together for a case study per model. Data needs to be already published and publicly available.

Constant
Fish data, capture-recapture. Can handle individuals with 2 or more sizes over time.

Power law
Seedlings maybe.

von Bertalanffy
Use Fonti's lizard data from here

Canham
Use hirttr sample of 50 from BCI. Contact Rick to ask about uploading the prepared data with the model to be used in vignettes and the like.

Add documentation to package

A good R package will contain these 5 forms of documentation:

  1. README which serves as a landing page for whoever finds your repo
  2. Function/objection/package documentation e.g. ?mean()
  3. Vignettes/Long form documentation serves as lengthier explainer of how to use your R package, usually a "Get Started" tutorial and other tutorials for more complex use cases
  4. R package website

{rmot} already has the skeleton for 1-3
To learn more on how to create these : https://fontikar.github.io/DIY_Rpkg/#Document_it_-_Object_documentation_%E2%9C%85

I like to have a look at other packages and see how their write their documentation

For 4 check out: https://github.com/fontikar/DIY_Rpkg_pkgdown and https://fontikar.github.io/DIY_Rpkg_pkgdown/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.