0

Trying to run following R code.

> sp_500 <- sp_500 %>%
+   
+   mutate(
+     stock.prices = map(ticker.symbol, 
+                        function(.x) get_stock_prices(.x, 
+                                                      return_format = "tibble",
+                                                      from = "2017-01-01",
+                                                      to = "2017-09-21")
+     ),
+     log.returns  = map(stock.prices, 
+                        function(.x) get_log_returns(.x, return_format = "tibble")),
+     mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)),
+     sd.log.returns   = map_dbl(log.returns, ~ sd(.$Log.Returns)),
+     n.trade.days = map_dbl(stock.prices, nrow) 

But I keep getting this error:

Warning: BRK.B download failed; trying again.
Error in mutate_impl(.data, dots) : 
  Evaluation error: BRK.B download failed after two attempts. Error message:
HTTP error 404..

Does anyone have an idea what am I doing wrong?

Best Regards AnSa

Spacedman
  • 92,590
  • 12
  • 140
  • 224
AnSa
  • 33
  • 1
  • 3
  • This is not an RStudio question so I've removed the tag. Please read the tag documentation before use. – Spacedman Sep 27 '17 at 08:33
  • Can you make it a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), so that we can basically copy-paste your code and run it in a fresh R session? (must include data, `library()` statements, and only what's necessary to reproduce the error (your job is to determine what is and what is not... This process of removing parts of code until you get to the bone of the problem might even lead you to an answer). – Aurèle Sep 27 '17 at 09:39

2 Answers2

0

It seems to have a problem downloading specific tickers. I am not a developer but I had the same problem as you do and fixed it by removing these tickers (less than 10). The code for removing them can be found on the same page where this piece of code is taken from and its

sp_500 <- sp_500 %>% 
    filter(ticker.symbol != "BRK.B")

I hope it helped.

Skatox
  • 4,237
  • 12
  • 42
  • 47
0

Basically there is something wrong with the BRK.B stock, I'm not sure what it is, but the way to solve it is by eliminating it/them.

There are other stocks that get stuck in the function, this is how I solve it:

sp_500 <- sp_500[c(-72,-86, -82, -163, -268, -460, -392),] %>%
 mutate(
stock.prices = map(ticker.symbol, 
                   function(.x) get_stock_prices(.x, 
                                                 return_format = "tibble",
                                                 from = "2007-01-01",
                                                 to = "2018-10-23")
),
log.returns  = map(stock.prices, 
                   function(.x) get_log_returns(.x, return_format = "tibble")),
mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)),
sd.log.returns   = map_dbl(log.returns, ~ sd(.$Log.Returns)),
n.trade.days = map_dbl(stock.prices, nrow)
)              

The [c(-72,-86, -82, -163, -268, -460, -392),] are the stocks that didn't work for me because it display and error, basically find the columns in which you get errors by looking at the stock name and see in which columns they are and eliminate them

Hope it helps

ArMnKnows
  • 1
  • 3