title: Spatial autocorrelation analysis withe several observations per location in R
body: <p>I have data collected from several sampling sites across a landscape.
  Each individual sample has a unique dependent variable (y) and explanatory variables shared with 9 other samples:</p>
  
  <pre><code>DependentVar&lt;-rnorm(60,mean = 9.5,sd=.5)
  Temperature&lt;-rep(c(23.5,18,26.1,24.7,20.8,20),each=10)
  SpatData&lt;-data.frame(x = rep(runif(6,0,100),each=10), y = rep(runif(6,0,100),each=10))
  SpatData$Temperature&lt;-Temperature
  SpatData$DependentVar&lt;-DependentVar
  </code></pre>
  
  <p>I have constructed a linear model to test the influence of temp. on the dependent variable:</p>
  
  <pre><code>SpatLM&lt;-lm(DependentVar~Temperature,data = SpatData)
  </code></pre>
  
  <p>Then I have tried to test for autocorrelation of the residuals like so:</p>
  
  <pre><code>library(DHARMa)
  sims&lt;-simulateResiduals(SpatLM)
  testSpatialAutocorrelation(sims, x=SpatData$x,y=SpatData$y)
  </code></pre>
  
  <p>The message I got was:</p>
  
  <blockquote>
    <p>Error in testSpatialAutocorrelation(sims, x = SpatData$x, y = SpatData$y) : 
      testing for spatial autocorrelation requires unique x,y values - if you have several observations per location, either use the recalculateResiduals function to aggregate residuals per location, or extract the residuals from the fitted object, and plot / test each of them independently for spatially repeated subgroups (a typical scenario would repeated spatial observation, in which case one could plot / test each time step separately for temporal autocorrelation). Note that the latter must be done by hand, outside testSpatialAutocorrelation.</p>
  </blockquote>
  
  <p>Using <code>recalculateResiduals()</code> doesn't seem to work, with or without grouping:
  <code>simsrecalc&lt;-recalculateResiduals(sims,group = SpatData$Temperature)</code></p>
  
  <p>Anyone has an idea how to solve this?</p>