[R-sig-Geo] krige.cv

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Sep 17 18:59:21 CEST 2008


Yes, this is a very annoying "feature" for which I couldn't find a good 
solution. Problem is that I chose both the old-style (pre-sp) formulation

krige(modE~1, ~x+y, dataFrame, dataFrame, model)

as well as

krige(modE~1, SpData, SpNewdata, model)

to be supported. As it decides on the base of the second argument, an S4 
approach was needed:
 > showMethods(krige.cv)
Function: krige.cv (package gstat)
formula="formula", locations="formula"
formula="formula", locations="Spatial"
formula="formula", locations="SpatialPointsDataFrame"
    (inherited from: formula="formula", locations="Spatial")

This however completely breaks when you start to name (some) arguments 
"data", and that's where the nice similarity to the lm-interface stops. 
The solution to this would have required (imo) hundreds of lines of 
unreadable code, dealing with all cases where some arguments are named, 
others not...

Francois JOUVIE wrote:
> Please,
>
> Again, a question about the underlying process of krige.cv :
>
> I have a set of 42 values at different coordinates points (x,y,z) in 
> 3D space.
> I put an arbitrary model of variogram and I do a cross validation.
>
> This below is working:
>
> datacv<-read.table("/Users/fj/Desktop/septembre 
> 08/DonK3D_elev.txt",header=TRUE)
> > m1<-vgm(4,"Exp",2500)
> > x<-krige.cv(modE~1,~x+y+z,data=datacv,model=m1)
> [using ordinary kriging]
> [using ordinary kriging]
> [using ordinary kriging]
> [using ordinary kriging]
>
> ..... 42 times.
>
> Now, if I had to define the coordinates in a previous work (to do a 
> variogram by example), I can't do the cross validation because there 
> is an ambiguity with these coordinates. By example:
>
> datacv<-read.table("/Users/fj/Desktop/septembre 
> 08/DonK3D_elev.txt",header=TRUE)
> coordinates(datacv)<-~x+y+z
> > m1<-vgm(4,"Exp",2500)
> > x<-krige.cv(modE~1,~x+y+z,data=datacv,model=m1)
> Erreur dans `coordinates<-`(`*tmp*`, value = ~x + y + z) :
>   setting coordinates cannot be done on Spatial objects, where they 
> have already been set
>
In this case you should do
x <- krige.cv(modE~1, datacv, m1)
> This command neither do not work:
>
> x<-krige.cv(modE~1,data=datacv,model=m1)
> Erreur dans function (classes, fdef, mtable)  :
>   unable to find an inherited method for function "krige.cv", for 
> signature "formula", "missing"
leave out the "data=" and it works.

The documentation of krige.cv gives:

     krige.cv(formula, locations, ...)
     krige.cv.locations(formula, locations, data, model = NULL, ..., 
beta = NULL, nmax = Inf,
             nmin = 0, maxdist = Inf, nfold = nrow(data), verbose = FALSE)
     krige.cv.spatial(formula, locations, model = NULL, ..., beta = 
NULL, nmax = Inf,
             nmin = 0, maxdist = Inf, nfold = nrow(locations), verbose = 
FALSE)
...
locations: formula with only independent variables that define the
          spatial data locations (coordinates), e.g. '~x+y', OR data
          object deriving from class 'Spatial', which has a
          'coordinates' method to extract its coordinates.
    data: data frame; should contain the dependent variable,
          independent variables, and coordinates; only to be provided
          if 'locations' is a formula

I'm moderately happy with the solution chosen, but at least it is 
documented...
--
Edzer
> >
> How to use the coordinates when they are known  (I have not a such 
> problem with the krige command alone...?)
>
> Thanks you very much
>
> Francois
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster,
Weseler Straße 253, 48151 Münster, Germany.  Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de/




More information about the R-sig-Geo mailing list