1

我想知道:克里金法时是否有必要重新定义自己的列?下面的错误似乎表明了这一点:

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)
4

1 回答 1

0

添加library(gstat)到代码的顶部,然后它是可重现的。

要直接回答您的问题,您收到undefined columns selected错误的原因是因为您newdata的列名不正确。列名需要与数据列名匹配,本例中为 Xs 和 Ys。重新定义 pred.grid 以具有列XsYs解决您的问题。我测试了,你的代码运行了。

pred.grid <- data.frame(Xs=rep(x,times=length(y)),Ys=rep(y,each=length(x)))

至于其他评论:Warning: singular model in variogram fit是无法根据样本半变异函数数据拟合模型的结果。如果你看一下你的数据图(下图),很明显没有经验函数能够适应这个。在您的情况下,这是因为每个 bin 只有一个点(总共 11 个点),所以实际上没有足够的数据来拟合半变异函数。即使减少 bin 的数量,仍然没有足够的数据支持来拟合经验半变异函数。

样本半变异函数,数据中只有 11 个点!

将样本数量更改为 500,

x = sample(x,500,replace=T)
y = sample(y,500,replace=T)
z = sample(0.532:3.7,500,replace=T)

很明显,您生成的数据是不相关的,因此在 xy 空间中彼此靠近的样本并不比更远的样本更相似(纯块金半变异函数)。这是你想要的吗?

在此处输入图像描述

于 2015-07-28T01:50:59.413 回答