问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/25534077/r-stockportfolio-package-not-connecting-to-yahoo