Feature: spatially explicit detectability covariates through a new cov.surface interface#99
Open
hambrecht wants to merge 3 commits into
Conversation
- add cov.surface support to Detectability and make.detectability - apply raster covariates in calculate.scale.param - validate cov.surface in simulation checks - add raster covariate test coverage - update related simulation/covariate paths used by tests
Update Detectability plot validation so covariates supplied via cov.surface are treated as valid even when absent from population covariates. Add raster-specific plotting path that reads surface values and uses raster quantiles to generate representative detection curves, enabling mixed covariate plotting (for example size + canopy) without false missing-covariate errors.
There was a problem hiding this comment.
Pull request overview
Adds spatially explicit detectability covariates via a new cov.surface interface, allowing detectability scale to depend on raster values sampled at each individual’s simulated location (optionally combined with existing individual-level covariates).
Changes:
- Added
cov.surfacesupport toDetectability/make.detectability, including validation and plotting support for mixed covariates. - Updated
calculate.scale.param()to sample raster covariates at animal locations and incorporate them into the log-linear scale calculation. - Added a dedicated raster covariate test suite and adjusted an existing test to use the internal namespaced helper.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
DESCRIPTION |
Adds terra as an imported dependency to support raster covariates. |
R/Detectability.R |
Extends Detectability with cov.surface, adds validation, and updates plotting to handle raster covariates. |
R/ClassConstructors.R |
Extends make.detectability() API and documents the new cov.surface parameter. |
R/calculate.scale.param.R |
Extracts raster values at individual locations and uses them as covariates in scale parameter computation. |
R/check.simulation.R |
Adds simulation-level consistency check for cov.surface names vs cov.param. |
tests/testthat/test-check_RasterCovariate.R |
Adds comprehensive tests for raster covariate behavior and validation. |
tests/testthat/test-check_IndividualCovariates.R |
Updates test to call dsims:::calculate.scale.param() explicitly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+49
to
+55
| if(any(is.na(vals))){ | ||
| warning(paste0("NA raster values for surface covariate '", sn, | ||
| "' at some animal locations (outside raster extent). ", | ||
| "Replacing with global raster mean."), | ||
| call. = FALSE, immediate. = TRUE) | ||
| vals[is.na(vals)] <- mean(vals, na.rm = TRUE) | ||
| } |
Comment on lines
+46
to
+48
| cell_ids <- terra::cellFromXY(surf, pts) | ||
| rast_vals <- terra::values(surf)[, 1] | ||
| vals <- rast_vals[cell_ids] |
Comment on lines
+146
to
+147
| } | ||
| } |
Comment on lines
+231
to
+240
| if(is.character(cov.surface)){ | ||
| cov.surface <- terra::rast(cov.surface) | ||
| } | ||
| surf.values <- terra::values(cov.surface)[,1] | ||
| surf.values <- surf.values[!is.na(surf.values)] | ||
| if(length(surf.values) == 0){ | ||
| quantiles <- c(0, 0, 0) | ||
| }else{ | ||
| quantiles <- as.numeric(quantile(surf.values, c(0.025, 0.5, 0.975))) | ||
| } |
| #' Use a negative \code{cov.param} slope to model lower detectability where raster values | ||
| #' are higher (e.g. denser canopy cover reduces detection range). | ||
| #' Animal locations outside the raster extent or with NA cell values are replaced with the | ||
| #' raster mean and a warning is issued. |
Updated the @slot cov.param docs to reflect the actual allowed shapes (numeric vectors for continuous/discrete + data.frames for categorical levels). Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Author
|
@copilot apply changes based on the comments in this thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for spatially explicit detectability covariates through a new cov.surface interface.
Users can now provide canopy (or other environmental) rasters and combine them with existing individual covariates in a single detectability model.
What Changed
Behavior
Detection scale now supports mixed effects of individual and spatial covariates using:
log(sigma_i) = log(sigma_0) + sum(beta_k * z_ik)
where z_ik may come from:
Validation and Errors
Tests
Notes
This PR is scoped to raster covariate detectability support and related fixes needed for end-to-end simulation and plotting workflows.
Related to Issue 96