@numero-uno-jpn/rstats 1.17.0 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/README.ja.md +7 -7
- package/README.md +7 -7
- package/dist/causal/mediationProcedures.js +18 -1
- package/dist/causal/moderation.js +11 -2
- package/dist/causal.js +2 -2
- package/dist/chunks/chunk-2S45NNTL.js +2 -0
- package/dist/chunks/chunk-4JFGT4PN.js +2 -0
- package/dist/chunks/chunk-4JRJU5F5.js +2 -0
- package/dist/chunks/{chunk-KY53MB3J.js → chunk-5NW4WT4F.js} +2 -2
- package/dist/chunks/chunk-6ZGEYAFC.js +2 -0
- package/dist/chunks/{chunk-XR7Y4ZV6.js → chunk-7D7FBLWD.js} +2 -2
- package/dist/chunks/chunk-ANFN3X2M.js +2 -0
- package/dist/chunks/{chunk-NCRL33PQ.js → chunk-AS2XTRHH.js} +2 -2
- package/dist/chunks/chunk-BJ5WAL2C.js +2 -0
- package/dist/chunks/{chunk-EIUK6TKF.js → chunk-CEOSH3H2.js} +2 -2
- package/dist/chunks/{chunk-VZSQAGL2.js → chunk-CMNKXRZF.js} +2 -2
- package/dist/chunks/{chunk-XEJX5HBJ.js → chunk-CNJLPH6W.js} +2 -2
- package/dist/chunks/{chunk-TTOJDLIQ.js → chunk-DXSVV47D.js} +2 -2
- package/dist/chunks/{chunk-GIIURG5X.js → chunk-FAKOND5W.js} +2 -2
- package/dist/chunks/chunk-FCGTZ6BD.js +2 -0
- package/dist/chunks/{chunk-O4D43LWM.js → chunk-FELX2TZZ.js} +2 -2
- package/dist/chunks/{chunk-EB45CDFA.js → chunk-G4B2SQR6.js} +2 -2
- package/dist/chunks/chunk-HPIZAWMT.js +2 -0
- package/dist/chunks/chunk-I5XUWUEV.js +2 -0
- package/dist/chunks/{chunk-JG4TWDQ4.js → chunk-ILQ4VWSD.js} +2 -2
- package/dist/chunks/chunk-JLFRYDYO.js +2 -0
- package/dist/chunks/{chunk-GTSK4IN4.js → chunk-JXKHFSGK.js} +1 -1
- package/dist/chunks/{chunk-YMI33F7W.js → chunk-LBU73UMS.js} +2 -2
- package/dist/chunks/{chunk-ZRKXHVTJ.js → chunk-LSZTDKIB.js} +2 -2
- package/dist/chunks/chunk-LWFKGF43.js +2 -0
- package/dist/chunks/{chunk-NCDPZSI2.js → chunk-M5J3DHXB.js} +2 -2
- package/dist/chunks/{chunk-DD2GG4EC.js → chunk-MOYWWJP4.js} +1 -1
- package/dist/chunks/{chunk-I2KJMF4G.js → chunk-MPBFXHXT.js} +1 -1
- package/dist/chunks/chunk-NWNJEAKI.js +2 -0
- package/dist/chunks/{chunk-GOYTZ5RB.js → chunk-OTETSONJ.js} +2 -2
- package/dist/chunks/{chunk-V54MBEJM.js → chunk-QH4GK3JN.js} +2 -2
- package/dist/chunks/chunk-QVE7V5UG.js +2 -0
- package/dist/chunks/{chunk-RNGDVSTS.js → chunk-RMGEIU4Z.js} +2 -2
- package/dist/chunks/{chunk-LKKUUREA.js → chunk-SAFH76DR.js} +2 -2
- package/dist/chunks/{chunk-QCPGV7XH.js → chunk-SJJJ7Y4X.js} +2 -2
- package/dist/chunks/{chunk-L77QFL3C.js → chunk-TVU4N6BA.js} +2 -2
- package/dist/chunks/{chunk-6MLDKHTY.js → chunk-U6RXYALO.js} +2 -2
- package/dist/chunks/chunk-VNLSP22M.js +2 -0
- package/dist/chunks/chunk-W4EITKLJ.js +2 -0
- package/dist/chunks/{chunk-JHABMYNH.js → chunk-WWRKCRTZ.js} +1 -1
- package/dist/chunks/{chunk-NPWWGIFG.js → chunk-XHHDBEHB.js} +2 -2
- package/dist/chunks/{chunk-72I7GDWL.js → chunk-XJMBX6A2.js} +2 -2
- package/dist/chunks/chunk-YZ6SBFFG.js +2 -0
- package/dist/descriptive/qcc.d.ts +320 -0
- package/dist/descriptive/qcc.js +524 -0
- package/dist/descriptive/qccTools.d.ts +301 -0
- package/dist/descriptive/qccTools.js +390 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions/normal.d.ts +4 -0
- package/dist/distributions/normal.js +4 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +2 -0
- package/dist/entries/descriptive.js +2 -0
- package/dist/entries/regression.d.ts +1 -0
- package/dist/entries/regression.js +2 -0
- package/dist/entries/tests.d.ts +1 -0
- package/dist/entries/tests.js +1 -0
- package/dist/htest/timeROC.d.ts +564 -0
- package/dist/htest/timeROC.js +1106 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/betareg.js +14 -2
- package/dist/models/bradleyterry.d.ts +188 -0
- package/dist/models/bradleyterry.js +533 -0
- package/dist/models/survreg.d.ts +5 -0
- package/dist/models/survreg.js +23 -3
- package/dist/models/tobit.js +16 -5
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -3
- package/dist/rstats.js +18 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-23QCIPET.js +0 -2
- package/dist/chunks/chunk-3UEYA6VG.js +0 -2
- package/dist/chunks/chunk-3Y4DNK4O.js +0 -2
- package/dist/chunks/chunk-4BGGWQWS.js +0 -2
- package/dist/chunks/chunk-5UFVACSV.js +0 -2
- package/dist/chunks/chunk-5W2OZAHQ.js +0 -2
- package/dist/chunks/chunk-BWDXJL7T.js +0 -2
- package/dist/chunks/chunk-GLABB425.js +0 -2
- package/dist/chunks/chunk-JV3POOET.js +0 -2
- package/dist/chunks/chunk-K3YSXDTU.js +0 -2
- package/dist/chunks/chunk-LY2UZGUL.js +0 -2
- package/dist/chunks/chunk-PSQ6MLJM.js +0 -2
- package/dist/chunks/chunk-RM5D7Q7W.js +0 -2
- package/dist/chunks/chunk-SA2E5GVO.js +0 -2
- package/dist/chunks/chunk-T5ZALLTJ.js +0 -2
- package/dist/chunks/chunk-XPYZOG5H.js +0 -2
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
// Shewhart control charts (qcc's qcc and shewhart.rules), the cusum and ewma charts and ewmaSmooth: the values only,
|
|
2
|
+
// no plots. Behaviour: oracle/qcc-bt-behaviour.md (clean room: from qcc's documentation, the textbook definitions it
|
|
3
|
+
// names and the output of R; no source of R or of a package was read). Written for rStats.js.
|
|
4
|
+
import { lgamma } from "../special/gamma.js";
|
|
5
|
+
import { qnorm } from "../distributions/normal.js";
|
|
6
|
+
import { qtukey } from "../distributions/tukey.js";
|
|
7
|
+
import { qchisq } from "../distributions/chisq.js";
|
|
8
|
+
import { qbinom } from "../distributions/binomial.js";
|
|
9
|
+
import { qpois } from "../distributions/poisson.js";
|
|
10
|
+
import { qgeom } from "../distributions/geometric.js";
|
|
11
|
+
import { mean as meanOf, sd as sdOf } from "./basic.js";
|
|
12
|
+
import { Warnings, reported, withWarnings, warn } from "../internal/warnings.js";
|
|
13
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
14
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
15
|
+
const QCC_KEYS = /* @__PURE__ */ new Set([
|
|
16
|
+
"type", "sizes", "center", "stdDev", "limits", "newdata", "newsizes", "labels", "newlabels", "nsigmas",
|
|
17
|
+
"confidenceLevel", "runLength", "k",
|
|
18
|
+
]);
|
|
19
|
+
const SHEWHART_RULES_KEYS_2 = ["lcl", "ucl"];
|
|
20
|
+
const SHEWHART_RULES_KEYS_3 = ["runLength"];
|
|
21
|
+
const CUSUM_KEYS = [
|
|
22
|
+
"sizes", "center", "stdDev", "headStart", "decisionInterval", "seShift", "newdata", "newsizes",
|
|
23
|
+
];
|
|
24
|
+
const EWMA_KEYS = ["sizes", "center", "stdDev", "lambda", "nsigmas", "newdata", "newsizes"];
|
|
25
|
+
const EWMA_SMOOTH_KEYS = ["lambda", "start"];
|
|
26
|
+
// d2 (expected relative range) and d3 (its standard error) for group sizes 1..25 and 1..50: the values of
|
|
27
|
+
// qcc.options("exp.R.unscaled") and qcc.options("se.R.unscaled") (the published table values; d2 stops at 25 in qcc)
|
|
28
|
+
const D2 = [NaN, 1.128, 1.693, 2.059, 2.326, 2.534, 2.704, 2.847, 2.97, 3.078, 3.173, 3.258, 3.336, 3.407, 3.472, 3.532,
|
|
29
|
+
3.588, 3.64, 3.689, 3.735, 3.778, 3.819, 3.858, 3.895, 3.931];
|
|
30
|
+
const D3 = [NaN, 0.8525033, 0.8883697, 0.8798108, 0.8640855, 0.8480442, 0.8332108, 0.8198378, 0.8078413, 0.7970584,
|
|
31
|
+
0.787323, 0.7784873, 0.7704257, 0.763033, 0.7562217, 0.7499188, 0.7440627, 0.7386021, 0.7334929, 0.728698, 0.7241851,
|
|
32
|
+
0.7199267, 0.7158987, 0.7120802, 0.7084528, 0.7050004, 0.7017086, 0.6985648, 0.6955576, 0.692677, 0.6899137,
|
|
33
|
+
0.6872596, 0.6847074, 0.6822502, 0.6798821, 0.6775973, 0.675391, 0.6732584, 0.6711952, 0.6691976, 0.6672619,
|
|
34
|
+
0.6653848, 0.6635632, 0.6617943, 0.6600754, 0.6584041, 0.656778, 0.655195, 0.6536532, 0.6521506];
|
|
35
|
+
/** d2(n), NaN outside the table (n above 25, as qcc) */
|
|
36
|
+
export function d2(n) {
|
|
37
|
+
return Number.isInteger(n) && n >= 1 && n < D2.length ? D2[n - 1] : NaN;
|
|
38
|
+
}
|
|
39
|
+
/** d3(n), NaN outside the table (n above 50) */
|
|
40
|
+
export function d3(n) {
|
|
41
|
+
return Number.isInteger(n) && n >= 1 && n < D3.length ? D3[n - 1] : NaN;
|
|
42
|
+
}
|
|
43
|
+
/** c4(n) = sqrt(2 / (n - 1)) Gamma(n / 2) / Gamma((n - 1) / 2) */
|
|
44
|
+
export function c4(n) {
|
|
45
|
+
return Math.sqrt(2 / (n - 1)) * Math.exp(lgamma(n / 2) - lgamma((n - 1) / 2));
|
|
46
|
+
}
|
|
47
|
+
const num = (v) => (v === null || v === undefined ? NaN : v);
|
|
48
|
+
const isMatrix = (d) => d.length > 0 && Array.isArray(d[0]);
|
|
49
|
+
/** The groups of a data set: rows of a matrix (missing values dropped) or one value per group */
|
|
50
|
+
function groupsOf(d) {
|
|
51
|
+
if (isMatrix(d))
|
|
52
|
+
return d.map((row) => row.map(num).filter((v) => !Number.isNaN(v)));
|
|
53
|
+
return d.map((v) => [num(v)]);
|
|
54
|
+
}
|
|
55
|
+
function sizesOf(given, count, what) {
|
|
56
|
+
if (given === undefined)
|
|
57
|
+
return undefined;
|
|
58
|
+
if (typeof given === "number")
|
|
59
|
+
return new Array(count).fill(given);
|
|
60
|
+
if (given.length !== count)
|
|
61
|
+
throw new RangeError(`${what} must have one value per group`);
|
|
62
|
+
return [...given];
|
|
63
|
+
}
|
|
64
|
+
const range = (g) => (g.length === 0 ? NaN : Math.max(...g) - Math.min(...g));
|
|
65
|
+
const sdGroup = (g) => (g.length > 1 ? sdOf(g) : NaN);
|
|
66
|
+
/** The charted statistic of each group */
|
|
67
|
+
function statisticsOf(type, groups, sizes) {
|
|
68
|
+
switch (type) {
|
|
69
|
+
case "xbar": return groups.map((g) => (g.length === 0 ? NaN : meanOf(g)));
|
|
70
|
+
case "R": return groups.map(range);
|
|
71
|
+
case "S": return groups.map(sdGroup);
|
|
72
|
+
case "p":
|
|
73
|
+
case "u": return groups.map((g, i) => g[0] / sizes[i]);
|
|
74
|
+
default: return groups.map((g) => g[0]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const sum = (xs) => xs.reduce((a, b) => a + b, 0);
|
|
78
|
+
const weighted = (x, w) => sum(x.map((v, i) => v * w[i])) / sum(w);
|
|
79
|
+
/** The grouped-data estimates of sigma (SAS/QC's definitions, named in qcc's help of sd.xbar) */
|
|
80
|
+
function groupedStdDev(method, groups) {
|
|
81
|
+
const n = groups.map((g) => g.length);
|
|
82
|
+
switch (method) {
|
|
83
|
+
case "UWAVE-R": return meanOf(groups.map((g, i) => range(g) / d2(n[i])));
|
|
84
|
+
case "MVLUE-R": {
|
|
85
|
+
const f = n.map((k) => (d2(k) / d3(k)) ** 2);
|
|
86
|
+
return weighted(groups.map((g, i) => range(g) / d2(n[i])), f);
|
|
87
|
+
}
|
|
88
|
+
case "UWAVE-SD": return meanOf(groups.map((g, i) => sdGroup(g) / c4(n[i])));
|
|
89
|
+
case "MVLUE-SD": {
|
|
90
|
+
const h = n.map((k) => c4(k) ** 2 / (1 - c4(k) ** 2));
|
|
91
|
+
return weighted(groups.map((g, i) => sdGroup(g) / c4(n[i])), h);
|
|
92
|
+
}
|
|
93
|
+
case "RMSDF": {
|
|
94
|
+
// the pooled standard deviation is formed from the group variances scaled by their largest standard deviation,
|
|
95
|
+
// so that it does not overflow or underflow where the data are in range
|
|
96
|
+
const s = groups.map(sdGroup), m = Math.max(...s.map(Math.abs));
|
|
97
|
+
const df = sum(n.map((k) => k - 1));
|
|
98
|
+
if (m === 0)
|
|
99
|
+
return 0;
|
|
100
|
+
return m * Math.sqrt(sum(s.map((v, i) => (n[i] - 1) * (v / m) ** 2)) / df) / c4(df + 1);
|
|
101
|
+
}
|
|
102
|
+
default: throw new RangeError(`unknown std.dev method "${method}"`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** xbar.one's estimates: moving ranges of k points / d2(k), or sd(x) / c4(n) */
|
|
106
|
+
function individualStdDev(method, x, k) {
|
|
107
|
+
const v = x.filter((u) => !Number.isNaN(u));
|
|
108
|
+
if (method === "SD")
|
|
109
|
+
return sdOf(v) / c4(v.length);
|
|
110
|
+
if (method !== "MR")
|
|
111
|
+
throw new RangeError("'arg' should be one of “MR”, “SD”");
|
|
112
|
+
const ranges = [];
|
|
113
|
+
for (let i = 0; i + k <= x.length; i++) {
|
|
114
|
+
const w = x.slice(i, i + k);
|
|
115
|
+
if (w.some(Number.isNaN))
|
|
116
|
+
continue;
|
|
117
|
+
ranges.push(range(w));
|
|
118
|
+
}
|
|
119
|
+
return meanOf(ranges) / d2(k);
|
|
120
|
+
}
|
|
121
|
+
const ALLOWED = {
|
|
122
|
+
xbar: ["UWAVE-R", "UWAVE-SD", "MVLUE-R", "MVLUE-SD", "RMSDF"],
|
|
123
|
+
R: ["UWAVE-R", "MVLUE-R"],
|
|
124
|
+
S: ["UWAVE-SD", "MVLUE-SD", "RMSDF"],
|
|
125
|
+
"xbar.one": ["MR", "SD"],
|
|
126
|
+
};
|
|
127
|
+
function argError(allowed) {
|
|
128
|
+
return new RangeError(`'arg' should be one of ${allowed.map((a) => `“${a}”`).join(", ")}`);
|
|
129
|
+
}
|
|
130
|
+
function centerAndSd(type, stats, groups, sizes, options) {
|
|
131
|
+
const given = options.stdDev;
|
|
132
|
+
const k = options.k ?? 2;
|
|
133
|
+
switch (type) {
|
|
134
|
+
case "xbar":
|
|
135
|
+
case "R":
|
|
136
|
+
case "S": {
|
|
137
|
+
const allowed = ALLOWED[type];
|
|
138
|
+
let method = null;
|
|
139
|
+
if (typeof given === "string") {
|
|
140
|
+
if (!allowed.includes(given))
|
|
141
|
+
throw argError(allowed);
|
|
142
|
+
method = given;
|
|
143
|
+
}
|
|
144
|
+
else if (given === undefined) {
|
|
145
|
+
// qcc: the xbar chart switches to RMSDF for groups larger than 25 (no d2 there), without a message
|
|
146
|
+
method = type === "S" ? "UWAVE-SD" : type === "xbar" && sizes.some((n) => n > 25) ? "RMSDF" : "UWAVE-R";
|
|
147
|
+
}
|
|
148
|
+
if (method !== null && sizes.some((n) => n < 2))
|
|
149
|
+
throw new RangeError("group sizes must be larger than one");
|
|
150
|
+
const sd = typeof given === "number" ? given : groupedStdDev(method, groups);
|
|
151
|
+
const center = options.center ?? weighted(stats, sizes);
|
|
152
|
+
return { center, sd, method };
|
|
153
|
+
}
|
|
154
|
+
case "xbar.one": {
|
|
155
|
+
let method = null;
|
|
156
|
+
if (typeof given === "string") {
|
|
157
|
+
if (!ALLOWED["xbar.one"].includes(given))
|
|
158
|
+
throw argError(ALLOWED["xbar.one"]);
|
|
159
|
+
method = given;
|
|
160
|
+
}
|
|
161
|
+
else if (given === undefined)
|
|
162
|
+
method = "MR";
|
|
163
|
+
const x = groups.map((g) => g[0]);
|
|
164
|
+
const sd = typeof given === "number" ? given : individualStdDev(method, x, k);
|
|
165
|
+
return { center: options.center ?? meanOf(x.filter((v) => !Number.isNaN(v))), sd, method };
|
|
166
|
+
}
|
|
167
|
+
case "p": {
|
|
168
|
+
const p = sum(groups.map((g) => g[0])) / sum(sizes);
|
|
169
|
+
return { center: options.center ?? p, sd: typeof given === "number" ? given : Math.sqrt(p * (1 - p)), method: null };
|
|
170
|
+
}
|
|
171
|
+
case "np": {
|
|
172
|
+
const p = sum(groups.map((g) => g[0])) / sum(sizes);
|
|
173
|
+
const equal = sizes.every((n) => n === sizes[0]);
|
|
174
|
+
const center = options.center ?? (equal ? sizes[0] * p : sizes.map((n) => n * p));
|
|
175
|
+
const sd = typeof given === "number" ? given
|
|
176
|
+
: equal ? Math.sqrt(sizes[0] * p * (1 - p)) : sizes.map((n) => Math.sqrt(n * p * (1 - p)));
|
|
177
|
+
return { center, sd, method: null };
|
|
178
|
+
}
|
|
179
|
+
case "c": {
|
|
180
|
+
const c = meanOf(stats);
|
|
181
|
+
return { center: options.center ?? c, sd: typeof given === "number" ? given : Math.sqrt(c), method: null };
|
|
182
|
+
}
|
|
183
|
+
case "u": {
|
|
184
|
+
const u = sum(groups.map((g) => g[0])) / sum(sizes);
|
|
185
|
+
return { center: options.center ?? u, sd: typeof given === "number" ? given : Math.sqrt(u), method: null };
|
|
186
|
+
}
|
|
187
|
+
case "g": {
|
|
188
|
+
const g = meanOf(stats);
|
|
189
|
+
// gbar below 1: R's sqrt of a negative number, NaN with its warning
|
|
190
|
+
if (typeof given !== "number" && g < 1)
|
|
191
|
+
warn("NaNs produced");
|
|
192
|
+
// sqrt(g (g - 1)) as g sqrt(1 - 1 / g): no overflow of g^2 for g near the largest double
|
|
193
|
+
const sd = g * Math.sqrt(1 - 1 / g);
|
|
194
|
+
return { center: options.center ?? g, sd: typeof given === "number" ? given : sd, method: null };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const at = (v, i) => (typeof v === "number" ? v : v[i]);
|
|
199
|
+
/** The limits at the sizes given (one pair per size) */
|
|
200
|
+
function limitsAt(type, center, sd, sizes, nsigmas, conf) {
|
|
201
|
+
const lo = conf === null ? NaN : (1 - conf) / 2, hi = conf === null ? NaN : (1 + conf) / 2;
|
|
202
|
+
const lcl = [], ucl = [];
|
|
203
|
+
const push = (l, u) => { lcl.push(l); ucl.push(u); };
|
|
204
|
+
sizes.forEach((n, i) => {
|
|
205
|
+
const c = at(center, i), s = at(sd, i);
|
|
206
|
+
switch (type) {
|
|
207
|
+
case "xbar": {
|
|
208
|
+
const z = conf === null ? nsigmas : qnorm(hi);
|
|
209
|
+
push(c - z * s / Math.sqrt(n), c + z * s / Math.sqrt(n));
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
case "xbar.one": {
|
|
213
|
+
const z = conf === null ? nsigmas : qnorm(hi);
|
|
214
|
+
push(c - z * s, c + z * s);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "R": {
|
|
218
|
+
if (conf === null) {
|
|
219
|
+
if (n > 50)
|
|
220
|
+
throw new RangeError("group size must be less than 51 when giving nsigmas");
|
|
221
|
+
push(Math.max(0, c - nsigmas * d3(n) * s), c + nsigmas * d3(n) * s);
|
|
222
|
+
}
|
|
223
|
+
else
|
|
224
|
+
push(qtukey(lo, n, Infinity) * s, qtukey(hi, n, Infinity) * s);
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "S": {
|
|
228
|
+
if (conf === null) {
|
|
229
|
+
const w = nsigmas * s * Math.sqrt(1 - c4(n) ** 2);
|
|
230
|
+
push(Math.max(0, c - w), c + w);
|
|
231
|
+
}
|
|
232
|
+
else
|
|
233
|
+
push(s * Math.sqrt(qchisq(lo, n - 1) / (n - 1)), s * Math.sqrt(qchisq(hi, n - 1) / (n - 1)));
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case "p": {
|
|
237
|
+
// the limits follow from the center (also a given center), not from the std.dev reported
|
|
238
|
+
if (conf === null) {
|
|
239
|
+
const w = nsigmas * Math.sqrt(c * (1 - c) / n);
|
|
240
|
+
push(Math.max(0, c - w), Math.min(1, c + w));
|
|
241
|
+
}
|
|
242
|
+
else
|
|
243
|
+
push(qbinom(lo, n, c) / n, qbinom(hi, n, c) / n);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
case "np": {
|
|
247
|
+
const p = c / n;
|
|
248
|
+
if (conf === null) {
|
|
249
|
+
const w = nsigmas * Math.sqrt(c * (1 - p));
|
|
250
|
+
push(Math.max(0, c - w), c + w);
|
|
251
|
+
}
|
|
252
|
+
else
|
|
253
|
+
push(qbinom(lo, n, p), qbinom(hi, n, p));
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case "c": {
|
|
257
|
+
if (conf === null)
|
|
258
|
+
push(Math.max(0, c - nsigmas * Math.sqrt(c)), c + nsigmas * Math.sqrt(c));
|
|
259
|
+
else
|
|
260
|
+
push(qpois(lo, c), qpois(hi, c));
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "u": {
|
|
264
|
+
if (conf === null) {
|
|
265
|
+
const w = nsigmas * Math.sqrt(c / n);
|
|
266
|
+
push(Math.max(0, c - w), c + w);
|
|
267
|
+
}
|
|
268
|
+
else
|
|
269
|
+
push(qpois(lo, c * n) / n, qpois(hi, c * n) / n);
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case "g": {
|
|
273
|
+
if (conf === null)
|
|
274
|
+
push(Math.max(0, c - nsigmas * s), c + nsigmas * s);
|
|
275
|
+
else
|
|
276
|
+
push(qgeom(lo, 1 / c), qgeom(hi, 1 / c));
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
return { lcl, ucl };
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* The points out of control of charted statistics: those beyond the limits and those in long runs on one side of the
|
|
285
|
+
* center. The limits are one pair for all points or one per point. An NA statistic is left out of both lists (qcc puts
|
|
286
|
+
* NA into them) and ends a run.
|
|
287
|
+
*
|
|
288
|
+
* Equivalent to `qcc::shewhart.rules()` in R.
|
|
289
|
+
* @category Descriptive statistics
|
|
290
|
+
*/
|
|
291
|
+
export function shewhartRules(statistics, center, limits, options = {}) {
|
|
292
|
+
checkOptions(limits, SHEWHART_RULES_KEYS_2, "shewhartRules"); // generated: option-keys.mjs
|
|
293
|
+
checkOptions(options, SHEWHART_RULES_KEYS_3, "shewhartRules"); // generated: option-keys.mjs
|
|
294
|
+
const runLength = options.runLength ?? 7;
|
|
295
|
+
const lim = (i) => limits.lcl.length === 1 ? [limits.lcl[0], limits.ucl[0]] : [limits.lcl[i], limits.ucl[i]];
|
|
296
|
+
const above = [], below = [], runs = [];
|
|
297
|
+
let side = 0, length = 0;
|
|
298
|
+
statistics.forEach((v, i) => {
|
|
299
|
+
const [l, u] = lim(i);
|
|
300
|
+
if (v > u)
|
|
301
|
+
above.push(i + 1);
|
|
302
|
+
if (v < l)
|
|
303
|
+
below.push(i + 1);
|
|
304
|
+
const c = typeof center === "number" ? center : center[i];
|
|
305
|
+
const s = Number.isNaN(v) ? 0 : Math.sign(v - c);
|
|
306
|
+
if (s !== 0 && s === side)
|
|
307
|
+
length++;
|
|
308
|
+
else {
|
|
309
|
+
side = s;
|
|
310
|
+
length = s === 0 ? 0 : 1;
|
|
311
|
+
}
|
|
312
|
+
if (length >= runLength)
|
|
313
|
+
runs.push(i + 1);
|
|
314
|
+
});
|
|
315
|
+
return { beyondLimits: [...above, ...below], violatingRuns: runs };
|
|
316
|
+
}
|
|
317
|
+
const GEOM_WARNING = "The Geometric distribution is quite skewed, it is better to set conf at the required confidence level (0 < conf < 1) instead of as a multiplier of sigma.";
|
|
318
|
+
/**
|
|
319
|
+
* A Shewhart control chart without the plot: the charted statistics, the center, the process standard deviation, the
|
|
320
|
+
* control limits and the points out of control. The charts:
|
|
321
|
+
*
|
|
322
|
+
* - xbar, R, S: data one row per group; sigma by UWAVE-R (default), MVLUE-R, UWAVE-SD, MVLUE-SD or RMSDF, the
|
|
323
|
+
* centers weighted by the group sizes; R limits `center +- k d3(n) sigma`, S limits `center +- k sigma
|
|
324
|
+
* sqrt(1 - c4(n)^2)`; probability limits from the range distribution (qtukey) and from chi-square.
|
|
325
|
+
* - xbar.one: individual values, sigma from moving ranges (MR) or the standard deviation (SD).
|
|
326
|
+
* - p, np, c, u, g: counts; limits from the binomial, Poisson and geometric models.
|
|
327
|
+
*
|
|
328
|
+
* As qcc: a group of size 1 is an error when sigma is estimated; groups larger than 25 switch the default of the xbar
|
|
329
|
+
* chart to RMSDF and give NA limits for the R chart (no d2 / d3 there); unequal sizes are an error for the c chart.
|
|
330
|
+
* Unlike qcc, NA statistics are left out of the violation indices.
|
|
331
|
+
*
|
|
332
|
+
* Equivalent to `qcc::qcc()` in R.
|
|
333
|
+
*
|
|
334
|
+
* @param data one value per group, or one row per group
|
|
335
|
+
* @param options the chart `type` and the options of qcc
|
|
336
|
+
* @category Descriptive statistics
|
|
337
|
+
*/
|
|
338
|
+
export function qcc(data, options) {
|
|
339
|
+
checkOptions(options, QCC_KEYS, "qcc"); // generated: option-keys.mjs
|
|
340
|
+
const sink = new Warnings();
|
|
341
|
+
const result = withWarnings(sink, () => fitQcc(data, options));
|
|
342
|
+
return reported("qcc", { ...result, warnings: sink.list });
|
|
343
|
+
}
|
|
344
|
+
function fitQcc(data, options) {
|
|
345
|
+
const type = options.type;
|
|
346
|
+
if (!["xbar", "R", "S", "xbar.one", "p", "np", "c", "u", "g"].includes(type))
|
|
347
|
+
throw new RangeError(`unknown chart type "${type}"`);
|
|
348
|
+
const grouped = type === "xbar" || type === "R" || type === "S";
|
|
349
|
+
if (grouped && !isMatrix(data))
|
|
350
|
+
throw new TypeError(`a "${type}" chart needs one row per group`);
|
|
351
|
+
if (!grouped && isMatrix(data))
|
|
352
|
+
throw new TypeError(`a "${type}" chart needs one value per group`);
|
|
353
|
+
const groups = groupsOf(data);
|
|
354
|
+
if (groups.length === 0)
|
|
355
|
+
throw new RangeError("no data");
|
|
356
|
+
let sizes;
|
|
357
|
+
if (grouped)
|
|
358
|
+
sizes = groups.map((g) => g.length);
|
|
359
|
+
else if (type === "p" || type === "np" || type === "u") {
|
|
360
|
+
const s = sizesOf(options.sizes, groups.length, "sizes");
|
|
361
|
+
if (s === undefined)
|
|
362
|
+
throw new TypeError(`sample 'sizes' must be given for a ${type} chart`);
|
|
363
|
+
sizes = s;
|
|
364
|
+
}
|
|
365
|
+
else
|
|
366
|
+
sizes = sizesOf(options.sizes, groups.length, "sizes") ?? new Array(groups.length).fill(1);
|
|
367
|
+
if (type === "c" && sizes.some((n) => n !== sizes[0]))
|
|
368
|
+
throw new RangeError("all sizes must be be equal for a c chart");
|
|
369
|
+
const statistics = statisticsOf(type, groups, sizes);
|
|
370
|
+
let newstatistics = [], newsizes = [], newGroups = [];
|
|
371
|
+
if (options.newdata !== undefined) {
|
|
372
|
+
if (grouped !== isMatrix(options.newdata))
|
|
373
|
+
throw new TypeError("newdata must have the form of data");
|
|
374
|
+
newGroups = groupsOf(options.newdata);
|
|
375
|
+
if (grouped)
|
|
376
|
+
newsizes = newGroups.map((g) => g.length);
|
|
377
|
+
else {
|
|
378
|
+
const ns = sizesOf(options.newsizes ?? (typeof options.sizes === "number" ? options.sizes : undefined), newGroups.length, "newsizes");
|
|
379
|
+
if (ns === undefined && (type === "p" || type === "np" || type === "u"))
|
|
380
|
+
throw new TypeError("'newsizes' must be given for newdata");
|
|
381
|
+
newsizes = ns ?? new Array(newGroups.length).fill(1);
|
|
382
|
+
}
|
|
383
|
+
newstatistics = statisticsOf(type, newGroups, newsizes);
|
|
384
|
+
}
|
|
385
|
+
const { center, sd, method } = centerAndSd(type, statistics, groups, sizes, options);
|
|
386
|
+
const conf = options.confidenceLevel ?? null;
|
|
387
|
+
if (conf !== null && !(conf > 0 && conf < 1))
|
|
388
|
+
throw new RangeError("confidenceLevel must be in (0, 1)");
|
|
389
|
+
const nsigmas = options.nsigmas ?? 3;
|
|
390
|
+
if (type === "g" && conf === null)
|
|
391
|
+
warn(GEOM_WARNING);
|
|
392
|
+
const allSizes = [...sizes, ...newsizes];
|
|
393
|
+
const equal = allSizes.every((n) => n === allSizes[0]);
|
|
394
|
+
let limits;
|
|
395
|
+
if (options.limits !== undefined)
|
|
396
|
+
limits = { lcl: [options.limits[0]], ucl: [options.limits[1]] };
|
|
397
|
+
else if (type === "np" && !equal) {
|
|
398
|
+
// the np chart with unequal sizes: center and sigma per group of data; newdata at the pooled proportion
|
|
399
|
+
const p = (typeof center === "number" ? center / sizes[0] : center[0] / sizes[0]);
|
|
400
|
+
const centers = allSizes.map((n) => n * p);
|
|
401
|
+
limits = limitsAt(type, centers, centers, allSizes, nsigmas, conf);
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
// qcc gives the p chart one pair of limits per group also when the sizes are equal (and fails for its probability
|
|
405
|
+
// limits then; rStats.js gives them)
|
|
406
|
+
const one = equal && type !== "p";
|
|
407
|
+
limits = limitsAt(type, center, sd, one ? [allSizes[0]] : allSizes, nsigmas, conf);
|
|
408
|
+
}
|
|
409
|
+
// R chart beyond the d3 table, or with sigma NA: NA limits (qcc's)
|
|
410
|
+
const runLength = options.runLength ?? 7;
|
|
411
|
+
const allStats = [...statistics, ...newstatistics];
|
|
412
|
+
const centerAll = typeof center === "number" ? center
|
|
413
|
+
: [...center, ...newsizes.map((n) => n * (center[0] / sizes[0]))];
|
|
414
|
+
const violations = shewhartRules(allStats, centerAll, limits, { runLength });
|
|
415
|
+
const labels = options.labels === undefined ? null
|
|
416
|
+
: [...options.labels, ...(options.newlabels ?? [])];
|
|
417
|
+
return {
|
|
418
|
+
type, statistics, sizes, center, stdDev: sd, stdDevMethod: method, limits,
|
|
419
|
+
nsigmas: conf === null ? nsigmas : null, confidenceLevel: conf, newstatistics, newsizes, labels, violations,
|
|
420
|
+
data: groups, runLength,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
/** The xbar (grouped) or xbar.one chart behind cusum and ewma */
|
|
424
|
+
function baseChart(data, o) {
|
|
425
|
+
const type = isMatrix(data) ? "xbar" : "xbar.one";
|
|
426
|
+
const q = fitQcc(data, {
|
|
427
|
+
type, ...(o.center === undefined ? {} : { center: o.center }), ...(o.stdDev === undefined ? {} : { stdDev: o.stdDev }),
|
|
428
|
+
...(o.newdata === undefined ? {} : { newdata: o.newdata }),
|
|
429
|
+
});
|
|
430
|
+
const sizes = type === "xbar" ? q.sizes : sizesOf(o.sizes, q.statistics.length, "sizes") ?? q.sizes;
|
|
431
|
+
const newsizes = type === "xbar" ? q.newsizes : sizesOf(o.newsizes ?? (typeof o.sizes === "number" ? o.sizes : undefined), q.newstatistics.length, "newsizes") ?? q.newsizes;
|
|
432
|
+
return { stats: [...q.statistics, ...q.newstatistics], sizes: [...sizes, ...newsizes], count: q.statistics.length,
|
|
433
|
+
center: q.center, sd: q.stdDev };
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* A cusum chart: the tabular cumulative sums of the standardized group means or individual values above and below the
|
|
437
|
+
* target. The reference value is `seShift / 2`; a sum beyond the decision interval signals.
|
|
438
|
+
*
|
|
439
|
+
* Equivalent to `qcc::cusum()` in R.
|
|
440
|
+
*
|
|
441
|
+
* @param data one row per group, or individual values
|
|
442
|
+
* @category Descriptive statistics
|
|
443
|
+
*/
|
|
444
|
+
export function cusum(data, options = {}) {
|
|
445
|
+
checkOptions(options, CUSUM_KEYS, "cusum"); // generated: option-keys.mjs
|
|
446
|
+
const h = options.decisionInterval ?? 5, shift = options.seShift ?? 1, h0 = options.headStart ?? 0;
|
|
447
|
+
if (!(h0 >= 0 && h0 < h))
|
|
448
|
+
throw new RangeError("head.start must be non-negative and less than decision.interval");
|
|
449
|
+
const sink = new Warnings();
|
|
450
|
+
const b = withWarnings(sink, () => baseChart(data, options));
|
|
451
|
+
const k = shift / 2;
|
|
452
|
+
const pos = [], neg = [];
|
|
453
|
+
let a = h0, c = h0;
|
|
454
|
+
b.stats.forEach((v, i) => {
|
|
455
|
+
const z = (v - b.center) / (b.sd / Math.sqrt(b.sizes[i]));
|
|
456
|
+
a = Math.max(0, a + z - k);
|
|
457
|
+
c = Math.max(0, c - z - k);
|
|
458
|
+
pos.push(a);
|
|
459
|
+
neg.push(-c);
|
|
460
|
+
});
|
|
461
|
+
const lower = [], upper = [];
|
|
462
|
+
pos.forEach((v, i) => { if (v > h)
|
|
463
|
+
upper.push(i + 1); });
|
|
464
|
+
neg.forEach((v, i) => { if (-v > h)
|
|
465
|
+
lower.push(i + 1); });
|
|
466
|
+
return reported("cusum", {
|
|
467
|
+
statistics: b.stats.slice(0, b.count), newstatistics: b.stats.slice(b.count), sizes: b.sizes, center: b.center,
|
|
468
|
+
stdDev: b.sd, pos, neg, headStart: h0,
|
|
469
|
+
decisionInterval: h, seShift: shift, violations: { lower, upper }, warnings: sink.list,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* An EWMA chart: the exponentially weighted moving average of the group means or individual values, started at the
|
|
474
|
+
* center. The limits are `center +- nsigmas sigma_i` with `sigma_i = sd / sqrt(n_i) sqrt(lambda / (2 - lambda)
|
|
475
|
+
* (1 - (1 - lambda)^(2 i)))`, qcc's form, with each point's own size also when the sizes differ.
|
|
476
|
+
*
|
|
477
|
+
* Equivalent to `qcc::ewma()` in R.
|
|
478
|
+
*
|
|
479
|
+
* @param data one row per group, or individual values
|
|
480
|
+
* @category Descriptive statistics
|
|
481
|
+
*/
|
|
482
|
+
export function ewma(data, options = {}) {
|
|
483
|
+
checkOptions(options, EWMA_KEYS, "ewma"); // generated: option-keys.mjs
|
|
484
|
+
const lambda = options.lambda ?? 0.2, nsigmas = options.nsigmas ?? 3;
|
|
485
|
+
if (!(lambda >= 0 && lambda <= 1))
|
|
486
|
+
throw new RangeError("lambda must be in [0, 1]");
|
|
487
|
+
const sink = new Warnings();
|
|
488
|
+
const b = withWarnings(sink, () => baseChart(data, options));
|
|
489
|
+
const y = [], sigma = [], lcl = [], ucl = [], violations = [];
|
|
490
|
+
let a = b.center;
|
|
491
|
+
b.stats.forEach((v, i) => {
|
|
492
|
+
a = lambda * v + (1 - lambda) * a;
|
|
493
|
+
y.push(a);
|
|
494
|
+
const s = b.sd / Math.sqrt(b.sizes[i]) * Math.sqrt(lambda / (2 - lambda) * (1 - (1 - lambda) ** (2 * (i + 1))));
|
|
495
|
+
sigma.push(s);
|
|
496
|
+
lcl.push(b.center - nsigmas * s);
|
|
497
|
+
ucl.push(b.center + nsigmas * s);
|
|
498
|
+
if (a < lcl[i] || a > ucl[i])
|
|
499
|
+
violations.push(i + 1);
|
|
500
|
+
});
|
|
501
|
+
return reported("ewma", {
|
|
502
|
+
statistics: b.stats.slice(0, b.count), newstatistics: b.stats.slice(b.count), sizes: b.sizes, center: b.center,
|
|
503
|
+
stdDev: b.sd, y, sigma, lambda, nsigmas,
|
|
504
|
+
limits: { lcl, ucl }, violations, warnings: sink.list,
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* EWMA smoothing of y in the order of x. It computes `z_t = lambda y_t + (1 - lambda) z_{t-1}` from `z_0 = start`.
|
|
509
|
+
*
|
|
510
|
+
* Equivalent to `qcc::ewmaSmooth()` in R.
|
|
511
|
+
* @category Descriptive statistics
|
|
512
|
+
*/
|
|
513
|
+
export function ewmaSmooth(x, y, options = {}) {
|
|
514
|
+
checkOptions(options, EWMA_SMOOTH_KEYS, "ewmaSmooth"); // generated: option-keys.mjs
|
|
515
|
+
if (x.length !== y.length)
|
|
516
|
+
throw new RangeError("x and y must have the same length");
|
|
517
|
+
const lambda = options.lambda ?? 0.2;
|
|
518
|
+
const order = x.map((_, i) => i).sort((i, j) => x[i] - x[j] || i - j);
|
|
519
|
+
const ys = order.map((i) => y[i]);
|
|
520
|
+
const start = options.start ?? ys[0];
|
|
521
|
+
let a = start;
|
|
522
|
+
const z = ys.map((v) => (a = lambda * v + (1 - lambda) * a));
|
|
523
|
+
return { x: order.map((i) => x[i]), y: z, lambda, start };
|
|
524
|
+
}
|