anhoej / qicharts2

R package: Quality improvement charts
36 stars 12 forks source link

Wrong calculation of control limits for xbar chart when using freeze or exclude argument #2

Closed huftis closed 6 years ago

huftis commented 6 years ago

When using the freeze or exclude argument in qic(), the control limits are not correctly calculated. Here’s a simple example:

I generate a data frame with 30 observations (rows), paired into subgroups (group 1 = row 1 and 2; group 2 = row 3 and 4 &c.). I then create an xbar chart using either a) the first 20 observations (10 groups), or b) all observations but with a freeze/exclude value after the first 20 observations. The result should be identical control limits, but they differ, and only the limits from the a) calculations seem to be correct.

# Generate some random data
set.seed(1)
d_full = data.frame(x = c(rnorm(20, 0), rnorm(10, 5)))
d_full$gr = rep(1:15, each = 2)
d1 = d_full[1:20, ]

# Limits: -1.802 to 2.183
qicharts2::qic(gr, x, data=d1, chart="xbar", decimals = 3)

# Limits: -1.775 to 2.156
qicharts2::qic(gr, x, data=d_full, chart="xbar", decimals = 3, freeze=10)
qicharts2::qic(gr, x, data=d_full, chart="xbar", decimals = 3, exclude=11:15)

# With the old qicharts package we get identical limits for
# the two different methods of calculating the limits
# (but the limits differ from the qicharts2 limits!)
qicharts::qic(x, x = gr, data=d1, chart="xbar", decimals = 3)
qicharts::qic(x, x = gr, data=d_full, chart="xbar", decimals = 3, freeze=10)

# The qicharts2 limits for d1 seems to be correct,
# i.e. they agree with the qcc package
library(qcc)
qcc::qcc(t(unstack(d1)), type="xbar") # Quick and ugly code for generating the chart
qcc::qcc(t(unstack(d1)), newdata=t(unstack(d_full))[11:15,], type="xbar")
anhoej commented 6 years ago

Thanx! Fixed. Please test and report.

huftis commented 6 years ago

I have tested it with the latest GitHub version, and it seems to work fine. Thanks for fixing the bug!