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(quadprog)
library(SharpeR)
library(PortfolioAnalytics)
library(zoo)
library(plotly)
library(portfolio.r)
library(portfolio_noshorts.r)
rk.free=0.005
rm( list=ls() )
portfolio <- read.csv2("File Directory/Document.csv")
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.