knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
Updated: r Sys.Date()
When I utilize tidyquant
package for economic analyses, I often find myself writing similar functions repeatedly. I also find the idea of time aware dataframe of tsibble
package 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.
This package is only for my own use for now. "You" means "future me".
You can install the development version with:
remotes::install_github("mitsuoxv/tqr")
As I read Hyndman, R.J., & Athanasopoulos, G. (2021) Forecasting: principles and practice, 3rd edition, OTexts: Melbourne, Australia. OTexts.com/fpp3., I use fpp3
meta package here.
library(fpp3) library(tqr)
If you are familiar with tsibble functions, like index_by
and group_by_key
, you can convert to lower frequency.
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.
aus_production aus_production %>% index_by(Year = year(Quarter)) %>% summarize(across(!Quarter, sum))
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".
aus_arrivals %>% group_by_key() %>% mutate(diff = difference(Arrivals)) %>% ungroup()
Or you can transform a existing variable "Arrivals". You can use difference
function flexibly.
aus_arrivals %>% group_by_key() %>% mutate(Arrivals = difference(Arrivals)) %>% ungroup()
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
function is created like below. tq_ma
, tq_gr
and tq_sa
functions are also created by this function factory.
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.
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
.
You can find "order_by" argument in tsibble::difference
document. It is optional in difference
function.
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.
tsbl %>% tq_diff() scrambled %>% tq_diff() %>% arrange(year)
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.
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.
aus_livestock %>% tq_ma(n = 3)
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.
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.
aus_arrivals %>% tq_gr(n = 4)
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.
aus_arrivals %>% group_by_key() %>% mutate(sa = season_adjust(Arrivals, Quarter)) %>% ungroup()
tq_sa
function transforms "Arrivals" variable in this example.
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.
aus_arrivals %>% slice(sample(nrow(aus_arrivals))) %>% tq_sa() %>% arrange(Origin, Quarter)
EOL
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.