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

Dutrieux, Loic loic.dutrieux at wur.nl
Tue May 19 16:47:20 CEST 2015


Hi Srinivas, 

I do not think calc handles the z dimension by itself, you need to format the time-series within the function you're giving to calc().
I got an example below kind of working but it is so slow that I suspect something is not going right with it. I think the key is to figure the right formatting for the time-series you give to StructTS().

library(raster)
library(bfast)
library(zoo)
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")))


time <- getZ(modisbrick)

xStructTS <- function(x) {
  ts <- bfastts(x, time, type='16-day')
  fun <- function(ts) {
    StructTS(ts, type = 'BSM')$fitted[1]
  }
  fit <- sapply(ts, fun)
  return(fit)
}

r1.structs<- calc(modisbrick, fun=xStructTS) # Extremely slow

r1.structs



You can try to find the right formatting for the StructTS function by working on a single vector.

ts <- zoo(t(modisbrick[1]), time) 
StructTS(ts, type = 'BSM') # Returns an error about frequency of the data

ts  <- bfastts(t(modisbrick[1]), time, type='16-day')
StructTS(ts, type = 'BSM') # Works but really slowly


Hope it helps,
Loïc
 
________________________________________
From: R-sig-Geo <r-sig-geo-bounces at r-project.org> on behalf of Srinivas V <srinivasv at feralindia.org>
Sent: Tuesday, May 19, 2015 12:47 PM
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Fitting a basic structural model to a raster brick

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

_______________________________________________
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