-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to predict detection probabilities for observations on specific distances? #189
Comments
@wlangera This isn't actually a bug, instead it is predicting detection probability within the covered region, integrating over all distances < truncation. When I have wanted to calculate detection probability at a given distance, in the face of covariates in the detection function (as you have), I have resorted to writing my own code to do so. Here is an example of the function, and its use:
Apply function to the selected
|
It occured to me that there must be code to do this already, as used in the plot.ds function. It turns out to be the
There may be a still more elegant method that doesn't require you to know which row contains which covariate! I also wonder if this might not make a nice article (vignette) at some point. (Also it took me a while to work out where the |
@erex @lenthomas thanks for your quick replies! I will test the code you provided. I was not sure if I was using the wrong function or the |
Probably would be handy to have an example such as this kicking around in our vignettes/documentation. I'll think about it. Unfortunately both |
Hi @wlangera, good suggestion regarding extending In the meantime, we could add a bit to the documentation for This doesn't preclude also having a more extensive example @erex. Regarding which package to put the vignette in -- how about (in any case) we add a web-only article to both the |
capturing the elements of this issue and placing them into a nascent .qmd vignette for further development |
Somewhat better; half-normal for detection probability; cleaner way to identify |
This version interrogates the design matrix for proper index to pass to |
Newest version: using
|
Thanks to your quick replies I was able to write a simple function that produces what I want :) predict_det_probs <- function(ds_model, ...) {
# Extract data
data <- ds_model$ddf$ds$aux$ddfobj$xmat
# Extract ddf object
ddf_object <- ds_model$ddf$ds$aux$ddfobj
# Calculate observation specific detection probabilities
det_probs <- sapply(seq_len(nrow(data)), function(i) {
mrds::detfct(
distance = data$distance[i],
ddfobj = ddf_object,
index = i,
...)
})
# Add detection probabilities to dataframe
data$det_prob <- det_probs
return(data)
} Example: # Load required library
library(Distance)
# Set seed for reproducibility
set.seed(123)
# Simulate dataset
n <- 200 # Number of observations
truncation_distance <- 250 # Maximum detection distance (truncation)
# Generate distances with a higher probability of being detected at shorter distances
distances <- rexp(n, rate = 0.01) # Exponential distribution for decreasing probability
distances <- distances[distances <= truncation_distance] # Truncate at 250 meters
# Add a categorical covariate (e.g., habitat type)
habitat <- sample(c("forest", "grassland"), length(distances), replace = TRUE)
# Combine into a data frame
data <- data.frame(
distance = distances,
habitat = habitat
)
# View the first few rows of the simulated data
head(data)
#> distance habitat
#> 1 84.345726 forest
#> 2 57.661027 grassland
#> 3 132.905487 forest
#> 4 3.157736 forest
#> 5 5.621098 forest
#> 6 31.650122 forest
# Fit a detection function using the Distance package
ds_model <- ds(
data = data,
truncation = truncation_distance,
key = "hn", # Half-normal key function
adjustment = NULL, # No adjustment terms
formula = ~habitat # Include habitat as a cov
)
#> Fitting half-normal key function
#> AIC= 1952.067
#> No survey area information supplied, only estimating detection function.
# Predict detection probability for custom distances
predict_df <- predict_det_probs(ds_model)
head(predict_df)
#> object habitat distance binned observer detected det_prob
#> 1 1 forest 84.345726 FALSE 1 1 0.6678533
#> 2 2 grassland 57.661027 FALSE 1 1 0.8852882
#> 3 3 forest 132.905487 FALSE 1 1 0.3670289
#> 4 4 forest 3.157736 FALSE 1 1 0.9994344
#> 5 5 forest 5.621098 FALSE 1 1 0.9982087
#> 6 6 forest 31.650122 FALSE 1 1 0.9447434 Created on 2024-12-02 with reprex v2.1.1 |
Of course this only returns predicted values and no measure of uncertainty ... |
Hi
I want to get detection probabilities for observations on specific distances, but I only manage to predict average detection probabilities.
Is there any way to do this?
In this example you see that I get the same value for 50 m and 150 m although the detection probability clearly decreases over distance.
Created on 2024-11-28 with reprex v2.1.1
The text was updated successfully, but these errors were encountered: