[Returnanalytics-commits] r2973 - in pkg/PortfolioAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Sep 3 07:23:35 CEST 2013


Author: rossbennett34
Date: 2013-09-03 07:23:35 +0200 (Tue, 03 Sep 2013)
New Revision: 2973

Modified:
   pkg/PortfolioAnalytics/R/charts.groups.R
   pkg/PortfolioAnalytics/man/chart.GroupWeights.Rd
   pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd
Log:
Adding barplot for group weights

Modified: pkg/PortfolioAnalytics/R/charts.groups.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.groups.R	2013-09-03 02:35:10 UTC (rev 2972)
+++ pkg/PortfolioAnalytics/R/charts.groups.R	2013-09-03 05:23:35 UTC (rev 2973)
@@ -7,6 +7,7 @@
 #'   \item{groups: }{group the weights group constraints}
 #'   \item{category_labels: }{group the weights by category_labels in portfolio object}
 #' }
+#' @param plot.type "line" or "barplot"
 #' @param main an overall title for the plot: see \code{\link{title}}
 #' @param las numeric in \{0,1,2,3\}; the style of axis labels
 #'       \describe{
@@ -21,61 +22,95 @@
 #' @param cex.axis The magnification to be used for x and y axis relative to the current setting of \code{cex}
 #' @author Ross Bennett
 #' @export
-chart.GroupWeights <- function(object,  ..., grouping=c("groups", "category"), main="Group Weights", las = 3, xlab=NULL, cex.lab = 1, element.color = "darkgray", cex.axis=0.8){
-  if(!inherits(object, "optimize.portfolio.ROI")) stop("object must be of class 'optimize.portfolio'")
+chart.GroupWeights <- function(object,  ..., grouping=c("groups", "category"), plot.type="line", main="Group Weights", las=3, xlab=NULL, cex.lab=0.8, element.color="darkgray", cex.axis=0.8){
+  if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")
   
+  if(plot.type %in% c("bar", "barplot")){
+    barplotGroupWeights(object=object, ...=..., grouping=grouping, main=main, 
+                        las=las, xlab=xlab, cex.lab=cex.lab, 
+                        element.color=element.color, cex.axis=cex.axis)
+  } else if(plot.type == "line"){
+    constraints <- get_constraints(object$portfolio)
+    tmp <- extractGroups(object)
+    grouping <- grouping[1]
+    
+    if(grouping == "groups"){
+      weights <- tmp$group_weights
+      if(is.null(weights)) stop("No weights detected for groups")
+      if(any(is.infinite(constraints$cUP)) | any(is.infinite(constraints$cLO))){
+        # set ylim based on weights if box constraints contain Inf or -Inf
+        ylim <- range(weights)
+      } else {
+        # set ylim based on the range of box constraints min and max
+        ylim <- range(c(constraints$cLO, constraints$cUP))
+      }
+    }
+    
+    if(grouping == "category"){
+      weights <- tmp$category_weights
+      if(is.null(weights)) stop("No weights detected for category")
+      ylim <- range(weights)
+    }
+    
+    columnnames = names(weights)
+    numgroups = length(columnnames)
+    
+    if(is.null(xlab))
+      minmargin = 3
+    else
+      minmargin = 5
+    if(main=="") topmargin=1 else topmargin=4
+    if(las > 1) {# set the bottom border to accommodate labels
+      bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
+      if(bottommargin > 10 ) {
+        bottommargin<-10
+        columnnames<-substr(columnnames,1,19)
+      }
+    }
+    else {
+      bottommargin = minmargin
+    }
+    par(mar = c(bottommargin, 4, topmargin, 2) +.1)
+    
+    plot(weights, axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, ...)
+    if(grouping == "groups"){
+      if(!any(is.infinite(constraints$cLO))){
+        points(constraints$cLO, type="b", col="darkgray", lty="solid", lwd=2, pch=24)
+      }
+      if(!any(is.infinite(constraints$cUP))){
+        points(constraints$cUP, type="b", col="darkgray", lty="solid", lwd=2, pch=25)
+      }
+    }
+    axis(2, cex.axis = cex.axis, col = element.color)
+    axis(1, labels=columnnames, at=1:numgroups, las=las, cex.axis = cex.axis, col = element.color)
+    box(col = element.color)
+  }
+}
+
+#' barplot of group weights
+#' @author Ross Bennett
+barplotGroupWeights <- function(object,  ..., grouping=c("groups", "category"), main="Group Weights", las=3, xlab=NULL, cex.lab=0.8, element.color="darkgray", cex.axis=0.8){
+  if(!inherits(object, "optimize.portfolio")) stop("object must be of class 'optimize.portfolio'")
+  
   constraints <- get_constraints(object$portfolio)
   tmp <- extractGroups(object)
-  grouping <- grouping[1]
+  grouping <- "groups"
   
   if(grouping == "groups"){
     weights <- tmp$group_weights
     if(is.null(weights)) stop("No weights detected for groups")
-    if(any(is.infinite(constraints$cUP)) | any(is.infinite(constraints$cLO))){
-      # set ylim based on weights if box constraints contain Inf or -Inf
-      ylim <- range(weights)
-    } else {
-      # set ylim based on the range of box constraints min and max
-      ylim <- range(c(constraints$cLO, constraints$cUP))
-    }
   }
   
   if(grouping == "category"){
     weights <- tmp$category_weights
     if(is.null(weights)) stop("No weights detected for category")
-    ylim <- range(weights)
   }
   
   columnnames = names(weights)
   numgroups = length(columnnames)
   
-  if(is.null(xlab))
-    minmargin = 3
-  else
-    minmargin = 5
-  if(main=="") topmargin=1 else topmargin=4
-  if(las > 1) {# set the bottom border to accommodate labels
-    bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab
-    if(bottommargin > 10 ) {
-      bottommargin<-10
-      columnnames<-substr(columnnames,1,19)
-    }
-  }
-  else {
-    bottommargin = minmargin
-  }
-  par(mar = c(bottommargin, 4, topmargin, 2) +.1)
-  
-  plot(weights, axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, ...)
-  if(grouping == "groups"){
-    if(!any(is.infinite(constraints$cLO))){
-      points(constraints$cLO, type="b", col="darkgray", lty="solid", lwd=2, pch=24)
-    }
-    if(!any(is.infinite(constraints$cUP))){
-      points(constraints$cUP, type="b", col="darkgray", lty="solid", lwd=2, pch=25)
-    }
-  }
-  axis(2, cex.axis = cex.axis, col = element.color)
-  axis(1, labels=columnnames, at=1:numgroups, las=las, cex.axis = cex.axis, col = element.color)
-  box(col = element.color)
+  barplot(weights, ylab = "", names.arg=columnnames,
+          border=element.color, cex.axis=cex.axis, main=main, las=las, 
+          cex.names=cex.lab, xlab=xlab, ...)
+  box(col=element.color)
 }

Modified: pkg/PortfolioAnalytics/man/chart.GroupWeights.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.GroupWeights.Rd	2013-09-03 02:35:10 UTC (rev 2972)
+++ pkg/PortfolioAnalytics/man/chart.GroupWeights.Rd	2013-09-03 05:23:35 UTC (rev 2973)
@@ -3,9 +3,9 @@
 \title{Chart weights by group or category}
 \usage{
   chart.GroupWeights(object, ...,
-    grouping = c("groups", "category"),
+    grouping = c("groups", "category"), plot.type = "line",
     main = "Group Weights", las = 3, xlab = NULL,
-    cex.lab = 1, element.color = "darkgray",
+    cex.lab = 0.8, element.color = "darkgray",
     cex.axis = 0.8)
 }
 \arguments{
@@ -17,6 +17,8 @@
   weights group constraints} \item{category_labels: }{group
   the weights by category_labels in portfolio object} }}
 
