prep_uhc: Prepare Data for UHC Plots for a Fitted Model

View source: R/uhc_plots.R

prep_uhcR Documentation

Prepare Data for UHC Plots for a Fitted Model

Description

Creates data used to make used-habitat calibration plots

Usage

prep_uhc(object, test_dat, n_samp = 1000, n_dens = 512, verbose = TRUE)

## S3 method for class 'glm'
prep_uhc(object, test_dat, n_samp = 1000, n_dens = 512, verbose = TRUE)

## S3 method for class 'fit_logit'
prep_uhc(object, test_dat, n_samp = 1000, n_dens = 512, verbose = TRUE)

## S3 method for class 'fit_clogit'
prep_uhc(object, test_dat, n_samp = 1000, n_dens = 512, verbose = TRUE)

Arguments

object

⁠[glm, fit_logit, fit_clogit]⁠
A fitted RSF or (i)SSF model. Should be fit to training dataset separate from the testing data.

test_dat

⁠[data.frame]⁠
A data.frame with testing data from which to sample test points. Should be separate from the data used to train the model passed to object.

n_samp

⁠[numeric = 1000]⁠ A vector of length 1 giving the number of samples to use to characterize the used habitat distribution under the model.

n_dens

⁠[numeric = 512]⁠ A numeric vector of length 1 giving the number of equally spaced points at which density (used, available, and sampled) is estimated. Passed to stats::density.default(), which indicates that n should usually be specified as a power of 2.

verbose

⁠[logical]⁠ Should messages be displayed (TRUE) or not (FALSE)?

Details

This function performs the heavy lifting of creating UHC plots. It creates the data used later by the plot() method, which actually draws the UHC plots. This function (1) creates density plots of the used and available locations from the test data, and (2) resamples the (a) fitted coefficients and (b) test data (weighted by the exponential habitat selection function) to create the distribution of used habitat under the model.

Note that test_dat should contain at least all of the variables that appear in the model object. Any further habitat variables in test_dat will also have UHC plots generated, treating these variables as possible candidate variables that are simply not included in this particular model.

Value

Returns a list of class uhc_data with elements:

  • orig: List of data.frames, one per variable (see vars). Each data.frame contains the density plot data (x and y) for the original used (dist == "U") and available (dist == "A") data.

  • samp: List of data.frames, one per variable (see vars). Each data.frame contains the density plot data (x and y) for each iteration of bootstrap resampling (iter).

  • vars: Character vector with names of the habitat variables for which to create UHC plots.

  • type: Named character vector with the type for each of vars (either "numeric" or "factor").

  • resp: Character vector of length 1 with the name of the response variable.

Author(s)

Brian J. Smith

References

Fieberg, J.R., Forester, J.D., Street, G.M., Johnson, D.H., ArchMiller, A.A., and Matthiopoulos, J. 2018. Used-habitat calibration plots: A new procedure for validating species distribution, resource selection, and step-selection models. Ecography 41:737–752.

See Also

See Fieberg et al. 2018 for details about UHC plots.

Default plotting method available: plot.uhc_data()

Coercion to data.frame: as.data.frame.uhc_data()

Subsetting method: Extract.uhc_data

Examples



# Load packages
library(amt)
library(dplyr)
library(terra)
library(sf)

# HSF ----------------------------------------------
# Load data
data(uhc_hsf_locs)
data(uhc_hab)
hab <- rast(uhc_hab, type = "xyz", crs = "epsg:32612")
# Convert "cover" layer to factor
levels(hab[[4]]) <- data.frame(id = 1:3,
                               cover = c("grass", "forest", "wetland"))

# Split into train (80%) and test (20%)
set.seed(1)
uhc_hsf_locs$train <- rbinom(n = nrow(uhc_hsf_locs),
                             size = 1, prob = 0.8)
train <- uhc_hsf_locs[uhc_hsf_locs$train == 1, ]
test <- uhc_hsf_locs[uhc_hsf_locs$train == 0, ]

# Available locations
avail_train <- random_points(st_as_sf(st_as_sfc(st_bbox(hab))),
                             n = nrow(train) * 10)

avail_test <- random_points(st_as_sf(st_as_sfc(st_bbox(hab))),
                            n = nrow(test) * 10)

# Combine with used
train_dat <- train |>
  make_track(x, y, crs = 32612) |>
  mutate(case_ = TRUE) |>
  bind_rows(avail_train) |>
  # Attach covariates
  extract_covariates(hab) |>
  # Assign large weights to available
  mutate(weight = case_when(
    case_ ~ 1,
    !case_ ~ 5000
  ))

test_dat <- test |>
  make_track(x, y, crs = 32612) |>
  mutate(case_ = TRUE) |>
  bind_rows(avail_test) |>
  # Attach covariates
  extract_covariates(hab) |>
  # Assign large weights to available
  mutate(weight = case_when(
    case_ ~ 1,
    !case_ ~ 5000
  ))

# Fit (correct) HSF
hsf1 <- glm(case_ ~ forage + temp + I(temp^2) + pred + cover,
            data = train_dat, family = binomial(), weights = weight)

# Drop weights from 'test_dat'
test_dat$weight <- NULL

# Prep UHC plots
uhc_dat <- prep_uhc(object = hsf1, test_dat = test_dat,
                    n_samp = 500, verbose = TRUE)

# Plot all variables
plot(uhc_dat)

# Plot only first variable
plot(uhc_dat[1])

# Plot only "cover" variable
plot(uhc_dat["cover"])

# Coerce to data.frame
df <- as.data.frame(uhc_dat)

# Simplify sampled lines to confidence envelopes
conf <- conf_envelope(df)

# Default plot for the envelopes version
plot(conf)



amt documentation built on June 25, 2024, 1:14 a.m.