[R-sig-Geo] spatial time series

Robert Hijmans r.hijmans at gmail.com
Mon Jun 6 21:34:59 CEST 2011


> 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


--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/spatial-time-series-tp6444907p6446630.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list