+  \item{plot.type}{"line" or "barplot"}
+
   \item{main}{an overall title for the plot: see
   \code{\link{title}}}
 

Modified: pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd	2013-09-03 02:35:10 UTC (rev 2972)
+++ pkg/PortfolioAnalytics/man/chart.Weights.EF.Rd	2013-09-03 05:23:35 UTC (rev 2973)
@@ -5,22 +5,24 @@
 \title{chart the weights along the efficient frontier}
 \usage{
   chart.Weights.EF(object, colorset = NULL, ...,
-    n.portfolios = 25, match.col = "ES",
+    n.portfolios = 25, by.groups = FALSE, match.col = "ES",
     main = "EF Weights", cex.lab = 0.8, cex.axis = 0.8,
     cex.legend = 0.8, legend.labels = NULL,
     element.color = "darkgray")
 
   chart.Weights.EF.efficient.frontier(object,
     colorset = NULL, ..., n.portfolios = 25,
-    match.col = "ES", main = "", cex.lab = 0.8,
-    cex.axis = 0.8, cex.legend = 0.8, legend.labels = NULL,
-    element.color = "darkgray", legend.loc = "topright")
+    by.groups = FALSE, match.col = "ES", main = "",
+    cex.lab = 0.8, cex.axis = 0.8, cex.legend = 0.8,
+    legend.labels = NULL, element.color = "darkgray",
+    legend.loc = "topright")
 
   chart.Weights.EF.optimize.portfolio(object,
     colorset = NULL, ..., n.portfolios = 25,
-    match.col = "ES", main = "", cex.lab = 0.8,
-    cex.axis = 0.8, cex.legend = 0.8, legend.labels = NULL,
-    element.color = "darkgray", legend.loc = "topright")
+    by.groups = FALSE, match.col = "ES", main = "",
+    cex.lab = 0.8, cex.axis = 0.8, cex.legend = 0.8,
+    legend.labels = NULL, element.color = "darkgray",
+    legend.loc = "topright")
 }
 \arguments{
   \item{object}{object of class \code{efficient.frontier}
@@ -34,6 +36,9 @@
   the efficient frontier. This is only used for objects of
   class \code{optimize.portfolio}}
 
+  \item{by.groups}{TRUE/FALSE. If TRUE, the weights by
+  group are charted.}
+
   \item{match.col}{match.col string name of column to use
   for risk (horizontal axis). Must match the name of an
   objective.}



More information about the Returnanalytics-commits mailing list