Package 'sbd'

Title: Size Biased Distributions
Description: Fitting and plotting parametric or non-parametric size-biased non-negative distributions, with optional covariates if parametric. Rowcliffe, M. et al. (2016) <doi:10.1002/rse2.17>.
Authors: Marcus Rowcliffe [aut, cre]
Maintainer: Marcus Rowcliffe <[email protected]>
License: GPL-3
Version: 0.1.0
Built: 2024-10-25 04:46:42 UTC
Source: https://github.com/marcusrowcliffe/sbd

Help Index


Get AIC of size biased model

Description

Extracts the Akaike Information Criterion from a fitted parametric size biased distribution model.

Usage

## S3 method for class 'sbm'
AIC(object, ..., k = 2)

Arguments

object

A size biased distribution model of class sbm.

...

Optional additional sbm objects.

k

Numeric, the penalty per parameter to be used; the default k = 2 is the classical AIC.

Value

A dataframe of AIC values, NA if the model is non-parametric (i.e. fitted with pdf = "none").

Examples

data(BCI_speed_data)
  lmod_null <- sbm(speed~1, BCI_speed_data, pdf="lnorm")
  lmod_mass <- sbm(speed~mass, BCI_speed_data, pdf="lnorm")
  AIC(lmod_null, lmod_mass)

Animal speed data

Description

A set of animal speed observations recorded by camera traps on Barro Colorado Island, Panama (Rowcliffe et al. 2016).

Format

A dataframe with 2158 rows and 3 variables:

species

character species common names

speed

numeric travel speed observations, unit metres per second

mass

numeric species average bodey mas, unit kg

Source

https://zslpublications.onlinelibrary.wiley.com/doi/full/10.1002/rse2.17

References

Rowcliffe, J.M., Jansen, P.A., Kays, R., Kranstauber, B., and Carbone, C. (2016). Wildlife speed cameras: measuring animal travel speed and day range using camera traps. Remote Sensing in Ecology and Conservation 2, 84-94.


Size biased gamma probability density

Description

Calculates size biased gamma probability density - the underlying gamma probability density when the likelihood of recording an observation is proportional to its value.

Usage

dsbgamma(x, lmean, lrate, log = FALSE, xlog = FALSE)

Arguments

x

A vector of positive numbers.

lmean

The mean of the distribution on the log scale.

lrate

The log rate parameter of the underlying gamma distribution.

log

A logical indicating whether to return log densities.

xlog

A logical indicating whether to return densities for log transformed data.

Value

A vector of probability densities.

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  dsbgamma(agoutiData$speed, 0, 0.1)

Size biased log-normal probability density

Description

Calculates size biased log-normal probability density - the underlying log-normal probability density when the likelihood of recording an observation is proportional to its value.

Usage

dsblnorm(x, lmean, lsig, log = FALSE, xlog = FALSE)

Arguments

x

A vector of positive numbers.

lmean

The mean of the distribution on the log scale.

lsig

The standard deviation of the distribution on the log scale.

log

A logical indicating whether to return log densities.

xlog

A logical indicating whether to return densities for log transformed data.

Value

A vector of probability densities.

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  dsblnorm(agoutiData$speed, 0, 0.1)

Size biased Weibull probability density

Description

Calculates size biased Weibull probability density - the underlying Weibull probability density when the likelihood of recording an observation is proportional to its value.

Usage

dsbweibull(x, lmean, lshape, log = FALSE, xlog = FALSE)

Arguments

x

A vector of positive numbers.

lmean

The mean of the distribution on the log scale.

lshape

The log shape parameter of the underlying Weibull distribution.

log

A logical indicating whether to return log densities.

xlog

A logical indicating whether to return densities for log transformed data.

Value

A vector of probability densities.

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  dsbweibull(agoutiData$speed, 0, 0.1)

Plot size biased distribution

Description

Plots the frequency histogram of data from a size biased model, with fitted parametric distribution line if the model is parametric without covariates.

Usage

## S3 method for class 'sbm'
hist(x, log = TRUE, add = FALSE, breaks = 20, lpar = list(col = "red"), ...)

Arguments

x

A size biased model fit of class sbm.

log

A logical specifying whether to plot the log transformed or untransformed data distribution.

add

A logical specifying whether to create a new plot, or add a parametric distribution curve to an existing plot.

breaks

Integer defining number of breaks when generating histogram (passed to hist).

lpar

A list of plotting parameters controlling appearance of the density curve line (if present - passed to lines).

...

Additional parameters passed to plot, including those affecting histogram appearance.

Value

None.

Examples

# Fit and plot log-normal and Weibull models
  data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  lmod <- sbm(speed~1, agoutiData, pdf="lnorm")
  wmod <- sbm(speed~1, agoutiData, pdf="weibull")
  hist(lmod, breaks = 40)
  hist(wmod, add=TRUE, lpar=list(col="blue"))

Harmonic Mean

Description

Calculates harmonic mean and its standard error.

Usage

hmean(x, na.rm = TRUE)

Arguments

x

A vector of positive numbers.

na.rm

A logical (TRUE or FALSE) indicating whether to strip out missing values before computing.

Value

A list with values mean and se.

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  hmean(agoutiData$speed)

Make new data for prediction

Description

Creates or converts a dataframe of covariates to pass to predict.sbm, based on the data used in the model to be used for prediction.

Usage

make_newdata(formula, data, newdata = NULL)

Arguments

