prep_uhc | R Documentation |
Creates data used to make used-habitat calibration plots
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)
object |
|
test_dat |
|
n_samp |
|
n_dens |
|
verbose |
|
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.
Returns a list
of class uhc_data
with elements:
orig
: List of data.frame
s, 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.frame
s, 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.
Brian J. Smith
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 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
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.