[R-sig-Geo] temporal aggregate STFDF of class SpatialPixels

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu May 7 18:46:40 CEST 2015


Aurelie, it is not so clear to me what is wrong with x, and why

x.sd = aggregate(st, st.grid, sd, na.rm=TRUE)

is not your standard deviation map (NA when n=1).

On 05/06/2015 08:46 PM, Aurelie Cosandey Godin wrote:
> I’m having a hard time to figure out how to aggregate my STFDF of class SpatialPixels by time. I have yet been successful with the different codes provided in the vignettes, including 'Spatio-temporal overlay and aggregation’. Below an example of what I did so far with some reproducible data (n=50). Ultimately, what I would like to get is 2 maps showing the mean & sd per grid cells for the time-serie.
> I would be grateful for any tips and/or further references.
> 
> library(rgdal)
> library(spacetime)
> library(maptools)
> library(fields)
> 
> ### create spacetime SpatialPoints
> km.proj<-"+proj=utm +zone=20 +ellps=GRS80 +datum=NAD83 +units=km +no_defs"
> sp0 <- SpatialPoints(cbind(smp[2:3]), CRS(km.proj))
> smp$date<-as.Date(smp$caught_date)
> o <- order(smp$date)
> ord.smp <- smp[o, ]
> st <- STIDF(sp0, ord.smp$date, data.frame(counts=ord.smp$lamnaN))
> class(st);dim(st)
> 
> ### create spacetime SpatialPixel
> yrs <- as.xts(ts(2000:2001, start = 2000, end = 2001))
> grid <- maptools::Sobj_SpatialGrid(sp0, maxDim = 20)$SG
> st.grid <- STF(as(grid, "SpatialPixels"), yrs)
> class(st.grid);dim(st.grid)
> 
> ### over and calculate sum per grid cells per time (year)
> o <- over(st, st.grid, timeInterval = TRUE)
> nG <- length(st.grid);nG ###length(grid)*length(yrs)
> xx <- rep(as.numeric(NA), nG)
> xx[na.omit(unique(o))] <- tapply(st$counts, o, sum)
> 
> ### now create STFDF
> st.df <- STFDF(as(grid, "SpatialPixels"), yrs, data.frame(counts=xx))
> class(st.df)
> 
> ### plot results - sum per year per grid cells - ok
> stplot(st.df[,,"counts"],col.regions=tim.colors(100))
> 
> ### aggreggate by year & calculate mean & sd per grid cells — ?
> x = aggregate(st, st.grid, mean, na.rm=TRUE)
> dim(x)
> 
> ### ‘smp' dataset from dput
> 
> structure(list(caught_date = structure(c(11553, 11493, 11163,
> 11230, 11136, 11167, 11555, 11175, 11203, 11151, 11156, 11494,
> 11511, 11119, 11509, 11185, 11152, 11496, 11519, 11146, 11530,
> 11551, 11531, 11553, 11561, 11180, 11171, 11537, 11510, 11547,
> 11123, 11178, 11167, 11145, 11158, 11503, 11532, 11116, 11494,
> 11136, 11486, 11553, 11594, 11541, 11554, 11136, 11140, 11552,
> 11528, 11187), class = "Date"), lon.1 = c(358.793648700216, 286.892896235879,
> 798.310180942244, 549.135889360351, 295.491807527005, 297.923053440568,
> 365.705980463698, 489.067053994588, 638.516556834411, 307.730448797207,
> 294.464328249258, 251.169144523995, 398.925128181111, 538.810507777824,
> 458.43921795352, 589.642893203881, 296.060530322952, 334.149870988617,
> 628.07289715988, 415.236403654722, 619.379291660565, 584.897269766797,
> 783.592928906592, 355.008272601906, 482.690947723179, 647.435430118951,
> 430.425648481571, 847.641625291344, 401.994005499202, 849.144604908023,
> 567.613658701662, 506.717635075427, 786.341895915623, 431.46321495197,
> 512.221678849205, 573.011188860812, 794.547821664531, 530.926063605998,
> 546.115559907762, 953.86614046656, 529.363903413769, 839.737718985791,
> 462.472901159312, 397.595046444824, 966.694817988262, 294.261805585718,
> 979.492106837036, 263.295370991572, 259.877252483983, 993.483814446571
> ), lat.1 = c(4699.31441351726, 4443.69642631574, 4841.4992143951,
> 4723.96662541948, 4597.16267100868, 4634.13120981602, 4701.033132503,
> 4738.61419257583, 4699.26053767776, 4635.71561133898, 4656.45644873692,
> 4392.93208529894, 4533.90089738162, 4612.86525216596, 4629.53486779131,
> 4757.6919040132, 4663.8228674752, 4459.17290316713, 4739.79501424199,
> 4829.82694893005, 4769.25379674495, 4918.69835234919, 4839.01123475013,
> 4712.35633026013, 4890.39979182653, 4790.17886139686, 4737.10188206371,
> 4877.29908563213, 4544.96262443328, 4873.66909269475, 4648.24420006752,
> 4888.53665975929, 4837.27011731383, 4703.77559588887, 4740.45970235832,
> 4526.1637933126, 4833.92057390398, 4520.316776434, 4551.84626738515,
> 4753.56467869758, 4546.20917123179, 4875.03924698965, 4614.7125586109,
> 4728.25897543145, 4940.46683542445, 4600.89803274237, 4719.95341291182,
> 4590.73349116477, 4350.0552020377, 4955.49038457206), lamnaN = c(0.132403257435239,
> 0.00986449910759576, 1.44198417375472, 24.0597205187391, 0.0896519212396812,
> 0.558793472572878, 0.157906364505386, 17.5795994790222, 2.35809137691014,
> 0.24964640218761, 0.0939528058140439, 0.0416547606589983, 0.014734211447768,
> 6.2508641477239, 0.0199380386844502, 13.5418886687007, 0.00328074837168618,
> 2.94167877888201, 0.0204603488249901, 0.00913488409772878, 0.0136811391853637,
> 10.582767887804, 1.53626363359338, 0.00438555392968387, 6.48135523554447,
> 1.38300940781837, 1.14603348696184, 3.90203176489085, 0.00928059417734763,
> 4.72367397453852, 1.66075463850196, 2.83411594233376, 15.12151247069,
> 13.5492935783335, 3.76539649494977, 31.0406032243628, 20.6771788315206,
> 7.61020373367985, 0.0957017085403933, 1.70078928840253, 0.60206011946782,
> 7.66127313641195, 0.194463222598053, 0.372038964017632, 0.142217215957269,
> 0.343157214996115, 0.359141531218846, 0.204396194758893, 0.00577353772801701,
> 0.228668002958544)), .Names = c("caught_date", "lon.1", "lat.1",
> "lamnaN"), row.names = c(6598L, 3679L, 18824L, 13966L, 4099L,
> 4150L, 6819L, 10682L, 16557L, 4763L, 4015L, 860L, 7834L, 13567L,
> 9535L, 15199L, 4070L, 5879L, 16300L, 8151L, 16057L, 15114L, 18640L,
> 6500L, 10467L, 16703L, 8680L, 19355L, 7949L, 19374L, 14597L,
> 11803L, 18666L, 8734L, 12069L, 14687L, 18756L, 13277L, 13795L,
> 20088L, 13112L, 19159L, 9675L, 7719L, 20285L, 4072L, 20252L,
> 1536L, 1740L, 20418L), class = "data.frame”)
> 
> --
> 
> Aurelie Cosandey-Godin, PhD
> Dalhousie University, Department of Biology
> godina at dal.ca<mailto:godina at dal.ca> | m: +1 902 499-0984
> 
> 
> 	[[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
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi),  University of Münster,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150507/bbba807f/attachment.bin>


More information about the R-sig-Geo mailing list