[R] aggregation of irregular interval time-series

Enrico Gabrielli enr|cog@br|e|||76@per@gr @end|ng |rom gm@||@com
Sun Jun 13 18:14:13 CEST 2021


Hello
thanks to my friend Diego, a computer agronomist,
I was able to figure out how to do it.
I still have only one problem: how do I calculate the data for the
first week? The traps were set up on the fields on May 5th. On that
date, a 0 could be entered for all time-series. But how to do it with a
simple command?
> # the data (as copied from kobotoolbox, with the datetime of each
> capture observation, and the farm (azienda) and type of trap
> (trappola)
> library(data.table)
> library(parsedate)
> vindicta_catture_cimici<-data.table::data.table(
>     datetime = parse_iso_8601(c("2021-05-29 17:00:00",
>                  "2021-06-03 09:20:00","2021-06-05 10:52:00","2021-
> 06-03 09:30:00",
>                  "2021-06-01 17:00:00","2021-05-16 08:33:00","2021-
> 05-16 08:34:00",
>                  "2021-06-05 17:00:00","2021-06-05 11:00:00","2021-
> 05-23 18:00:00",
>                  "2021-06-03 08:45:00","2021-06-01 07:30:00",
>                  "2021-05-29 05:30:00","2021-05-30 15:00:00","2021-
> 05-29 06:00:00",
>                  "2021-05-23 06:30:00","2021-05-20 13:00:00","2021-
> 05-20 13:15:00",
>                  "2021-05-20 12:45:00","2021-05-16 16:15:00","2021-
> 05-16 16:30:00",
>                  "2021-05-20 13:00:00","2021-05-15 12:09:00",
>                  "2021-06-11 15:27:00","2021-06-11 15:30:00","2021-
> 06-11 15:43:00",
>                  "2021-06-11 15:50:00","2021-06-12 19:01:00")),
>      catture = c(25,0,92,0,23,0,2,5,0,0,6,0,30,
>                  43,0,23,3,0,0,0,13,4,15,100,0,15,2,20),
>      azienda = as.factor(c("arvaia","toderici",
>                            "arvaia","toderici","arvaia","arvaia","arv
> aia",
>                            "arvaia","arvaia","arvaia","toderici","mag
> li",
>                            "arvaia","magli","arvaia","arvaia","arvaia
> ","arvaia",
>                            "magli","magli","magli","magli","arvaia",
>                            "arvaia","arvaia","magli","magli","arvaia"
> )),
>     trappola =
> as.factor(c("fmach__feromoni___vibrazione","pessl_elettronica",
>                            "fmach__feromoni___vibrazione","cymatrap__
> feromoni",
>                            "fmach__feromoni___vibrazione","cymatrap__
> feromoni",
>                            "fmach__feromoni___vibrazione","fmach__fer
> omoni___vibrazione",
>                            "cymatrap__feromoni","cymatrap__feromoni",
>                            "fmach__feromoni___vibrazione","fmach__fer
> omoni___vibrazione",
>                            "fmach__feromoni___vibrazione",
>                            "fmach__feromoni___vibrazione","cymatrap__
> feromoni",
>                            "fmach__feromoni___vibrazione","fmach__fer
> omoni___vibrazione",
>                            "cymatrap__feromoni","cymatrap__feromoni",
> "cymatrap__feromoni",
>                            "fmach__feromoni___vibrazione",
>                            "fmach__feromoni___vibrazione","fmach__fer
> omoni___vibrazione",
>                            "fmach__feromoni___vibrazione","cymatrap__
> feromoni",
>                            "fmach__feromoni___vibrazione","cymatrap__
> feromoni",
>                            "fmach__feromoni___vibrazione"))
> )
> 
> # calculates the cumulative capture by type of trap + farm
> library(dplyr)
> vindicta_catture_cimici = vindicta_catture_cimici %>%
> group_by(trappola,azienda) %>% arrange(datetime) %>% mutate(cs =
> cumsum(catture))
> vindicta_catture_cimici <-
> group_by(vindicta_catture_cimici,trappola,azienda)
> 
> # generates a matrix of the different irregular time-series of the
> cumulative values of capture per trap
> library(reshape2)
> library(xts)
> vindicta_catture_cimici_xts <-
> as.xts.data.table(data.table(dcast(vindicta_catture_cimici, datetime
> ~ trappola + azienda,  value.var = "cs")))
> 
> # calculates by interpolation the capture data at the end of each
> week, starting from May 10 (i.e. the week we started capturing)
> tseq <- seq(ISOdate(2021,5,10,0,0,0),
> end(vindicta_catture_cimici_xts), by = "week")
> vindicta_catture_cimici_xts_week <-
> na.approx(vindicta_catture_cimici_xts,xout=tseq)
> vindicta_catture_settimanali <-
> diff.xts(vindicta_catture_cimici_xts_week)
> 
> library(TSstudio)
> ts_plot(vindicta_catture_settimanali)

Thank you

Enrico Gabrielli



More information about the R-help mailing list