错误:`mutate()` 列 `nested.col` 有问题。ℹ `nested.col = purrr::map(...)`

时间:2021-08-01 12:56:10

标签: r

我正在尝试计算一组股票的对数回报... 我不确定这个错误是什么意思。

错误信息:

ℹ `nested.col = purrr::map(...)`.
x fun = periodReturn(, period = "daily", col_rename = "ret", type = "log") not a valid option.
ℹ The error occurred in group 1: symbol = "INFY.NS".
Run `rlang::last_error()` to see where the error occurred.

有人可以帮忙吗?这是我的代码供参考:

#install packages
install.packages(c("tidyquant", "tidyverse", "plotly", "quantmod", "timetk"))

#load libraries
library(tidyquant) # To download the data
library(plotly) # To create interactive charts
library(timetk) # To manipulate the data series
library(quantmod)
# 4 STOCKS
tick <- c('SBIN.NS', 'RELIANCE.NS', 'TCS.NS', 'INFY.NS')

price_data <- tq_get(tick,
                     from = '2014-01-01',
                     to = '2018-05-31',
                     get = 'stock.prices')
View(price_data)

# COMPUTING LOG RETURNS
log_ret_tidy <- price_data %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn(,
               period = 'daily',
               col_rename = 'ret',
               type = 'log'))
head(log_ret_tidy)

1 个答案:

答案 0 :(得分:0)

您在代码中存在一些语法问题。试试 -

library(tidyquant) 
library(timetk) 
library(quantmod)
librart(tidyverse)

log_ret_tidy <- price_data %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn, period = 'daily',
                            col_rename = 'ret', type = 'log')

log_ret_tidy

# A tibble: 4,344 x 3
# Groups:   symbol [4]
#   symbol  date            ret
#   <chr>   <date>        <dbl>
# 1 SBIN.NS 2014-01-01  0      
# 2 SBIN.NS 2014-01-02 -0.0128 
# 3 SBIN.NS 2014-01-03 -0.0133 
# 4 SBIN.NS 2014-01-06 -0.0185 
# 5 SBIN.NS 2014-01-07 -0.0160 
# 6 SBIN.NS 2014-01-08 -0.00256
# 7 SBIN.NS 2014-01-09 -0.0105 
# 8 SBIN.NS 2014-01-10 -0.0195 
# 9 SBIN.NS 2014-01-13  0.0204 
#10 SBIN.NS 2014-01-14 -0.00841
# … with 4,334 more rows