Closed DEHewitt closed 8 months ago
Hi @DEHewitt,
Thanks for the note. That is not quite the correct way to generate a marginal effects plot. Instead of adding the SD to the values, you should instead calculate the 95% CI and use that. Here is an example of how to do that, which you should be able to adapt to your data set. Also note that instead of supplying dummy coordinates, it's better to convert the class to PGOcc, which will eliminate the need for supplying the coordinates matrix (and just set the spatial random effects to 0). I'm hoping to add in a function at some point that makes it less clunky to do that. Let me know if you have any other questions
rm(list = ls())
library(spOccupancy)
library(ggplot2)
# 1. Data prep ------------------------------------------------------------
# Read in the data source (reads in an object called data.goldfinch)
load(url("https://github.com/doserjef/spOccupancy_examples/raw/main/data/europeanGoldfinchSwiss.rda"))
str(data.goldfinch)
# 2. Model fitting --------------------------------------------------------
# Fit a spatial, single-species occupancy model using an NNGP
out.sp <- spPGOcc(occ.formula = ~ scale(elevation) + I(scale(elevation)^2) + scale(forest),
det.formula = ~ scale(date) + I(scale(date^2)) + scale(dur),
data = data.goldfinch,
n.batch = 400,
batch.length = 25,
NNGP = TRUE,
n.neighbors = 5,
n.thin = 10,
n.burn = 5000,
n.chains = 3,
n.report = 100)
summary(out.sp)
# 3. Prediction -----------------------------------------------------------
# Predict relationship with forest cover
# Create a set of values across the range of observed forest values
forest.pred.vals <- seq(min(data.goldfinch$occ.covs$forest),
max(data.goldfinch$occ.covs$forest),
length.out = 100)
# Standardize forest prediction values by those used to fit the model
forest.0 <- (forest.pred.vals - mean(data.goldfinch$occ.covs$forest)) /
sd(data.goldfinch$occ.covs$forest)
# Create prediction design matrix
# Notice I set the other covariates to 0 (which is their average value since I standardized them)
X.0 <- as.matrix(data.frame(intercept = 1, elevation = 0,
elevation.2 = 0, forest = forest.0))
# Convert out.sp object to have a class of PGOcc. This allows you to do prediction
# with a spatial occupancy model while setting the spatial random effect values to 0.
class(out.sp)
class(out.sp) <- "PGOcc"
# Predict at new locations
out.pred <- predict(out.sp, X.0)
# Convert the class of out.sp back to spPGOcc to avoid any future problems
class(out.sp) <- 'spPGOcc'
str(out.pred)
psi.0.quants <- apply(out.pred$psi.0.samples, 2, quantile,
prob = c(0.025, 0.5, 0.975))
psi.plot.dat <- data.frame(psi.med = psi.0.quants[2, ],
psi.low = psi.0.quants[1, ],
psi.high = psi.0.quants[3, ],
forest = forest.pred.vals)
ggplot(psi.plot.dat, aes(x = forest, y = psi.med)) +
geom_ribbon(aes(ymin = psi.low, ymax = psi.high), fill = 'grey70') +
geom_line() +
theme_bw() +
scale_y_continuous(limits = c(0, 1)) +
labs(x = 'Forest (% cover)', y = 'Occupancy Probability')
Thanks @doserjef. A function provided by the package would be great! I expect that some relationships we will want to fit may nonlinear (so will likely use {splines}
) and there may be interactions... Is it naive to hope that this doesn't affect the example above too much? For the interaction terms, instead of setting the other variable to its mean would I just generate all unique combinations of the two (e.g., via expand.grid(x1, x2)
?
Hi @doserjef,
I wasn't sure if you'd prefer this here or via email, as it isn't an issue with the package rather just some clarification. I opted for here in case it may be useful to others and it is easier to format code here than in an email.
I am trying to develop some code to predict latent occurrence probabilities across the range of a covariate. My model is an
spPGOcc
fit with the following call:I am interested in the effects of
orientation
on occurrence. I think you'll recognize the code I've adapted from your Introduction vignette, where you show how to predict these values at new locations and produce some occurrence maps. For the purposes of my research we're more interested in visualizing the covariate effect and I've attempted to do so by:This code works, in that it produces a plot (below), but I am wondering if this is the correct way to produce a plot like this? I am a little doubtful since the SD exceeds 1...
Thanks!