0

I am trying to replicate this example as I am new to portfolio optimization through R:

http://economistatlarge.com/portfolio-theory/r-optimized-portfolio

However, I keep getting the following error:

    R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(stockPortfolio)
Warning message:
package ‘stockPortfolio’ was built under R version 3.1.1 
> library(quadprog)
Warning message:
package ‘quadprog’ was built under R version 3.1.1 
> stocks <- c(
+ "SPY" = .30,
+ "EFA" = .20,
+ "IWM" = .15,
+ "VWO" = .10,
+ "LQD" = .15,
+ "HYG" = .10)
> returns <- getReturns(names(stocks), freq="week")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  unable to connect to 'ichart.finance.yahoo.com' on port 80.

I have no idea how to troubleshoot this, any suggestions?

user3390169
  • 1,015
  • 1
  • 11
  • 34
  • I know this is an old post, and you probably figured this out by now, but just in case you didn't...and for the benefit of others... stocks <- c("SPY" = .30,"EFA" = .20,"IWM" = .15,"VWO" = .10,"LQD" = .15,"HYG" = .10) Without the '+' characters, works fine for me. Make sure you are connected to the web when you run the code; the historical stock prices get downloaded form the web. – ASH Oct 01 '15 at 06:29
  • So, the code should look like this: library(stockPortfolio) library(quadprog) stocks <- c("SPY" = .30,"EFA" = .20,"IWM" = .15,"VWO" = .10,"LQD" = .15,"HYG" = .10) returns <- getReturns(names(stocks), freq="week") – ASH Oct 01 '15 at 06:30
  • I can't figure out why everything gets concatenated together. It's very hard to read this when everything gets wrapped up, and jammed together... Anyway, take a look at this: http://economistatlarge.com/portfolio-theory/r-optimized-portfolio – ASH Oct 01 '15 at 06:32

1 Answers1

0

This may not be the exact answer to what is causing the error. But it is intended as a starting point. Plus, it's very important for maintaining proper, intended functionality of installed packages.


From the top of your code, your R version is

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"

If you get a warning message when you attach a package with library (or require), that should spark your interest. Your code shows the following warning messages.

> library(stockPortfolio)
# Warning message:
# package ‘stockPortfolio’ was built under R version 3.1.1 
> library(quadprog)
# Warning message:
# package ‘quadprog’ was built under R version 3.1.1 

Your packages were built with a newer version of R than you are using. This may be causing the problem because of changes in code or other things.

I recommend you update to the latest version of R and then try again.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
  • 1
    My company finally allowed me to install 3.1.1 and I got the same error. However, I found this site: https://sites.google.com/site/swlazlowski/Home/hardware/using-r-behind-a-firewall and typed setInternet2() and it worked (although I got a weird error after I typed that command). Not sure if it worked because I upgraded to 3.1.1 or not but thanks for your help. – user3390169 Sep 11 '14 at 18:51