spatial time series

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

spatial time series

Martin
Dear list,

I have some rasters, respresentig a time series (let's say 5 years and 24 rasters for each year). The rasters are in a raster stack or a SpatialGridDataframe, whatever is better.

Now I want to do some time series analysis which call for a ts class object.


# I have a brick called gimms with 144 layers:

gimms=brick(b)

# I summarize the cell values and create a time series:
 
s=cellStats(gimms, stat='mean')
gimms.ts = ts(s, start=c(2001,1), end=c(2006,24), frequency=24)


# I do some decomposition. The final result is the slope of a regression of the decomp. trend and the time:

gimms2.decomp = decompose(gimms.ts, type="multi")
gimms.trend = gimms2.decomp$trend

gimms.new = time(gimms.ts)
x = lm(gimms.trend ~ gimms.new)

summary(x)$coefficients[2]


Now what I want to do is to do the same with every single pixel and get a gridded result. What is the best appoach to start with? Is there a way to create a time series with my brick or SpatialGidDataframe? Beside this example there are also other things I want to do with my rasters which call for a "ts" class object, but I don't know where to start and I'd be thankful for any hints...

best wishes,
Martin

Martin Brandt

Department of Geography and
Regional Research
UZA II, Althanstr. 14
1090 Wien, AUSTRIA
http://geooekologie.univie.ac.at
Reply | Threaded
Open this post in threaded view
|

Re: spatial time series

Andy Bunn-3
> > Now what I want to do is to do the same with every single pixel and
> get
> > a
> > gridded result. What is the best appoach to start with? Is there a
> way
> > to
> > create a time series with my brick or SpatialGidDataframe? Beside
> this
> > example there are also other things I want to do with my rasters
> which
> > call
> > for a "ts" class object, but I don't know where to start and I'd be
> > thankful
> > for any hints...
> >
>
When I have to do this kind of thing I take my stack and turn it into
an array. Then I write a function to do what I want and apply that
function over the array. Then remake the raster. Something like:

    bar <- as.array(my.raster.stack)
    foo <- apply(bar,c(1,2),function(x) {
        stuff(x)
        })
    foo <- raster(foo)
    extent(foo) <- extent(my.raster.stack)
    projection(foo) <- projection(my.raster.stack)

I'm sure there are other/better ways but this works pretty well for
what I do.

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spatial time series

Robert Hijmans
In reply to this post by Martin
> I have some rasters, respresentig a time series (let's say 5 years and 24 rasters for each year). The rasters are in a
> raster stack or a SpatialGridDataframe, whatever is better.
> Now I want to do some time series analysis which call for a ts class object.
> ...
> Now what I want to do is to do the same with every single pixel and get a gridded result.

Martin, Here is an approach using the 'raster' function 'calc' . Robert

library(raster)
# creating a RasterStack with 144 layers:
r <- raster(nc=10, nr=10)
gimms <- stack(lapply(1:144, function(x) setValues(r, runif(ncell(r)))))

tsfun <- function(x) {  
        gimms.ts = ts(x, start=c(2001,1), end=c(2006,24), frequency=24)
        gimms2.decomp = decompose(gimms.ts, type="multi")
        gimms.trend = gimms2.decomp$trend
        gimms.new = time(gimms.ts)
        x = lm(gimms.trend ~ gimms.new)
        summary(x)$coefficients[2]
}

res <- calc(gimms, fun=tsfun)

plot(res)
res
Reply | Threaded
Open this post in threaded view
|

Re: spatial time series

Martin
thanks a lot for both tips!
Robert, slowly but surely I understand the functionality of the calc functions :-)
this opens endless possibilities...

Martin Brandt

Department of Geography and
Regional Research
UZA II, Althanstr. 14
1090 Wien, AUSTRIA
http://geooekologie.univie.ac.at