Title: | Bayesian Inference for Avian Moult Phenology Models |
---|---|
Description: | Precompiled Stan models to conduct full Bayesian inference in regression models for the phenology of avian primary moult based on the approach of Underhill & Zucchini (1988) <DOI:10.1111/j.1474-919X.1988.tb00993.x> and related hierarchical models to accommodate repeated-measures data (within-season recaptures of individuals). |
Authors: | Philipp Boersch-Supan [aut, cre]
|
Maintainer: | Philipp Boersch-Supan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-01-24 06:09:46 UTC |
Source: | https://github.com/pboesu/moultmcmc |
Precompiled Stan models to conduct full Bayesian inference in regression models for the phenology of avian primary moult based on the approach of Underhill & Zucchini (1988) and related hierarchical models to accommodate repeated-measures data (within-season recaptures of individuals).
Boersch-Supan et al. (2022) Bayesian inference for models of moult duration and timing in birds arXiv DOI:10.48550/arXiv.2205.12120
Underhill & Zucchini (1988) A model for avian primary moult. Ibis 130:358 DOI:10.1111/j.1474-919X.1988.tb00993.x
Stan Development Team (2022). RStan: the R interface to Stan. R package version 2.21.5. https://mc-stan.org
Visual comparison of moult models
compare_plot(..., names = NULL)
compare_plot(..., names = NULL)
... |
two or a moult or moultmcmc model |
names |
optional character vector of model names |
a plot comparing parameter estimates and their uncertainties
This is a helper function to format input data for the Type 1+2 moult model.
consolidate_moult_records(moult_score, moult_cat)
consolidate_moult_records(moult_score, moult_cat)
moult_score |
a numeric vector of (linearized) moult scores in [0,1] (0 = old plumage,1 = new plumage). |
moult_cat |
a numeric vector of categorical moult codes (1 = old plumage,2 = moulting, 3 = new plumage) |
a numeric vector of scores and categorical records with values [0,1] for old, new and continuous active moult scores, and value 2 for categorical active moult records.
Extract the population-level ('fixed') effects
from a moultmcmc
object.
## S3 method for class 'moultmcmc' fixef(object, summary = TRUE, probs = c(0.025, 0.975), pars = NULL, ...)
## S3 method for class 'moultmcmc' fixef(object, summary = TRUE, probs = c(0.025, 0.975), pars = NULL, ...)
object |
a moultmcmc model |
summary |
logical, should posterior samples be summarised for each parameter |
probs |
numeric, desired quantiles for summary statistics |
pars |
Optional names of coefficients to extract. By default, all coefficients are extracted. |
... |
Currently ignored. |
If summary
is TRUE
, a matrix for the population-level effects.
If summary
is FALSE
, a matrix with one row per
posterior draw and one column per population-level effect.
Efficient approximate leave-one-out cross-validation (LOO)
## S3 method for class 'moultmcmc' loo(x, cores = getOption("mc.cores", 1))
## S3 method for class 'moultmcmc' loo(x, cores = getOption("mc.cores", 1))
x |
A moultmcmc model object |
cores |
The number of cores to use for parallelization. This defaults to the option mc.cores which can be set for an entire R session by options(mc.cores = NUMBER). The old option loo.cores is now deprecated but will be given precedence over mc.cores until loo.cores is removed in a future release. As of version 2.0.0 the default is now 1 core if mc.cores is not set, but we recommend using as many (or close to as many) cores as possible. Note for Windows 10 users: it is strongly recommended to avoid using the .Rprofile file to set mc.cores (using the cores argument or setting mc.cores interactively or in a script is fine). |
The loo() methods return a named list with class c("psis_loo", "loo"). See ?loo::loo
moult_plot generic
moult_plot(...)
moult_plot(...)
... |
... |
...
Moult plot for moult model
## S3 method for class 'moult' moult_plot(x, prob = 0.95, plot_data = TRUE, plot = TRUE, ...)
## S3 method for class 'moult' moult_plot(x, prob = 0.95, plot_data = TRUE, plot = TRUE, ...)
x |
moult model object created with moult::moult |
prob |
coverage probability of active moult in the sampled population |
plot_data |
logical, if TRUE plot observations |
plot |
logical, if TRUE (default) return a plot, else return a dataframe with start and end dates of moult based on model intercepts |
... |
not currently used |
a plot or data.frame
Moult plot for moultmcmc model
## S3 method for class 'moultmcmc' moult_plot( x, prob = 0.95, prob_ci = NULL, plot_data = TRUE, plot = TRUE, newdata = NULL, col.data = NULL, ... )
## S3 method for class 'moultmcmc' moult_plot( x, prob = 0.95, prob_ci = NULL, plot_data = TRUE, plot = TRUE, newdata = NULL, col.data = NULL, ... )
x |
moultmcmc model object |
prob |
coverage probability of active moult in the sampled population |
prob_ci |
coverage probability of credible interval |
plot_data |
logical, if TRUE plot observations |
plot |
logical, if TRUE (default) return a plot, else return a dataframe with start and end dates of moult based on model intercepts |
newdata |
optional data.frame of conditions to plot (passed to predict.moultmcmc) |
col.data |
optional, name of column in model data to colour data points |
... |
not currently used |
a plot or data.frame
Bayesian inference for Underhill-Zucchini moult models and expansions
moultmcmc( moult_column, date_column, id_column = NULL, start_formula = ~1, duration_formula = ~1, sigma_formula = ~1, type = 2, lump_non_moult = FALSE, data, init = "auto", flat_prior = TRUE, beta_sd = 0, log_lik = FALSE, use_phi_approx = FALSE, active_moult_recaps_only = TRUE, same_sigma = FALSE, ... )
moultmcmc( moult_column, date_column, id_column = NULL, start_formula = ~1, duration_formula = ~1, sigma_formula = ~1, type = 2, lump_non_moult = FALSE, data, init = "auto", flat_prior = TRUE, beta_sd = 0, log_lik = FALSE, use_phi_approx = FALSE, active_moult_recaps_only = TRUE, same_sigma = FALSE, ... )
moult_column |
the name the column in |
date_column |
the name the column in |
id_column |
(optional) factor identifier. Usually a season-individual combination to encode within-season recaptures, defaults to NULL. When provided moultmcmc will attempt to fit the relevant recaptures model. |
start_formula |
model formula for start date |
duration_formula |
model formula for duration |
sigma_formula |
model formula for start date sigma |
type |
integer (one of 1,2,3,4,5,12) referring to type of moult data and consequently model to be fitted (see details) |
lump_non_moult |
logical; should pre- and post-moult observations be treated as indistinguishable? if TRUE and type %in% c(1,2,12), the relevant lumped model will be fitted (see details). |
data |
Input data frame must contain a numeric column "date" and a column "moult_cat" which is a numeric vector of categorical moult codes (1 = old plumage,2 = moulting,3 = new plumage). |
init |
Specification of initial values for all or some parameters. Can be the string "auto" for an automatic guess based on the data, or any of the permitted |
flat_prior |
use uniform prior on start date and duration (TRUE) or vaguely informative truncated normal prior (FALSE). Defaults to TRUE. |
beta_sd |
use zero-centred normal priors for regression coefficients other than intercepts? If <= 0 the stan default of improper flat priors is used. |
log_lik |
boolean retain pointwise log-likelihood in output? This enables model assessment and selection via the loo package. Defaults to FALSE, can lead to very large output arrays when sample size is large. |
use_phi_approx |
logical flag whether to use stan's Phi_approx function to calculate the "old" likelihoods |
active_moult_recaps_only |
logical flag whether to ignore repeated observations outside the active moult phase |
same_sigma |
logical flag, currently unused |
... |
Arguments passed to |
type refers to the type of moult data available (see Underhill and Zucchini (1998) and Underhill, Zucchini and Summers (1990)).
type = 1 sample is representative of entire population (not yet moulting, in moult, and birds which have completed moult). For type 1 data, any value between 0 and 1 (> 0 and < 1) can be used as the moult index for birds in active moult. The value used does not matter, only the fact that they are in moult. type = 2 (default) sample is representative of entire population (not yet moulting, in moult, and birds which have completed moult). Moult scores are required.
type = 3 sample is representative only of birds in moult. Individuals with moult scores 0 or 1 are ignored.
type = 4 sample is representative only of birds in moult and those that have completed moult. Individuals with moult scores 0 are ignored.
type = 5 sample is representative only of birds that have not started moult or that are in moult. Individuals with moult scores 1 are ignored.
An object of class stanfit
returned by rstan::sampling
original version by Ben Bolker: https://stackoverflow.com/a/16951524
namedList(...)
namedList(...)
... |
args |
a named list
Predict method for moultmcmc models
## S3 method for class 'moultmcmc' predict( object, newdata = NULL, predict.type = "parameters", summary = TRUE, intervals = 0.1, prob = 0.95, ... )
## S3 method for class 'moultmcmc' predict( object, newdata = NULL, predict.type = "parameters", summary = TRUE, intervals = 0.1, prob = 0.95, ... )
object |
a fitted moultmcmc model |
newdata |
data.frame with explanatory variables for which to make predictions |
predict.type |
specifies form of predictions, see details. |
summary |
logical, if TRUE (default) return a data.frame of posterior means, otherwise return a list of arrays of the full posterior sample of the predicted quantity (with one list element per predicted quantity and array dimensions nrow(newdata) *number or posterior draws) |
intervals |
not currently used |
prob |
nominal coverage probability of credible interval |
... |
further arguments |
a data.frame or list, depending on input arguments
Extract the individual-level ('random') effects
from a moultmcmc
object.
## S3 method for class 'moultmcmc' ranef(object, summary = TRUE, probs = c(0.025, 0.975), pars = NULL, ...)
## S3 method for class 'moultmcmc' ranef(object, summary = TRUE, probs = c(0.025, 0.975), pars = NULL, ...)
object |
a moultmcmc model |
summary |
logical, should posterior samples be summarised for each parameter |
probs |
numeric, desired quantiles for summary statistics |
pars |
Optional names of coefficients to extract. By default, all coefficients are extracted. |
... |
Currently ignored. |
If summary
is TRUE
, a matrix for the individual-level effects.
If summary
is FALSE
, a matrix with one row per
posterior draw and one column per individual-level effect.
A dataset containing the moult scores and capture dates for a simulated population of passerines including recaptures
recaptures
recaptures
A data frame with 299 rows and 13 variables:
individual identifier
number of observations for the said individual
individual moult initiation date
individual duration
simulation set
observation date (days since January 01)
true PFMG at sampling occasion
PFMG incorporating measurement error, if any
descriptor of sampling bias type, if any
proportion of individuals with more than 1 capture
relative population standard deviation of the moult duration
measurement error
simulation scenario
P. Boersch-Supan
A dataset containing the moult scores and capture dates for a simulated population of passerines including recaptures. Simulation is based on an underlying population moult start date of 196.83, moult duration of 77.80 days and a population standard deviation of the start date of 8.1 days.
recaptures2
recaptures2
A data frame with 299 rows and 13 variables:
individual identifier
number of observations for the said individual
individual moult initiation date
individual duration
simulation set
observation date (days since January 01)
true PFMG at sampling occasion
PFMG incorporating measurement error, if any
descriptor of sampling bias type, if any
proportion of individuals with more than 1 capture
relative population standard deviation of the moult duration
measurement error
simulation scenario
P. Boersch-Supan
residual_plot generic
residual_plot(...)
residual_plot(...)
... |
... |
...
This function displays a plot that shows where observed dates and moult scores fall relative to the predictions of the fitted model. Active moult observations should generally fall within +/- 3 start-date standard deviations of the regression line.
## S3 method for class 'moult' residual_plot(x, plot = TRUE, ...)
## S3 method for class 'moult' residual_plot(x, plot = TRUE, ...)
x |
moult model object created with moult::moult |
plot |
logical, if TRUE (default) return a plot, else return a data.frame with calculated quantities |
... |
not currently used |
a plot
A dataset containing simulated moult scores and capture dates of Eurasian Siskins (Spinus spinus), based on a sub-urban Scottish population.
siskins
siskins
A data frame with 299 rows and 3 variables:
sampling date (days since Jan 01)
PFMG at sampling occasion
individual identifier
"true" individual start date
"true" individual moult duration
Philipp Boersch-Supan
Insley et al. (in prep). Breeding and moult phenology of siskins in the Scottish Highlands.
Summary table generic
summary_table(...)
summary_table(...)
... |
... |
...
Summary Table for moult model
## S3 method for class 'moult' summary_table(x, prob = 0.95, tidy_names = TRUE, ...)
## S3 method for class 'moult' summary_table(x, prob = 0.95, tidy_names = TRUE, ...)
x |
moult model object created with moult::moult |
prob |
nominal coverage probability of confidence interval |
tidy_names |
adjust default parameter names from moult() to follow consistent nomenclature of model.matrix() |
... |
not currently used |
a tibble
## Not run: m1 <- moult::moult(Mindex ~ Day, data = sanderlings) ## Not run: summary_table(m1)
## Not run: m1 <- moult::moult(Mindex ~ Day, data = sanderlings) ## Not run: summary_table(m1)
Summary Table
## S3 method for class 'moultmcmc' summary_table( x, pars = x$stanfit@sim$pars_oi, prob = 0.95, include = TRUE, ... )
## S3 method for class 'moultmcmc' summary_table( x, pars = x$stanfit@sim$pars_oi, prob = 0.95, include = TRUE, ... )
x |
moultmcmc fit object |
pars |
A character vector of parameter names. The default is all parameters for which samples are saved. If include = FALSE, then the specified parameters are excluded from the printed summary. |
prob |
nominal coverage probability of credible interval |
include |
Logical scalar (defaulting to TRUE) indicating whether to include or exclude the parameters named by the pars argument. |
... |
Additional arguments passed to the summary method for stanfit objects. |
a tibble
Summary method for moultmcmc models
## S3 method for class 'moultmcmc' summary(object, ...)
## S3 method for class 'moultmcmc' summary(object, ...)
object |
a moultmcmc object |
... |
passed to summary_table |
a summary table
A dataset containing the moult scores and capture dates for Southern Masked Weavers (Ploceus velatus) from the Western Cape, South Africa.
weavers_processed
weavers_processed
A data frame with 5282 rows and 6 variables:
sampling date (calendar date)
SAFRING sex code
Sampling Year
feather scores for primaries
Proportion of feather mass grown
sampling date (days since August 01 of each year)
Erni et al. 2013
Oschadleus 2005, Patterns of primary moult in the weavers, Ploceidae. University of Cape Town.