plot_residuals: Plotting of Dunn-Smyth Residuals

Description Usage Arguments Details Value Functions References Examples

Description

Plotting of Dunn-Smyth Residuals

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
plot_residuals.residual(
  residuals,
  covar,
  plotfunction = facet_species_covariate,
  aggregatefcn = mean,
  ...
)

facet_species_covariate(data, ...)

facet_covariate(data, ...)

plot_residuals_detection.fit(
  fit,
  detectionresiduals = NULL,
  varidx = NULL,
  esttype = NULL,
  aggregatefcn = mean,
  ...
)

plot_residuals_occupancy.fit(
  fit,
  occupancyresidual = NULL,
  varidx = NULL,
  esttype = NULL,
  conditionalLV = TRUE,
  aggregatefcn = mean,
  ...
)

Arguments

residuals

is a table returned by ds_occupancy_residuals.fit or ds_detection_residuals.fit. Each row is a unique ModelSite. It must have a column named 'ModelSite', all other columns are assumed contain residuals, and the column name corresponds to species names.

covar

Is a table of covariate values, it must have a column labelled 'ModelSite' that gives the ModelSite of covariate value. Rows with duplicated ModelSite values are averaged, which is in keeping with Warton et al for detection residuals.

plotfunction

A plotting method to use. Default is facet_species_covariate.

...

Extra arguments to pass. Currently ignored (no extra arguments accepted).

data

Input tibble. Columns of Species, Residual, Covariate and CovariateValue

fit

The fitted runjags object.

detectionresiduals

Optional. A tibble of already calculated detection residuals. Must have column names identical to output of ds_detection_residuals.fit. If not supplied then detection residuals are computed from fit using ds_detection_residuals.fit.

varidx

The index of the covariate to plot. If NULL, then all occupancy covariates will be plotted

esttype

The point estimate extracted from fit. Passed to ds_detection_residuals.fit as argument type.

occupancyresiduals

Optional. A tibble of already calculated occupancy residuals. Must have column names identical to output of ds_detection_residuals.fit. If not supplied then detection residuals are computed from fit using ds_detection_residuals.fit.

Details

The inclusion of a smoothed fit for easier interpretability follows suggestions by Warton et al (2017)

Value

A ggplot object. Data is in the data slot.

A ggplot object.

A ggplot object.

A ggplot object. Data is saved in the data slot.

A ggplot object. Data is saved in the data slot.

Functions

References

D. I. Warton, J. Stoklosa, G. Guillera-Arroita, D. I. MacKenzie, and A. H. Welsh, "Graphical diagnostics for occupancy models with imperfect detection," Methods in Ecology and Evolution, vol. 8, no. 4, pp. 408-419, 2017, doi: 10.1111/2041-210X.12761.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fit <- readRDS("./tmpdata/7_1_mcmcchain_20200424.rds")
fit <- runjags::add.summary(fit)
source("./R/calcpredictions.R")
source("./R/DS_residuals.R")
fit$data <- as_list_format(fit$data)
detection_resids <- ds_detection_residuals.fit(fit, type = "median", conditionalLV = FALSE)

# Plot Detection Residual 
plt <- plot_residuals_detection.fit(fit, detection_resids, varidx = 2, aggregatefcn = max, conditionalLV = FALSE)
plt
plt + coord_cartesian(ylim = c(-1, +1))
plot_residuals_detection.fit(fit, varidx = 2, esttype = "median", conditionalLV = FALSE)
plot_residuals_detection.fit(fit, varidx = c(2, 3), esttype = "median",
  plotfunction = facet_covariate, conditionalLV = FALSE) + 
  coord_cartesian(ylim = c(-1, +1))

plot_residuals_occupancy.fit(fit, varidx = 3, esttype = "median", conditionalLV = FALSE)
plot_residuals_occupancy.fit(fit, varidx = c(2, 3), esttype = "median",
 plotfunction = facet_covariate, conditionalLV = FALSE)

# Residuals against an unincluded covariate:
source("./scripts/7_1_import_site_observations.R")
covar <- occ_covariates[ , "ms_cover", drop = FALSE] %>%
  tibble::rowid_to_column(var = "ModelSite")
residuals <- ds_occupancy_residuals.fit(fit, type = "median", conditionalLV = FALSE)
pltobj <- plot_residuals.residual(residuals, covar, 
                 plotfunction = facet_species_covariate)
pltobj + scale_y_continuous(name = "Occupancy Residuals") 
pltobj + scale_y_continuous(name = "Occupancy Residuals") + coord_cartesian(ylim = c(-1, +1))

## Example using 7_2_1 data
data_7_2_1 <- readRDS("./private/data/clean/7_2_1_input_data.rds")
fitp <- readRDS("./tmpdata/7_2_2_detectiononly_smallAmodel_run_20200529.rds")
fit <- results.jags("./runjagsfiles_13", read.monitor = c("LV", fitp$monitor))
fit <- add.summary(fit)
covar <- data_7_2_1$plotsmerged_detection %>%
  dplyr::select(-all_of(data_7_2_1$detection_data_specieslist)) %>%
  dplyr::select_if(is.numeric) %>%
  rename(ModelSite = ModelSiteID)
plot_residuals_detection.fit(fit, varidx = 2, esttype = "median", conditionalLV = TRUE) + coord_cartesian(ylim = c(-1, 1))
plt <- plot_LVvscovar.fit(fit, esttype = "median", covar = covar, aggregatefcn = min)
plt + coord_cartesian(ylim = c(-1, 1))

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