Kriging
Kriging
Kriging measures spatial variability of geologically meaningful data, which is advanced to predict or
estimate the value at the location where the true value is unknown. It's a method of interpolation by
calculating the weighted average of known values of the neighborhood points. Some packages in R are
designed delicately for Kriging, in this lab, we use "rgdal","maptools","sp","gstat" packages to implement a
common type of spatial interporlation: Ordinary Kriging.
Spatial data of chemical content is imported along with a "border" shape file, on which a grid is defined for
the kriging. Then variogram modeling should be properly decided to obtain the weighted factors of Kriging.
To test the functionality of Ordinary Kriging, data is divided into 2 parts: training and test data. Therefore,
Kriging is applied to the training subset while a cross validation can be practiced to estimate the
prediction results based on the test dataset.
Though Kriging originates in the mining area, and widely spreads to petroleum applications, it can be
effectively applied to domains from environmental to fisheries, forestry, environmental remediation and so
on.
1. Data preparation
Install and load the required packages before reading the data: “rgdal”,”maptools”,”gstat”,”sp”. “rgdal”
provides bindings for the geospatial data abstraction library and access to projection/transformation
operations from the PROJ.4 library; “maptools” is set for reading the ESRI shapefile "border" in this lab;
“sp” contains classes and methods for spatial data; “gstat” enables variogram modelling, simple, ordinary
and universal (co)kriging.
library(rgdal)
library(maptools)
library(sp)
library(gstat)
In the “data.txt” file, 3 columns are representative for coordinates (Latitude and Longitude) and content of
Oxigen respectively. First, read the text file, transform the data frame into a spatial data frame, which
converts into spatial points data frame. Second, set the coordinate system: epsg numbers “2078” stands
for UTM33N, more projections can be found: http://spatialreference.org/ref/.
data=read.table("data.txt",sep="",header=T)
coordinates(data)=~Lat+Lon
str(data)
proj4string(data)=CRS("+init=epsg:2078")
Next, import area border from ESRI shape file “border.shp”, and also set the coordinate system by adding
the same projection, transform to spatial points data frame.
border=readOGR("border.shp","border")
proj4string(border)=CRS("+init=epsg:2078")
str(border)
For Kriging, a prediction grid is essential for the interpolation. The interporlation precision is largely
affected by grid size. Starting from the shape file, you can change the grid size when defining the
“gridRes” value.
vals=border@bbox
gridRes =5
grd =GridTopology(vals[,1],c(gridRes,gridRes),c(gridSizeX,gridSizeY))
pts =SpatialPoints(coordinates(grd))
pts1 =SpatialPointsDataFrame(as.data.frame(pts),
data=as.data.frame(rep(1,nrow(as.data.frame(pts)))))
Overlay=overlay(pts1,border)
pts1$border=Overlay
nona=na.exclude(as.data.frame(pts1))
Set the same projection for the grid and output the grid as an ascii file. Plot the grid if you want to see
how it looks like.
coordinates(nona)=~x+y
gridded(nona)=TRUE
proj4string(nona)=CRS("+init=epsg:2078")
writeAsciiGrid(nona,"prediction_grid.asc")
plot(nona)
Steps above are to create the grid which is consistent with the border file, otherwise you can just create a
rectangle grid by calling GridTopology and SpatialGrid functions .
2. Variogram Modeling
plot(variogram(Oxigen~1,data))
mod=vgm(psill=var(data$Oxigen),model="Sph",range=sqrt(areaSpatialGrid(n
ona))/4, nugget=0)
fit_reml=fit.variogram.reml(Oxigen~1,data,model=mod)
15 15
semivariance
semivariance
10 10
5 5
distance distance
3. Kriging
With a legitimate variogram model, we can apply Kriging to the dataset. However, in order to experiment
an independent validation, we first need to separate the data into a training data and test data. Then we
use the training dataset to predict the value in the test dataset. 10% of the data has been excluded for
testing. Applying Kriging to the training subset, allows us to see the difference of varied variogram models.
i=sample(nrow(data), round(nrow(data)*10/100))
training=data[!data$ID%in%i,]
test=data[data$ID%in%i,]
krig=krige(Oxigen~1, training, model=fit_reml, newdata=test)
4. Cross validation
Two Goodness of fit indexes, Pearson’s R Squared (RSQR) and Root Mean Square Deviation (RMSD),
are calculated to evaluate the estimation. Try a cross validation on estimated results with test dataset.
Plot the observed versus predicted values, since training and test samples are randomly selected, they
differ everytime, repeat the Kriging steps several times until RSQR and RMSD indexes are minimized.
RSQR=as.numeric(cor.test(test$Oxigen,krig$var1.pred)$estimate)^2
RMSD=sqrt(sum((test$Oxigen-krig$var1.pred)^2)/length(test$Oxigen))
plot(test$Oxigen,krig$var1.pred,asp=1, xlab="Observed",
ylab="Predicted")
abline(0,1,col="red",cex=0.5)
Now that we finished the cross-validation part, we can proceed to the creation of the map. In this case,
the OLS model is good and fast. Plot the map by the “spplot” function in the “sp” package. Adjust the plot
function to change the color regions or add scales /titles.
Ordinary Kriging
+
40
+ + + +
+ + 44
+ + + + +
+ + +
+ + +
5555550 + + + + + +
+ + + + + 42
38
+ + + + + + +
+ + + + + + +
Predicted
+ + + + +
5555500
+ + + + + + + + 40
+ + + + + + + + + +
+ + + + +
+ + + + + + + + +
36
+ + + + + + +
5555450
+ + + + + 38
+ + + + + + +
+ + + + + + + + + +
+ + + + + +
+ + + + + + + + + 36
5555400 + + + + + + + + + +
34
+ + +
+ + + + +
+ + + + + + + + + +
+ + + + + +
34
5555350
+ + + + + +
+ + + + +
+ + + + + + + +
32
32
+ + + +
+ + + + + + +
5555300 + + + + + +
+ + +
+ + + 30
418800 418900 419000 419100
30
30 32 34 36 38 40 42
Observed
Disclaimer: Data and codes sources are taken from tutorials on websites, and this lab is just for
instructional lab practice.