-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
276 lines (174 loc) · 7.83 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
title: "Introduction to tqr package"
author: "Mitsuo Shiota"
date: "2019-07-19"
output:
github_document:
toc: TRUE
editor_options:
chunk_output_type: console
---
<!-- badges: start -->
[![R-CMD-check](https://github.com/mitsuoxv/tqr/workflows/R-CMD-check/badge.svg)](https://github.com/mitsuoxv/tqr/actions)
<!-- badges: end -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
Updated: `r Sys.Date()`
# tqr: add-on to tsibble, inspired by tidyquant
When I utilize [`tidyquant` package](https://cran.r-project.org/web/packages/tidyquant/readme/README.html) for economic analyses, I often find myself writing similar functions repeatedly. I also find the idea of time aware dataframe of [`tsibble` package](https://cran.r-project.org/web/packages/tsibble/index.html) may help me deal with time series data more confidently. So I have decided to build this package to facilitate my work.
As of version 0.0.0.9011, I have added time-wise functions to transform a numeric vector, and a function factory using those time-wise functions, rewritten functions to transform a tsibble using that function factory, and deprecated old function factories.
1. Time-wise functions to transform a numeric vector
- moving_average: transform into moving averages
- growth_rate: transform into growth rates
- season_adjust: transform into seasonally adjusted values
2. A function factory to produce functions to transform a tsibble
- cal_time_wise: produce functions using time_wise vector functions
3. Functions to transform a tsibble
- tq_diff: transform numeric columns into differences
- tq_ma: transform numeric columns into moving averages
- tq_gr: transform numeric columns into growth rates
- tq_sa: transform numeric columns into seasonally adjusted values
4. Deprecated function factories
- cal_factory: produce functions
- cal_factory_zoo: produce functions utilizing zoo package
- cal_factory_ts: produce functions utilizing ts class
- cal_factory_xts: produce functions utilizing xts package
## Installation
This package is only for my own use for now. "You" means "future me".
You can install the development version with:
```{r install, eval=FALSE}
remotes::install_github("mitsuoxv/tqr")
```
## Libraries
As I read [Hyndman, R.J., & Athanasopoulos, G. (2021) Forecasting: principles and practice, 3rd edition, OTexts: Melbourne, Australia. OTexts.com/fpp3.](https://otexts.com/fpp3/), I use `fpp3` meta package here.
```{r setup, message=FALSE}
library(fpp3)
library(tqr)
```
## Transform a tsibble into lower frequency
If you are familiar with tsibble functions, like `index_by` and `group_by_key`, you can convert to lower frequency.
```{r convert1}
aus_livestock
aus_livestock %>%
index_by(Quarter = yearquarter(Month)) %>%
group_by_key() %>%
summarize(Count = sum(Count))
```
If you are familiar with dplyr and tidyselect functions, you can convert multiple variables at once.
```{r convert2}
aus_production
aus_production %>%
index_by(Year = year(Quarter)) %>%
summarize(across(!Quarter, sum))
```
## Time-wise functions to transform a numeric vector are useful and flexible
In the above examples, `sum` function transforms a numeric vector.
tsibble package provides `difference` function, which also transforms a time-wise numeric vector. Using it, you can create a new variable "diff".
```{r difference1}
aus_arrivals %>%
group_by_key() %>%
mutate(diff = difference(Arrivals)) %>%
ungroup()
```
Or you can transform a existing variable "Arrivals". You can use `difference` function flexibly.
```{r difference2}
aus_arrivals %>%
group_by_key() %>%
mutate(Arrivals = difference(Arrivals)) %>%
ungroup()
```
## cal_time_wise: function factory to produce a function to transform a tsibble
`cal_time_wise` function is functionized from the last example, and is a function factory. It receives a time-wise function to transform a numeric vector as its first argument, and it returns a function to transform a tsibble.
## tq_diff: calculate differences
`tq_diff` function is created like below. `tq_ma`, `tq_gr` and `tq_sa` functions are also created by this function factory.
```{r tq_diff1, eval=FALSE}
tq_diff <- cal_time_wise(
tsibble::difference
)
```
The variables, which these created functions transform, are all numeric variables which are not key or index variables.
In the below example, "Month" is the index variable, and "State" and "Industry" are the key variables. "Series ID" is a character vector. So `tq_diff` transforms just "Turnover", a numeric variable.
```{r tq_diff2}
aus_retail %>%
tq_diff()
```
For arguments other than the first one, a tsibble, of `tq_diff` function, look at `tsibble::difference` document. Note that `n` in the previous version of `tq_diff` is changed to `lag`.
## "order_by" argument in time-wise functions to transform a numeric vector
You can find "order_by" argument in `tsibble::difference` document. It is optional in `difference` function.
```{r order_by1}
tsbl <- tsibble(year = 2000:2005, value = (0:5)^2, index = year)
scrambled <- tsbl %>% slice(sample(nrow(tsbl)))
wrong <- mutate(scrambled, diff = difference(value))
arrange(wrong, year)
right <- mutate(scrambled, diff = difference(value, order_by = year))
arrange(right, year)
```
`cal_time_wise` function assigns the index variable in a tsibble to "order_by" argument of `difference` function. As a result, `tq_diff` function returns right even if the rows are reshuffled.
```{r order_by2}
tsbl %>%
tq_diff()
scrambled %>%
tq_diff() %>%
arrange(year)
```
## moving_average and tq_ma: calculate moving averages
`moving_average` function calculates moving averages using `slider` package. "n" specifies the window width. "order_by" is optional, and works in the same way as in `difference` function. For other arguments, refer to its document.
```{r moving_average}
aus_livestock %>%
mutate(Count = if_else(row_number() == 4, NA_real_, Count)) %>%
group_by_key() %>%
mutate(
ma3 = moving_average(Count, n = 3),
ma3_na.rm = moving_average(Count, n = 3, na.rm = TRUE),
ma3_left = moving_average(Count, n = 3, .align = "left")
) %>%
ungroup()
```
`tq_ma` function transforms "Count" variable. For arguments other than the first one, a tsibble, look at `moving_average` document.
```{r tq_ma}
aus_livestock %>%
tq_ma(n = 3)
```
## growth_rate and tq_gr: calculate growth rates
`growth_rate` function calculates growth rates. "n" specifies the lag. "order_by" is optional, and works in the same way as in `difference` function. For other arguments, refer to its document.
```{r growth_rate}
aus_arrivals %>%
group_by_key() %>%
mutate(
gr_yoy = growth_rate(Arrivals, n = 4),
gr_annualized = growth_rate(Arrivals, n = 1, annualize = 4),
gr_yoy_not_pct = growth_rate(Arrivals, n = 4, pct = FALSE)
)
```
`tq_gr` function transforms "Arrivals" variable in this example. For arguments other than the first one, a tsibble, look at `growth_rate` document.
```{r tq_gr}
aus_arrivals %>%
tq_gr(n = 4)
```
## season_adjust and tq_sa: calculate seasonally adjusted values
`season_adjust` function calculates seasonally adjusted values using `season` package. It requires not just "x" argument, a numeric vector, but also "order_by" argument, a time vector. For other arguments, refer to `season` document.
```{r season_adjust}
aus_arrivals %>%
group_by_key() %>%
mutate(sa = season_adjust(Arrivals, Quarter)) %>%
ungroup()
```
`tq_sa` function transforms "Arrivals" variable in this example.
```{r tq_sa1}
aus_arrivals %>%
tq_sa()
```
`tq_sa` function returns right even if the rows are reshuffled, as `tq_diff`, `tq_ma` and `tq_gr` functions do.
```{r tq_sa2}
aus_arrivals %>%
slice(sample(nrow(aus_arrivals))) %>%
tq_sa() %>%
arrange(Origin, Quarter)
```
EOL