[R-sig-Geo] Fitting a basic structural model to a raster brick

Srinivas V srinivasv at feralindia.org
Tue May 19 12:47:16 CEST 2015


Dear Members
I'm trying to write a function to fit a basic structural model to a 
raster brick of NDVI data. I looked up earlier post and figured out the 
use of wrapper functions and calc(). However I have not been very 
successful.
library(raster)
f <- system.file("extdata/modisraster.grd", package="bfast")
modisbrick<-brick(f)
modisbrick<-setZ(modisbrick,as.Date(strptime(paste(substr(names(modisbrick),start=2, 
stop=12)),"%Y.%m.%d")))
xStructTS <- function(x) {
   fit <- StructTS((x),type="level")
   return(fit$fitted[1])}

r1.structs<- calc(modisbrick, fun=function(x){
   res <- (apply(x, 2, xStructTS))
   return(res)
})

r1.structs

Returns a brick with levels not a problem so far. However when I change 
the class of the model I get errors.


### Change type from level to BSM ###
xStructTS <- function(x) {
   fit <- StructTS((x),type="BSM")
   return(fit$fitted[1])}
r1.structs<- calc(modisbrick, fun=function(x){
   res <- (apply(x, 2, xStructTS))
   return(res)
})
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot 
use this function
Not sure why I'm getting this error message, the data does not have any 
NA. Is this error related to the the class of structural model? How do I 
overcome this error?

Second actual NDVI data for which I'm writting the function has NAs due 
to a cropping and masking. Any suggestion to fix this error and also 
address issues of NAs is greatly appreciated.

Thanks

-- 

Srinivas Vaidyanathan
Senior Research Fellow
Foundation for Ecological Research, Advocacy & Learning

Web: www.feralindia.org


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list