0

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. (R gstat krige() - Covariance matrix singular at location [5.88,47.4,0]: skipping)

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 ?

1 Answer 1

0

If your data is a spatial data object then you give it to the locations argument - you only do locations = ~x+y if you have a non-spatial data frame with coordinates in those columns. So I think:

coordinates(krig.grid) <- ~x+y

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

will work for you.

I can reproduce your error with a test dataset, and make it work:

> 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]

From the help:

locations: object of class ‘Spatial’ or ‘sf’, or (deprecated) formula
          defines the spatial data locations (coordinates) such as
          ‘~x+y’

    data: data frame: should contain the dependent variable,
          independent variables, and coordinates, should be missing if
          locations contains data.
4
  • I tried but now I get this as error: 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 I don't really understand why it doesn't work : > typeof(clean.donnees.Ni) [1] "S4"
    – Jouline
    Dec 10, 2021 at 14:02
  • Somewhere you've got a data.table object. Krig.grid perhaps if its not your data points. Edit your Q and show summary and/or class of your inputs.
    – Spacedman
    Dec 10, 2021 at 23:46
  • Hi, you were right, my krig.grid had to be also an S4 object ! I still got troubles to plot this, because I've never worked with S4 object. Maybe you can help me ?
    – Jouline
    Dec 13, 2021 at 11:07
  • If you've got more problems, ask a new question, and give as much detail as possible on the data you are feeding the functions - show their summary or print them, or create sample data from things we can all use. Tag it "r" and "kriging" and I'll see it!
    – Spacedman
    Dec 13, 2021 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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