pbs-assess / sdmTMB

:earth_americas: An R package for spatial and spatiotemporal GLMMs with TMB
https://pbs-assess.github.io/sdmTMB/
183 stars 26 forks source link

Facing error while prediction for a presence model #238

Closed rahulkgour closed 1 year ago

rahulkgour commented 1 year ago

Hi,

I am facing error while prediction, below are the code snippet and error.

I have multiple covariates, with presence data for my species at 101 locations, located in an area of around 18000 sq km.

I was trying to give a mock run with a linear combination of all the covariates, using sdmTMB().

Is there any other way to do prediction? like using a raster stack of all the covariates instead of creating a new data with all the covariates present into it.

mesh <- make_mesh(mugger, c("X", "Y"), cutoff = 0.30)

m <- sdmTMB(
  data = mugger,
  formula = present ~ AI + bio_1 +bio_2 +   bio_3 + bio_10 + Canopy_cover + Dist_road + 
    Dist_settle + Dist_water + Elevation + LST + NDVI + Slope + TWI + Wetland,
  mesh = mesh,
  family = binomial(link = "logit"),
  spatial = "on"
)

## Spatial predictions
p <- predict(m)
select(p, X, Y, est:omega_s) %>%
  as_tibble()

ggplot(p, aes(X, Y, fill = plogis(est))) +
  geom_raster() +
  scale_fill_viridis_c() +
  coord_fixed()

# Extract the values from the raster stack at the same locations as the occurrence data
newdata <- read.csv(file.choose())

# Convert the extracted values into a data frame
newdata <- as.data.frame(newdata)

# Make spatial predictions using the sdmTMB model
predictions <- predict(m, newdata = newdata)

# Let’s make a small function to help make maps.
plot_map <- function(dat, column) {
  ggplot(dat, aes(X, Y, fill = {{ column }})) +
    geom_raster() +
    coord_fixed()
}

plot_map(predictions, exp(est)) +
  scale_fill_viridis_c(
    trans = "sqrt",
    # trim extreme high values to make spatial variation more visible
    na.value = "yellow", limits = c(0, quantile(exp(predictions$est), 0.995))
  ) +
  ggtitle("Prediction (fixed effects + all random effects)",
          subtitle = paste("maximum estimated biomass density =", round(max(exp(predictions$est))))
  )

Error in geom_raster(): ! Problem while converting geom to grob. ℹ Error occurred in the 1st layer. Caused by error: ! vector memory exhausted (limit reached?) Run rlang::last_trace() to see where the error occurred. Warning messages: 1: Raster pixels are placed at uneven horizontal intervals and will be shifted ℹ Consider using geom_tile() instead. 2: Raster pixels are placed at uneven horizontal intervals and will be shifted ℹ Consider using geom_tile() instead.

thanks and regards, Rahul

seananderson commented 1 year ago

Your error is from using ggplot2::geom_raster() with values that are not on a regular grid. Here, you could use geom_point() or I suppose geom_tile() (with appropriate width and height) if that makes sense.

You're currently predicting at the data locations. If you want to predict on a grid, you will need a data frame that has the various covariate columns at relevant values. If you want to isolate the effect of a variable, you could hold all the other covariate values at constants such as their means (but be wary of spatial covariates that covary in reality). If your data is in raster format, you'll need to convert to data frame first.

Are your data presence and absence or presence only? If presence only, you may want to look into a pseudo-absence strategy. E.g., https://pbs-assess.github.io/sdmTMB-teaching/dfo-tesa-2023/

rahulkgour commented 1 year ago

Thank you Sean. My data is presence only. Thanks for sharing the tutorial.