florianhartig / DHARMa

Diagnostics for HierArchical Regession Models
http://florianhartig.github.io/DHARMa/
208 stars 22 forks source link

Spline correlogram for DHARMa #327

Open lionel68 opened 2 years ago

lionel68 commented 2 years ago

Hello Florian,

A reviewer on an ongoing manuscript asked to provide "a supplementary figure showing Moran’s correlograms for each model residuals", I am not sure if it would make sense to compute Moran's I for every pair of residuals and to plot these against their distance, but I was wondering if it would make sense to derive (spline) correlogram from DHARMa scaled residuals via package ncf, as follow:

# simulate some data
n <- 100
dat <- data.frame(x = runif(n, 0, 1),
                  y = runif(n, 0, 1),
                  resp = rnorm(n))

mod <- lm(resp ~ 1, data = dat)
dd <- DHARMa::simulateResiduals(mod)

nn <- ncf::spline.correlog(dat$x, dat$y, dd$scaledResiduals)
plot(nn)

So do you think that it would make sense to allow for spline correlogram computation based on DHARMa scaled residuals?

Maybe it could be implemented via a new argument in the testSpatialAutocorrelation function.

florianhartig commented 2 years ago

Hi Lionel,

I have indeed been playing around with adding a spatial variogram to the testSpatialAutocorrelation function, see also https://github.com/florianhartig/DHARMa/issues/311. Finally, I didn't include it in the last update because I was still hesitant with adding another dependency, generality of the plot and all those things. But I do agree that a graphical analysis of the lags could be useful.

About the question what to plot exactly: I have a slight tendency to prefer directional spatial variograms, such as this one

image

Over a general one. May I ask what purpose the plot should have in your case? I assume that you want to diagnose the form / distance of the RSA?

florianhartig commented 2 years ago

Just to add to that: if course, it's easy for people to just plot DHARMa residuals in whatever way they want - there are so many packages that provide plots for correlograms / variograms. If I implement something in DHARMa, it should be something super general, and ideally with very low dependencies.

lionel68 commented 2 years ago

Thanks for your swift reply Florian. The purpose would be to (I quote the reviewer) "understand the spatial structure of the data", which would point towards using directional variograms.

Just to be clear for the plot you produced you used something like:

gstat::variogram(residuals~x.coord+y.coord, data, alpha = c(0, 90, 135, 45))

Maybe there will be DHARMa add-ons with extended capacities and dependencies popping up :)

florianhartig commented 2 years ago

Hi, yes, this is what I did - I'm not sure if this really has to be added to the function, maybe enough to just add code in the help that demonstrates how to do further plots? I have become careful with hard CRAN dependencies after having had a number of issues with that.