A simple example of Inverse Distance Weight (IDW) for Envinronmental Interpolation in R

Feature: use idw(gstat) function

Data was taken from the lecture: Nicolas Christon. Ordinary kriging using geoR and gstat. Statistics C173/C273. Available online at :http://www.stat.ucla.edu/~nchristo/statistics_c173_c273/c173c273_lec11_w11.pdf

Packages requirement are sp and gstat. They can be downloaded by using install.packages command or Tools/Install Packages…

library(sp)
## Warning: package 'sp' was built under R version 3.2.5
library(gstat)

Known sampled data of X,Y coordinates with corresponding of Z value

X = c(61,63,64,68,71,73,75)
Y = c(139,140,129,128,140,141,128)
Z = c(477,696,227,646,606,791,783)

Here’s unknown sampled data of X, Y coordinate

X1 = 65; Y1 = 137

Question is that what is the Z1 value corresponding to cordinate (X1, Y1) We create data frame for known sampled data

knowndt = data.frame(X,Y,Z)

Here’s coordinates for known sampled data

coordinates(knowndt)  <- ~ X + Y

Here’s data frame for unknown sampled data

unknowndt = data.frame(X1,Y1)

Here’s coordinate for known sampled data

coordinates(unknowndt)  <- ~ X1 + Y1

Here’s IDW prediction command

idwmodel = idw(Z ~1, knowndt,unknowndt,
                   maxdist = Inf, idp = 2) # set radius distance is infitive and expotinal parameter is 5
## [inverse distance weighted interpolation]
predZ= idwmodel@data$var1.pred
predZ
## [1] 597.6204