Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

r - tibbletime with tq_transmute producing strange error, column obviously exists but says it doesn't

I could have sworn this code worked a week ago, but I guess that I am mistaken. I keep getting the error: Error: Can't subset columns that don't exist. x 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()?

I've taken it step by step to try and see where it is in my code and I can tell that it is after I group the assets, and occurs when I start to add on the tq_transmute. If someone could please help that would be greatly appreciated. I will have code that you should be able to just run automatically and see what I am talking about. It doesn't make any sense because "asset" does exist after gathering the data and grouping it.

library(tidyverse)
library(lubridate) 
library(readxl)
library(highcharter) 
library(tidyquant) 
library(timetk) 
library(tibbletime) 
library(quantmod) 
library(PerformanceAnalytics)
library(scales)
library(magrittr)
library(broom)
library(purrr)

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")

prices <- getSymbols(symbols,
                     src = 'yahoo',
                     from = "2012-12-31",
                     to = "2017-12-31",
                     auto.assign = TRUE,
                     warnings = FALSE) %>%
  map(~Ad(get(.)))%>% #the period here in get(.) refers to our intial object
  reduce(merge) %>%
  `colnames<-`(symbols)


# WHERE MY PROBLEM OCCURS 
asset_returns_tbltime <-
  prices %>% 
  tk_tbl(preserve_index = TRUE,
                    rename_index = "date")%>%
  # this is the the tibbletime function
  as_tbl_time(index = date) %>% 
  as_period(period = "month",
            side = "end") %>%
  gather(asset, returns, -date) %>%
  group_by(asset) %>% 
  tq_transmute(mutate_fun = periodReturn, #GETTING THE ERROR SOMEWHERE IN HERE
                                   type = "log") %>%
  spread(asset, monthly.returns) %>%
  select(date, symbols) %>%
  slice(-1)
question from:https://stackoverflow.com/questions/65713816/tibbletime-with-tq-transmute-producing-strange-error-column-obviously-exists-bu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

gather has been replaced with pivot_longer and spread with pivot_wider. If you change gather code to pivot_longer it works. I am not exactly sure why it fails with gather though.

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) %>%
  slice(-1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...