我想知道:克里金法时是否有必要重新定义自己的列?下面的错误似乎表明了这一点:
Warning: singular model in variogram fit
> sk1 <- krige(formula=Zs~1, locations=~Xs+Ys, data=sampled, newdata=pred.grid, model=fit.sph, beta=0)
Error in `[.data.frame`(object, , -coord.numbers, drop = FALSE) :
undefined columns selected
有我没有看到的问题吗?或者,我需要定义自己的列吗?谢谢。
以下程序从这里开始是完全可重现和可运行的:
library(gstat)
x <- seq(0,2000,by=20)
y <- seq(0,2000,by=20)
x = sample(x,10,replace=T)
y = sample(y,10,replace=T)
z = sample(0.532:3.7,10,replace=T)
samples = data.frame(x,y,z)
# detrend the samples:
print(mean(samples$z))
#create object of class gstat
h <- gstat(formula=z~1, locations=~x+y, data=samples)
samples.vgm <- variogram(h) # create method of class "gstatVariogram"
plot(samples.vgm,main='Variogram of Samples NOT detrended') # plot method for class "gstatVariogram"
# DETREND
z = samples$z
x = samples$x
y = samples$y
trend <- lm(z~x+y)
c = trend$coefficients[[1]]
a = trend$coefficients[[2]]
b = trend$coefficients[[3]]
#z_prime = z - (a*x + b*y +c)
# SUBTRACT THE PREDICTED LINE
Xs <- c()
Ys <- c()
Zs <- c()
print('started the loop')
for (i in 1:nrow(samples)){
i = samples[i,]
x=i$x
y=i$y
z=i$z
z_prime = z - (a*x+b*y+c)
Xs <- c(Xs,x)
Ys <- c(Ys,y)
Zs <- c(Zs,z_prime)
}
sampled <- data.frame(Xs=Xs,Ys=Ys,Zs=Zs)
print(sampled)
print('the length of sampled is')
print(length(sampled[[1]]))
# "result" is the new dataset with Z's detrended
# print(levelplot(Zs~Xs+Ys,sampled))
# define the domain or kriging estimation
x <- seq(0,2000,by=20)
y <- seq(0,2000,by=20)
# make data frame with prediction locations
pred.grid <- data.frame(x=rep(x,times=length(y)),y=rep(y,each=length(x)))
#create object of class gstat
g <- gstat(formula=Zs~1, locations=~Xs+Ys, data=sampled)
sampled.vgm <- variogram(g) # create method of class "gstatVariogram"
plot(sampled.vgm,main='Variogram of Samples hopefully detrended') # plot method for class "gstatVariogram"
vg.sph <- vgm(psill=1.0,model='Sph', range = 500)
fit.sph <- fit.variogram(sampled.vgm, model = vg.sph)
sk1 <- krige(formula=Zs~1, locations=~Xs+Ys, data=sampled, newdata=pred.grid, model=fit.sph, beta=0)