set.seed(12345)
library(optimizeAPA)
library(ggplot2)

redNMix

redNMix is an R package for fitting N-mixture models. The package has several unique features, allowing for:

  1. grouped/coarse counts data
  2. parallel processing
  3. applications to large populations

The package can be installed from github:

remotes::install_github("mrparker909/redNMix")
library(redNMix)

Generate Data

There are helper functions for quickly generating N-mixtures data. These are particularily useful for testing and for simulation studies.

Closed Population

We generate a closed population with 3 sampling sites and 4 sampling occasions, with site abundance parameter lambda=5, and a probability of detection of 0.80.

pop1 = redNMix::gen_Nmix_closed(num_sites = 3,
                                num_times = 4,
                                lambda    = 5,
                                pdet      = 0.8)

pop1 contains two matrices, the first is the true population size for each sampling:

pop1$Ni

while the second matrix are the observed counts:

pop1$nit

Open Population

We generate an open population with 3 sampling sites and 4 sampling occasions, with initial abundance parameter lambda=5, recruitment parameter gamma=1, survival probability omega=0.25, and a probability of detection of 0.80.

pop2 = redNMix::gen_Nmix_open(num_sites = 3,
                              num_times = 4,
                              lambda    = 5,
                              gamma     = 1,
                              omega     = 0.25,
                              pdet      = 0.8)

pop2 contains two matrices, the first is the true population size for each sampling:

pop2$Ni

while the second matrix are the observed counts:

pop2$nit

Model Fitting

There are many options for model fitting, see the documentation in R for more examples (eg: ?redNMix::fit_red_Nmix_open).

Closed Population Example

out <- redNMix::fit_red_Nmix_closed(nit=pop1$nit, red=1, K=20)
out

The optimization algorithm converged after r out$steps iterations. The value of the likelihood function at its maximum is r out$f. The parameter estimates are shown under out$x, and need to be transformed back to their usual units; lambda was log transformed, so we need to exponentiate it; pdet was logit transformed, so we need to inverse logit it.

The lambda estimate is:

exp(out$x[1])

The probability of detection estimate is:

plogis(out$x[2])

The estimates are reasonably close to the true parameter values of 5 and 0.8.

If we want to inspect the convergence paths of the parameters, we should add keepValues=TRUE when model fitting:

out2 <- redNMix::fit_red_Nmix_closed(nit=pop1$nit, red=1, K=20, tolerance=10^-2, keepValues=TRUE)
optimizeAPA::plotConvergence(out2)

Notice that we also set the tolerance down from the default of 10^-6 to 10^-2. This reduced the number of iterations from r out$steps to r out2$steps. In this case the parameter estimates don't change much, so the lower tolerance suffices.

How to Cite

Parker, M.R.P. (2020). redNMix: An R package for N-mixtures models. R package version r packageVersion("redNMix"). https://github.com/mrparker909/redNMix



mrparker909/redNMix documentation built on April 4, 2020, 12:24 a.m.