florianhartig/DHARMa

Test spatial autocorrelation error: Dimensions of x / y coordinates don't match the dimension of the residuals

Opened this issue · 5 comments

Hello, I am trying to test the spatial auto correlation of my binomial models ran in glmmTMB with the Dharma package. My model has plotID as a random effect to account for sub-plots nested together.

I have repeated x coordinates (same for pairs of 2 sub-plots within a plot) even though some of the y coordinates are slightly different in some cases. So I tried to aggregate residuals per location. However when I follow the steps in the R documentation for the function I keep running into the error: Dimensions of x / y coordinates don't match the dimension of the residuals. For reference I have 162 sub-plots therefore 81 unique coordinates ("PlotID" is therefore the unique group). Is this because my original residuals are using a set of 162 coordinates vs. 81 for recalculated residuals?

For reference my "res" has a length of 17 and my "res2" has a length of 28.

image

I used the steps found here: (https://rdrr.io/cran/DHARMa/man/testSpatialAutocorrelation.html )

Code:

res=simulateResiduals(fittedmodel)

groupLocations = aggregate(data[, 33:32], list(data$plotID), mean)
#33 and 32 are my lat/ long columns

res2 = recalculateResiduals(res, data$plotID)

testSpatialAutocorrelation(res2, groupLocations$long, groupLocations$lat)

Additionally I did not do this step: # create first ar group with unique values for each location

data$long = as.numeric(data$plotID)
data$lat = as.numeric(data$plotID)

When I tried the given code it overrode my lat long values with unique numbers not coordinates which I figured was in-correct.

I also wonder if I need to account for "clusters" as my experimental design is that there are 9 pairs of 2 sub-plots nested with a 1-hectare site (times multiple sites). I did originally account for plotID nested within sites in my model however the site variance and st.dev was <0 so I removed site as a random effect.

I hope you can help me sort this error out! Thank-you.

Doesn't look obviously wrong to me. Can you checked

  • if the locations in groupLocations are really unique?
  • how many locations you have in groupLocations and how many residuals in res2

If that doesn't yield any results I would probably need a reproducible example, see https://github.com/florianhartig/DHARMa/wiki/Asking-for-help

Same lat is no problem if long is different.

About res2 -> no, you have to check length(residuals(res2))

I would probably need your data to say more!

Hi @milkweedgirl, I'm just checking to see if you could solve the problem.
If not, please send us a reproducible example ( https://github.com/florianhartig/DHARMa/wiki/Asking-for-help) of the problem for our investigation.
If everything is ok, I'll close this issue.