Hi everyone,
I am creating single season single species occupancy models and have ran into problems when I try to create plots with occupancy probability on the y-axis and a covariate of interest that is contained in my model on the x-axis. I had no issues graphing detection covariates, but when I moved to occupancy covariates whenever I create a plot (using the code below) the lower bound confidence interval is essentially 0, and the upper bound confidence interval is essentially 1.
For context, my top model has date for the detection covariate, and habitat type (a categorical variable with 6 levels), number of fires, high development percentage, and open water percentage for the occupancy covariates.
Code:
# Calculate the mean Julian date for detection covariates (ignoring NA values)
mean_date <- 140.1
# Generate a sequence of TotalFires values from 0 to 25 (example range)
total_fires <- seq(0, 12, length.out = 100)
# Calculate the mean values for other covariates (ignoring NA values)
mean_HighDevelopment_800m <- mean(data1$HighDevelopment_800m, na.rm = TRUE)
mean_OpenWater_800m <- mean(data1$OpenWater_800m, na.rm = TRUE)
# Get the unique levels of HabitatType
habitat_levels <- unique(data1$HabitatType)
# Initialize storage for predictions
all_predictions <- data.frame(
TotalFires = total_fires,
Predicted = rep(0, length(total_fires)),
lower = rep(0, length(total_fires)),
upper = rep(0, length(total_fires))
)
# Loop over each habitat type to generate predictions and accumulate them
for (habitat in habitat_levels) {
new_data <- data.frame(
date = mean(mean_date), # Using the overall mean of mean_date
HabitatType = factor(rep(habitat, length(total_fires)), levels = habitat_levels),
HighDevelopment_800m = mean_HighDevelopment_800m,
OpenWater_800m = mean_OpenWater_800m,
TotalFires = total_fires
)
pred_prob <- predict(bestmodel, type = "state", newdata = new_data)
all_predictions$Predicted <- all_predictions$Predicted + pred_prob$Predicted
all_predictions$lower <- all_predictions$lower + pred_prob$lower
all_predictions$upper <- all_predictions$upper + pred_prob$upper
}
# Average the predictions across all habitat types
all_predictions$Predicted <- all_predictions$Predicted / length(habitat_levels)
all_predictions$lower <- all_predictions$lower / length(habitat_levels)
all_predictions$upper <- all_predictions$upper / length(habitat_levels)
# Plot the predicted occupancy probabilities against TotalFires
plot(total_fires, all_predictions$Predicted, type = "l",
xlim = c(0, 12), ylim = c(0, 1),
xlab = "Total Fires", ylab = "Occupancy Probability")
# Add confidence interval lines
lines(total_fires, all_predictions$lower, lty = 2, lwd = 1)
lines(total_fires, all_predictions$upper, lty = 2, lwd = 1)
This is my first time doing anything in R so I am definitely a beginner and have been relying on many tutorials to get to the point, so any help is greatly appreciated. Thank you!