How to code rule number 4 from Western Electric rule's for quality control charts

972 Views Asked by At

I am new to statistics. I have a problem at hand for which I need to code all the 4 rules of Western Electric rules for Quality control. I have been able to code the first one and second with the help of my peer, could anyone help me out in writing down rule number 4 - "NINE consecutive points fall on the same side of the centerline"

enter image description here

I have plotted rule 1 by getting the data below and above the threshold and then ran the matplotlib plot in single cell.

I am not able to get the data for rule number 4.

2

There are 2 best solutions below

0
On

Really late followup, but you could do something like "if minimum of previous 9 points and maximum of previous 9 points are both same side (> or <) mean, failure."

This shows the rolling min, rolling max should work similarly https://stackoverflow.com/a/33920859/18474867

Generate a "rolling min" and "rolling max" column for the last 9 rows, if anywhere they're both + or both -, flag it as a failure.

0
On

Even if no one answered it, it gave me enough motivation to workout a solution. although I know my code is not optimal, please suggest me if I need any changes further.

temp_up=[]
temp_down=[]
for i in range(len(data)):
    if arr_data[i] > y_mean:
        temp_up.append(i)
    else:
        temp_down.append(i)

#now we have index values for both data above and below mean, 
#we will now get the sequence of the index to know if there's any run of or greater than length 9
from itertools import groupby
from operator import itemgetter
d_up=[]
d_down=[]
for k, g in groupby(enumerate(temp_up), lambda ix : ix[0] - ix[1]):
    t_up=(list(map(itemgetter(1), g)))
    if len(t_up)>=9:#check if the length of the sequence is greater than or equal to 9
        #get index to mark red for the data
        for i in range(8, len(t_up), 1):
            d_up.append(t_up[i])#index number of data points voilating number 4 rule (above mean)

for k, g in groupby(enumerate(temp_down), lambda ix : ix[0] - ix[1]):
    t_down=(list(map(itemgetter(1), g)))
    if len(t_down)>=9:#check if the length of the sequence is greater than or equal to 9
        #print(t_down)
        #get index to mark red for the data
        for i in range(8, len(t_down), 1):
            d_down.append(t_down[i])#index number of data points voilating number 4 rule (above mean)

data_above_r4 = pd.DataFrame(data.iloc[d_up])