Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Hex Sticker and Website #47

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ LICENSE.md
codecov.yml
^doc$
^Meta$
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.github$
^README\.Rmd$
49 changes: 49 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from /~https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at /~https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
release:
types: [published]
workflow_dispatch:

name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
folder: docs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ data-raw/

# Output from vignettes
figure/
docs
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ Suggests:
VignetteBuilder: knitr
Config/testthat/edition: 3
LazyData: true
URL: https://traitecoevo.github.io/hmde/
BugReports: /~https://github.com/traitecoevo/austraits/issues
87 changes: 87 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
output: github_document
---

# hmde <img src="man/figures/hmde_hex.png" align="right" alt="" width="220" />

The goal of `hmde` 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](https://mc-stan.org/) via [RStan](https://mc-stan.org/users/interfaces/rstan), built under [R](https://cran.r-project.org/) 4.3.3. The inbuilt models are based on case studies in ecology. The hierarchical Bayesian longitudinal method was first introduced in O'Brien et al., [2024](https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.14463).

As `hmde` is first intended for biologists, the initial set of vignettes (`hmde`, `constant-growth`, `von-bertalanffy`, and `canham`) are written aimed at an audience more interested in applications than the underlying theory. A vignette for the more mathematically interested is under development.

## The Maths

The general use case is to estimate a vector of parameters <span>$\boldsymbol{\theta}$</span> 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](https://doi.org/10.1890/1051-0761(2006)016%5B0540:NAOCTC%5D2.0.CO;2))
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](/~https://github.com/) with:

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

## Quick demo

Create constant growth data with measurement error:

``` r
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:

``` r
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](/~https://github.com/traitecoevo/hmde/issues) with details of the bug. A [reprex](https://reprex.tidyverse.org/) would be particularly helpful with the bug-proofing process!
92 changes: 55 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,74 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# hmde

<!-- badges: start -->

[![R-CMD-check](/~https://github.com/traitecoevo/hmde/actions/workflows/R-CMD-check.yaml/badge.svg)](/~https://github.com/traitecoevo/hmde/actions/workflows/R-CMD-check.yaml)
[![Codecov test
coverage](https://codecov.io/gh/traitecoevo/hmde/branch/master/graph/badge.svg)](https://app.codecov.io/gh/traitecoevo/hmde?branch=master)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
<!-- badges: end -->

The goal of hmde 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](https://mc-stan.org/) via [RStan](https://mc-stan.org/users/interfaces/rstan), built under [R](https://cran.r-project.org/) 4.3.3. The inbuilt models are based on case studies in ecology.
# hmde <img src="man/figures/hmde_hex.png" align="right" alt="" width="220" />

The goal of `hmde` 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](https://mc-stan.org/) via
[RStan](https://mc-stan.org/users/interfaces/rstan), built under
[R](https://cran.r-project.org/) 4.3.3. The inbuilt models are based on
case studies in ecology. The hierarchical Bayesian longitudinal method
was first introduced in O’Brien et al.,
[2024](https://besjournals.onlinelibrary.wiley.com/doi/10.1111/2041-210X.14463).

As `hmde` is first intended for biologists, the initial set of vignettes
(`hmde`, `constant-growth`, `von-bertalanffy`, and `canham`) are written
aimed at an audience more interested in applications than the underlying
theory. A vignette for the more mathematically interested is under
development.

## 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}$$
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.
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.
`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.

### 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.
$$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.
$$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](https://doi.org/10.1890/1051-0761(2006)016%5B0540:NAOCTC%5D2.0.CO;2))
al. 2004](https://doi.org/10.1890/1051-0761(2006)016%5B0540:NAOCTC%5D2.0.CO;2))
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.
$$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.

##

Expand All @@ -73,12 +87,13 @@ remotes::install_github("traitecoevo/hmde")
Create constant growth data with measurement error:

``` r
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.
$$s_{ij} \sim \mathcal{N}\left( 0, \sigma_e \right)$$ blows up as
$\sigma_e$ approaches 0.

Fit the model:

Expand All @@ -95,4 +110,7 @@ constant_fit <- hmde_model("constant_single_ind") |>

## Found a bug?

Please submit a [GitHub issue](/~https://github.com/traitecoevo/hmde/issues) with details of the bug. A [reprex](https://reprex.tidyverse.org/) would be particularly helpful with the bug-proofing process!
Please submit a [GitHub
issue](/~https://github.com/traitecoevo/hmde/issues) with details of the
bug. A [reprex](https://reprex.tidyverse.org/) would be particularly
helpful with the bug-proofing process!
26 changes: 26 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
url: https://traitecoevo.github.io/hmde/
template:
bootstrap: 5
bootswatch: flatly

navbar:
structure:
left: [intro, reference, articles]
right: [search, github]
components:
articles:
text: Articles
menu:
- text: "Case Studies"
- text: Constant growth model
href: 'articles/constant-growth.html'
- text: von Bertalanffy model
href: 'articles/von-bertalanffy.html'
- text: Canham model
href: 'articles/canham.html'
github:
icon: fab fa-github fa-lg
href: /~https://github.com/traitecoevo/hmde/



39 changes: 39 additions & 0 deletions inst/figures/hex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(hexSticker)

measurement_data_transformed <- Tree_Size_Ests$measurement_data %>%
group_by(ind_id) %>%
mutate(
delta_y_obs = y_obs - lag(y_obs),
obs_interval = time - lag(time),
obs_growth_rate = delta_y_obs/obs_interval,
delta_y_est = y_hat - lag(y_hat),
est_growth_rate = delta_y_est/obs_interval
) %>%
ungroup()

#Plots of size over time for a sample of 5 individuals
sample_ids <- sample(1:nrow(Tree_Size_Ests$individual_data), size=5)
plot_data <- measurement_data_transformed %>%
filter(ind_id %in% sample_ids)

plot <- ggplot(data=plot_data, aes(group = ind_id)) +
geom_point(aes(x = time, y=y_obs, colour = as.factor(ind_id)),
shape = 1) +
geom_line(aes(x = time, y=y_obs, colour = as.factor(ind_id)),
linetype = "dashed") +
geom_point(aes(x = time, y=y_hat, colour = as.factor(ind_id)),
shape = 2) +
geom_line(aes(x = time, y=y_hat, colour = as.factor(ind_id)),
linetype = "solid") +
labs(x="Time (years)", y="DBH (cm)", colour="Ind. ID") +
theme_void() +
theme(legend.position = "none")

hexSticker::sticker(plot, package="hmde",
p_color = "white",
p_size=40,
p_y = 1,
s_x=1, s_y=1,
s_width=1.35, s_height = 1.35,
h_fill = "navy", h_color = "white",
filename="inst/figures/hmde_hex.png")
Binary file added inst/figures/hmde_hex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions man/figures/hex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(hexSticker)

measurement_data_transformed <- Tree_Size_Ests$measurement_data %>%
group_by(ind_id) %>%
mutate(
delta_y_obs = y_obs - lag(y_obs),
obs_interval = time - lag(time),
obs_growth_rate = delta_y_obs/obs_interval,
delta_y_est = y_hat - lag(y_hat),
est_growth_rate = delta_y_est/obs_interval
) %>%
ungroup()

#Plots of size over time for a sample of 5 individuals
sample_ids <- sample(1:nrow(Tree_Size_Ests$individual_data), size=5)
plot_data <- measurement_data_transformed %>%
filter(ind_id %in% sample_ids)

plot <- ggplot(data=plot_data, aes(group = ind_id)) +
geom_point(aes(x = time, y=y_obs, colour = as.factor(ind_id)),
shape = 1) +
geom_line(aes(x = time, y=y_obs, colour = as.factor(ind_id)),
linetype = "dashed") +
geom_point(aes(x = time, y=y_hat, colour = as.factor(ind_id)),
shape = 2) +
geom_line(aes(x = time, y=y_hat, colour = as.factor(ind_id)),
linetype = "solid") +
labs(x="Time (years)", y="DBH (cm)", colour="Ind. ID") +
theme_void() +
theme(legend.position = "none")

hexSticker::sticker(plot, package="hmde",
p_color = "white",
p_size=40,
p_y = 1,
s_x=1, s_y=1,
s_width=1.35, s_height = 1.35,
h_fill = "navy", h_color = "white",
filename="inst/figures/hmde_hex.png")
Binary file added man/figures/hmde_hex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading