Stock prices are known for been non-stationary. Use get_returns() as a pre-processing step for econometric modeling.

get_returns(.tbl, .group, .type, .omit_na = FALSE, ...)

# S3 method for default
get_returns(.tbl, .group, .type, .omit_na = FALSE, ...)

# S3 method for tbl_df
get_returns(.tbl, .group, .type, .omit_na = FALSE, ...)

Arguments

.tbl

A tidy tibble.

.group

The column in which the data should be grouped. This will often be a column with stock tickers or stocks names.

.type

The method used to calculate returns. One of:

  • log

  • arithmetic

.omit_na

Should NA values be omitted? Default is FALSE (NA's are replaced by 0).

...

The column (or columns) in which the calculation should be conducted.

Value

A tidy tibble.

Examples

library(YahooTickers)
library(dplyr)
get_tickers(dow) %>%
  slice(1:3) %>%
  get_stocks(.) %>%
  get_returns(., .group = tickers, .type = arithmetic,
              .omit_na = TRUE, adjusted)
#> # A tibble: 8,985 × 3
#>    date       tickers  adjusted
#>    <date>     <fct>       <dbl>
#>  1 2010-01-05 MMM     -0.00626 
#>  2 2010-01-06 MMM      0.0142  
#>  3 2010-01-07 MMM      0.000717
#>  4 2010-01-08 MMM      0.00705 
#>  5 2010-01-11 MMM     -0.00403 
#>  6 2010-01-12 MMM      0.000834
#>  7 2010-01-13 MMM     -0.00321 
#>  8 2010-01-14 MMM     -0.00334 
#>  9 2010-01-15 MMM     -0.00156 
#> 10 2010-01-19 MMM      0.0210  
#> # … with 8,975 more rows