Calculating Moving Ranges


   The next thing I decided to work on was calculating the moving ranges to computer the upper and lower control limits. I am going to try to create it in the eventReactive() function. It wasn't working with the separate function.


                                     Limits for X = Average ± 2.66 * Average Moving Range
                                   
   For some reason, it is not finding my mRangeCount variable, even though I already defined it. I think I was writing the loop incorrectly. I decided to use a for-loop instead of a while loop, and it worked. Now, I need to use that to calculate the control limits.

   So, my control limits are there, but I don't know if it's right. It seems too close together. I wonder if I messed up in calculating the moving range. The control limits are too close together. I made adjustments. I realized that I kept replacing the sum of the absolute value of the moving ranges with the length of the list that contains them.

   But now, I am getting a different error. Error in plot.window: need finite 'ylim' values. I am getting this when I create the spcChart with the qcc() function. Once I created a vector for the finite y-axis values, my control lines are now null.

  I realized that the mRangeSum variable was possibly adding null values, so I set na.rm = TRUE and it worked. But, my LCL is a negative number, which doesn't seem right. I recalculated the average moving range, and for all but one variable, it was negative. I'll ask my mentor about that.

   Once I get this app to work fine on this data set, I am going to try to get it to work on different datasets to see if it would still work. It is not recognizing the rows in the new dataset.



library(qcc)
library(shiny)
library(DT)




mydata <-read.csv("dummydata.csv")
variables <- 1:ncol(mydata)
names(variables) <- names(mydata)
equipment <- mydata[1:nrow(mydata), c("CON")]



ui <- fluidPage(
  titlePanel("Control Charts"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose a file in CSV format", accept = c("text/csv", "text/comma-separated-values,text/plain",
                                                                   ".csv")
      ), #fileInput
     
      hr(),
     
      selectInput(inputId = "equip", label = "Contract Order Number:", choices = equipment), #selectInput
     
     
      selectInput(inputId = "var", label = "Choose which variable you want to graph", choices = variables), #selectInput
     
     
     
      ############################
     
      #Added by A. Coleman 20180225
     
      br(),
     
      actionButton("goButton", "Go!"),
     
      p("Update your variables"),
     
      ############################
     
     
      hr(),
     
      helpText("Created by Cameron Lowry, Senior Thesis, 2018")
     
    ), #sidebar panel
   
   
   
    mainPanel(
     
      plotOutput(outputId = "controlchart"),
      dataTableOutput(outputId = "datachart")
     
     
    ) #main panel
  ) #sidebar layout
) #user interface




server <- function(input, output, session) {
 
 
 
  loadedFile <- reactive({
    file <- input$file1
    file
  }) #loadedFile()
 
  observe({
   
    f <- loadedFile()
    if(is.null(f))
      return(NULL)
   
    temp <- read.csv(f$datapath)
    v <- 1:ncol(temp)
    names(v) <- names(temp)
    e <- temp[1:nrow(temp), c("CON")]
   
    updateSelectInput(session, 'equip', choices = e)
    updateSelectInput(session, 'var', choices = v)
   
  }) #observe UpdateSelectInput
 
 
  graph <- eventReactive(input$goButton, {
   
    file <-loadedFile()
   
    if(is.null(file))
    {
      col <- as.integer(input$var)
     
      d <- mydata[mydata$CON %in% input$equip, col ]
     
     
    }
    else
    {
      d <- read.csv(file$datapath, header = TRUE)
      col <- as.integer(input$var)
      namesCol <- names(file)
     
      if ('CON' %in% namesCol) #finish this if statement
       
     
      d <- file[file$CON %in% input$equp, col] #for some reason, it is not interpreting the row as integers
     
    }
   
    mRange <- vector('numeric')
    difference <- 0
   
    for (num in c(2 : length(d)))
    {
       difference <- d[num] - d[num - 1]
       difference = abs(difference)
       mRange[num] <- difference
     }
    mRangeSum <- sum(mRange, na.rm = TRUE)
    mRangeLength <- length(mRange)
   
   
   
    AverageMovingRange = (mRangeSum) / (mRangeLength)
    mean = mean(d)
   
    UCL <-  mean + 2.66*AverageMovingRange
    LCL <- mean - 2.66*AverageMovingRange
   
    maxYValue <- max(d, na.rm = TRUE) + 100
   
    if (UCL > maxYValue)
      maxYValue <- UCL + 100
   
   
    spcChart <- qcc(d, type = "xbar.one", ylim = c(0, maxYValue), plot = TRUE ) #qcc
   
   
   
    (warn.limits <- limits.xbar(spcChart$center, spcChart$std.dev, spcChart$sizes, 2))
    plot(spcChart, add.stats = TRUE, chart.all = TRUE, label.limits = c("LCL", "UCL"), title = "XmR Chart",
         xlab = "Increments", ylab = "Values",ylim = c(0, maxYValue), restore.par =  FALSE)
   
   
  })#eventReactive
 
 
  output$controlchart <- renderPlot({
   
    graph()
   
  }) #render plot
 
 
  output$datachart <- DT::renderDataTable({
   
    file <-loadedFile()
   
    if(is.null(file))
    {
      d <-mydata
    }
    else
    {
      d <-read.csv(file$datapath)
    }
    d
  }) #render data table
 
 
} #server function

shinyApp(ui = ui, server = server)





Sources Used:

www.spcpress.com/pdf/DJW206.pdf

https://stackoverflow.com/questions/26508519/how-to-add-elements-to-a-list-in-r-loop

https://stackoverflow.com/questions/27350636/r-argument-is-of-length-zero-in-if-statement

https://www.datamentor.io/r-programming/list

https://stackoverflow.com/questions/12614953/how-to-create-a-numeric-vector-of-zero-length-in-r

https://stackoverflow.com/questions/22235809/append-value-to-empty-vector-in-r

https://www.statmethods.net/input/missingdata.html

Comments