formula

A two-sided formula of the form response ~ covariate + ...

data

A dataframe containing the fields named in formula.

newdata

A dataframe of covariate values with fields matching variables in formula.

Details

When newdata is missing, a new dataframe is created with numeric variables held at their mean values, and all combinations of factors. When a newdata dataframe is provided it is checked for compatibility with formula variables, and character variables are converted to factors, but otherwise passed unchanged.

Value

A dataframe of covariate values.

Examples

data(BCI_speed_data)
  make_newdata(speed ~ species, BCI_speed_data)

Predict estimates

Description

Generates predicted underlying averages from a size biased model, given a set of covariates if these are used in the model.

Usage

## S3 method for class 'sbm'
predict(object, newdata = NULL, reps = 999, ...)

Arguments

object

A size biased model fit of class sbm.

newdata

A dataframe of covariate values with fields matching covariates used in object.

reps

Integer giving the number of random draws for variance estimation.

...

Additional arguments (unused).

Details

When newdata is missing, make_table is used to generate a dataframe of covariates at which to predict, based on the model formula and covariate data.

Value

A dataframe of predictions with fields est (estimated average), se (estimated standard error), and lcl, ucl (lower and upper 95 percent confidence limits).

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")
  lmod_mass <- sbm(speed~mass, agoutiData, pdf="lnorm")
  nd <- data.frame(mass = c(1, 10, 100))
  predict(lmod_mass, nd)

Size Biased Distributions

Description

Fitting and plotting parametric or non-parametric size-biased non-negative distributions, with optional covariates in the case of parametric. Supports three parametric options, log-normal, Weibull, and gamma.

Details

The core function is sbm, which fits a model to non-negative observations to estimate the average of the underlying distribution assuming that the probability of making an observation is proportional to the size of that observation. The default gives a non-parametric fit (the harmonic mean), and three parametric options are also available: log-normal, Weibull, and gamma. Covariates can be included in parametric models. The output is a list of class sbm, which has methods plot, predict, summary, and AIC. The functions were developed to support the analysis of speed observations from camera trap data described by Rowcliffe et al. (2016).

Author(s)

Maintainer: Marcus Rowcliffe [email protected]

References

Patil, G. P. 2002 Weighted distributions. Pp. 2369–2377 in A.H. El-Shaarawi, W. W. Piegorsch, eds. Encycolpedia of Environmetrics. Wiley, Chichester.

Rowcliffe, J.M., Jansen, P.A., Kays, R., Kranstauber, B., and Carbone, C. (2016). Wildlife speed cameras: measuring animal travel speed and day range using camera traps. Remote Sensing in Ecology and Conservation 2, 84-94.

See Also

Useful links:


Fit a size biased model

Description

Fits a parametric or non-parametric size biased distribution model to a positive response variable.

Usage

sbm(
  formula,
  data,
  pdf = c("none", "lnorm", "gamma", "weibull"),
  var.range = c(-4, 4),
  trace = FALSE,
  ...
)

Arguments

formula

A two-sided formula of the form response ~ covariate + ...

data

A dataframe containing the fields named in formula.

pdf

A text value naming the probability density function to use.

var.range

The range of log variance within which to search when fitting parametric distributions.

trace

Logical defining whether to show diagnostic information when fitting parametric distributions (passed to mle2).

...

Arguments passed to predict.sbm (options: newdata, reps).

Details

Response values must be strictly positive. To fit a distribution without covariates use 1 on the right hand side of the formula. When pdf = "none", the harmonic mean and it's standard error are calculated, and no covariates can be used.

The contents of the the estimate component of the result depends on the type of model. When no covariates are used, it contains a single overall average estimate. When covariates are used and newdata = NULL, it contains one estimate per unique combination of factor covariate levels, with any quantitative covariates held at their mean values. When covariates are used and a dataframe with valid covariate fields is supplied to newdata, it replicates newdata appending averages estimated at the covariate values supplied.

Value

A list of class sbm with methods summary.sbm, predict.sbm, hist.sbm, and AIC.sbm. The list has elements:

"estimate"

A dataframe of estimated averages, their standard errors and 95% confidence limits.

"data"

A dataframe containing the data used to fit the model.

"model"

A model object of class mle2.

"formula"

The formula supplied to the function call.

"pdf"

Character string recording the probability density function used to fit the model.

Examples

data(BCI_speed_data)
  agoutiData <- subset(BCI_speed_data, species=="agouti")

  # harmonic mean estimate for agouti
  hmod <- sbm(speed~1, agoutiData)

  # lognormal estimate with or without a covariates
  lmod <- sbm(speed~1, agoutiData, pdf="lnorm")
  lmod_mass <- sbm(speed~mass, BCI_speed_data, pdf="lnorm")
  lmod_spp <- sbm(speed~species, BCI_speed_data, pdf="lnorm")

  # inspect estimates
  hmod$estimate
  lmod$estimate
  lmod_mass$estimate
  lmod_spp$estimate

Summarise a linear model

Description

For parametric models only, summarises the linear model coefficients from a an sbm object.

Usage

## S3 method for class 'sbm'
summary(object, ...)

Arguments

object

A size biased model fit of class sbm.

...

Additional arguments (unused).

Value

A dataframe with fields estimate, stdError, tValue, and pValue.

Examples

data(BCI_speed_data)
  mod <- sbm(speed~mass, BCI_speed_data, pdf="lnorm")
  summary(mod)