-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathREADME.Rmd
More file actions
162 lines (116 loc) · 4.21 KB
/
README.Rmd
File metadata and controls
162 lines (116 loc) · 4.21 KB
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
---
output: github_document
---
<!-- 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%"
)
```
# gratis <img src="man/figures/logo.PNG" align="right" height="210"/>
<!-- badges: start -->
[](https://github.com/ykang/gratis/actions)
[](https://CRAN.R-project.org/package=gratis)
<!-- badges: end -->
The R package `gratis` (previously known as `tsgeneration`) provides efficient algorithms for generating time series with
diverse and controllable characteristics. This repository also contains a Python implementation under `python/`, with source
code in `python/gratis`.
## Installation
### [CRAN version]( https://CRAN.R-project.org/package=gratis)
```r
install.packages("gratis")
```
### Development version
You can install the **development** version of `gratis` package from [GitHub
Repository](https://github.com/ykang/gratis) with:
``` r
devtools::install_github("ykang/gratis")
```
### Python package
The Python package is developed from the `python/` subdirectory:
``` sh
python -m pip install -e ./python
```
Install the Python development dependencies when running the Python tests:
``` sh
python -m pip install -e "./python[dev]"
```
The Python package depends on `statsmodels` for ARIMA and ETS simulation and `matplotlib` for plotting. The Python source is intentionally excluded from
`R CMD build`; see `python/README.md` for Python-specific usage and testing notes.
## Usage
### Tutorial video
Watch [this YouTube video](https://www.youtube.com/watch?v=F3lWECtFa44) provided by [Prof. Rob Hyndman](https://robjhyndman.com/).
### Load the package
```{r, message=FALSE}
library(gratis)
library(feasts)
```
### Generate diverse time series
```{r}
set.seed(27)
mar_model(seasonal_periods=12) %>%
generate(length=120, nseries=2) %>%
autoplot(value)
```
### Generate mutiple seasonal time series
```{r}
mar_model(seasonal_periods=c(24, 24*7)) %>%
generate(length=24*7*10, nseries=12) %>%
autoplot(value)
```
### Generate time series with controllable features
```{r, message=FALSE}
library(dplyr)
# Function to return spectral entropy, and ACF at lags 1 and 2
# given a numeric vector input
my_features <- function(y) {
c(tsfeatures::entropy(y), acf = acf(y, plot = FALSE)$acf[2:3, 1, 1])
}
# Produce series with entropy = 0.5, ACF1 = 0.9 and ACF2 = 0.8
df <- generate_target(
length = 60, feature_function = my_features, target = c(0.5, 0.9, 0.8)
)
df %>%
as_tibble() %>%
group_by(key) %>%
reframe(value = my_features(value),
feature=c("entropy","acf1", "acf2")
)
autoplot(df)
```
### Web application
You can also run the time series generation procedure in a shiny app
``` r
app_gratis()
```
Or visit our [online Shiny APP](https://ebsmonash.shinyapps.io/tsgeneration/)
## Python usage
The Python API mirrors the main model constructors from the R package and returns NumPy arrays.
``` python
import numpy as np
import gratis
model = gratis.mar_model(
phi=np.array([[0.8, 0.6], [0.0, 0.3]]),
d=0,
sigmas=[1.0, 2.0],
weights=[0.8, 0.2],
)
series = gratis.simulate(model, n=100, rng=1)
many = gratis.generate(model, length=100, nseries=5, rng=1)
gratis.plot_series(many, title="Generated MAR series")
```
Python ARIMA and ETS models use `statsmodels` by default. Local NumPy simulators remain available with `backend = "numpy"`.
## See also
- R package `tsfeatures` from [GitHub Repository](https://github.com/robjhyndman/tsfeatures).
## References
- Kang, Y., Hyndman, R.J, and Li, F. (2020). **GRATIS**: **G**ene**RA**ting **TI**me **S**eries with
diverse and controllable characteristics. [Statistical Analysis and Data Mining](https://doi.org/10.1002/sam.11461).
## License
This package is free and open source software, licensed under GPL-3.
## Acknowledgements
Feng Li and Yanfei Kang are supported by the National Natural Science Foundation of China
(No. 11501587 and No. 11701022 respectively). Rob J Hyndman is supported by the Australian
Centre of Excellence in Mathematical and Statistical Frontiers.