Hi
I have a raster brick composed of multiple date imagery, and I want to regress the pixel values on image dates to generate an image of the slope coefficents. This is covered in the Raster package help files (p35) as

# regression of values in one brick (or stack) with 'time'
time <- 1:nlayers(s)
fun <- function(x) { lm(x ~ time)$coefficients[2] }
x2 <- calc(s, fun)

The above to work well if there are sufficient non NA values in each pixel column to allow a regression. However if the brick includes pixel columns that are NA on all dates, the above approach delivers the following for me

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :   cannot use this function

I have tried inserting an additional layer (layer 1 below) in the raster brick that provides a count of the number of non NA cells in that pixel column, and use this to determine conduct a regression only where sufficient data are available, but the same error is returned. ie

> fun <- function(data) { ifelse (subset(data,1)>4,lm(subset(data,c(2:5)) ~ 1)$coefficients[2],99) }
> out<-calc(data,fun)
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :   cannot use this function

Can anyone suggest where I am going wrong? I suspect this is too common a problem not to have been covered somewhere before, but I was unable to find a relevant post so apologies if I have missed it in the archive,

Many thanks



Dr Terry Beutel

Senior Scientist

Agri-Science Queensland



	[[alternative HTML version deleted]]

