R/PlotScoreByWaitTime.R

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

  ##############################################################################
  # File:  PlotScoreByWaitTime.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 wait time.
  #
  # TODO: add RData for example
  # TODO: add testing section
  ###############################################################################
  
  (dF ##<< The dataFrame for which to convert \code{satisfaction_score} into a 
      ## number.
   ){
 
  ##details<< This function calls the \code{\link{ConvertSatisfactionScoreToNumber}} 
  ## function to assign the \code{day} values used.  
  dF <- ConvertSatisfactionScoreToNumber(dF)
  
   #Handling 'nan.0' values
  dF$requester_wait_time_in_minutes[dF$requester_wait_time_in_minutes == "nan.0"] <- NA

  dF$value <- as.numeric(dF$requester_wait_time_in_minutes)
  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 wait time.  If the red line (the result of the \code{\link{lm}}
  ## in the produced plot trends down, then wait time has a negative affect on 
  ## satisfaction score.  If the red line is parallel or very near to parallel, 
  ## then there is relationship between satisfaction score and wait time.
  
  xlabel <- "Wait time (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.