如何使用gstat预测普通克里金法 [英] How to make Ordinary Kriging by using gstat predict
问题描述
我试图在R中使用gstat库编写代码以创建插值. 我已经阅读了gstat手册,并且基于互联网上的一些示例,我设法编写了这段代码(这只是一部分):
I am trying to write a code in R that use gstat library in order to create an interpolation. I have already read the gstat manual and based on some examples on internet I had managed to write this code (this is only a part):
g <- gstat(id="tec", formula=TEC ~ 1, data=data) ##I create an object
v <- variogram(g) # plot the empirical variogram
plot(v)
mod<-vgm(sill=var(data$TEC),model="Sph",range=200,nugget=200) #create the variogram model
v.fit <- fit.variogram(v, model=mod,fit.method=1) #fit the empirical variogram
Theor_variogram=plot(variogram(g),v.fit,main="WLS Model") #plot the theoretical variogram
plot(Theor_variogram)
## Kriging interpolation
p <- predict.gstat(g, model=v.fit, newdata=predGrid)
我的问题是,当我运行最后一个命令(预测)而不是使用普通克里格插值获得结果时,我得到了一个具有反距离权重(IDW)的结果. 我在gstat手册中读到:当未指定任何方差图时,反距离加权插值 是默认操作. 当指定了方差图时,默认的预测方法是普通的 克里金法."
My problem is that, when I run the last command (predict) instead of getting a result with ordinary kriging interpolation, I get one with inverse distance weighted (IDW). I read in the gstat manual that: "When no variograms are specified, inverse distance weighted interpolation is the default action. When variograms are specified the default prediction method is ordinary kriging."
但是,正如您在我的代码中看到的那样,我同时指定了经验方差和理论方差. 您知道为什么我会继续获得IDW而不是普通的克里金法吗?它可以与我拥有的坐标类型相关吗?例如,如果我的坐标彼此接近,或者感兴趣的区域太大? 任何帮助都将非常有用.
But, as you can see in my code, I specify both the empirical and theoretical variogram. Do you know why I keep getting IDW instead of ordinary kriging? Can it be related with the type of coordinates that I have? If for example I have coordinates close to each other, or if the region of interest is too big? Any help would be really useful.
先谢谢了 Dimitris
Thanks in advance Dimitris
推荐答案
您需要包括gstat对象的创建,而不是在预测阶段:
You need to include the creation of the gstat object, not in de prediction phase:
g <- gstat(id="tec", formula=TEC ~ 1, data=data, model = v.fit)
但是,我建议使用krige
为gstat
使用标准接口.这将gstat
对象的构建和预测合并为一个函数.您很少需要自己构建gstat
对象.例如:
However, I would recommend using the standard interface for gstat
using krige
. This combines the building of the gstat
object and the prediction into one functions. Very rarely do you need to build gstat
objects yourself. For example:
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y
m <- vgm(.59, "Sph", 874, .04)
# OK:
x <- krige(log(zinc)~1, meuse, meuse.grid, model = m)
您还可以使用automap
包(我是作者),并让方差图模型自动拟合到数据中.例如,使用meuse
数据集:
You could also use the automap
package (which I am the author of) and let the variogram model be automatically fitted to the data. For example using the meuse
dataset:
library(automap)
kr = autoKrige(log(zinc)~1, meuse, meuse.grid)
这将自动构建样本方差图,并为该样本方差图拟合方差图模型.
This will automatically build a sample variogram, and fit a variogram model to that sample variogram.
这篇关于如何使用gstat预测普通克里金法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!