florianhartig / DHARMa

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

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

Open milkweedgirl opened 10 months ago

milkweedgirl commented 10 months ago

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.

florianhartig commented 10 months ago

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

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

milkweedgirl commented 10 months ago

Hi Florian- Thank-you for your timely response. Yes, it does seem some of group locations still have the same lat (x) which is why I am maybe getting the same error. I tried to group them by plotID (so paired sub-plots would have a unique ID). Additionally, I have 81 locations in the grouplocations and the res2 says it is a list of 28 which I assumes means 28 residuals...?

UPDATE: I re-ran the lat/ long data with more decimal places to ensure each location was unique but am still getting this issue.

thank-you,

Emma

On Wed, Jan 24, 2024 at 7:13 AM Florian Hartig @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/florianhartig/DHARMa/issues/399#issuecomment-1908206349, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDY3YPEWKAEQDGDWOHFE7BLYQEJHXAVCNFSM6AAAAABCH3HSPCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBYGIYDMMZUHE . You are receiving this because you authored the thread.Message ID: @.***>

florianhartig commented 10 months ago

Same lat is no problem if long is different.

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

florianhartig commented 10 months ago

I would probably need your data to say more!

melina-leite commented 1 month ago

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.