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


On Sun, Aug 21, 2011 at 7:12 PM, Richard Ma <xuanlong.ma@uts.edu.au> wrote:

> Hi all,
>
> I have a RasterStack with 23 layers. Now I want to fit a local polynomial
> regression through 23 layers. I use the package "KernSmooth" to fit the
> regression function.
>
> Here is the example data, you can download them from my Dropbox folder,
> it's
> a native raster package format:
> http://dl.dropbox.com/u/19228076/cmg_stack.grd
> http://dl.dropbox.com/u/19228076/cmg_stack.gri
>
> Here is my code:
> #########################################
> require(raster)
> require(KernSmooth)
>
> cmg.stack <- stack("cmg_stack.grd")
>
> # Calculate the bandwidth for each cell
> time <- seq(1, 353, 16)
> gs <- 365
> fun.bw <- function(x) dpill(x, x = time, gridsize = gs)
> bw <- calc(cmg.stack, fun.bw)  # This line works fine
>
> # Create a function to apply the "locpoly" function
> fun.lp <- function(x) {yvalues <- locpoly(x = time, y = x, bandwidth = bw,
> gridsize = gs)$y
> return(yvalues)}
>
> #########################################
> #### However, I got an error:
> #########################################
>
> > cmg.stack.lp <- calc(cmg.stack, fun.lp, progress = "text")
> Error in .local(x, fun, ...) : cannot use this function
>
> #########################################
>
> Can you please point my problem? Is that possible to apply this function by
> "calc"? Or are there any other solution to my question?
>
> Thanks in advance for any help.
>
> Sincerely,
> Richard
>
>
> -----
> 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-tp6709976p6709976.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

