[Returnanalytics-commits] r2864 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 23 20:32:05 CEST 2013
Author: rossbennett34
Date: 2013-08-23 20:32:04 +0200 (Fri, 23 Aug 2013)
New Revision: 2864
Added:
pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
Log:
Adding function to chart the weights along the efficient frontier.
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-08-23 18:13:55 UTC (rev 2863)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-08-23 18:32:04 UTC (rev 2864)
@@ -18,6 +18,7 @@
export(chart.Scatter.ROI)
export(chart.Scatter.RP)
export(chart.Weights.DE)
+export(chart.Weights.EF)
export(chart.Weights.GenSA)
export(chart.Weights.optimize.portfolio.DEoptim)
export(chart.Weights.optimize.portfolio.GenSA)
Modified: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-23 18:13:55 UTC (rev 2863)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R 2013-08-23 18:32:04 UTC (rev 2864)
@@ -176,3 +176,62 @@
axis(2, cex.axis = cex.axis, col = element.color)
box(col = element.color)
}
+
+#' chart the weights along the efficient frontier
+#'
+#' This creates a stacked column chart of the weights of portfolios along the efficient frontier.
+#'
+#' @param object object of class 'efficient.frontier' created by \code{\link{create.EfficientFrontier}}.
+#' @param colorset color palette to use.
+#' @param ... passthrough parameters to \code{chart.StackedBar}.
+#' @param match.col match.col string name of column to use for risk (horizontal axis).
+#' Must match the name of an objective.
+#' @param main main title used in the plot.
+#' @param las sets the orientation of the axis labels, as described in \code{\link{par}}.
+#' @param cex.lab The magnification to be used for x- and y-axis labels relative to the current setting of 'cex'.
+#' @param cex.axis The magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}.
+#' @param cex.legend The magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}.
+#' @param legend.loc places a legend into a location on the chart similar to \code{\link{chart.TimeSeries}}. The default, "under," is the only location currently implemented for this chart. Use 'NULL' to remove the legend.
+#' @param legend.labels character vector to use for the legend labels
+#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc.
+#' @author Ross Bennett
+#' @export
+chart.Weights.EF <- function(object, colorset=NULL, ..., match.col="ES", main="EF Weights", las=1, cex.lab=0.8, cex.axis=0.8, cex.legend=0.8, legend.loc="under", legend.labels=NULL, element.color="darkgray"){
+ if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'")
+
+ # get the columns with weights
+ cnames <- colnames(object)
+ wts_idx <- grep(pattern="^w\\.", cnames)
+ wts <- object[, wts_idx]
+
+ if(!is.null(legend.labels)){
+ # use legend.labels passed in by user
+ colnames(wts) <- legend.labels
+ } else {
+ # remove w. from the column names
+ colnames(wts) <- gsub(pattern="^w\\.", replacement="", cnames[wts_idx])
+ }
+
+ # get the "mean" column
+ mean.mtc <- pmatch("mean", cnames)
+ if(is.na(mean.mtc)) {
+ mean.mtc <- pmatch("mean.mean", cnames)
+ }
+ if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output")
+
+ # get the match.col column
+ mtc <- pmatch(match.col, cnames)
+ if(is.na(mtc)) {
+ mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames)
+ }
+ if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+
+ # plot the 'match.col' (risk) as the x-axis labels
+ xlabels <- round(object[, mtc], 4)
+
+ chart.StackedBar(w=wts, colorset=colorset, ..., main=main, las=las, space=0,
+ cex.lab=cex.lab, cex.axis=cex.axis, cex.legend=cex.legend,
+ legend.loc=legend.loc, element.color=element.color,
+ xaxis.labels=xlabels, xlab=match.col, ylab="Weights")
+}
+
Modified: pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/extract.efficient.frontier.R 2013-08-23 18:13:55 UTC (rev 2863)
+++ pkg/PortfolioAnalytics/R/extract.efficient.frontier.R 2013-08-23 18:32:04 UTC (rev 2864)
@@ -67,6 +67,8 @@
if(is.na(mtc)) {
mtc = pmatch(paste(match.col,match.col,sep='.'),columnnames)
}
+ if(is.na(mtc)) stop("could not match match.col with column name of extractStats output")
+
if(is.null(from)){
from <- min(xtract[, mtc])
}
Added: pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd 2013-08-23 18:32:04 UTC (rev 2864)
@@ -0,0 +1,60 @@
+\name{chart.Weights.EF}
+\alias{chart.Weights.EF}
+\title{chart the weights along the efficient frontier}
+\usage{
+ chart.Weights.EF(object, colorset = NULL, ...,
+ match.col = "ES", main = "EF Weights", las = 1,
+ cex.lab = 0.8, cex.axis = 0.8, cex.legend = 0.8,
+ legend.loc = "under", legend.labels = NULL,
+ element.color = "darkgray")
+}
+\arguments{
+ \item{object}{object of class 'efficient.frontier'
+ created by \code{\link{create.EfficientFrontier}}.}
+
+ \item{colorset}{color palette to use.}
+
+ \item{...}{passthrough parameters to
+ \code{chart.StackedBar}.}
+
+ \item{match.col}{match.col string name of column to use
+ for risk (horizontal axis). Must match the name of an
+ objective.}
+
+ \item{main}{main title used in the plot.}
+
+ \item{las}{sets the orientation of the axis labels, as
+ described in \code{\link{par}}.}
+
+ \item{cex.lab}{The magnification to be used for x- and
+ y-axis labels relative to the current setting of 'cex'.}
+
+ \item{cex.axis}{The magnification to be used for sizing
+ the axis text relative to the current setting of 'cex',
+ similar to \code{\link{plot}}.}
+
+ \item{cex.legend}{The magnification to be used for sizing
+ the legend relative to the current setting of 'cex',
+ similar to \code{\link{plot}}.}
+
+ \item{legend.loc}{places a legend into a location on the
+ chart similar to \code{\link{chart.TimeSeries}}. The
+ default, "under," is the only location currently
+ implemented for this chart. Use 'NULL' to remove the
+ legend.}
+
+ \item{legend.labels}{character vector to use for the
+ legend labels}
+
+ \item{element.color}{provides the color for drawing
+ less-important chart elements, such as the box lines,
+ axis lines, etc.}
+}
+\description{
+ This creates a stacked column chart of the weights of
+ portfolios along the efficient frontier.
+}
+\author{
+ Ross Bennett
+}
+
More information about the Returnanalytics-commits
mailing list