R/plotExternalCostTotalCost.R

#' plotExternalCostTotalCost
#' 
#' Plots the welfare loss measures for the voters of interest, in the round the proposal passed, for each of the kMajority rules.
#' @param outputDataList The output data list of summaries, that is generated by the iterations() function
#' @param plotMeanEC if TRUE plot the costs for the Mean Group
#' @param plotBestEC if TRUE plot the costs for the Best Group
#' @param plotWorstEC if TRUE plot the costs for the Worst Group
#' @return A plot of the mean, across all iterations, of the external cost incurred for each k-majority rule.
#' @export

plotExternalCostTotalCost <- function(outputDataList,plotMeanEC,plotBestEC,plotWorstEC){  
  
  # Set the margins so the labels in the Top Margin will display.
  # par(mar=c(5.1, 4.1, 5.1, 2.1))
  
  #Extract External Cost
  meanExternalCost <- outputDataList$externalCost$meanOfMeanVotersExternalCostEachIteration
  bestExternalCost <- outputDataList$externalCost$meanOfbestOffGroupsMeanExternalCostEachIteration
  worstExternalCost <- outputDataList$externalCost$meanOfworstOffGroupsMeanExternalCostEachIteration
  # Calculate Decision Cost
  decisionCost <- outputDataList$rounds$meanNumberOfProposalsConsideredEachIteration * outputDataList$theInputParameters$perProposalDecisionCost
  # Calculate Total Cost for Mean, Highest and Lowest groups
  meanTotalCost <- meanExternalCost + decisionCost
  bestTotalCost <- bestExternalCost + decisionCost
  worstTotalCost <- worstExternalCost + decisionCost
  # Store a few parameters for use in plots
  parameters <- outputDataList$theInputParameters
  numK <- nrow(outputDataList$externalCost)
  K <- seq(1:numK)
  # PLOTS
  xRange <- c(0,numK)
  yRange <- c(0,1)  
  ## full yaxis ## yRange <- (0,max(tc.best))
  x_axislabels <- seq(0,numK,by=10) 
  y_axislabels <- seq(0,1,by=.1)
  ## full yaxis ## y_axislabels <- seq(0,max(tc.best),by=.1)
  plot(xRange, yRange, type="n", main="", xlab="K-Majority Rule", ylab="Expected Costs", ylim=c(0,1), font=2, axes=FALSE, frame=TRUE)
  ## full yaxis ##  plot(K,ec.worst,type="n", main="", xlab="K-Majority Rule", ylab="Expected Costs", ylim=c(0,max(tc.best)), font=2, axes=FALSE, frame=TRUE)
  axis(side=1, at=x_axislabels)
  axis(side=2, at=y_axislabels)
  lines(K,decisionCost,lty=1,lwd=3,col='black')
  lineNames <- c("decision costs") # For the Legend
  lineLty <- c(1) # For the Legend
  lineLwd <- c(3) # For the Legend
  lineCol <- c("black")
  
  if (plotMeanEC==TRUE){
    lines(K,meanExternalCost,lty=1,lwd=1,col='black')
    lines(K,meanTotalCost,lty=2,lwd=3,col='black')
    lineNames <- c(lineNames, "mean group e.c.", "mean group t.c.")
    lineLty <- c(lineLty,1,2) # For the Legend
    lineLwd <- c(lineLwd,1,3) # For the Legend
    lineCol <- c(lineCol,"black")
  }
  if (plotBestEC==TRUE){
    lines(K,bestExternalCost,lty=1,lwd=1,col='black')
    lines(K,bestTotalCost,lty=2,lwd=3,col='black')
    lineNames <- c(lineNames, "best group e.c.", "best group t.c.")
    lineLty <- c(lineLty,1,2) # For the Legend
    lineLwd <- c(lineLwd,1,3) # For the Legend
    lineCol <- c(lineCol,"black")
  }
  if (plotWorstEC==TRUE){
    lines(K,worstExternalCost,lty=1,lwd=1,col='black')
    lines(K,worstTotalCost,lty=2,lwd=3,col='black')
    lineNames <- c(lineNames, "worst group e.c.", "worst group t.c.")
    lineLty <- c(lineLty,1,2) # For the Legend
    lineLwd <- c(lineLwd,1,3) # For the Legend
    lineCol <- c(lineCol,"black")
  }
  
  mtext(text=paste("Group Size:", paste(outputDataList$theInputParameters$groupSize, collapse=", ")),
        side=3,
        line=1.50,
        adj=0,
        cex=1.1,
        font=2)
  
  mtext(text=paste("Initial Utility:", paste(
    toupper(
      substring(
        outputDataList$allGroups[[1]]$utilityDistribution,1,1)),
    "(",
    sub('^(-)?0[.]', '\\1.', outputDataList$allGroups[[1]]$utilityDistributionParam1),
    ",",
    sub('^(-)?0[.]', '\\1.',outputDataList$allGroups[[1]]$utilityDistributionParam2),
    ")", 
    collapse=", ", sep="")),
    side=3,
    line=1.50,
    adj=1,
    cex=1.1,
    font=2)
  
  mtext(text=paste("Group Error:", paste(
    toupper(
      substring(
        outputDataList$allGroups[[1]]$errorDistribution,1,1)),
    "(",
    sub('^(-)?0[.]', '\\1.', outputDataList$allGroups[[1]]$errorDistributionParam1),
    ",",
    sub('^(-)?0[.]', '\\1.',outputDataList$allGroups[[1]]$errorDistributionParam2),
    ")", 
    collapse=", ", sep="")),
    side=3,
    line=.25,
    adj=0,
    cex=1.1,
    font=2)
  
  mtext(text=paste("Change Mean Utility:", paste(outputDataList$theInputParameters$groupPostFailingProposalMeanUiIncrease, collapse=", ")),
        side=3,
        line=.25,
        adj=1,
        cex=1.1,
        font=2)
  
  mtext(text=paste("Per Round Decision Cost:", paste(outputDataList$theInputParameters$perProposalDecisionCost, collapse=", ")),
        side=1,
        line=2.00,
        adj=0,
        cex=1.1,
        font=2)
  
  legend(5,1, # places a legend at the appropriate place
         lineNames, # puts text in the legend 
         lty=lineLty, # gives the legend appropriate symbols (lines)
         lwd=lineLwd, # gives the legend the appropriate weight
         col=lineCol # gives the legend the appropriate color
  ) # 
} 
codeForReviewer/kMajorityRule documentation built on May 13, 2019, 8:47 p.m.