1

I am trying to using ordinary kriging to spatially predict data where an animal will occur based on predictor variables using the gstat or automap package in R. I have many (over 100) duplicate coordinate points, which I cannot throw out since those stations were sampled multiple times over many years. Every time that I run the code below for ordinary kriging, I get an LDL error, which is due to the duplicate points. Does anyone know how to fix this problem without throwing out data? I have tried the code from the automap package that is supposed to correct for duplicates but I can't get that to work. Thank you for the help!

coordinates(fish) <- ~ LONGITUDE+LATITUDE
x.range <- range(fish@coords[,1])
y.range <- range(fish@coords[,2])
grd <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=3), y=seq(from=y.range[1], to=y.range[2], by=3))
coordinates(grd) <- ~ x+y
plot(grd, pch=16, cex=.5)
gridded(grd) <- TRUE

library(gstat)
zerodist(fish) ###146 duplicate points
v <- variogram(log(WATER_TEMP) ~1, fish, na.rm=TRUE)
plot(v)
vgm()
f <- vgm(1, "Sph", 300, 0.5)
print(f)
v.fit <- fit.variogram(v,f)
plot(v, model=v.fit) ####In fit.variogram(v, d) : Warning: singular model in variogram fit

krg <- krige(log(WATER_TEMP) ~ 1, fish, grd, v.fit) 
## [using ordinary kriging]
##"chfactor.c", line 131: singular matrix in function LDLfactor()Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim,: LDLfactor

##automap code for correcting for duplicates
fish.dup = rbind(fish, fish[1,]) # Create duplicate
coordinates(fish.dup) = ~LONGITUDE + LATITUDE 
kr = autoKrige(WATER_TEMP, fish.dup, grd)
###Error in inherits(formula, "SpatialPointsDataFrame"):object 'WATER_TEMP' not found
###somehow my predictor variables are no longer available when in a Spatial Points Data Frame??
ms.elasmo
  • 13
  • 4
  • Please edit Q to include `library` call (or link) that makes `fish` accessible. – IRTFM Feb 04 '16 at 19:46
  • Oh I'm sorry, I'm new to this. Do I have to provide my personal data (i.e. fish)? I didn't know if it was possible to look at the code and see where I went wrong. – ms.elasmo Feb 04 '16 at 19:58
  • It might be possible if you find a knowledgeable user of that package. I am not so I would need to have a data-object to work with. Can you get the error with one of the data objects provided in gstat? (The first error also does not look like a complete R error message.) – IRTFM Feb 04 '16 at 20:04
  • When I use the provided data set of (meuse) for that package, everything works fine. I am not sure what it is about my data that won't work. I fixed the error messages in my original question to the full message. – ms.elasmo Feb 04 '16 at 20:15

3 Answers3

1

automap::autoKrige expects a formula as first argument, try

kr = autoKrige(WATER_TEMP~1, fish.dup, grd)
Edzer Pebesma
  • 3,814
  • 16
  • 26
  • I did finally get this to work, though I'll have to play around with the dimensions of my grid. Thanks! – ms.elasmo Feb 08 '16 at 17:10
0

automaphas a very simple fix for duplicate observations, and that is to discard them. So, automapdoes not really solves the issue you have. I see some options:

  • Discard the duplicates.
  • Slightly perturb the coordinates of the duplicates so that they are not on exactly the same location anymore.
  • Perform space-time kriging using gstat.

In regard to your specific issue, please make your example reproducible. What I can guess is that rbind of your fish object is not doing what you expect...

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thank you for your suggestions, Paul. I took your advice and was able to run the autoKrige function with my duplicate coordinates slightly perturbed. The kriging prediction grids come out very unclear (i.e. very pixelated grids) but that may be a function of how I originally set up my grid dimensions. Thank you! – ms.elasmo Feb 08 '16 at 17:19
0

Alternatively you can use the function jitterDupCoords of geoR package. https://cran.r-project.org/web/packages/geoR/geoR.pdf