# How to calculate the global minimum variance portfolio in R?

I am attempting to use the globalMin.portfolio command to calculate the global minimum variance portfolio in RStudio. My code is as follows (note that several libraries have been included which may not be necessary, since I am unclear which ones are needed to carry out the functions):

#Libraries
library(corpcor)
library(tseries)
library(fPortfolio)
library(SharpeR)
library(PortfolioAnalytics)
library(zoo)
library(plotly)
library(portfolio.r)
library(portfolio_noshorts.r)

rk.free=0.005

rm( list=ls() )
T <- nrow(portfolio)
portfolio <- portfolio[ seq(T,1,-1) , ]
stock1_r<- diff(log(portfolio$stock1))*100 stock2_r <- diff(log(portfolio$stock2))*100
stock3_r<- diff(log(portfolio$stock3))*100 stock4_r <- diff(log(portfolio$stock4))*100
stock5_r    <- diff(log(portfolio$stock5))*100 stock6_r<- diff(log(portfolio$stock6))*100
stock7_r<- diff(log(portfolio$stock7))*100 stock8_r<- diff(log(portfolio$t))*100
stock9_r<- diff(log(portfolio$stock9))*100 stock10_r<- diff(log(portfolio$stock10))*100

portfolio_r <- data.frame(stock1_r, stock2_r, stock3_r, stock4_r, stock5_r, stock6_r, stock7_r, stock8_r, stock9_r, stock10_r)
er1=c(mean(stock1_r), mean(stock2_r), mean(stock3_r), mean(stock4_r), mean(stock5_r), mean(stock6_r), mean(stock7_r), mean(stock8_r), mean(stock9_r), mean(stock10_r))

#Covariance Matrix
covmat1<-cov(portfolio_r)

#Shrinkage estimate of covariance
cov.shrink(portfolio_r)

#Transform covariance to correlation matrix
cov2cor(covmat1)
ew1=rep(1,10)/10
er1
covmat1
ew1

#Compute global minimum variance portfolio
gmin.port = globalMin.portfolio(er1, covmat1)
attributes(gmin.port)
print(gmin.port)
summary(gmin.port, rk.free=r.free)
plot(gmin.port, col="blue")


However, when I attempt to calculate the global minimum variance portfolio, I simply get the error message; "Error: could not find function "globalMin.portfolio". This happens even though the expected return (er1) and covariance matrix (covmat1) are calculated and output is shown when the program is run. Would appreciate any help.

• getting the same problem myself, how did you get the function to work? Thanks! – D JT Nov 4 '16 at 1:54
• Please do not remove contact from questions. This website is collaborative so any question asked here is meant to remain available with its full content. – SRKX Nov 4 '16 at 10:04

The last 2 calls must be changed from

library(portfolio.r)

library(portfolio_noshorts.r)


to

source(portfolio.r)

source(portfolio_noshorts.r)


The correct files must be availbale at http://faculty.washington.edu/ezivot/econ424/.

• That did not work. It does not appear to be a problem with the library since the code works when running this example: faculty.washington.edu/ezivot/econ424/testport.r – percy May 29 '16 at 19:38
• testport.r is sourcing portfolio_noshorts.r at the begining – Rohit Arora May 30 '16 at 14:08
• It worked. Had to make some modifications in the code. – percy May 30 '16 at 14:09