2

I'd like to create a variogram fit from some semivariance data in R.

I realize that a variogram fit requires an object generated by vgm, and raw.vgm gneerated by variogram, for example:

x = seq(0,300,by=300/100)
y = seq(0,0.9,by=(0.9/100))
Z = seq(0.0,10.0,by=(10/100))
raw.dat = data.frame(x=x,y=y,Z=Z)

g <- gstat(formula=Z~1, locations=~x+y, data=raw.dat)
raw.vgm <- variogram(g)

vg.exp <- vgm(psill=0.8,model='Exp', range = 50)
fit.exp <- fit.variogram(raw.vgm , model = vg.exp)

However, if I already have the gamma (semivariance) and distance values, how do I obtain a gstat object from that?

The following throws an error:

dist = seq(0,30,by=0.3)
gamma = seq(0,0.9,by=(0.9/100))
raw.vgm = data.frame(dist=dist,gamma=gamma)
vg.exp <- vgm(psill=0.8,model='Exp',range=20)
fit.exp <- fit.variogram(raw.vgm, model=vg.exp)

The error is the folowing:

Error in fit.variogram(raw.vgm, model = vg.exp) : 
  object should be of class gstatVariogram or variogramCloud

I only have the gamma and distance lags. how do I create a gstatVariogram or variogramCloud? thanks.

makansij
  • 9,303
  • 37
  • 105
  • 183

1 Answers1

1

After creating raw.vgm, you have to set its class by

class(raw.vgm) = c("gstatVariogram", "data.frame")

then, fit.variogram also expects that a variogram has an np field, with the number of pairs of points used; I'm setting it to one by

raw.vgm$np = rep(1, nrow(raw.vgm))

and finally the default fit.method assumes you have no variogram values for distance zero, so you may want to try other values, like

fit.exp <- fit.variogram(raw.vgm, model=vg.exp,fit.method=1)
Edzer Pebesma
  • 3,814
  • 16
  • 26