Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save timelyportfolio/4658217 to your computer and use it in GitHub Desktop.
Save timelyportfolio/4658217 to your computer and use it in GitHub Desktop.
#copied this function almost entirely from http://tradeblotter.wordpress.com/
#I only take credit for the ugly/bad code
#amended to accept data in long form and replace time with manager
# Histogram, QQPlot and ECDF plots aligned by scale for comparison
page.Distributions.long <- function (R, mgrcol, perfcol, ylim=c(0,0.25)) {
require(PerformanceAnalytics)
op <- par(no.readonly = TRUE)
# c(bottom, left, top, right)
par(oma = c(5,0,2,1), mar=c(0,0,0,3))
mgr = unique(R[,mgrcol])
layout(matrix(1:(4*length(mgr)), ncol=4, byrow=TRUE), widths=rep(c(.6,1,1,1),length(mgr)))
# layout.show(n=21)
chart.mins=min(R[,perfcol], na.rm=TRUE)
chart.maxs=max(R[,perfcol], na.rm=TRUE)
row.names = mgr
for(i in 1:length(mgr)){
if(i==length(mgr)){
plot.new()
text(x=1, y=0.5, adj=c(1,0.5), labels=row.names[i], cex=1.1)
chart.Histogram(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", xlim=c(chart.mins, chart.maxs), ylim=ylim,
breaks=seq(round(chart.mins, digits=2)-0.01, round(chart.maxs, digits=2)+0.01, by=0.01),
show.outliers=TRUE, methods=c("add.normal"), colorset =
c("black", "#00008F", "#005AFF", "#23FFDC", "#ECFF13", "#FF4A00", "#800000"))
abline(v=0, col="darkgray", lty=2)
chart.QQPlot(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", pch=20, envelope=0.95, col=c(1,"#005AFF"), ylim=c(chart.mins, chart.maxs))
abline(v=0, col="darkgray", lty=2)
chart.ECDF(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", xlim=c(chart.mins, chart.maxs), lwd=2)
abline(v=0, col="darkgray", lty=2)
}
else{
plot.new()
text(x=1, y=0.5, adj=c(1,0.5), labels=row.names[i], cex=1.1)
chart.Histogram(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", xlim=c(chart.mins, chart.maxs), ylim=ylim,
breaks=seq(round(chart.mins, digits=2)-0.01, round(chart.maxs, digits=2)+0.01, by=0.01),
xaxis=FALSE, yaxis=FALSE, show.outliers=TRUE, methods=c("add.normal"), colorset =
c("black", "#00008F", "#005AFF", "#23FFDC", "#ECFF13", "#FF4A00", "#800000"))
abline(v=0, col="darkgray", lty=2)
chart.QQPlot(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", xaxis=FALSE, yaxis=FALSE, pch=20, envelope=0.95, col=c(1,"#005AFF"), ylim=c(chart.mins, chart.maxs))
abline(v=0, col="darkgray", lty=2)
chart.ECDF(R[which(R[,mgrcol]==mgr[i]),perfcol], main="", xlim=c(chart.mins, chart.maxs), xaxis=FALSE, yaxis=FALSE, lwd=2)
abline(v=0, col="darkgray", lty=2)
}
}
par(op)
}
#data from pimco and vanguard websites imported into Excel and translated into csv
#if local uncomment next line
#pimco_vanguard <- read.csv("vanguard_pimco.csv")
#get data from published google doc spreadsheet
pimco_vanguard <- read.csv("https://docs.google.com/spreadsheet/pub?key=0AieeEIaS0AOsdDFET0ZmbTBKWDNoMnZrZ0oySWRia1E&single=true&gid=0&output=csv")
#do col 4 which is 1 year or past 12 months
#exclude 0 assuming that data does not exist for this fund
page.Distributions.long(pimco_vanguard[pimco_vanguard$X1Y != 0,], perfcol = 4, mgrcol = 1, ylim = c(0,10))
#do col 3 which is ytd
page.Distributions.long(pimco_vanguard[pimco_vanguard$YTD != 0,], perfcol = 3, mgrcol = 1, ylim = c(0,30))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment