@numero-uno-jpn/rstats 1.7.0 → 1.9.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 +38 -0
- package/README.ja.md +8 -7
- package/README.md +7 -6
- package/dist/chunks/{chunk-CB7STL5Z.js → chunk-2COJEI44.js} +2 -2
- package/dist/chunks/{chunk-V2Y2W7LE.js → chunk-2SO7Z3NV.js} +2 -2
- package/dist/chunks/{chunk-MOJ2KIQE.js → chunk-3J2HFC4A.js} +2 -2
- package/dist/chunks/chunk-3LDUKHJZ.js +3 -0
- package/dist/chunks/{chunk-VALULDEX.js → chunk-4IHHZ3F4.js} +1 -1
- package/dist/chunks/chunk-4QWW62QN.js +2 -0
- package/dist/chunks/chunk-575F6RXJ.js +2 -0
- package/dist/chunks/{chunk-CUT3IHMY.js → chunk-5C7XHQKP.js} +1 -1
- package/dist/chunks/{chunk-J7ZVV4DN.js → chunk-7F7PY35D.js} +2 -2
- package/dist/chunks/{chunk-PLQQ4VFN.js → chunk-7VPO2LTR.js} +2 -2
- package/dist/chunks/{chunk-THSV22QF.js → chunk-ANGKH5D4.js} +2 -2
- package/dist/chunks/{chunk-TY3IFCLI.js → chunk-BX47HGID.js} +2 -2
- package/dist/chunks/chunk-CWS62KPG.js +2 -0
- package/dist/chunks/chunk-D3CWLSLS.js +2 -0
- package/dist/chunks/{chunk-HSJ4OT3H.js → chunk-DEAU6HWA.js} +1 -1
- package/dist/chunks/chunk-DRSA7ITF.js +2 -0
- package/dist/chunks/{chunk-56AMJQ27.js → chunk-G3XMR7PA.js} +2 -2
- package/dist/chunks/chunk-I7E5J5PG.js +3 -0
- package/dist/chunks/{chunk-YKNRDPIB.js → chunk-IEATFACS.js} +2 -2
- package/dist/chunks/{chunk-HORF36FD.js → chunk-IXK3JA2R.js} +2 -2
- package/dist/chunks/{chunk-CZUOG2U5.js → chunk-J3TS4PGN.js} +2 -2
- package/dist/chunks/{chunk-BJMPSDJ7.js → chunk-MNIICUNF.js} +2 -2
- package/dist/chunks/chunk-MT7XELUM.js +2 -0
- package/dist/chunks/{chunk-76FLTZVF.js → chunk-OGHJJKUR.js} +2 -2
- package/dist/chunks/{chunk-QWDK3ZC2.js → chunk-RNK6EHEK.js} +2 -2
- package/dist/chunks/{chunk-RIP2RKAW.js → chunk-T3NUXZRL.js} +2 -2
- package/dist/chunks/chunk-TKMNASCD.js +2 -0
- package/dist/chunks/chunk-UA66IB37.js +2 -0
- package/dist/chunks/chunk-UUZVQS4O.js +2 -0
- package/dist/chunks/chunk-VO2NIKVB.js +2 -0
- package/dist/chunks/chunk-WFE52E6F.js +2 -0
- package/dist/chunks/{chunk-SDT3YHAG.js → chunk-WOZEQS2I.js} +2 -2
- package/dist/chunks/{chunk-FQRNZEH2.js → chunk-XCKQQVN7.js} +2 -2
- package/dist/chunks/chunk-XNLZFKL5.js +2 -0
- package/dist/chunks/chunk-YFZBGUIT.js +2 -0
- package/dist/chunks/{chunk-YVJ4LDDE.js → chunk-YK7Q6CNX.js} +2 -2
- package/dist/chunks/{chunk-B7XZMIQO.js → chunk-Z47NJMYU.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/multivariate.d.ts +3 -0
- package/dist/entries/multivariate.js +4 -0
- package/dist/entries/regression.d.ts +4 -0
- package/dist/entries/regression.js +6 -0
- package/dist/entries/tests.d.ts +3 -1
- package/dist/entries/tests.js +3 -1
- package/dist/htest/grubbs.d.ts +68 -0
- package/dist/htest/grubbs.js +151 -0
- package/dist/htest/multipleComparisons.d.ts +21 -0
- package/dist/htest/multipleComparisons.js +47 -0
- package/dist/htest/rankTests.d.ts +35 -0
- package/dist/htest/rankTests.js +89 -1
- package/dist/internal/summary.js +8 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/gee.d.ts +280 -0
- package/dist/models/gee.js +704 -0
- package/dist/models/glmnet.d.ts +259 -0
- package/dist/models/glmnet.js +1112 -0
- package/dist/models/modelMatrix.d.ts +2 -0
- package/dist/models/rq.d.ts +203 -0
- package/dist/models/rq.js +1089 -0
- package/dist/multivariate/cancor.d.ts +55 -0
- package/dist/multivariate/cancor.js +167 -0
- package/dist/multivariate/lda.d.ts +146 -0
- package/dist/multivariate/lda.js +325 -0
- package/dist/multivariate/manova.d.ts +106 -0
- package/dist/multivariate/manova.js +303 -0
- package/dist/multivariate.js +2 -0
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -3
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/llms.txt +5 -1
- package/package.json +6 -1
- package/dist/chunks/chunk-AIEPW6DX.js +0 -2
- package/dist/chunks/chunk-B6KJ4RWG.js +0 -2
- package/dist/chunks/chunk-JXEXOD7K.js +0 -2
- package/dist/chunks/chunk-KSH43GX4.js +0 -2
- package/dist/chunks/chunk-LMRCFKVK.js +0 -2
- package/dist/chunks/chunk-PCTRVBEJ.js +0 -3
- package/dist/chunks/chunk-R5BMFVIH.js +0 -2
- package/dist/chunks/chunk-R7F7ELS7.js +0 -3
- package/dist/chunks/chunk-RX6MZO2Y.js +0 -2
- package/dist/chunks/chunk-VQOU7U7R.js +0 -2
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type VectorOrMatrix } from "../linalg/matrix.js";
|
|
2
|
+
/** Options for canonical correlation.
|
|
3
|
+
* @category Multivariate analysis
|
|
4
|
+
*/
|
|
5
|
+
export interface CancorOptions {
|
|
6
|
+
/** Center the columns of x by their means, leave them uncentered, or supply explicit centers. Default true. */
|
|
7
|
+
xcenter?: boolean | readonly number[];
|
|
8
|
+
/** Center the columns of y by their means, leave them uncentered, or supply explicit centers. Default true. */
|
|
9
|
+
ycenter?: boolean | readonly number[];
|
|
10
|
+
/** Numerical tolerance used to determine the ranks of x and y. Default 1e-7. */
|
|
11
|
+
tol?: number;
|
|
12
|
+
/** Names for the columns of x. */
|
|
13
|
+
xNames?: readonly string[];
|
|
14
|
+
/** Names for the columns of y. */
|
|
15
|
+
yNames?: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
/** Result of canonical correlation.
|
|
18
|
+
* @category Multivariate analysis
|
|
19
|
+
*/
|
|
20
|
+
export interface CancorResult {
|
|
21
|
+
/** Canonical correlations, ordered from largest to smallest. */
|
|
22
|
+
cor: number[];
|
|
23
|
+
/** Canonical coefficients for x, with original variable names in rows. */
|
|
24
|
+
xcoef: {
|
|
25
|
+
/** Names of the x variables (`null` without names), as R's row names. */
|
|
26
|
+
rows: string[] | null;
|
|
27
|
+
/** Always `null`: R gives the columns (the canonical variates) no names. */
|
|
28
|
+
cols: null;
|
|
29
|
+
/** The coefficients: one row per x variable, one column per canonical variate. */
|
|
30
|
+
values: number[][];
|
|
31
|
+
};
|
|
32
|
+
/** Canonical coefficients for y, with original variable names in rows. */
|
|
33
|
+
ycoef: {
|
|
34
|
+
/** Names of the y variables (`null` without names), as R's row names. */
|
|
35
|
+
rows: string[] | null;
|
|
36
|
+
/** Always `null`: R gives the columns (the canonical variates) no names. */
|
|
37
|
+
cols: null;
|
|
38
|
+
/** The coefficients: one row per y variable, one column per canonical variate. */
|
|
39
|
+
values: number[][];
|
|
40
|
+
};
|
|
41
|
+
/** Centers applied to the columns of x. */
|
|
42
|
+
xcenter: number[];
|
|
43
|
+
/** Centers applied to the columns of y. */
|
|
44
|
+
ycenter: number[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Canonical correlation analysis. Equivalent to R's `stats::cancor()`. Coefficients use the unscaled cross-products, with deterministic paired signs.
|
|
48
|
+
*
|
|
49
|
+
* @param x First data matrix or vector.
|
|
50
|
+
* @param y Second data matrix or vector.
|
|
51
|
+
* @param options Column centering, rank tolerance, and optional column names.
|
|
52
|
+
*
|
|
53
|
+
* @category Multivariate analysis
|
|
54
|
+
*/
|
|
55
|
+
export declare function cancor(x: VectorOrMatrix, y: VectorOrMatrix, options?: CancorOptions): CancorResult;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Canonical correlation from the cross-product matrices and their symmetric whitening decompositions.
|
|
2
|
+
import { eigen } from "../linalg/eigen.js";
|
|
3
|
+
import { svd } from "../linalg/svd.js";
|
|
4
|
+
import { qr } from "../linalg/qr.js";
|
|
5
|
+
import { fromMat, toMatOrColumn } from "../linalg/matrix.js";
|
|
6
|
+
import { ldexp, mean } from "../internal/summary.js";
|
|
7
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
8
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
9
|
+
const CANCOR_KEYS = ["xcenter", "ycenter", "tol", "xNames", "yNames"];
|
|
10
|
+
function cross(a, b = a) {
|
|
11
|
+
const out = Array.from({ length: a[0]?.length ?? 0 }, () => new Array(b[0]?.length ?? 0).fill(0));
|
|
12
|
+
for (let i = 0; i < a.length; i++)
|
|
13
|
+
for (let j = 0; j < a[i].length; j++)
|
|
14
|
+
for (let k = 0; k < (b[0]?.length ?? 0); k++)
|
|
15
|
+
out[j][k] += a[i][j] * b[i][k];
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
function mul(a, b) {
|
|
19
|
+
const out = Array.from({ length: a.length }, () => new Array(b[0]?.length ?? 0).fill(0));
|
|
20
|
+
for (let i = 0; i < a.length; i++)
|
|
21
|
+
for (let k = 0; k < (a[0]?.length ?? 0); k++)
|
|
22
|
+
for (let j = 0; j < (b[0]?.length ?? 0); j++)
|
|
23
|
+
out[i][j] += a[i][k] * b[k][j];
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function inverseSqrt(a, tol) {
|
|
27
|
+
const e = eigen(a, { symmetric: true });
|
|
28
|
+
const max = Math.max(0, ...e.values);
|
|
29
|
+
const keep = e.values.map((v, i) => v > max * tol ? i : -1).filter((i) => i >= 0);
|
|
30
|
+
if (keep.length !== a.length)
|
|
31
|
+
throw new RangeError("rank reduction failed in canonical correlation");
|
|
32
|
+
const q = e.vectors;
|
|
33
|
+
const out = Array.from({ length: a.length }, () => new Array(a.length).fill(0));
|
|
34
|
+
for (const k of keep)
|
|
35
|
+
for (let i = 0; i < a.length; i++)
|
|
36
|
+
for (let j = 0; j < a.length; j++)
|
|
37
|
+
out[i][j] += q[i][k] * q[j][k] / Math.sqrt(e.values[k]);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function centerArg(arg, a, name) {
|
|
41
|
+
if (arg === false)
|
|
42
|
+
return new Array(a[0].length).fill(0);
|
|
43
|
+
if (Array.isArray(arg)) {
|
|
44
|
+
if (arg.length !== a[0].length || !arg.every(Number.isFinite))
|
|
45
|
+
throw new RangeError(`${name} must be a finite vector with one value per column`);
|
|
46
|
+
return Array.from(arg);
|
|
47
|
+
}
|
|
48
|
+
return a[0].map((_, j) => mean(a.map((r) => r[j])));
|
|
49
|
+
}
|
|
50
|
+
function namesOrDefault(names, p, prefix) {
|
|
51
|
+
if (names !== undefined && names.length !== p)
|
|
52
|
+
throw new RangeError(`${prefix}Names must match the number of columns`);
|
|
53
|
+
return names === undefined ? Array.from({ length: p }, (_, i) => `${prefix}${i + 1}`) : Array.from(names);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Canonical correlation analysis. Equivalent to R's `stats::cancor()`. Coefficients use the unscaled cross-products, with deterministic paired signs.
|
|
57
|
+
*
|
|
58
|
+
* @param x First data matrix or vector.
|
|
59
|
+
* @param y Second data matrix or vector.
|
|
60
|
+
* @param options Column centering, rank tolerance, and optional column names.
|
|
61
|
+
*
|
|
62
|
+
* @category Multivariate analysis
|
|
63
|
+
*/
|
|
64
|
+
export function cancor(x, y, options = {}) {
|
|
65
|
+
checkOptions(options, CANCOR_KEYS, "cancor"); // generated: option-keys.mjs
|
|
66
|
+
const xm = fromMat(toMatOrColumn(x)), ym = fromMat(toMatOrColumn(y));
|
|
67
|
+
if (xm.length !== ym.length)
|
|
68
|
+
throw new RangeError("x and y must have the same number of rows");
|
|
69
|
+
if (xm.length < 2 || xm[0].length === 0 || ym[0].length === 0)
|
|
70
|
+
throw new RangeError("at least two rows and one column are required");
|
|
71
|
+
if (xm.some((r) => r.some((v) => !Number.isFinite(v))) || ym.some((r) => r.some((v) => !Number.isFinite(v)))) {
|
|
72
|
+
throw new RangeError("NA/NaN/Inf in foreign function call (arg 1)");
|
|
73
|
+
}
|
|
74
|
+
const tol = options.tol ?? 1e-7;
|
|
75
|
+
if (!(Number.isFinite(tol) && tol >= 0))
|
|
76
|
+
throw new RangeError("'tol' must be a non-negative finite number");
|
|
77
|
+
const xc = centerArg(options.xcenter, xm, "xcenter"), yc = centerArg(options.ycenter, ym, "ycenter");
|
|
78
|
+
const exponents = (a, center) => center.map((c, j) => {
|
|
79
|
+
let m = Math.abs(c);
|
|
80
|
+
for (const row of a)
|
|
81
|
+
m = Math.max(m, Math.abs(row[j]));
|
|
82
|
+
return m === 0 ? 0 : Math.floor(Math.log2(m));
|
|
83
|
+
});
|
|
84
|
+
const xExponent = exponents(xm, xc), yExponent = exponents(ym, yc);
|
|
85
|
+
const x0 = xm.map((r) => r.map((v, j) => ldexp(v, -xExponent[j]) - ldexp(xc[j], -xExponent[j])));
|
|
86
|
+
const y0 = ym.map((r) => r.map((v, j) => ldexp(v, -yExponent[j]) - ldexp(yc[j], -yExponent[j])));
|
|
87
|
+
const sx = x0[0].map((_, j) => { let m = 0; for (const r of x0)
|
|
88
|
+
m = Math.max(m, Math.abs(r[j])); return m || 1; });
|
|
89
|
+
const sy = y0[0].map((_, j) => { let m = 0; for (const r of y0)
|
|
90
|
+
m = Math.max(m, Math.abs(r[j])); return m || 1; });
|
|
91
|
+
const xs = x0.map((r) => r.map((v, j) => v / sx[j]));
|
|
92
|
+
const ys = y0.map((r) => r.map((v, j) => v / sy[j]));
|
|
93
|
+
const qx = qr(xs, { tol }), qy = qr(ys, { tol });
|
|
94
|
+
if (qx.rank === 0 || qy.rank === 0)
|
|
95
|
+
throw new RangeError("at least one non-constant column is required in each matrix");
|
|
96
|
+
const px = qx.pivot.slice(0, qx.rank).sort((a, b) => a - b), py = qy.pivot.slice(0, qy.rank).sort((a, b) => a - b);
|
|
97
|
+
const xr = xs.map((r) => px.map((j) => r[j])), yr = ys.map((r) => py.map((j) => r[j]));
|
|
98
|
+
const sxx = cross(xr), syy = cross(yr), sxy = cross(xr, yr);
|
|
99
|
+
const ix = inverseSqrt(sxx, tol), iy = inverseSqrt(syy, tol);
|
|
100
|
+
const k = mul(mul(ix, sxy), iy);
|
|
101
|
+
const requested = Math.min(qx.rank, qy.rank);
|
|
102
|
+
const dec = svd(k, { nu: requested, nv: requested });
|
|
103
|
+
const ncan = Math.min(requested, dec.d.length);
|
|
104
|
+
const u = dec.u, v = dec.v;
|
|
105
|
+
// Convert coefficients from scaled columns back to original units.
|
|
106
|
+
const axScaled = mul(ix, u).map((r, i) => r.map((z) => z / sx[px[i]]));
|
|
107
|
+
const byScaled = mul(iy, v).map((r, i) => r.map((z) => z / sy[py[i]]));
|
|
108
|
+
const ax = axScaled.map((r, i) => r.map((z) => ldexp(z, -xExponent[px[i]])));
|
|
109
|
+
const by = byScaled.map((r, i) => r.map((z) => ldexp(z, -yExponent[py[i]])));
|
|
110
|
+
// Orient every X basis column by its largest absolute original-scale coefficient.
|
|
111
|
+
for (let j = 0; j < ax[0].length; j++) {
|
|
112
|
+
let imax = 0;
|
|
113
|
+
for (let i = 1; i < ax.length; i++)
|
|
114
|
+
if (Math.abs(ax[i][j]) > Math.abs(ax[imax][j]))
|
|
115
|
+
imax = i;
|
|
116
|
+
if (ax[imax][j] < 0) {
|
|
117
|
+
for (const r of ax)
|
|
118
|
+
r[j] = -r[j];
|
|
119
|
+
for (const r of axScaled)
|
|
120
|
+
r[j] = -r[j];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Paired Y columns take the sign that makes canonical correlation positive; extra columns use a deterministic sign.
|
|
124
|
+
for (let j = 0; j < by[0].length; j++) {
|
|
125
|
+
if (j < ncan) {
|
|
126
|
+
let cov = 0;
|
|
127
|
+
for (let i = 0; i < xr.length; i++) {
|
|
128
|
+
let xv = 0, yv = 0;
|
|
129
|
+
for (let a = 0; a < axScaled.length; a++)
|
|
130
|
+
xv += xr[i][a] * axScaled[a][j] * sx[px[a]];
|
|
131
|
+
for (let b = 0; b < byScaled.length; b++)
|
|
132
|
+
yv += yr[i][b] * byScaled[b][j] * sy[py[b]];
|
|
133
|
+
cov += xv * yv;
|
|
134
|
+
}
|
|
135
|
+
if (cov < 0) {
|
|
136
|
+
for (const r of by)
|
|
137
|
+
r[j] = -r[j];
|
|
138
|
+
for (const r of byScaled)
|
|
139
|
+
r[j] = -r[j];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
let imax = 0;
|
|
144
|
+
for (let i = 1; i < by.length; i++)
|
|
145
|
+
if (Math.abs(by[i][j]) > Math.abs(by[imax][j]))
|
|
146
|
+
imax = i;
|
|
147
|
+
if (by[imax][j] < 0) {
|
|
148
|
+
for (const r of by)
|
|
149
|
+
r[j] = -r[j];
|
|
150
|
+
for (const r of byScaled)
|
|
151
|
+
r[j] = -r[j];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const xNames = namesOrDefault(options.xNames, xm[0].length, "x");
|
|
156
|
+
const yNames = namesOrDefault(options.yNames, ym[0].length, "y");
|
|
157
|
+
const xRows = options.xNames === undefined ? null : px.map((i) => xNames[i]);
|
|
158
|
+
const yRows = options.yNames === undefined ? null : py.map((i) => yNames[i]);
|
|
159
|
+
const cor = dec.d.slice(0, ncan);
|
|
160
|
+
return {
|
|
161
|
+
cor,
|
|
162
|
+
xcoef: { rows: xRows, cols: null, values: ax },
|
|
163
|
+
ycoef: { rows: yRows, cols: null, values: by },
|
|
164
|
+
xcenter: xc,
|
|
165
|
+
ycenter: yc,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { type VectorOrMatrix } from "../linalg/matrix.js";
|
|
2
|
+
import { type GroupLabel } from "../htest/groups.js";
|
|
3
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
4
|
+
/** Options for LDA fitting.
|
|
5
|
+
* @category Multivariate analysis
|
|
6
|
+
*/
|
|
7
|
+
export interface LdaOptions {
|
|
8
|
+
/** Fitting method; only "moment" is supported. Default "moment". */
|
|
9
|
+
method?: "moment" | string;
|
|
10
|
+
/** Prior class probabilities, in class-level order. Defaults to observed class proportions. */
|
|
11
|
+
prior?: readonly number[];
|
|
12
|
+
/** Numerical tolerance for rank determination and within-class covariance calculations. Default 1e-4. */
|
|
13
|
+
tol?: number;
|
|
14
|
+
/** Return leave-one-out cross-validation predictions instead of a fitted model. Default false. */
|
|
15
|
+
CV?: boolean;
|
|
16
|
+
/** Names for predictor columns in matrix input. */
|
|
17
|
+
xNames?: readonly string[];
|
|
18
|
+
/** Declared class levels; unused levels remain in `lev` and produce warnings. */
|
|
19
|
+
groupLevels?: readonly GroupLabel[];
|
|
20
|
+
/** Explicit factor levels for formula variables. */
|
|
21
|
+
levels?: Record<string, readonly string[]>;
|
|
22
|
+
}
|
|
23
|
+
/** Options for LDA prediction.
|
|
24
|
+
* @category Multivariate analysis
|
|
25
|
+
*/
|
|
26
|
+
export interface LdaPredictionOptions {
|
|
27
|
+
/** Prediction method; only "plug-in" is supported. Default "plug-in". */
|
|
28
|
+
method?: "plug-in" | string;
|
|
29
|
+
/** New predictor rows; omitted to predict the observations used to fit the model. */
|
|
30
|
+
newdata?: VectorOrMatrix;
|
|
31
|
+
/** Prior class probabilities for this prediction. Defaults to the fitted priors. */
|
|
32
|
+
prior?: readonly number[];
|
|
33
|
+
/** Number of discriminant score dimensions to return. Defaults to all available dimensions. */
|
|
34
|
+
dimen?: number;
|
|
35
|
+
}
|
|
36
|
+
/** A labelled LDA result matrix.
|
|
37
|
+
* @category Multivariate analysis
|
|
38
|
+
*/
|
|
39
|
+
export interface LdaTable {
|
|
40
|
+
/** Row labels, or null when rows are observations without labels. */
|
|
41
|
+
rows: string[] | null;
|
|
42
|
+
/** Column labels, or null when columns are not named. */
|
|
43
|
+
cols: string[] | null;
|
|
44
|
+
/** Numeric matrix entries in row-major order. */
|
|
45
|
+
values: number[][];
|
|
46
|
+
}
|
|
47
|
+
/** A fitted linear discriminant model.
|
|
48
|
+
* @category Multivariate analysis
|
|
49
|
+
*/
|
|
50
|
+
export interface LdaFit {
|
|
51
|
+
/** Prior class probabilities in the order of `lev`. */
|
|
52
|
+
prior: number[];
|
|
53
|
+
/** Number of training observations in each class. */
|
|
54
|
+
counts: number[];
|
|
55
|
+
/** Class means, with classes in rows and predictors in columns. */
|
|
56
|
+
means: LdaTable;
|
|
57
|
+
/** Metric-normalised discriminant directions, with predictors in rows and dimensions in columns. */
|
|
58
|
+
scaling: LdaTable;
|
|
59
|
+
/** Singular values associated with the discriminant dimensions. */
|
|
60
|
+
svd: number[];
|
|
61
|
+
/** Number of observations used to fit the model. */
|
|
62
|
+
N: number;
|
|
63
|
+
/** Class labels in prediction and prior order. */
|
|
64
|
+
lev: string[];
|
|
65
|
+
/** Warnings produced while fitting the model. */
|
|
66
|
+
warnings: string[];
|
|
67
|
+
/** @internal Unscaled class means retained for prediction. */
|
|
68
|
+
_means: number[][];
|
|
69
|
+
/** @internal Class labels with non-empty training observations. */
|
|
70
|
+
_activeLev: string[];
|
|
71
|
+
/** @internal Prior-weighted center of the predictor columns. */
|
|
72
|
+
_center: number[];
|
|
73
|
+
/** @internal Internal discriminant directions in scaled predictor coordinates. */
|
|
74
|
+
_scaling: number[][];
|
|
75
|
+
/** @internal Inverse pooled within-class covariance matrix. */
|
|
76
|
+
_covInverse: number[][];
|
|
77
|
+
/** @internal Predictor scale factors used internally. */
|
|
78
|
+
_scale: number[];
|
|
79
|
+
/** @internal Column origins in coordinates divided by `_scale`. */
|
|
80
|
+
_origin: number[];
|
|
81
|
+
/** @internal Column spread factors after removing a dominant offset. */
|
|
82
|
+
_spread: number[];
|
|
83
|
+
/** @internal Scaled training predictor matrix. */
|
|
84
|
+
_x: number[][];
|
|
85
|
+
/** @internal Predictor column names. */
|
|
86
|
+
_xNames: string[];
|
|
87
|
+
/** @internal Rank tolerance used to fit the model. */
|
|
88
|
+
_tol: number;
|
|
89
|
+
}
|
|
90
|
+
/** Plug-in prediction from an LDA fit.
|
|
91
|
+
* @category Multivariate analysis
|
|
92
|
+
*/
|
|
93
|
+
export interface LdaPrediction {
|
|
94
|
+
/** Predicted class label for each observation. */
|
|
95
|
+
class: string[];
|
|
96
|
+
/** Posterior class probabilities, with classes in columns. */
|
|
97
|
+
posterior: LdaTable;
|
|
98
|
+
/** Discriminant scores for each observation. */
|
|
99
|
+
x: LdaTable;
|
|
100
|
+
}
|
|
101
|
+
/** Leave-one-out LDA predictions.
|
|
102
|
+
* @category Multivariate analysis
|
|
103
|
+
*/
|
|
104
|
+
export interface LdaCVResult {
|
|
105
|
+
/** Leave-one-out predicted class label for each training observation. */
|
|
106
|
+
class: string[];
|
|
107
|
+
/** Leave-one-out posterior class probabilities, with classes in columns. */
|
|
108
|
+
posterior: LdaTable;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Linear discriminant analysis (moment method). Equivalent to R's `MASS::lda()`. The moment method only. Supports matrix/group and formula/data-frame inputs.
|
|
112
|
+
* Directions are metric-normalised and their signs are deterministic.
|
|
113
|
+
*
|
|
114
|
+
* @param x Predictor matrix.
|
|
115
|
+
* @param grouping Class labels.
|
|
116
|
+
* @param options Prior probabilities, tolerance, and leave-one-out CV.
|
|
117
|
+
* @category Multivariate analysis
|
|
118
|
+
*/
|
|
119
|
+
export declare function lda(x: VectorOrMatrix, grouping: readonly GroupLabel[], options?: LdaOptions & {
|
|
120
|
+
CV?: false;
|
|
121
|
+
}): LdaFit;
|
|
122
|
+
/** Leave-one-out cross-validation result overload. */
|
|
123
|
+
export declare function lda(x: VectorOrMatrix, grouping: readonly GroupLabel[], options: LdaOptions & {
|
|
124
|
+
CV: true;
|
|
125
|
+
}): LdaCVResult;
|
|
126
|
+
/**
|
|
127
|
+
* Linear discriminant analysis from a formula, e.g. `"class ~ x1 + x2"` or `"class ~ ."`.
|
|
128
|
+
*
|
|
129
|
+
* @param formula Formula.
|
|
130
|
+
* @param data Data frame.
|
|
131
|
+
* @param options Prior probabilities, tolerance, and leave-one-out CV.
|
|
132
|
+
*/
|
|
133
|
+
export declare function lda(formula: string, data: DataFrame, options?: LdaOptions & {
|
|
134
|
+
CV?: false;
|
|
135
|
+
}): LdaFit;
|
|
136
|
+
export declare function lda(formula: string, data: DataFrame, options: LdaOptions & {
|
|
137
|
+
CV: true;
|
|
138
|
+
}): LdaCVResult;
|
|
139
|
+
/**
|
|
140
|
+
* Plug-in prediction for a fitted {@link lda} model. Equivalent to R's `predict.lda()`. The plug-in method only.
|
|
141
|
+
*
|
|
142
|
+
* @param fit A fitted LDA model.
|
|
143
|
+
* @param options New observations, prior override, and number of discriminant scores.
|
|
144
|
+
* @category Multivariate analysis
|
|
145
|
+
*/
|
|
146
|
+
export declare function ldaPredict(fit: LdaFit, options?: LdaPredictionOptions): LdaPrediction;
|