Error with code

Hi everybody,

I have an error with this code and I would like to ask for help. Thank you in advance.

symbols <- sort(c("SPY","VGT","EFA","DBC","AGG"))
prices <- 
  getSymbols(symbols, src = 'yahoo', from = "2013-01-01", 
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Cl(get(.))) %>%
  reduce(merge) %>% 
  `colnames<-`(symbols)

# Get monthly returns.
portfolio_component_monthly_returns_xts <- 
  prices %>%  
  # Convert to tibble so can stay in the tidyquant/verse.
  as_tibble(preserve_row_names = TRUE) %>%
  # Add a date column. 
  mutate(date = ymd(row.names)) %>% 
  # Remove the row.names column
  select(-row.names) %>% 
  # Have the date column as the first column. 
  select(date, everything()) %>% 
  # Gather into long format in order to use tq_transmute().
  gather(asset, return, -date) %>%
  group_by(asset) %>% 
  # Use the function from tidyquant; note how easily we could change to
  # a different time period like weekly or yearly.
  tq_transmute(mutate_fun = periodReturn, period = "monthly") %>%
  # Put the results back to wide format.
  spread(asset, monthly.returns) %>% 
  # Convert back to an xts, so we can use the cov() and StdDev() functions.
  as_xts(date_col = date)

head(portfolio_component_monthly_returns_xts)

What is the error? What packages are you using?

The error is like that.

Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'

These are packages:
library(dplyr)
library(tidyverse)
library(tsibble)
library(lubridate)
library(rtsdata)
library(quantmod)

note that I needed to add - library(timetk)

library(quantmod)
library(tidyquant)
library(lubridate)
library(tidyverse)
library(timetk) # had to add

symbols <- sort(c("SPY","VGT","EFA","DBC","AGG"))
prices <- 
  getSymbols(symbols, src = 'yahoo', from = "2013-01-01", 
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Cl(get(.))) %>%
  reduce(merge) %>% 
  `colnames<-`(symbols)

# Get monthly returns.
portfolio_component_monthly_returns <- 
  prices %>%  
  # Convert to tibble so can stay in the tidyquant/verse.
  as_tibble(rownames = "row.names") %>%
  # Add a date column. 
  mutate(date = ymd(row.names)) %>% 
  # Remove the row.names column
  select(-row.names) %>% 
  # Have the date column as the first column. 
  select(date, everything()) %>% 
  # Gather into long format in order to use tq_transmute().
  gather(asset, return, -date) %>%
  group_by(asset) %>% 
  # Use the function from tidyquant; note how easily we could change to
  # a different time period like weekly or yearly.
  tq_transmute(mutate_fun = periodReturn, period = "monthly") %>%
  # Put the results back to wide format.
  spread(asset, monthly.returns) 

portfolio_component_monthly_returns_xts <- 
  portfolio_component_monthly_returns %>% 
  # Convert back to an xts, so we can use the cov() and StdDev() functions.
  tk_xts()

head(portfolio_component_monthly_returns_xts)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.