【问题标题】:Transformation of daily prices in monthly log returns月度对数收益中每日价格的转换
【发布时间】:2021-07-06 19:48:55
【问题描述】:

我正在尝试从“Reproducible Finance with R”一书中复制代码。除了以下部分,一切都很好。

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)

这在字符串之后给了我以下错误 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()?

数据来源于以下代码:

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)

如果有人能将我的注意力指向代码(或其他东西)的问题,那就太好了,因为我认为我已经迷路了。

【问题讨论】:

    标签: r tibbletime


    【解决方案1】:

    如果您使用更新后的 pivot_longerpivot_wider 函数而不是已停用的 gatherspread,则此方法有效。

    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
    

    【讨论】:

    • 非常感谢您的快速回复。您如何让自己了解开发人员对命令/功能所做的更改?我大部分时间都在更新 R,它没有说“收集”和“传播”已经过时了。
    • 简单 - 远离 tidyverse 并保持向后兼容性。
    • @dkolkin 这是tidyverse 的一个问题,他们总是过于频繁地改变事物。如果您更新了tidyr 的版本,?gather?spread 的文档应该会提到这一点。
    • @RonakShah,如果事情在没有公开宣布的情况下快速变化(不过我现在可能错了),那么您如何处理旧代码?
    • @tester,如果我的工作与数据科学密切相关,并且我大量使用 verse 提供的工具,你能否澄清一下我应该如何“远离 tidyverse”?
    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多