# public timelyportfolio / knitR Performance Attempt 2.rnw Created 2012-04-18

knitR Performance Attempt 2.rnw
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 %% LyX 2.0.2 created this file. For more info, see http://www.lyx.org/.%% Do not edit unless you really know what you are doing.\documentclass[english,nohyper,noae]{tufte-handout}\usepackage{helvet}\usepackage[T1]{fontenc}\usepackage[latin9]{inputenc}\usepackage{babel}\usepackage[unicode=true,pdfusetitle, bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=1, breaklinks=true,pdfborder={0 0 0},backref=false,colorlinks=false] {hyperref}\usepackage{breakurl} \makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands. \title{knitr Performance Summary Attempt 2}\author{Timely Portfolio}\providecommand{\LyX}{\texorpdfstring% {L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\@} {LyX}} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.<>= if(exists(".orig.enc")) options(encoding = .orig.enc)@ \makeatother \begin{document}\maketitle\begin{abstract}This time we will try to incorporate some \LyX{} brilliance to helpus format the end result. Unfortunately, this process (most likelydue to my ignorance) was not nearly as seamless as the first experimentswith knitr. I eventually went back to old-school manual coding in RStudio.\end{abstract} \section{Performance Overview} We all know that performance reports generally blend both text tablesand graphical charts to communicate. In this example, we will usetwo of the most popular Vanguard funds (vbmfx and vfinx) as the subjectsfor our performance evaluation. <>=require(quantmod)require(PerformanceAnalytics)getSymbols("VFINX",from="1990-01-01",adjust=TRUE)getSymbols("VBMFX",from="1990-01-01",adjust=TRUE)perf <- na.omit(merge(monthlyReturn(VBMFX[,4]),monthlyReturn(VFINX[,4])))colnames(perf) <- c("VBMFX","VFINX")@  <>=require(lattice)require(latticeExtra)require(reshape2)#note: table.CalendarReturns only handles monthly dataperf.annual<-as.data.frame(table.CalendarReturns(perf)[,13:14])perf.annual<-cbind(rownames(perf.annual),perf.annual)perf.annual.melt <- melt(perf.annual,id.vars=1)colnames(perf.annual.melt)<-c("Year","Fund","Return")p1 <- dotplot(Year~Return,group=Fund,data=perf.annual.melt, pch=19, lattice.opts=theEconomist.opts(), par.settings = theEconomist.theme(box = "transparent"), main="Annual Returns of VFINX and VBMFX", auto.key=list(space="right"), xlim=c(min(perf.annual.melt[,3]),max(perf.annual.melt[,3])))p2 <- densityplot(~Return, group=Fund, data=perf.annual.melt, lattice.opts=theEconomist.opts(), par.settings = theEconomist.theme(box = "transparent"))print(p1,position=c(0,0,0.6,1),more=TRUE)print(p2+p1,position=c(0.6,0,1,1))@<>=@<>=require(xtable)print(xtable(t(last(table.CalendarReturns(perf)[,13:14],13))), floating=FALSE)@\newpage\section{Risk and Return}Although the summary and distribution of annual returns is a good first step, any real due diligence will require much more than just return. Let's do a very basic plot of risk and return. Of course, there are much more sophisticated methods, which we will explore in future versions.\begin{figure}<>=perf.stats <- table.Stats(perf)#eliminate observations,na,skewness,and kurtosisperf.stats <- perf.stats[3:(NROW(perf.stats)-2),]perf.stats.melt <- melt(as.data.frame(cbind(rownames(perf.stats),perf.stats),stringsAsFactors=FALSE),id.vars=1)colnames(perf.stats.melt)<-c("Statistic","Fund","Value")barchart(Statistic~Value,group=Fund,data=perf.stats.melt, origin=0, lattice.opts=theEconomist.opts(), par.settings=theEconomist.theme(box="transparent"), main="Risk and Return Statistics")@\end{figure} \section{Diversification}In today's markets with almost universally high positive correlations, diversification is much more difficult. However, most performance reports should include some analysis of both correlation and diversification.\begin{figure}<>=chart.Correlation(perf, main="Correlation Analysis")@<>=chart.RollingCorrelation(perf[,1],perf[,2],main="VBMFX and VFINX Rolling 12 Month Correlation",xlab="Correlation")@\end{figure}\begin{figure}<>=require(fPortfolio)frontier <- portfolioFrontier(as.timeSeries(perf))frontierPlot(frontier,pch=19,title=FALSE)singleAssetPoints(frontier,col=c("steelblue2","steelblue3"),pch=19,cex=2)title(main="Efficient Frontier with VBMFX and VFINX",adj=0)@\end{figure} \end{document}
knitr Performance Attempt 2.r
R
 1 2 require(knitr)knit2pdf("knitr performance 2.rnw")