-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
142 lines (104 loc) · 3 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# warp10r
<!-- badges: start -->
[](https://www.tidyverse.org/lifecycle/#experimental)
<!-- badges: end -->
R client for executing WarpScript on a Warp 10 instance.
## Fork
The original package [https://github.com/senx/warp10-r] has been forked to make the package more concilient with current developments of R packages :
- Package has been moved to the root of the git repository
- Dependancies have been added to the DESCRIPTION
- Construct a warp 10 script with helpers function and send them to Warp 10 database
## Installation
```r
remotes::install_github("centreon/warp10-r")
```
## First steps
### Hello World
```{r}
library(warp10r)
# Create a connection
con <- wrp_connect(endpoint = "https://warp.senx.io/api/v0/exec")
print(con)
# set_script store a script in the connection object.
set_script(con, "'Hello World'")
set_script(con, "NOW")
print(con)
# We can see the script
cat(get_script(con))
# Execute the script
wrp_exec(con)
```
### Example with Geo Time Series
```{r}
library(tibble)
df1 <- tibble(ds = 1:10, y = rnorm(10))
df2 <- tibble(ds = 2:11, y = rnorm(10))
con %>%
clear_script() %>%
wrp_new_gts() %>%
wrp_rename("randGTS") %>%
wrp_add_value_df(df1, tick = ds, value = y) %>%
wrp_new_gts() %>%
wrp_add_value_df(df2, tick = ds, value = y) %>%
wrp_rename("nogeoTS") %>%
wrp_exec()
```
## Outlier detection
Anomaly detection is already implemented in Warpscript.
Let's take twitter data as an example.
### Set up and show data
```{r}
library(AnomalyDetection)
library(dplyr)
library(ggplot2)
library(hrbrthemes)
library(lubridate)
library(purrr)
data("raw_data")
raw_data %>%
ggplot(aes(timestamp, count)) +
geom_line() +
theme_ipsum(base_family = "Verdana")
```
### Create a subset of the data
Default settings do not authorize more than 1,000 operations for a warpscript.
Therefore, let's take the previous data and concatenate them into 3 hours timespan.
```{r}
df <- raw_data %>%
group_by(date_hour = ceiling_date(timestamp, "3 hours")) %>%
summarise_at("count", sum)
df %>%
ggplot(aes(date_hour, count)) +
geom_line() +
theme_ipsum(base_family = "Verdana")
```
### Lets find some anomalies
```{r}
res <- con %>%
wrp_new_gts() %>%
wrp_rename("twitter data") %>%
wrp_add_value_df(df, tick = "date_hour", value = "count") %>%
wrp_bucketize(span = "3 h", bucketizer = "sum") %>%
wrp_hybridtest(period = 8, piece = 4, k = 4) %>%
wrp_exec()
print(res)
# Results is a list of abnormal dates in microseconds
res_dbl <- map_dbl(res[[1]], ~ .x / 1e6)
df %>%
ggplot(aes(date_hour, count)) +
geom_line() +
geom_vline(xintercept = res_dbl, linetype = 2, color = "red") +
theme_ipsum(base_family = "Verdana")
```