-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDA.Rmd
179 lines (135 loc) · 3.55 KB
/
EDA.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: "EDA"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(zoo)
```
# Set correct path
```{r}
#path = "~/Documents/University/WessexWater/wessexwater/"
setwd(path)
getwd()
```
# Load Flow data and Repair Jobs
```{r}
load("../datFlow.rda")
load("../repFlow.rda")
```
# Select a Flow Series & Transform date to Date format
```{r}
myid = "AW008"
df = datFlow %>%
filter(id == myid) %>%
mutate(date = as.Date(date))
```
# Plot it
```{r}
ggplot(data=df, aes(x=date, y=y)) +
geom_point()
```
# Grab repFlow and filter by the same ID. Find start and end dates
```{r}
tmpStart = repFlow %>%
filter(id == myid) %>%
select(starts_with("Start")) %>%
select_if(~ !any(is.na(.))) %>%
mutate_all( ~ as.Date(.)) %>%
gather
```
```{r}
tmpEnd <- repFlow %>% filter(id == myid) %>%
select(starts_with("End")) %>%
select_if(~ !any(is.na(.))) %>%
mutate_all( ~ as.Date(.)) %>% gather
```
# Plot all the repair jobs, if there are any
```{r}
pl = ggplot(data=df, aes(x=date, y=y)) +
geom_point()
if(nrow(tmpStart)){
pl <- pl + geom_vline(xintercept = tmpStart$value, col = 3)
}
if(nrow(tmpEnd)){
pl <- pl + geom_vline(xintercept = tmpEnd$value, col = 2)
}
```
# Compute minimum night flow
```{r}
mnf = df[(9 <= df$tod) & (df$tod <= 21), ] %>% group_by(id, date) %>% summarise(mnf=min(y)) %>% ungroup()
```
# Compute average daily flow
```{r}
adf = df[(9 > df$tod) | (df$tod > 21), ] %>% group_by(id, date) %>% summarise(adf=mean(y)) %>% ungroup()
```
# Rolling average
```{r}
n_days = 7
rolling = data.frame(
date=as.Date(rollmean(mnf$date, k=n_days)),
mnf=rollmean(mnf$mnf, k=n_days),
adf=rollmean(adf$adf, k=n_days)
)
```
# Plot
```{r}
pl = rolling %>%
gather(key, value, -date) %>%
{ggplot(data=.) + geom_line(aes(x=date, y=value, color=key))}
if(nrow(tmpStart)){
pl <- pl + geom_vline(xintercept = tmpStart$value, col = 3)
}
if(nrow(tmpEnd)){
pl <- pl + geom_vline(xintercept = tmpEnd$value, col = 2)
}
```
# Put the series together
```{r}
mnf_adf = mnf %>% mutate(adf=adf$adf)
mnf_adf %>% gather(key, value, -id, -date) %>%
{ggplot(data=.) + geom_line(aes(x=date, y=value, color=key))}
```
# New Idea
Clustering. I want to grab a subset of timeseries. For each time series, I want to obtain the ratio between rolling average MNF and rolling average ADF. I call this ratio_rolling. Then I want to cluster these timeseries based on ratio rolling.
### Grab first 50 series
```{r}
series = unique(datFlow$id)[1:50]
```
### grab equivalent data
```{r}
df = datFlow %>%
filter(id %in% series)
```
### Decide start of night and end of night
```{r}
night_start = 9 # 2 am
night_end = 21 # 5:30 am
```
### Calculate MNF & ADF
```{r}
mnf = df %>% filter((night_start <= tod) & (tod <= night_end)) %>% group_by(id, date) %>% summarise(mnf=min(y)) %>% ungroup()
adf = df %>% filter((night_start > tod) | (tod > night_end)) %>% group_by(id, date) %>% summarise(adf=mean(y)) %>% ungroup()
```
### Compute 7 days rolling average for MNF
```{r}
inter_roll <- function(df, n_days=7){
new <- df %>%
mutate(mnf_roll=rollmean(mnf, n_days, na.pad=TRUE)) %>%
select(-mnf)
new$mnf_roll = na.spline(new$mnf_roll, 1:nrow(new))
return(new)
}
```
```{r}
dfs = mnf %>%
filter(id == series[1]) %>%
mutate(mnf_roll=rollmean(mnf, 7, na.pad=TRUE))
for (ix in 2:50){
dfs = mnf %>%
filter(id == series[ix]) %>%
mutate(mnf_roll=rollmean(mnf, 7, na.pad=TRUE)) %>%
rbind(dfs, .)
}
```