-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
215 lines (160 loc) · 6.4 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---
output: github_document
---
[![Coverage status](https://codecov.io/gh/martakarass/runstats/branch/master/graph/badge.svg)](https://codecov.io/github/martakarass/runstats?branch=master)
[![CRAN status](https://www.r-pkg.org/badges/version/runstats)](https://cran.r-project.org/package=runstats)
[![](https://cranlogs.r-pkg.org/badges/grand-total/runstats)](https://cran.r-project.org/package=runstats)
[![](https://cranlogs.r-pkg.org/badges/last-month/runstats)](https://cran.r-project.org/package=runstats)
[![R-CMD-check](https://github.com/martakarass/runstats/workflows/R-CMD-check/badge.svg)](https://github.com/martakarass/runstats/actions)
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# runstats
Package `runstats` provides methods for fast computation of running sample statistics for time series. The methods utilize Convolution Theorem to compute convolutions via Fast Fourier Transform (FFT). Implemented running statistics include:
1. mean,
2. standard deviation,
3. variance,
4. covariance,
5. correlation,
6. euclidean distance.
### Website
Package website is located [here](https://martakarass.github.io/runstats/).
### Installation
```{r, eval = FALSE}
# devtools::install_github("martakarass/runstats")
install.packages("runstats")
```
### Usage
```{r, eval = FALSE}
library(runstats)
## Example: running correlation
x0 <- sin(seq(0, 2 * pi * 5, length.out = 1000))
x <- x0 + rnorm(1000, sd = 0.1)
pattern <- x0[1:100]
out1 <- RunningCor(x, pattern)
out2 <- RunningCor(x, pattern, circular = TRUE)
## Example: running mean
x <- cumsum(rnorm(1000))
out1 <- RunningMean(x, W = 100)
out2 <- RunningMean(x, W = 100, circular = TRUE)
```
### Running statistics
To better explain the details of running statistics, package's function `runstats.demo(func.name)` allows to visualize how the output of each running statistics method is generated. To run the demo, use `func.name` being one of the methods' names:
1. `"RunningMean"`,
2. `"RunningSd"`,
3. `"RunningVar"`,
4. `"RunningCov"`,
5. `"RunningCor"`,
6. `"RunningL2Norm"`.
```{r, eval = FALSE}
## Example: demo for running correlation method
runstats.demo("RunningCor")
```
![](https://i.imgur.com/JQObAu7.gif)
```{r, eval = FALSE}
## Example: demo for running mean method
runstats.demo("RunningMean")
```
![](https://i.imgur.com/FWah3aG.gif)
### Performance
We use `rbenchmark` to measure elapsed time of `RunningCov` execution, for different lengths of time-series `x` and fixed length of the shorter pattern `y`.
```{r, echo=FALSE, include=FALSE}
library(rbenchmark)
library(ggplot2)
```
```{r, eval = FALSE}
library(rbenchmark)
set.seed (20181010)
x.N.seq <- 10^(3:7)
x.list <- lapply(x.N.seq, function(N) runif(N))
y <- runif(100)
## Benchmark execution time of RunningCov
out.df <- data.frame()
for (x.tmp in x.list){
out.df.tmp <- benchmark("runstats" = runstats::RunningCov(x.tmp, y),
replications = 10,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
out.df.tmp$x_length <- length(x.tmp)
out.df.tmp$pattern_length <- length(y)
out.df <- rbind(out.df, out.df.tmp)
}
```
```{r, echo = FALSE, eval = FALSE}
# out.df.path <- "inst/benchmark_results/2018-12-17-performance_rbenchmark_suite1.csv"
# out.df.path <- "inst/benchmark_results/2019-11-13-performance_rbenchmark_suite1.csv"
out.df.path <- "inst/benchmark_results/2019-11-14-performance_rbenchmark_suite1.csv"
write.csv(out.df, out.df.path, row.names = FALSE, quote = FALSE)
```
```{r, echo = FALSE, eval = TRUE}
# out.df.path <- "inst/benchmark_results/2018-12-17-performance_rbenchmark_suite1.csv"
# out.df.path <- "inst/benchmark_results/2019-11-13-performance_rbenchmark_suite1.csv"
out.df.path <- "inst/benchmark_results/2019-11-14-performance_rbenchmark_suite1.csv"
out.df <- read.csv(out.df.path)
options("scipen" = 10, "digits" = 4)
```
```{r}
knitr::kable(out.df)
```
##### Compare with a conventional method
To compare `RunStats` performance with "conventional" loop-based way of computing running covariance in `R`, we use `rbenchmark` package to measure elapsed time of `RunStats::RunningCov` and running covariance implemented with `sapply` loop, for different lengths of time-series `x` and fixed length of the shorter time-series `y`.
```{r, eval = FALSE}
## Conventional approach
RunningCov.sapply <- function(x, y){
l_x <- length(x)
l_y <- length(y)
sapply(1:(l_x - l_y + 1), function(i){
cov(x[i:(i+l_y-1)], y)
})
}
set.seed (20181010)
out.df2 <- data.frame()
for (x.tmp in x.list[c(1,2,3,4)]){
out.df.tmp <- benchmark("conventional" = RunningCov.sapply(x.tmp, y),
"runstats" = runstats::RunningCov(x.tmp, y),
replications = 10,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
out.df.tmp$x_length <- length(x.tmp)
out.df2 <- rbind(out.df2, out.df.tmp)
}
```
```{r, echo = FALSE, eval = FALSE}
# out.df.path <- "inst/benchmark_results/2018-12-17-performance_rbenchmark_suite2.csv"
# out.df.path <- "inst/benchmark_results/2019-11-13-performance_rbenchmark_suite2.csv"
out.df.path <- "inst/benchmark_results/2019-11-14-performance_rbenchmark_suite2.csv"
write.csv(out.df2, out.df.path, row.names = FALSE, quote = FALSE)
```
```{r, echo = FALSE, eval = TRUE}
# out.df.path <- "inst/benchmark_results/2018-12-17-performance_rbenchmark_suite2.csv"
# out.df.path <- "inst/benchmark_results/2019-11-13-performance_rbenchmark_suite2.csv"
out.df.path <- "inst/benchmark_results/2019-11-14-performance_rbenchmark_suite2.csv"
out.df2 <- read.csv(out.df.path)
```
Benchmark results
```{r, fig.width=10, fig.height=5}
library(ggplot2)
plt1 <-
ggplot(out.df2, aes(x = x_length, y = elapsed, color = test)) +
geom_line() + geom_point(size = 3) + scale_x_log10() +
theme_minimal(base_size = 14) +
labs(x = "Vector length of x",
y = "Elapsed [s]", color = "Method",
title = "Running covariance rbenchmark") +
theme(legend.position = "bottom")
plt2 <-
plt1 +
scale_y_log10() +
labs(y = "Log of elapsed [s]")
cowplot::plot_grid(plt1, plt2, nrow = 1, labels = c('A', 'B'))
```
Platform info
```{r}
sessioninfo::platform_info()
```