In this post, I will show you how a very basic R code can be used to estimate quality control constants needed to construct X-Individuals, X-Bar, and R-Bar charts. The value of this approach is that it gives you a mechanical sense of where these constants come from and some reinforcement on their application. If you work in a production or quality control environment, chances are you've made or seen a control chart. If you're new to control charting or need a refresher check out Understanding Statistical Process Control, Wheeler et. al. If you want to dive in and start making control charts with R, check out R packages ggQC: ggplot based QC charting qcc: base graphics QC charting If your familiar with control charts, you've likely encountered cryptic alpha-numeric constants like d2, A2, E2, d3, D3, and asked, “What are they and where do they come from?” Short (not so satisfying) answer: They are constants you plug into formulas to determine your control limits. Their value depends on how many samplings you do at a time and the type of chart you are making. For example, if you measure the size of 5 widgets per lot of 50, then your subgroup size, n, is 5 and you should be using a set of control chart constants for n = 5. So where do they come from and how are they calculated? Read on. X-Bar and X-Individuals Constants Often, control charts represent variability in terms of the mean range, R, observed over several subgroup rather than the mean standard deviation. The table below should make the idea of subgroup range and mean range more clear. Why range? My guess is that, historically, employees at all levels would have understood the concept of range. Range requires no special computation, just (max-min). Speculation aside, we begin our quest to understand where control constants come from with the relationship shown in Eq. 1 that the mean subgroup range is proportional to standard deviation of the individual values. The proportionality constant between R(XSub_Grp_Indv) and S(Xindv) is d2, the first constant we'll be estimating. The relationship is expressed in Eq.2 To estimate d2 for n = 2 (i.e, the subgroup size is 2), we start by drawing two samples from a normal distribution with mean = 0 and sd = 1. Why you ask? Because it makes the math really simple. Consider Eq. 2, if S(Xindv) = 1 then R(XSub_Grp_Indv) = d2. So all we need to do to determine d2 when n = 2 is: Draw 2 individuals from the normal distribution, Determine the range of the 2 samples. Repeat many, many times Take the average of all the ranges you’ve calculated, R R = d2 (when the mean = 0 and sd = 1) The R code for the process is shown below. ```r require(magrittr) #Bring in the Pipe Function reps % #Draw Two From the Normal Distibution range() %__% #Determine the Range Vector = (Max, Min) diff() %__% #Determine the Difference of the Range Vector abs() #Take the Absolution Value to make sure the Result is positive ) %__% # Replicate the above proceedure 1,000,000 Times mean() -__ R_BAR -__ d2 #Take the mean of the 1,000,000 ranges d2 ``` ``` ## [1] 1.12804 ``` The pipes make the above code easy to read but slow things down quite a bit. The following code does the same thing about 12.5 times faster. ```r reps