[R-sig-Geo] How to apply a local polynomial regression function to a RasterStack object by "calc" function?

Richard Ma xuanlong.ma at uts.edu.au
Tue Aug 23 02:17:37 CEST 2011


Hi Robert,

Thank you so much! Problem solved.

As you said, function "locpoly" can't receive a RasterLayer as argument, why
after we stack them, the function works?

Here I paste all my code:
###################################
Aug 22, 2011
Sydney, OZ
Richard Ma
Local polynomial smoothing raster images

# Package required
require(raster)
require(KernSmooth)

# Read the data in
dir <- ("/Users/forestinthesea/Downloads/")
file <- ("cmg_stack.grd")

cmg.stack <- stack(paste(dir, file, sep = ""))

# Calculate the smoothing bandwidth
time <- seq(1, 353, 16)
gs <- 353
fun.bw <- function(x) dpill(x, x = time, gridsize = gs)
bw <- calc(cmg.stack, fun.bw)

# Local polynomial smoothing
s <- stack(bw, cmg.stack)
fun.lp <- function(x) {locpoly(x = time, y = x[-1], bandwidth = x[1],
gridsize = gs)$y}
cmg.stack.lp <- calc(s, fun.lp, progress = "text" )
###################################

Regards,
Richard


Robert Hijmans wrote:
> 
> Richard,
> 
> You are supplying a RasterLayer as the value of the bandwidth argument of
> function "locpoly". That is not what locpoly expects, but I think you can
> let raster::calc deal with that like this:
> 
> s <- stack(bw, cmg.stack)
> fun.lp <- function(x) {locpoly(x = time, y = x[-1], bandwidth = x[1],
> gridsize = gs)$y}
> cmg.stack.lp <- calc(cmg.stack, fun.lp, progress = "text")
> 
> Robert
> 


-----
Richard Ma
PhD student, Ecology & Remote Sensing
Climate Change Cluster, Department of Environment Science
University of Technology, Sydney
http://everydropr.wordpress.com
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/How-to-apply-a-local-polynomial-regression-function-to-a-RasterStack-object-by-calc-function-tp6709976p6714317.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list