predsumspecies: Expected Biodiversity

Description Usage Arguments Details Value Functions Examples

View source: R/expected_biodiversity.R

Description

The expected number of species occupying a ModelSite, or the expected number of species detected at a ModelSite.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
expectedspeciesnum.ModelSite.theta(
  Xocc,
  Xobs = NULL,
  u.b,
  v.b = NULL,
  lv.coef = NULL,
  LVvals = NULL
)

predsumspecies(
  fit,
  desiredspecies = fit$species,
  chains = NULL,
  UseFittedLV = TRUE,
  nLVsim = 1000,
  type = "median",
  cl = NULL
)

predsumspecies_raw(
  Xocc,
  Xobs = NULL,
  ModelSite = NULL,
  numspeciesinmodel,
  desiredspecies = 1:numspeciesinmodel,
  nlv,
  draws,
  useLVindraws = TRUE,
  nLVsim = NULL,
  cl = NULL
)

predsumspecies_newdata(
  fit,
  Xocc,
  Xobs = NULL,
  ModelSiteVars = NULL,
  desiredspecies = fit$species,
  chains = NULL,
  nLVsim = 1000,
  type = "marginal",
  cl = NULL
)

Arguments

Xocc

A matrix of occupancy covariates, each row is a ModelSite

Xobs

A matrix of detection covariates. Each row is a visit. The visited ModelSite (row of Xocc) is given by ModelSite

LVvals

A matrix of LV values. Each column corresponds to a LV. To condition on specific LV values, provide a matrix of row 1.

desiredspecies

List of species to sum over. Names must match names in fit$species. Default is to sum over all species.

chains

The chains of MCMC to use. Default is all chains.

UseFittedLV

If TRUE the fitted LV variables are used, if false then 1000 LV values are simulated.

nLVsim

The number of simulated LV values if not using fitted LV values (only applies if useLVindraws = FALSE).

type

If "draws" then predictions are given per draw and a 3-array is returned with dimensions of draws, sites and statistical summaries. If "marginal" then the statistical summaries marginalise the posterior distribution and a matrix is returned with rows of statistical summaries and columns of sites. There will be four summaries: the expection and variance of the number of species occupied or detected. If "median" then the expected number of species occupying and observed is returned for the median of the posterior distribution, the variance and expected number of species, marginal over the posterior is also returned as they is useful for showing variation due to model parameter uncertainty.

cl

A cluster object created by parallel::makeCluster. If NULL no cluster is used.

ModelSite

is a list of integers giving the row in Xocc corresponding to a row in Xobs

numspeciesinmodel

Integer. The number of species in the model, which is needed to match up with the BUGS names in draws

draws

A matrix of posterior parameter draws. Each row is a draw. Column names follow the BUGS naming convention

useLVindraws

Use the LV values corresponding to each draw from within the draws object. If FALSE nLVsim simulated LV values will be used for each draw.

theta

A vector of model parameters, labelled according to the BUGS labelling convention seen in runjags

Details

No scaling or centering of Xocc or Xobs is performed by predsumspecies_raw

Value

A named vector of the expectation and variance of the numbers of species occupying the ModelSite and given parameter set. If observational covariates are supplied then the expection and variance of numbers of species detected is also returned.

A matrix or 3 array with each column a ModelSite. Dimensions are labelled. The statistical summaries returned are the predicted expection and variance of the number of species occupied or detected. These expectations are with respect to the full posterior distribution of the model parameters, with the exception of the LV values which depends on UseFittedLV.

A 3-dimensional array. Dimensions are draws, sites and statistical summaries of the number of species random variables. There will be four summaries: the expection and variance of the number of species occupied or detected.

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fit <- readRDS("./tmpdata/7_2_9_addyear_msnm_year_time_2lv.rds")
theta <- get_theta(fit, type = 1)
Xocc <- fit$data$Xocc[2, , drop = FALSE]
Xobs <- fit$data$Xobs[fit$data$ModelSite == 2, , drop = FALSE]
numspecies <- fit$data$n
lvsim <- matrix(rnorm(2 * 1), ncol = 2, nrow = 2) #dummy lvsim vars
lv.coef.bugs <- matrix2bugsvar(matrix(0, nrow = fit$data$n, ncol = 2), "lv.coef")
theta <- c(theta, lv.coef.bugs)

Enumspec <- predsumspecies(fit, UseFittedLV = TRUE, return = "median")


indata <- readRDS("./private/data/clean/7_2_10_input_data.rds")
predsumspecies_newdata(fit, Xocc = indata$holdoutdata$Xocc, Xobs = indata$holdoutdata$yXobs, ModelSiteVars = "ModelSiteID", return = "median", cl = NULL)
draws_sites_summaries <- predsumspecies_newdata(fit, 
                                                Xocc = indata$holdoutdata$Xocc,
                                                Xobs = indata$holdoutdata$yXobs,
                                                ModelSiteVars = "ModelSiteID",
                                                bydraw = TRUE,
                                                cl = NULL)
# Median expected biodiversity with 95% credible intervals for expected biodiversity
Enumspec_quantiles_drawssitessummaries(draws_sites_summaries, probs  = c(0.025, 0.5, 0.0975))

# approximate 95% posterior density interval for sum of species detected using Gaussian approximation and variance.
numspec_interval <- numspec_posteriorinterval_Gaussian_approx(draws_sites_summaries)

sustainablefarms/linking-data documentation built on Oct. 28, 2020, 2:41 a.m.