florianhartig / DHARMa

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

testTemporalAutocorrelation within selection of locations #369

Closed skiology90 closed 1 year ago

skiology90 commented 1 year ago

Hi, Thanks for all of your continual efforts on DHARMa. I'm still very new to the package and having some trouble on how to evaluate only a selection of locations where repeated observations occurred.

I'm attempting to evaluate temporal correlation of the residuals from a glmer model looking at species presence/absence in streams across watersheds. The dataset (trainInsit) includes observations over the same domain from ~20 years but most we're resampled each year, meaning different stream segments (i.e. locations) were surveyed each year. However, there are approximately 1/4 of the stream segments during the 20 year period that did have repeated observations in time and those are the observations I need to consider temporal correlation.

The survey locations are defined by stream segment (COMID) and my time variable is (Year). I created a binary grouping variable (Repeated) to indicate if the COMID had any repeated observations in time.

My attempts so far have returned the below error which I assume is because I have not correctly clarified that it is only to consider Year within COMID:

Any help would be appreciated:


res = simulateResiduals(mod1) res2= recalculateResiduals(res, sel= trainInsit$Repeated == 1) res3 = recalculateResiduals(res2, group = trainInsit$COMID) testTemporalAutocorrelation(res3, time = trainInsit$Year)

Error in testTemporalAutocorrelation(res2, time = trainInsit$Year) : testing for temporal autocorrelation requires unique time values - if you have several observations per time value, either use the recalculateResiduals function to aggregate residuals per time step, or extract the residuals from the fitted object, and plot / test each of them independently for temporally repeated subgroups (typical choices would be location / subject etc.). Note that the latter must be done by hand, outside testTemporalAutocorrelation.

florianhartig commented 1 year ago

Hi skiology90,

sorry for the late reply!

Your problem is that if you have repeated observation per year, you have to group per year, i.e.

res3 = recalculateResiduals(res2, group = trainInsit$Year)

so that you have one residual per year / time point. Then, you can test for temporal autocorrelation. Grouping per location is needed if you want to test for spatial autocorrelation.

Cheers, Florian

florianhartig commented 1 year ago

OK, I will consider this closed for the moment, in case you have further questions feel free to re-open the issue!