R/PlotScoreByResolutionTime.R

PlotScoreByResolutionTime <- structure(
function # Plot the numeric satisfaction score as a function of wait time

  ##############################################################################
  # File:  PlotScoreByResolutionTime.R
  ##author<<  Steven H. Ranney
  ## Contact: \email{Steven.Ranney@gmail.com}
  # Created: 12/31/14  
  # Last Edited: 12/31/14 by SHR
  ##description<< This function plots the numeric value of satisfaction score 
  ## (calculated by \code{\link{ConvertSatisfactionScoreToNumber}} as a function 
  ## of resolution time.
  #
  # TODO: add RData for example
  # TODO: add testing section
  ###############################################################################
  
  (dF, ##<< The dataFrame for which to convert \code{satisfaction_score} into a 
      ## number.
  resTime = "first" ##<< A character vector that indicates which resolution time
                     ## to plot.  Other values include \code{"full"}.
  ){
 
  ##details<< This function calls the \code{\link{ConvertSatisfactionScoreToNumber}} 
  ## function to assign the \code{day} values used.  
  dF <- ConvertSatisfactionScoreToNumber(dF)
  
  #Handling 'nan.0' values
  dF$first_resolution_time_in_minutes_within_business_hours[dF$first_resolution_time_in_minutes_within_business_hours == "nan.0"] <- NA
  dF$full_resolution_time_in_minutes_within_business_hours[dF$full_resolution_time_in_minutes_within_business_hours == "nan.0"] <- NA

  if(resTime == "first") dF$value <- as.numeric(dF$first_resolution_time_in_minutes_within_business_hours)
  if(resTime == "full") dF$value <- as.numeric(dF$full_resolution_time_in_minutes_within_business_hours)
  
  dF$value <- round(dF$value/60, 1)
  
  if(length(which(is.na(dF$scoreNumber))) == nrow(dF)){
    stop("There is no satisfaction data in this data frame.")
    } else {
  
  ##details<< Plots are generated by the function \code{\link{ggplot}}
  
  ##details<< This function uses a linear model to evaluate the relationship between
  ## satisfaction score and resolution time.  If the red line (the result of the 
  ## \code{\link{lm}} in the produced plot trends down, then resolution time has 
  ## a negative affect on satisfaction score.  If the red line is parallel or 
  ## very near to parallel, then there is no relationship between satisfaction 
  ## score and resolution time.
  
  ##details<< In most cases, there are very few or no satisfaction scores.  As a 
  ## result, these data are sparse.
  
  xlabel <- NA
  if(resTime == "first") xlabel <- "First resolution (hours)"
  if(resTime == "full") xlabel <- "Full resolution (hours)"
  
   ylabel <- "Satisfaction score (numeric)"
  
  #Create a linear model of score as a function of wait time (hours)
  lmMod <- lm(dF$scoreNumber~dF$value) 

  g <- ggplot(dF, aes(x = value, y = scoreNumber))#, fill = dayOfWeek))

    p1 <- g + geom_point() + labs(ylab(ylabel)) + labs(xlab(xlabel)) +  
              geom_abline(intercept=lmMod$coefficients[1], slope=lmMod$coefficients[2], 
                          colour = "red", size = 1.01)
    
    } # end else
    return(p1)    

})
stevenranney/ispiranteRanney documentation built on May 30, 2019, 4:46 p.m.