-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_preprocess_gasprice_data.Rmd
65 lines (50 loc) · 1.36 KB
/
02_preprocess_gasprice_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: "Connecting to PostgreSQL"
author: "Henning"
output: html_document
---
# Dataprep
### Inspect and merge old data for Aral-Timeseries
```{r}
library(xts)
merge_ts <- function(ts_old, ts_akt, col = "e10"){
ts_old <- xts(x = ts_old[col], order.by = ts_old$date)
ts_akt <- xts(x = ts_akt[col], order.by = ts_akt$date)
ts_merged <- rbind(ts_old, ts_akt)
return(ts_merged)
}
diesel <- merge_ts(ts_old, ts_new, col = "diesel")
```
## Put the data on a hourly grid (take last price)
```{r}
on_hourly_grid <- function(ts){
# every 1 hours
s1 <- align.time(index(first(ts)), n = 3600)
s2 <- align.time(index(last(ts)), n = 3600)
x1 <- seq(s1, s2, by = "1 hours")
d1 <- align.time(ts, n = 3600)
m2 <- cbind(d1, xts(order.by = x1))
ts_hourlygrid <- na.locf(m2)
return(ts_hourlygrid)
}
diesel_h <- on_hourly_grid(diesel)
```
## Average over multiple prices on the same grid point
```{r}
avg_ts <- function(ts){
ts_avg <- aggregate(ts, index(ts), function(d) mean(d))
ts_avg <- as.xts(ts_avg)
return(ts_avg)
}
diesel_h <- avg_ts(diesel_h)
saveRDS(diesel_h, "data/diesel_h.RDS")
```
# Aggregate Time Series
```{r}
# diesel_h
diesel_d <- apply.daily(diesel_h, maen, na.rm = TRUE)
diesel_w <- apply.weekly(diesel_h, mean, na.rm = TRUE)
diesel_m <- apply.monthly(diesel_h, mean, na.rm = TRUE)
d <- cbind(diesel_h, diesel_d, diesel_w, diesel_m)
plot(as.zoo(d))
```