You can use the gdistance
package to create a cost surface raster, then use the shortestPath
function to compute the distances between all your data points. You then need to do your interpolation using that distance matrix as your distance metric and not pythagoras. I think gstat will let you feed in a distance matrix for kriging.
You'll probably also need to use shortestPath
to compute the distances between your data points and the grid points for your interpolated grid when you come to compute the mean and variance of your kriging estimates over your space.
Assuming this is really two-dimensional...
There was a thread on R-sig-geo in 2010 that is relevant and there was a solution using GRASS to compute the distances and a modified geoR to do the kriging:
http://www.mail-archive.com/r-sig-geo@stat.math.ethz.ch/msg06855.html
Usually, it's not good practice to remove outliers, unless you know why they are outliers and have a good justification for the removal. For example, if you know these points were measured incorrectly (wrong equipment, wrong method, wrong annotation, etc) while the others were not, then it is ok. This is what the ESRI blog says in its introduction:
Outliers often result from malfunctions in the monitoring equipment or typos during data entry, such as accidentally removing a decimal. These erroneous data points should be manually corrected or removed before attempting to interpolate.
So, as you mentioned, the purpose of that blog post was to provide a solution when one can't have a good justification to exclude/ignore outliers:
The solution provided was to remove the outliers only in the modelling phase, i.e., when building the variograms, but using the whole dataset in the prediction phase.
While there is no single method that is guaranteed to work, a potential solution is to split the kriging process into two steps...
This workflow is effective because the modeling is not corrupted by the outliers, but the prediction surface still accounts for the extreme values.
If it's the case to ignore outliers, always try to validate the interpolation the most complete way as possible, for example:
- i) using (leave-one-out) cross-validation; and/or
- ii) interpolating values in places not used in the modelling process and comparing them with actual values.
Other methods of validation are described in whuber's answer on:
Spatial interpolation - appropriate method to estimate the accuracy of the surface
Lastly, if one can't get good results with kriging, it might not be the appropriate method for what is being investigated. For example, the assumption of stationarity could not be hold.
Best Answer
Here is an R example that corresponds to the example in the manual you point to:
In the kriging step, the trend value is assumed to be known, the kriging error only refers to predicting the residual, and does not include the estimation error for the trend coefficients, as you would get with universal kriging:
where the trend is estimated as part of the kriging (by generalized least squares).