@@ -3,42 +3,41 @@ Introduction to tqr package
3
3
Mitsuo Shiota
4
4
2019-07-19
5
5
6
- - [ tqr: add-on to tsibble, inspired by
6
+ - [ tqr: add-on to tsibble, inspired by
7
7
tidyquant] ( #tqr-add-on-to-tsibble-inspired-by-tidyquant )
8
- - [ Installation] ( #installation )
9
- - [ Libraries] ( #libraries )
10
- - [ Create time aware dataframe of tsibble
8
+ - [ Installation] ( #installation )
9
+ - [ Libraries] ( #libraries )
10
+ - [ Create time aware dataframe of tsibble
11
11
class] ( #create-time-aware-dataframe-of-tsibble-class )
12
- - [ Insert missing rows, if
12
+ - [ Insert missing rows, if
13
13
necessary] ( #insert-missing-rows-if-necessary )
14
- - [ tq\_ diff: calculate
14
+ - [ tq\_ diff: calculate
15
15
differences] ( #tq_diff-calculate-differences )
16
- - [ tq\_ ma: calculate moving
16
+ - [ tq\_ ma: calculate moving
17
17
averages] ( #tq_ma-calculate-moving-averages )
18
- - [ tq\_ gr: calculate growth rates] ( #tq_gr-calculate-growth-rates )
19
- - [ Convert to lower frequency] ( #convert-to-lower-frequency )
20
- - [ tq\_ sa: calculate seasonally adjusted
18
+ - [ tq\_ gr: calculate growth rates] ( #tq_gr-calculate-growth-rates )
19
+ - [ Convert to lower frequency] ( #convert-to-lower-frequency )
20
+ - [ tq\_ sa: calculate seasonally adjusted
21
21
values] ( #tq_sa-calculate-seasonally-adjusted-values )
22
- - [ cal\_ factory: function factory for
22
+ - [ cal\_ factory: function factory for
23
23
calculation] ( #cal_factory-function-factory-for-calculation )
24
- - [ cal\_ factory\_ zoo: function factory for calculation utilizing
24
+ - [ cal\_ factory\_ zoo: function factory for calculation utilizing
25
25
zoo
26
26
package] ( #cal_factory_zoo-function-factory-for-calculation-utilizing-zoo-package )
27
- - [ cal\_ factory\_ ts: function factory for calculation utilizing ts
27
+ - [ cal\_ factory\_ ts: function factory for calculation utilizing ts
28
28
class] ( #cal_factory_ts-function-factory-for-calculation-utilizing-ts-class )
29
- - [ cal\_ factory\_ xts: function factory for calculation utilizing
29
+ - [ cal\_ factory\_ xts: function factory for calculation utilizing
30
30
xts
31
31
package] ( #cal_factory_xts-function-factory-for-calculation-utilizing-xts-package )
32
32
33
33
<!-- README.md is generated from README.Rmd. Please edit that file -->
34
-
35
34
<!-- badges: start -->
36
35
37
36
[ ![ Travis build
38
37
status] ( https://travis-ci.org/mitsuoxv/tqr.svg?branch=master )] ( https://travis-ci.org/mitsuoxv/tqr )
39
38
<!-- badges: end -->
40
39
41
- Updated: 2020-12-24
40
+ Updated: 2021-03-21
42
41
43
42
# tqr: add-on to tsibble, inspired by tidyquant
44
43
@@ -106,18 +105,19 @@ eer
106
105
# > 9 1994-09-30 defla… 123. 132. 108. 85.6 90.8 205. 3690.
107
106
# > 10 1994-10-31 defla… 121. 133. 108. 86.2 91.2 201. 3512.
108
107
# > # … with 938 more rows, and 53 more variables: Canada <dbl>, Chile <dbl>,
109
- # > # China <dbl>, ` Chinese Taipei` <dbl>, Colombia <dbl>, Croatia <dbl>,
110
- # > # Cyprus <dbl>, ` Czech Republic` <dbl>, Denmark <dbl>, Estonia <dbl>, `Euro
111
- # > # area` <dbl>, Finland <dbl>, France <dbl>, Germany <dbl>, Greece <dbl>,
112
- # > # ` Hong Kong SAR` <dbl>, Hungary <dbl>, Iceland <dbl>, India <dbl>,
108
+ # > # China <dbl>, Chinese Taipei <dbl>, Colombia <dbl>, Croatia <dbl>,
109
+ # > # Cyprus <dbl>, Czech Republic <dbl>, Denmark <dbl>, Estonia <dbl>,
110
+ # > # Euro area <dbl>, Finland <dbl>, France <dbl>, Germany <dbl>, Greece <dbl>,
111
+ # > # Hong Kong SAR <dbl>, Hungary <dbl>, Iceland <dbl>, India <dbl>,
113
112
# > # Indonesia <dbl>, Ireland <dbl>, Israel <dbl>, Italy <dbl>, Japan <dbl>,
114
113
# > # Korea <dbl>, Latvia <dbl>, Lithuania <dbl>, Luxembourg <dbl>,
115
- # > # Malaysia <dbl>, Malta <dbl>, Mexico <dbl>, Netherlands <dbl>, `New
116
- # > # Zealand` <dbl>, Norway <dbl>, Peru <dbl>, Philippines <dbl>, Poland <dbl>,
117
- # > # Portugal <dbl>, Romania <dbl>, Russia <dbl>, `Saudi Arabia` <dbl>,
118
- # > # Singapore <dbl>, Slovakia <dbl>, Slovenia <dbl>, `South Africa` <dbl>,
119
- # > # Spain <dbl>, Sweden <dbl>, Switzerland <dbl>, Thailand <dbl>, Turkey <dbl>,
120
- # > # `United Arab Emirates` <dbl>, `United Kingdom` <dbl>, `United States` <dbl>
114
+ # > # Malaysia <dbl>, Malta <dbl>, Mexico <dbl>, Netherlands <dbl>,
115
+ # > # New Zealand <dbl>, Norway <dbl>, Peru <dbl>, Philippines <dbl>,
116
+ # > # Poland <dbl>, Portugal <dbl>, Romania <dbl>, Russia <dbl>,
117
+ # > # Saudi Arabia <dbl>, Singapore <dbl>, Slovakia <dbl>, Slovenia <dbl>,
118
+ # > # South Africa <dbl>, Spain <dbl>, Sweden <dbl>, Switzerland <dbl>,
119
+ # > # Thailand <dbl>, Turkey <dbl>, United Arab Emirates <dbl>,
120
+ # > # United Kingdom <dbl>, United States <dbl>
121
121
122
122
class(eer )
123
123
# > [1] "tbl_df" "tbl" "data.frame"
@@ -226,7 +226,7 @@ Let us see the differences year-over-year.
226
226
``` r
227
227
eer_ts %> %
228
228
tq_diff(n = 12 ) %> %
229
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
229
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
230
230
filter(symbol == " reer" ) %> %
231
231
filter(area %in% c(" Japan" , " Euro area" , " United States" )) %> %
232
232
ggplot(aes(x = date , y = value , color = area )) +
@@ -248,7 +248,7 @@ Let us see 6 month moving average movements.
248
248
``` r
249
249
eer_ts %> %
250
250
tq_ma(n = 6 ) %> %
251
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
251
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
252
252
filter(symbol == " reer" ) %> %
253
253
filter(area %in% c(" Japan" , " Euro area" , " United States" )) %> %
254
254
ggplot(aes(x = date , y = value , color = area )) +
@@ -270,7 +270,7 @@ Let us see year-over-year growth rates, percents.
270
270
``` r
271
271
eer_ts %> %
272
272
tq_gr(n = 12 ) %> %
273
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
273
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
274
274
filter(symbol == " reer" ) %> %
275
275
filter(area %in% c(" Japan" , " Euro area" , " United States" )) %> %
276
276
ggplot(aes(x = date , y = value , color = area )) +
@@ -291,7 +291,7 @@ You can convert from “month” to “quarter”.
291
291
292
292
``` r
293
293
eer_q <- eer_ts %> %
294
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
294
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
295
295
group_by(symbol , area ) %> %
296
296
index_by(quarter = yearquarter(date )) %> %
297
297
summarize(value = mean(value ))
@@ -320,7 +320,7 @@ package](https://www.rdocumentation.org/packages/seasonal/versions/1.7.0).
320
320
321
321
``` r
322
322
greece <- eer_ts %> %
323
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
323
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
324
324
filter(symbol == " deflator" , area == " Greece" ) %> %
325
325
mutate(symbol = " original" )
326
326
@@ -396,8 +396,8 @@ tq_range <- cal_factory(
396
396
397
397
eer_ts %> %
398
398
tq_range() %> %
399
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
400
- pivot_wider(names_from = date , values_from = value ) %> %
399
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
400
+ pivot_wider(names_from = date ) %> %
401
401
mutate(range = max - min ) %> %
402
402
filter(symbol == " deflator" ) %> %
403
403
arrange(desc(range ))
@@ -444,21 +444,21 @@ tq_acf <- cal_factory(
444
444
defl_gr1 %> %
445
445
tq_acf() %> %
446
446
filter(date == " acf12" ) %> %
447
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " ) %> %
447
+ pivot_longer(! c(date , symbol ), names_to = " area" ) %> %
448
448
arrange(desc(value ))
449
449
# > # A tibble: 60 x 4
450
450
# > date symbol area value
451
451
# > <chr> <chr> <chr> <dbl[,1,1]>
452
- # > 1 acf12 deflator Greece 0.891
453
- # > 2 acf12 deflator Luxembourg 0.831
454
- # > 3 acf12 deflator Spain 0.802
455
- # > 4 acf12 deflator Euro area 0.779
456
- # > 5 acf12 deflator Netherlands 0.744
457
- # > 6 acf12 deflator Portugal 0.714
458
- # > 7 acf12 deflator Germany 0.672
459
- # > 8 acf12 deflator Czech Republic 0.667
460
- # > 9 acf12 deflator France 0.667
461
- # > 10 acf12 deflator Colombia 0.655
452
+ # > 1 acf12 deflator Greece 0.891 …
453
+ # > 2 acf12 deflator Luxembourg 0.831 …
454
+ # > 3 acf12 deflator Spain 0.802 …
455
+ # > 4 acf12 deflator Euro area 0.779 …
456
+ # > 5 acf12 deflator Netherlands 0.744 …
457
+ # > 6 acf12 deflator Portugal 0.714 …
458
+ # > 7 acf12 deflator Germany 0.672 …
459
+ # > 8 acf12 deflator Czech Republic 0.667 …
460
+ # > 9 acf12 deflator France 0.667 …
461
+ # > 10 acf12 deflator Colombia 0.655 …
462
462
# > # … with 50 more rows
463
463
```
464
464
@@ -497,20 +497,20 @@ choose, instead consider to spread to wide format.
497
497
``` r
498
498
system.time(tq_ma(eer_ts , n = 3 ))
499
499
# > user system elapsed
500
- # > 0.248 0.000 0.248
500
+ # > 0.246 0.001 0.247
501
501
system.time(tq_rollmean(eer_ts , k = 3 , align = " right" , fill = NA ))
502
502
# > user system elapsed
503
- # > 0.472 0.000 0.471
503
+ # > 0.465 0.003 0.469
504
504
505
505
eer_ts_long <- eer_ts %> %
506
- pivot_longer(! c(date , symbol ), names_to = " area" , values_to = " value " )
506
+ pivot_longer(! c(date , symbol ), names_to = " area" )
507
507
508
508
system.time(tq_ma(eer_ts_long , n = 3 ))
509
509
# > user system elapsed
510
- # > 0.763 0.003 0.768
510
+ # > 0.815 0.001 0.816
511
511
system.time(tq_rollmean(eer_ts_long , k = 3 , align = " right" , fill = NA ))
512
512
# > user system elapsed
513
- # > 1.176 0.001 1.177
513
+ # > 1.132 0.003 1.136
514
514
```
515
515
516
516
## cal\_ factory\_ ts: function factory for calculation utilizing ts class
0 commit comments