How to extract or calcualte special cause variations shown in a qcc chart (xmr, spc)

211 Views Asked by At

I created a qcc chart in R and would like to hold the special cause variations from the qcc chart in variables for use else where.

I have tried to extract the special causes for chart 'Number beyond limits' and 'Number violating runs' with no luck. So I am trying to recalculate the special causes staring with 'seven points continually increasing'.

Q1 . What is wrong with my for loop that is meant to detect 'seven points continually increasing'. Error message:

Error in incremnt : object 'incremnt' not found

Q2. how can I get the gcc chart to show the date on the x-axis

Useful links that I used for this project:

https://improvement.nhs.uk/documents/2171/statistical-process-control.pdf https://www.r-bloggers.com/xmr-chart-step-by-step-guide-by-hand-and-with-r/

SPC - Control Charts by Group in R

....R
install.packages("qcc")
install.packages("ggQC")
library(qcc)
library(ggQC)


exampl_data <- data.frame(
 # ScrewID = as.factor(1:20),
  ScrewID = seq(as.Date("2000/1/1"), by = "month", length.out = 20),
  Length = c( 
    2.92,    3.16,    2.88,    2.90,    2.92,
    2.94,    2.96,    2.98,    3.02,    2.67,
    3.09,    3.07,    3.04,    3.06,    3.05,
    3.03,    3.07,    2.91,    3.07,    3.30
  )
)

qcc_chart <- qcc(exampl_data$Length, type = "xbar.one", plot = FALSE)
# add warning limits at 2 and 3 std. deviations
(warn.limits  <- limits.xbar(qcc_chart$center, qcc_chart$std.dev, qcc_chart$sizes, 1))
(warn.limits2 <- limits.xbar(qcc_chart$center, qcc_chart$std.dev, qcc_chart$sizes, 2)
)
plot(qcc_chart, restore.par = TRUE)
abline(h = warn.limits, lty = 3, col = "gray")
abline(h = warn.limits2, lty = 3, col = "gray")


# Not working: My attempt at loop to identify 7 row of incresing values
for (i in 1:length(exampl_data$Length))
{
  increment <- 0
  while (exampl_data$Length[i + increment] <= exampl_data$Length[(i + increment) +
                                                                 1] & increment < 8)
  {
    if (incremnt == 7)
    {
      print(exampl_data$Length[i + increment])
      break()
    }
    increment <- increment + 1
  }
}


....
1

There are 1 best solutions below

0
On

This is what I have worked out with help from this post Longest run of changes for each dataframe in a list

Code for identifying seven points consecutively ascending is below.

# Test one, identify a run of seven values increasing


 library(tidyverse)

increment_run <- rep('False', time = length(exampl_data$Length))

for (i in 1:length(exampl_data$Length))
{

  if(exampl_data$Length[i+1] >= exampl_data$Length[i] & i < 20)
  {
    increment_run[i+1] = 'Ture'
    print(paste(i+1,' Ture', sep=" ")) # debug

  }else if (i < 20){ print(paste(i+1,' False', sep=" ")) #debug 
  }

}


exampl_data <- exampl_data %>%
            mutate(streak = sequence(rle(as.character(increment_run))$lengths))

exampl_data %>%
    #select(values)%>%
    filter(streak > 5)

If anyone has better of doing this then share?