[R] how does one apply Western Electric / AT&T rules to R plots?

Jason Turner jasont at indigoindustrial.co.nz
Wed May 22 08:18:33 CEST 2002


On Mon, May 20, 2002 at 03:51:58PM -0400, default wrote:
> I have searched for info on how to apply the Western Electric rules for
> process control, to data and plots I have in R, but I have not been able
> to learn how.

The Western Electric rules are based on consecutive measures showing 
suspicous behaviour.  I've found run length encoding to be helpful
for this:

---
## imagine this was *supposed* to be a zero-mean, normally-distributed
## process, but it's drifting....

len <- 80
zz <- ts(rnorm(len)+exp(2/(len:1)))
zz.s <- sd(zz)

plot(zz,ylim=c(-4*zz.s,4*zz.s),type="l")
abline(h=0)
abline(h=-3*zz.s,col="red")
abline(h=3*zz.s,col="red")
abline(h=2*zz.s,col="red",lty=2)
abline(h=-2*zz.s,col="red",lty=2)
abline(h=zz.s,col="red",lty=3)
abline(h=-zz.s,col="red",lty=3)
## Western Electric rule 1
## 2 consecutive points fall outside 2 sd limits
zz.rule1 <- abs(zz) > 2*zz.s
zz.rule1.rle <- rle(as.vector(zz.rule1))
zz.rule1.warn <- which(zz.rule1.rle$lengths >= 2 
                       & zz.rule1.rle$values == TRUE)

## Western Electric rule 2
## 4 consecutive points fall beyond 1 sd limits
zz.rule2 <- abs(zz) > zz.s
zz.rule2.rle <- rle(as.vector(zz.rule2))
zz.rule2.warn <- which(zz.rule2.rle$lengths >= 4
                       & zz.rule2.rle$values == TRUE)

## Western Electric rule 3
## 8 consecutive points fall on one side of the centerline
zz.rule3 <- zz > 0
zz.rule3.rle <- rle(as.vector(zz.rule3))
zz.rule3.warn <- which(zz.rule3.rle$lengths >= 8)

## now colour the graph in to show where it's going wrong.
## using rule1 = red, rule2 = orange, rule3 = blue
##
## this is tedious enough that i'd brew up a function
## that takes the rle object, and the warning indicies
## and plots the right points

plot.warn.x <- function(tsobj, rleobj, ind, col,...) {
    if(length(ind) <= 0) return()
    ## got here, there must be something interesting
    ##
    ## need to convert lengths back to indicies
    ind.x <- c(1,cumsum(rleobj$lengths)+1)

    for(i in seq(along=ind)) {
        x.coords <- ind.x[ind[i]]:(ind.x[ind[i]+1]-1)
        lines(x.coords + start(tsobj)[1]-1,tsobj[x.coords],col=col,...)
        rug(x.coords + start(tsobj)[1],col=col,...)
    }
    
}
    
plot.warn.x(zz, zz.rule3.rle, zz.rule3.warn, "blue")
plot.warn.x(zz, zz.rule2.rle, zz.rule2.warn, "orange")
plot.warn.x(zz, zz.rule1.rle, zz.rule1.warn, "red")

---

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
64-21-343-545
jasont at indigoindustrial.co.nz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list