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:

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
  • 1
    This is not a prediction issue. It looks like you may have some duplicate observations. Since you only provide information on the error and not you code and objects passed to the function I cannot provide any additional insight. Please look at the forum guidelines on posting a good question. – Jeffrey Evans May 6 '16 at 14:04
  • @JeffreyEvans I edited the question. Please have look to see if you can find the problem. Cheers – mohammad gerami May 7 '16 at 4:30

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.


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)

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)     
| improve this answer | |
  • @Jeffry Evans Thank you very much. It helped a lot. I appreciate. – mohammad gerami May 8 '16 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.