[R-sig-Geo] Regression of a raster brick on image dates with NAs

Beutel, Terry S Terry.Beutel at daff.qld.gov.au
Tue Dec 4 00:27:47 CET 2012


Apologies, but after further searching, I see the problem is addressed here

https://stat.ethz.ch/pipermail/r-sig-geo/2012-April/014824.html

Regards

Terry

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Beutel, Terry S
Sent: Monday, 3 December 2012 6:55 PM
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Regression of a raster brick on image dates with NAs

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]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list