Aggregation

Aggregation

DateValgator is a data type that supports operation for aggregation to minimize noise and lessen the occurrence of missing data. It expects to receive one argument which is the date-time interval for grouping values by taking their median. For example, hourly median as the basis of aggregation can be carried out by passing this argument: :dateinterval => Dates.Hour(1)

To illustrate DateValgator usage, let's start by generating an artificial data with sample frequencey every 5 minutes and print the first 10 rows.

using Dates, DataFrames
gdate = DateTime(2014,1,1):Dates.Minute(5):DateTime(2014,5,1)
gval = rand(length(gdate))

df = DataFrame(Date=gdate,Value=gval)
first(df,10)

10 rows × 2 columns

DateValue
Dates…Float64
12014-01-01T00:00:000.317564
22014-01-01T00:05:000.286774
32014-01-01T00:10:000.702091
42014-01-01T00:15:000.497805
52014-01-01T00:20:000.0628874
62014-01-01T00:25:000.817261
72014-01-01T00:30:000.412464
82014-01-01T00:35:000.449599
92014-01-01T00:40:000.00206474
102014-01-01T00:45:000.513834

DateValgator

Let's apply the aggregator and try diffent groupings: hourly vs half hourly vs daily aggregates of the data.

using TSML, TSML.TSMLTransformers, TSML.Utils, TSML.TSMLTypes

hourlyagg = DateValgator(Dict(:dateinterval => Dates.Hour(1)))
halfhourlyagg = DateValgator(Dict(:dateinterval => Dates.Minute(30)))
dailyagg = DateValgator(Dict(:dateinterval => Dates.Day(1)))

fit!(halfhourlyagg,df)
halfhourlyres = transform!(halfhourlyagg,df)

fit!(hourlyagg,df)
hourlyres = transform!(hourlyagg,df)

fit!(dailyagg,df)
dailyres = transform!(dailyagg,df)

The first 5 rows of half-hourly, hourly, and daily aggregates:

julia> first(halfhourlyres,5)
5×2 DataFrames.DataFrame
│ Row │ Date                │ Value    │
│     │ Dates.DateTime      │ Float64⍰ │
├─────┼─────────────────────┼──────────┤
│ 1   │ 2014-01-01T00:00:00 │ 0.317564 │
│ 2   │ 2014-01-01T00:30:00 │ 0.412464 │
│ 3   │ 2014-01-01T01:00:00 │ 0.634207 │
│ 4   │ 2014-01-01T01:30:00 │ 0.576241 │
│ 5   │ 2014-01-01T02:00:00 │ 0.490236 │

julia> first(hourlyres,5)
5×2 DataFrames.DataFrame
│ Row │ Date                │ Value    │
│     │ Dates.DateTime      │ Float64⍰ │
├─────┼─────────────────────┼──────────┤
│ 1   │ 2014-01-01T00:00:00 │ 0.407684 │
│ 2   │ 2014-01-01T01:00:00 │ 0.481717 │
│ 3   │ 2014-01-01T02:00:00 │ 0.626427 │
│ 4   │ 2014-01-01T03:00:00 │ 0.498177 │
│ 5   │ 2014-01-01T04:00:00 │ 0.285262 │

julia> first(dailyres,5)
5×2 DataFrames.DataFrame
│ Row │ Date                │ Value    │
│     │ Dates.DateTime      │ Float64⍰ │
├─────┼─────────────────────┼──────────┤
│ 1   │ 2014-01-01T00:00:00 │ 0.46017  │
│ 2   │ 2014-01-02T00:00:00 │ 0.503287 │
│ 3   │ 2014-01-03T00:00:00 │ 0.488554 │
│ 4   │ 2014-01-04T00:00:00 │ 0.515489 │
│ 5   │ 2014-01-05T00:00:00 │ 0.496321 │