Wednesday, 27 April 2016

spatial analyst - Error in In predict.gstat in R


I am trying to do kriging by gstat package through R.


Here is my Code:


krg<-krige(var ~ 1,data,grid,model=vgm,nmax=30)

But each time I recive this warning:



1: In predict.gstat(g, newdata = newdata, block = block,  ... :


Warning: Covariance matrix (nearly) singular at location [1,4501,0]: skipping...

and when I try to plot it by spplot this error appears:


Error in seq.default(zrng[1], zrng[2], length.out = cuts + 2) : 


'from' cannot be NA, NaN or infinite

In addition: Warning messages:
1: In asMethod(object) :
complete map seems to be NA's -- no selection was made
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

Here is the full code: You can download my data through this link: Mydata and here is my full code:


library(gstat)                                         
library(sp)
library(raster)

coordinates(Mydata) <- ~East + North
proj4string(Mydata) <- CRS("+proj=utm +zone=10+datum=WGS84")
ext <- as(extent(Mydata), "SpatialPolygons")
r <- rasterToPoints(raster(ext), spatial = TRUE)
proj4string(r) <- proj4string(Mydata)
v1 <- variogram(cpue_female ~ 1, Mydata)
vgm_mod_omni <- vgm(psill = 0.0003, model = "Mat", range = 50000, nugget = 33, kappa = 0.5)
G1 <- krige(cpue_female ~ 1, Mydata, r, vgm_mod_omni, nmax = 30)
gridded(G1) <- TRUE
spplot(G1)


Answer



As I pointed out, you had identical observations. Additionally, you were not using the "resolution" argument in the raster function so, were only creating 100 pixel observations to predict to. I had to fix the tab returns in the file that you posted on dropbox, which was not appreciated.


Here is code that I got to work.


library(gstat)                                         
library(sp)
library(raster)
setwd("D:/TMP/gstat")

coordinates(Mydata) <- ~East + North
proj4string(Mydata) <- CRS("+proj=utm +zone=10+datum=WGS84")


Here is where I check for, and remove, identical observations. All this is doing is screening for observations sharing the same [x,y] space. I would question why there are 66 (out of 201) identical observations. If the identical values are valid you should apply a function that aggregates the values into single z[x,y] observations (eg., using mean).


Mydata <- Mydata [-zerodist(Mydata)[,1],]

The difference here is that I am defining a resolution to the raster that is being coerced into a SpatialPoints object for use in the krige function.


s = 500
ext <- as(extent(Mydata), "SpatialPolygons")
r <- rasterToPoints(raster(ext, resolution = s), spatial = TRUE)
proj4string(r) <- proj4string(Mydata)
length(r)


Now, apply the kriging model.


G1 <- krige(cpue_female ~ 1, Mydata, r, vgm(psill = 0.0003, model = "Mat",  
range = 50000, nugget = 33, kappa = 0.5), nmax = 30)

gridded(G1) <- TRUE
G1 <- stack(G1)
plot(G1)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...