krige() function with SpatialPointsDataFrame in R

I would like to make some kriging in a belgian territory.

I got a data set called clean.donnees.Ni after removing outliers and NAvalues.

I first tried simply with this, but got this warning as result :

Ni.krig <- krige(formula = Ni~1,
                 data = clean.donnees.Ni,
                 locations = ~x+y,
                 newdata = krig.grid, 
                 model = Ni.vg.fit)

Warnings : Covariance matrix singular at location [50686, 156893,0]: skipping

I found on other topics that I probably got points at the same location.
(interpolation - R gstat krige() - Covariance matrix singular at location [5.88,47.4,0]: skipping - Geographic Information Systems Stack Exchange)

They advice to use this to remove points on the same location:

clean.donnees.Ni <- clean.donnees.Ni[-zerodist(clean.donnees.Ni)[,1],]

When I did this, I finally thought I could make krige() work, but instead it nows returns me this error :

Ni.krig <- krige(formula = Ni~1,
                 data = clean.donnees.Ni,
                 locations = ~x+y,
                 newdata = krig.grid, 
                 model = Ni.vg.fit)

Error in `coordinates<-`(`*tmp*`, value = locations) : 
  setting coordinates cannot be done on Spatial objects, where they have already been set

Because I had to transform clean.donnees.Ni in a SpatialPointsDataFrame, locations can't read ~x+y anymore.

Does anyone knows how to makes kriging work ?

try

krige(formula = Ni~1,
                 locations = clean.donnees.Ni,
                 newdata = krig.grid, 
                 model = Ni.vg.fit)

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

> d = data.frame(x=runif(100), y=runif(100), Z=rnorm(100))
> coordinates(d)=~x+y
> 
> k = krige(formula = Z~1, data=d, locations=~x+y, newdata=d)
Error in `coordinates<-`(`*tmp*`, value = locations) : 
  setting coordinates cannot be done on Spatial objects, where they have already been set
> 
> k = krige(formula = Z~1, locations=d, newdata=d)
[inverse distance weighted interpolation]

That's a simpler example, and actually your solution should work.
With my error

Ni.krig <- krige(formula = Ni~1,
                   locations = clean.donnees.Ni,
                   newdata = krig.grid, 
                   model = Ni.vg.fit)

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'obj' in selecting a method for function 'coordinates': trying to get slot "bbox" from an object (class "data.table") that is not an S4 object 

there is something strange, as you can see my clean.donnees.Ni is well a S4 object :

> typeof(clean.donnees.Ni) 
[1] "S4"

the type is less important than the class. but I'm afraid we will get into a strange dance without a clear example of your issue to investigate

I finally found my mistake, and your answer is the right to my question !
(I had to transform krig.grid to S4 object too)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.