Search code examples
rfinanceportfoliocoding-efficiencyr-portfolioanalytics

Is there an easy way to plot the Efficient Frontier with R


I have a risk return plot and dataframe (dfRiskReturn in dput below). I don't want to use Yahoo Finance to download stocks, I've already got the risk return dataframe another way. All I want to know is now that I have the dataframe, how do I get the Efficient Frontier.

This is an old video, but it is pretty much what I want. Maybe I'd like to check correlation(the Cluster Column in the dput) with the Efficient Frontier (if it is possible). Only choose non correlated stocks with eachother or something to find the best efficient frontier, I don't know. https://www.youtube.com/watch?v=zkXIByRwJ-g

I was reading the youtube comments, and they recommended the package, fPortfolio, but I have no idea how it works.

I also see that lately a guy is doing it with python, but I want it in R: https://www.youtube.com/watch?v=Isutk-wqJfE


dput(dfRiskReturn):

structure(list(Return = c(12, -2, -4.5), Volatility = c(25, 12, 
34), cluster = structure(c(1L, 2L, 2L), .Label = c("1", "2"), class = "factor"), 
    X5 = c("FALSE", "FALSE", "FALSE")), row.names = c("ACWI", 
"TLT", "GLD"), class = "data.frame")

EDIT:

I got this one working as said to the answer of the question, but I have no idea What the coordinates of the optimal point is, or kind-of how to get it. [It has correlation at 0.5 or something]: https://quant.stackexchange.com/questions/15178/calculating-the-efficient-frontier-from-expected-returns-and-sd/41182#41182


Solution

  • Here's an example. I used the edhec-data from PerformanceAnalytics for computing the tangency portfolio with fPortfolio.

    library(fPortfolio)
    library(PerformanceAnalytics)
    
    data("edhec")
    
    rets <- edhec
    
    # compute the tangency portfolio
    tp <- tangencyPortfolio(as.timeSeries(edhec))
    
    frontier <- portfolioFrontier(as.timeSeries(edhec))
    plot(frontier) # select 1 and 3
    
    # get tangency point
    > tp@portfolio@portfolio[["targetReturn"]][["mean"]]
    [1] 0.004483096
    > tp@portfolio@portfolio[["targetRisk"]][["Sigma"]]
    [1] 0.006325268
    

    enter image description here