0

I'm trying to replicate a code from a book, "Reproducible Finance with R". All went quite good except for the following part.

asset_returns_tbltime <-
prices %>%
tk_tbl(preserve_index = TRUE,
rename_index = "date") %>%
as_tbl_time(index = date) %>%
as_period(period = "month",
side = "end") %>%
gather(asset, returns, -date) %>%
group_by(asset) %>%
tq_transmute(mutate_fun = periodReturn,
type = "log") %>%
spread(asset, monthly.returns) %>%
select(date, symbols)

That gives me the following error after the string tq_transmute(mutate_fun = periodReturn, type = "log") :

Error: Can't subset columns that don't exist. Column "asset" doesn't exist. Run rlang::last_error() to see where the error occurred. In addition: Warning message: "..." must not be empty for ungrouped data frames. Did you want data = everything()?

The data comes from the following code:

symbols <- c("SPY","EFA", "IJS", "EEM","AGG")
prices <-
getSymbols(symbols,
src = 'yahoo',
from = "2012-12-31",
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)

It would be nice if someone could point my attention towards the issue with the code (or with something else) as I think I've been lost.

dkolkin
  • 81
  • 10

1 Answers1

1

If you use the updated pivot_longer and pivot_wider function instead of retired gather and spread this works.

library(tidyverse)
library(tibbletime)
library(tidyquant)
library(quantmod)

prices %>%
  tk_tbl(preserve_index = TRUE,
         rename_index = "date") %>%
  as_tbl_time(index = date) %>%
  as_period(period = "month",
            side = "end") %>%
  pivot_longer(cols = -date, names_to = 'asset', values_to = 'returns') %>%
  group_by(asset) %>%
  tq_transmute(mutate_fun = periodReturn,
               type = "log") %>%
  pivot_wider(names_from = asset, values_from = monthly.returns) %>%
  select(date, symbols)

#   date           SPY     EFA      IJS      EEM       AGG
#   <date>       <dbl>   <dbl>    <dbl>    <dbl>     <dbl>
# 1 2012-12-31  0       0       0        0        0       
# 2 2013-01-31  0.0499  0.0366  0.0521  -0.00294 -0.00623 
# 3 2013-02-28  0.0127 -0.0130  0.0162  -0.0231   0.00589 
# 4 2013-03-28  0.0373  0.0130  0.0403  -0.0102   0.000985
# 5 2013-04-30  0.0190  0.0490  0.00122  0.0121   0.00964 
# 6 2013-05-31  0.0233 -0.0307  0.0420  -0.0495  -0.0202  
# 7 2013-06-28 -0.0134 -0.0271 -0.00140 -0.0547  -0.0158  
# 8 2013-07-31  0.0504  0.0519  0.0635   0.0132   0.00269 
# 9 2013-08-30 -0.0305 -0.0197 -0.0347  -0.0257  -0.00830 
#10 2013-09-30  0.0312  0.0753  0.0639   0.0696   0.0111  
# … with 94 more rows
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thank you so much for a quick reply. How do you keep yourself updated with what changes developers introduce to commands/functions? I keep R updated most of the time and it didn't say anything about ‘gather’ and ‘spread’ became obsolete. – dkolkin Jul 06 '21 at 15:09
  • Easy - stay away from tidyverse and you keep backwards compatibility. – tester Jul 06 '21 at 18:59
  • @dkolkin That is one issue with `tidyverse`, they keep on changing things way too frequently. If you have updated version of `tidyr`, the documentation of `?gather` and `?spread` should mention that. – Ronak Shah Jul 07 '21 at 01:54
  • @RonakShah, if things change fast with no public announcing (I may be wrong at this point though), then how do you deal with older codes? – dkolkin Jul 07 '21 at 04:31
  • @tester, could you please clarify how I should 'stay away from the tidyverse' if my job is closely related to data science, and I heavily use tools the verse offers? – dkolkin Jul 07 '21 at 04:34
  • Announcement is done but I think it does not receive proper visibility. You can follow the authors on Twitter or check tidyverse blog. Most of the changes though are backward compatible. I mean `gather` and `spread` still work in other cases. This I think is just an exception. – Ronak Shah Jul 07 '21 at 04:37
  • Yes, I read in the description to 'pivot_longer' & 'pivot_wider' that both 'gather' & 'spread' work well, but don't have any further developments. – dkolkin Jul 07 '21 at 04:45
  • You could use base R or data.table + xts, PerformanceAnalytics etc. for the task at hand without having to use functions that change its name, arguments or functionality regularly. With the new base-pipe `|>` you can even do the piping without having to load the tidyverse or magrittr. – tester Jul 08 '21 at 18:42
  • This sounds quite interesting and challenging at the same time. Since my main purpose is to do analysis and modeling of the financial data, which requires quite a bit of knowledge and time to follow the market, I would like to stick to 3-5 packages only to make it less complicated. My main source of information I’m following is a book “Reproducible Finance” (http://www.reproduciblefinance.com/), which I find quite substantial for my current tasks and skill level. I would also appreciate any suggestions and advice too. – dkolkin Jul 12 '21 at 15:51