I have written an R function that performs a robust regression (least absolute deviation method) against a DEM to up-sample climate variables. It works quite well for smaller areas where the gradient in the [X,Y] domain does not effect the estimates and is quite superior to resampling and interpolation techniques. It is a loose implementation of Nick Zimmermann's Fortran code. I find that a random sample usually provides the best results however, for comparison sake, you may want to try a systematic sample as well. Other than needing enough RAM to hold the subsample and run the model, as long as a output raster file is specified, it is memory safe.
##########################################################################
# PROGRAM: RasterUpSample.R
# USE: UP SAMPLES A RASTER USING ROBUST REGRESSION
# REQUIRES: RGDAL FORMAT COMPATIBLE RASTERS
# PACKAGES: MASS, sp, raster, rgdal
#
# ARGUMENTS:
# x X (HIGHER RESOLUTION) INDEPENDENT VARIABLE RASTER
# y Y (LOWER RESOLUTION) DEPENDENT VARIABLE RASTER
# p PERCENT SUBSAMPLE (DEFAULT=0.05 or 5%)
# sample.type TYPE OF SAMPLE (random OR systematic); DEFAULT IS random
# file IF SPECIFIED, A RASTER SURFACE WILL BE WRITTEN TO DISK.
# THE FILE EXTENSION WILL DICTATE THE RASTER FORMAT.
# ... ADDITIONAL ARGUMENTS PASSED TO predict
#
# EXAMPLES:
# setwd("C:/ANALYSIS/TEST/RRR")
# x <- paste(getwd(), paste("elev", "img", sep="."), sep="/")
# y <- paste(getwd(), paste("precip90", "img", sep="."), sep="/")
# RasterUpSample(x=x, y=y, p=0.01, sample.type="random", filename="RPREDICT.img")
# praster <- raster( paste(getwd(), "RPREDICT.img", sep="/"))
# Y <- raster(paste(getwd(), paste("precip90", "img", sep="."), sep="/"))
# op <- par(mfrow = c(1, 2))
# plot(Y)
# plot(praster)
# par(op)
#
# CONTACT:
# Jeffrey S. Evans
# Senior Landscape Ecologist
# The Nature Conservancy
# Central Science/DbyD
# Affiliate Assistant Professor
# Environment and Natural Resources
# University of Wyoming
# Laramie, WY 82070
# jeffrey_evans@tnc.org
# (970) 672-6766
##########################################################################
RasterUpSample <- function(x, y, p=0.05, sample.type="random", filename=FALSE, ...) {
if (!require(MASS)) stop("MASS PACKAGE MISSING")
if (!require(sp)) stop("sp PACKAGE MISSING")
if (!require(raster)) stop("raster PACKAGE MISSING")
if (!require(rgdal)) stop("rgdal PACKAGE MISSING")
X <- stack(x)
Y <- raster(y)
if(sample.type == "random") {
print("SAMPLE TYPE RANDOM")
SubSamps <- sampleRandom(X, ((nrow(X)*ncol(X))*p), sp=TRUE)
}
if(sample.type == "systematic") {
print("SAMPLE TYPE SYSTEMATIC")
SubSamps <- sampleRegular(X, ((nrow(X)*ncol(X))*p), asRaster=TRUE)
SubSamps <- as(SubSamps, 'SpatialPointsDataFrame')
}
Yvalues <- extract(Y, SubSamps)
SubSamps@data <- data.frame(SubSamps@data, Y=Yvalues)
( rrr <- rlm(as.formula(paste(names(SubSamps@data)[2], ".", sep=" ~ ")),
data=SubSamps@data, scale.est="Huber", psi=psi.hampel,
init="lts") )
if (filename != FALSE) {
predict(X, rrr, filename=filename, na.rm=TRUE, progress='window',
overwrite=TRUE, ...)
print(paste("RASTER WRITTEN TO", filename, sep=": "))
}
print(paste("MEAN RESIDUAL ERROR", round(mean(rrr$residuals), digits=5), sep=":"))
print(paste("AIC", round(AIC(rrr), digits=5), sep=": "))
return(rrr)
}