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

Srinivas V srinivasv at feralindia.org
Fri May 22 15:21:29 CEST 2015


Hi Loïc,
Thanks for the response. I took a few pixels and checked it converting 
it to a vector the errors were due formatting of the time-series.
As you have pointed out the revised script is very slow and I suspect it 
is slow because it returns a raster layer instead of a brick, it should 
have the same number of layers as the original brick. Caused by sapply? 
How do I resolve this?

Thanks for your advice.
Regrads

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

Web:www.feralindia.org  

On Tuesday 19 May 2015 08:17 PM, Dutrieux, Loic wrote:
> 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


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list