Ordinary Kriging Example: GRASS-R Bindings
Update: 2012-02-13
Many of the examples used in this demonstration are now somewhat dated, probably inefficient, and in need of revision. I'll spend some time on an updated version for the GRASS wiki soon.
Overview:
A simple example of how to use GRASS+R to perform interpolation with ordinary kriging, using data from the spearfish sample dataset. This example makes use of the gstat library for R.
Helpful References:
- Issaks, E.H. & Srivastava, R.M. An Introduction to Applied Geostatistics Oxford University Press, 1989
- GSTAT Manual
- GSTAT Examples
Elevation Data and Sample Points: 300 randomly placed points where elevation data was sampled.
Data Prep:
As a contrived example, we will generate 300 random points within the current region, and sample an elevation raster at each of these points.
# set region:g.region rast=elevation.dem# extract some random points from an elevation datasetv.random out=rs n=300# create attribute table:v.db.addtable map=rs columns="elev double"# extract raster data at pointsv.what.rast vect=rs rast=elevation.dem column=elev# simple display:d.rast elevation.demd.vect rs size=4# start RRX
Load GRASS Data into R:
Remember that you will need to install these R packages onto your computer.
##load librarieslibrary(gstat)library(spgrass6)## read in vector dataset from aboveG <- gmeta6()x.has.na <- readVECT6('rs')# remove records with NAx <- x.has.na[-which(is.na(x.has.na@data$elev)),]## create a grid wihch will define the interpolation## note that it is based on the current GRASS region settingsgrd <- gmeta2grd()## make a new grid of (1)s## be sure to use original data's proj data...## doesn't work with the stuff stored in G...new_data <- SpatialGridDataFrame(grd, data=data.frame(k=rep(1,G$cols*G$rows)), proj4string=CRS(x@proj4string@projargs))## optionally we can use another raster of 1's as our interpolation maskmask <- as(readRAST6("rushmore"), "SpatialPixelsDataFrame")## need to manually set the coordinate system information:mask@proj4string <- x@proj4string## this new object could then be used in the 'newdata' argument to predict(), i.e.## x.pred_OK <- predict(g, id='elev', newdata=mask)X
Variogram Modeling:
A very simple example, using default parameters for a non-directional variogram is presented below. Modeling the variogram for an actual spatial problem requires knowlege of both your dataset (distribution, collection methods, etc.), the natural processes involved (stationarity vs. anisotropy ?), and a bit about the assumptions of geostatistics.
## init our gstat object, with the model formula## note that coordinates are auto-identified from the GRASS objectg <- gstat(id="elev", formula=elev ~ 1, data=x)## view a variogram with specified parametersplot(variogram(g, width=250, cutoff=10000, map=FALSE))## optionally make a variogram map, and plot semi-variance for 10-point pairs or greater:plot(variogram(g, width=250, cutoff=10000, map=TRUE), threshold=10)## fit a linear variogram model- as this looks appropriate## ... using default parametersv.fit <- fit.variogram(variogram(g) ,vgm(model="Lin") )plot(variogram(g, width=250, cutoff=10000, map=FALSE), model=v.fit)## update gstat object with fitted variogram modelg <- gstat(g, id="elev", model=v.fit )X
Variogram and Fit Model: A Linear variogram model was fit to the elevation data.
Interpolation by Ordinary Kriging:
The prediction is done for every instance of a '1' in the object passed to the newdata= argument.
## interpolate with ord. krigingx.pred_OK <- predict(g, id='elev', newdata=new_data)X
Send Results Back to GRASS:
## write raster back to GRASS: interpolation and kriging variance:## system('g.remove rast=elev.ok')writeRAST6(x.pred_OK, 'elev.ok', zcol='elev.pred')writeRAST6(x.pred_OK, 'elev.ok_var', zcol='elev.var')## quit:q()X
Viewing Results in GRASS:
# reset colors to match original data:r.colors map=elev.ok rast=elevation.dem# give the kriging variance a grey color mapr.colors map=elev.ok_var color=rules <<EOF0% white100% blackEOF## display the kriged interpolation, with intensity | saturation based on varianced.his s=elev.ok_var h=elev.ok# optional method:# d.his i=elev.ok_var h=elev.okd.vect rs size=4X
Interpolated Elevation Data via Ordinary Kriging: Hue is interpolated elevation value, saturation is based on the kriging variance.
Simple Comparison with RST:
RST (regularized splines with tension) and OK (ordinary kriging) are two common interpolation methods. Computing the RMSE (root-mean-square-error) between the interpolated raster and the original raster provides a simple quantitative measure of how well the interpolation performed, at least in terms mean magnitude of error. A spatial description of interpolation error can be generated by subtracting the new raster from the original. Note that the steps involve cell-wise computation of the square-error (SE), region-wise computation of the mean-square-error (MSE); the square root of MSE gives the root-mean-square-error or RMSE.
## compare with RST - tension of 60, same points#v.surf.rst in=rs elev=elev.rst zcol=elev tension=60r.colors map=elev.rst rast=elevation.dem# compute SE between kriged map and originalr.mapcalc "d = (elev.ok - elevation.dem)^2 "r.colors map=d color=rainbowd.rast dd.vect rs size=4# compute SE between RST map and originalr.mapcalc "d2 = (elev.rst - elevation.dem)^2"r.colors map=d2 color=rainbowd.rast d2d.vect rs size=4## compare results:## compute RMSE for OK [sqrt(MSE)]r.univar d# compute RMSE for RST [sqrt(MSE)]r.univar d2# see table below:X
Root-mean-square-error Comparison:
Looks like the RSME are pretty close...
Method | OK | RST |
RMSE | 61 meters | 64 meters |
Software
- General Purpose Programming with Scripting Languages
- LaTeX Tips and Tricks
- PostGIS: Spatially enabled Relational Database Sytem
- PROJ: forward and reverse geographic projections
- GDAL and OGR: geodata conversion and re-projection tools
- R: advanced statistical package
- Access Data Stored in a Postgresql Database
- Additive Time Series Decomposition in R: Soil Moisture and Temperature Data
- Aggregating SSURGO Data in R
- Cluster Analysis 1: finding groups in a randomly generated 2-dimensional dataset
- Color Functions
- Comparison of Slope and Intercept Terms for Multi-Level Model
- Comparison of Slope and Intercept Terms for Multi-Level Model II: Using Contrasts
- Creating a Custom Panel Function (R - Lattice Graphics)
- Customized Scatterplot Ideas
- Estimating Missing Data with aregImpute() {R}
- Exploration of Multivariate Data
- Interactive 3D plots with the rgl package
- Making Soil Property vs. Depth Plots
- Numerical Integration/Differentiation in R: FTIR Spectra
- Plotting XRD (X-Ray Diffraction) Data
- Using lm() and predict() to apply a standard curve to Analytical Data
- Working with Spatial Data
- Customizing Maps in R: spplot() and latticeExtra functions
- Converting Alpha-Shapes into SP Objects
- Some Ideas on Interpolation of Categorical Data
- Visual Interpretation of Principal Coordinates (of) Neighbor Matrices (PCNM)
- Visualizing Random Fields and Select Components of Spatial Autocorrelation
- Generation of Sample Site Locations [sp package for R]
- Target Practice and Spatial Point Process Models
- Ordinary Kriging Example: GRASS-R Bindings
- Point-process modelling with the sp and spatstat packages
- Simple Map Creation
- Comparison of PSA Results: Pipette vs. Laser Granulometer
- GRASS GIS: raster, vector, and imagery analysis
- Generic Mapping Tools: high quality map production