@numero-uno-jpn/rstats 1.5.0 → 1.7.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 +34 -0
- package/README.ja.md +17 -10
- package/README.md +18 -10
- package/dist/chunks/chunk-56AMJQ27.js +2 -0
- package/dist/chunks/{chunk-TP4PRPVQ.js → chunk-76FLTZVF.js} +2 -2
- package/dist/chunks/chunk-AIEPW6DX.js +2 -0
- package/dist/chunks/chunk-B6KJ4RWG.js +2 -0
- package/dist/chunks/{chunk-73QCE2CI.js → chunk-B7XZMIQO.js} +2 -2
- package/dist/chunks/chunk-BJMPSDJ7.js +2 -0
- package/dist/chunks/chunk-CB7STL5Z.js +2 -0
- package/dist/chunks/{chunk-MYRCEQ2R.js → chunk-CUT3IHMY.js} +1 -1
- package/dist/chunks/{chunk-MJV5G7FG.js → chunk-CZUOG2U5.js} +2 -2
- package/dist/chunks/{chunk-LVB22NKV.js → chunk-FQRNZEH2.js} +2 -2
- package/dist/chunks/{chunk-2IYY74LX.js → chunk-HORF36FD.js} +2 -2
- package/dist/chunks/{chunk-GDMNF4P3.js → chunk-HSJ4OT3H.js} +1 -1
- package/dist/chunks/{chunk-E5M5RAVB.js → chunk-J7ZVV4DN.js} +2 -2
- package/dist/chunks/chunk-JXEXOD7K.js +2 -0
- package/dist/chunks/chunk-KSH43GX4.js +2 -0
- package/dist/chunks/chunk-LMRCFKVK.js +2 -0
- package/dist/chunks/{chunk-CX4XK7WC.js → chunk-MOJ2KIQE.js} +2 -2
- package/dist/chunks/chunk-PCTRVBEJ.js +3 -0
- package/dist/chunks/{chunk-F2MURX2Y.js → chunk-PLQQ4VFN.js} +2 -2
- package/dist/chunks/{chunk-L7PDJNJ3.js → chunk-QWDK3ZC2.js} +2 -2
- package/dist/chunks/{chunk-F7SQG6BV.js → chunk-R5BMFVIH.js} +2 -2
- package/dist/chunks/chunk-R7F7ELS7.js +3 -0
- package/dist/chunks/{chunk-2MYUAEYE.js → chunk-RIP2RKAW.js} +2 -2
- package/dist/chunks/chunk-RX6MZO2Y.js +2 -0
- package/dist/chunks/{chunk-VAWD57ZB.js → chunk-SDT3YHAG.js} +2 -2
- package/dist/chunks/{chunk-FCF6FBJP.js → chunk-THSV22QF.js} +2 -2
- package/dist/chunks/{chunk-AIXJAVDS.js → chunk-TY3IFCLI.js} +2 -2
- package/dist/chunks/{chunk-BJOZZ5J6.js → chunk-V2Y2W7LE.js} +2 -2
- package/dist/chunks/{chunk-UJM7AJII.js → chunk-VALULDEX.js} +1 -1
- package/dist/chunks/chunk-VQOU7U7R.js +2 -0
- package/dist/chunks/{chunk-DYCWYHYV.js → chunk-YKNRDPIB.js} +1 -1
- package/dist/chunks/chunk-YVJ4LDDE.js +2 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +2 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/regression.d.ts +3 -0
- package/dist/entries/regression.js +6 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +5 -5
- package/dist/models/mixedConfint.d.ts +42 -0
- package/dist/models/mixedConfint.js +573 -0
- package/dist/models/mixedSimulate.d.ts +36 -0
- package/dist/models/mixedSimulate.js +232 -0
- package/dist/models/nls.d.ts +286 -0
- package/dist/models/nls.js +1007 -0
- package/dist/models/nlsFormula.d.ts +37 -0
- package/dist/models/nlsFormula.js +295 -0
- package/dist/models/polr.d.ts +341 -0
- package/dist/models/polr.js +1028 -0
- package/dist/models/rlm.d.ts +282 -0
- package/dist/models/rlm.js +766 -0
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -0
- package/dist/rstats.js +1 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +14 -14
- package/llms.txt +8 -2
- package/package.json +6 -1
- package/dist/chunks/chunk-3BEE6BHN.js +0 -2
- package/dist/chunks/chunk-FMLFYM7N.js +0 -2
- package/dist/chunks/chunk-JBJTXMRJ.js +0 -2
- package/dist/chunks/chunk-MXOGMN4P.js +0 -2
- package/dist/chunks/chunk-Q32WODL4.js +0 -2
- package/dist/chunks/chunk-RZBX6VYO.js +0 -3
- package/dist/chunks/chunk-WBEYJ5W5.js +0 -3
- package/dist/chunks/chunk-YK2EJU6T.js +0 -2
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// Simulations from fitted mixed models. The random stream is shared with the public R-compatible generators.
|
|
2
|
+
import { setSeed } from "../random/rng.js";
|
|
3
|
+
import { rnorm1 } from "../random/continuous.js";
|
|
4
|
+
import { rbinom1 } from "../random/binomial.js";
|
|
5
|
+
import { rpois1 } from "../random/poisson.js";
|
|
6
|
+
import { rgamma1 } from "../random/gamma.js";
|
|
7
|
+
import { logicalOption, lmerInternals } from "./lmer.js";
|
|
8
|
+
import { glmerInternals, isGlmerFit } from "./glmer.js";
|
|
9
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
10
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
11
|
+
const SIMULATE_LMER_KEYS = ["nsim", "seed", "useU", "reForm"];
|
|
12
|
+
const SIMULATE_GLMER_KEYS = ["nsim", "seed", "useU", "reForm"];
|
|
13
|
+
function simulationCount(value) {
|
|
14
|
+
const n = value ?? 1;
|
|
15
|
+
if (typeof n !== "number" || !Number.isFinite(n))
|
|
16
|
+
throw new TypeError("nsim must be a finite number");
|
|
17
|
+
const k = Math.trunc(n);
|
|
18
|
+
if (k < 1)
|
|
19
|
+
throw new RangeError("(nsim <- as.integer(nsim[1])) > 0 is not TRUE");
|
|
20
|
+
return k;
|
|
21
|
+
}
|
|
22
|
+
function selectedTerms(terms, groups, reForm) {
|
|
23
|
+
if (reForm === undefined || reForm === "NA" || reForm === "~0" || reForm === "~0+0") {
|
|
24
|
+
return { columns: new Map(), completeTerms: new Set() };
|
|
25
|
+
}
|
|
26
|
+
if (reForm === null) {
|
|
27
|
+
return {
|
|
28
|
+
columns: new Map(terms.map((term, i) => [i, new Set(term.columns.map((_, j) => j))])),
|
|
29
|
+
completeTerms: new Set(terms.map((_, i) => i)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (typeof reForm !== "string")
|
|
33
|
+
throw new TypeError("reForm must be a formula, null, or \"NA\"");
|
|
34
|
+
const compact = reForm.replace(/\s+/g, "");
|
|
35
|
+
if (!compact.startsWith("~"))
|
|
36
|
+
throw new SyntaxError("reForm must be a one-sided formula");
|
|
37
|
+
const selected = new Map();
|
|
38
|
+
const completeTerms = new Set();
|
|
39
|
+
const pattern = /\(([^|]+)\|([^()]+)\)/g;
|
|
40
|
+
for (const match of compact.matchAll(pattern)) {
|
|
41
|
+
const lhs = match[1];
|
|
42
|
+
const group = match[2];
|
|
43
|
+
const intercept = !/(^|\+)0(\+|$)/.test(lhs) && !lhs.includes("-1");
|
|
44
|
+
for (let i = 0; i < terms.length; i++) {
|
|
45
|
+
const term = terms[i];
|
|
46
|
+
if (groups[term.group].name.replace(/\s+/g, "") !== group)
|
|
47
|
+
continue;
|
|
48
|
+
const matchedColumns = new Set();
|
|
49
|
+
term.columns.forEach((name, j) => {
|
|
50
|
+
if ((name === "(Intercept)" && intercept) || (name !== "(Intercept)" && lhs.includes(name)))
|
|
51
|
+
matchedColumns.add(j);
|
|
52
|
+
});
|
|
53
|
+
if (matchedColumns.size > 0) {
|
|
54
|
+
const cols = selected.get(i) ?? new Set();
|
|
55
|
+
for (const column of matchedColumns)
|
|
56
|
+
cols.add(column);
|
|
57
|
+
selected.set(i, cols);
|
|
58
|
+
// A union of several partial terms is not a whole-term match.
|
|
59
|
+
if (matchedColumns.size === term.columns.length)
|
|
60
|
+
completeTerms.add(i);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (selected.size === 0)
|
|
65
|
+
throw new RangeError("reForm does not select a random-effects term in this fit");
|
|
66
|
+
return { columns: selected, completeTerms };
|
|
67
|
+
}
|
|
68
|
+
/** Fill new random effects in Z-column order; terms and levels follow the fitted model structure. */
|
|
69
|
+
function newEffects(terms, theta, groups, selected, completeTerms, conditional, scale) {
|
|
70
|
+
const b = [...conditional];
|
|
71
|
+
for (let ti = 0; ti < terms.length; ti++) {
|
|
72
|
+
const term = terms[ti];
|
|
73
|
+
const nc = term.columns.length;
|
|
74
|
+
const conditioned = selected.get(ti) ?? new Set();
|
|
75
|
+
const levels = groups[term.group].levels.length;
|
|
76
|
+
// A complete term selected by re.form is fitted, not regenerated.
|
|
77
|
+
if (completeTerms.has(ti)) {
|
|
78
|
+
for (let level = 0; level < levels; level++) {
|
|
79
|
+
for (let i = 0; i < nc; i++)
|
|
80
|
+
b[term.zOffset + level * nc + i] = conditional[term.zOffset + level * nc + i];
|
|
81
|
+
}
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
// A partial match does not select the whole fitted term: generate its
|
|
85
|
+
// complete new block, then add the fitted contribution for selected columns.
|
|
86
|
+
// This is an additive overlay, not a conditional Gaussian draw.
|
|
87
|
+
const lower = Array.from({ length: nc }, () => new Array(nc).fill(0));
|
|
88
|
+
let k = term.thetaOffset;
|
|
89
|
+
for (let j = 0; j < nc; j++)
|
|
90
|
+
for (let i = j; i < nc; i++)
|
|
91
|
+
lower[i][j] = theta[k++] * scale;
|
|
92
|
+
for (let level = 0; level < levels; level++) {
|
|
93
|
+
const z = Array.from({ length: nc }, () => rnorm1(0, 1));
|
|
94
|
+
for (let i = 0; i < nc; i++) {
|
|
95
|
+
let value = 0;
|
|
96
|
+
for (let j = 0; j <= i; j++)
|
|
97
|
+
value += lower[i][j] * z[j];
|
|
98
|
+
const index = term.zOffset + level * nc + i;
|
|
99
|
+
b[index] = value + (conditioned.has(i) ? conditional[index] : 0);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return b;
|
|
104
|
+
}
|
|
105
|
+
function allTermsSelected(terms, completeTerms) {
|
|
106
|
+
return terms.every((_, i) => completeTerms.has(i));
|
|
107
|
+
}
|
|
108
|
+
/** Add the random-effects contribution Zb to a vector of fixed-effect predictors. */
|
|
109
|
+
function addRandomEffects(base, z, b) {
|
|
110
|
+
const out = [...base];
|
|
111
|
+
for (let j = 0; j < z.length; j++) {
|
|
112
|
+
const column = z[j];
|
|
113
|
+
const value = b[j];
|
|
114
|
+
for (let k = 0; k < column.rows.length; k++)
|
|
115
|
+
out[column.rows[k]] += column.values[k] * value;
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Simulations from a linear mixed-model fit. Equivalent to R's `lme4::simulate.merMod()`.
|
|
121
|
+
*
|
|
122
|
+
* @category Mixed models
|
|
123
|
+
*/
|
|
124
|
+
export function simulateLmer(fit, options = {}) {
|
|
125
|
+
checkOptions(options, SIMULATE_LMER_KEYS, "simulateLmer"); // generated: option-keys.mjs
|
|
126
|
+
if (typeof fit !== "object" || fit === null)
|
|
127
|
+
throw new TypeError("fit must be a result returned by lmer");
|
|
128
|
+
if (options.useU !== undefined && options.reForm !== undefined) {
|
|
129
|
+
throw new Error("should specify only one of ‘use.u’ and ‘re.form’");
|
|
130
|
+
}
|
|
131
|
+
const n = simulationCount(options.nsim);
|
|
132
|
+
if (options.seed === "NA")
|
|
133
|
+
throw new Error("supplied seed is not a valid integer");
|
|
134
|
+
if (options.seed !== undefined && options.seed !== null && typeof options.seed !== "number")
|
|
135
|
+
throw new TypeError("seed must be a number or null");
|
|
136
|
+
const useU = options.useU === undefined ? undefined : logicalOption(options.useU, false);
|
|
137
|
+
const internals = lmerInternals(fit);
|
|
138
|
+
const s = internals.structure;
|
|
139
|
+
const selection = useU === true
|
|
140
|
+
? { columns: new Map(s.terms.map((term, i) => [i, new Set(term.columns.map((_, j) => j))])), completeTerms: new Set(s.terms.map((_, i) => i)) }
|
|
141
|
+
: useU === false
|
|
142
|
+
? { columns: new Map(), completeTerms: new Set() }
|
|
143
|
+
: selectedTerms(s.terms, s.groups, options.reForm);
|
|
144
|
+
const selected = selection.columns;
|
|
145
|
+
if (options.seed !== undefined && options.seed !== null)
|
|
146
|
+
setSeed(options.seed);
|
|
147
|
+
const conditionedAll = allTermsSelected(s.terms, selection.completeTerms);
|
|
148
|
+
const fittedRandom = conditionedAll ? [] : addRandomEffects(new Array(fit.nObs).fill(0), s.z, internals.b);
|
|
149
|
+
const fixed = conditionedAll ? fit.fitted : fit.fitted.map((v, i) => v - fittedRandom[i]);
|
|
150
|
+
const sims = [];
|
|
151
|
+
sims.names = [];
|
|
152
|
+
// lme4 draws all new random effects for all replicates before drawing any response vectors.
|
|
153
|
+
const effects = [];
|
|
154
|
+
for (let j = 0; j < n; j++)
|
|
155
|
+
effects.push(conditionedAll ? [] : newEffects(s.terms, internals.theta, s.groups, selected, selection.completeTerms, internals.b, fit.sigma));
|
|
156
|
+
for (let j = 0; j < n; j++) {
|
|
157
|
+
const eta = conditionedAll ? fixed : addRandomEffects(fixed, s.z, effects[j]);
|
|
158
|
+
sims.push(eta.map((m) => rnorm1(m, fit.sigma)));
|
|
159
|
+
}
|
|
160
|
+
sims.names = Array.from({ length: n }, (_, j) => `sim_${j + 1}`);
|
|
161
|
+
return sims;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Simulations from a generalized linear mixed-model fit. Equivalent to R's `lme4::simulate.merMod()`.
|
|
165
|
+
*
|
|
166
|
+
* @category Mixed models
|
|
167
|
+
*/
|
|
168
|
+
export function simulateGlmer(fit, options = {}) {
|
|
169
|
+
checkOptions(options, SIMULATE_GLMER_KEYS, "simulateGlmer"); // generated: option-keys.mjs
|
|
170
|
+
if (!isGlmerFit(fit))
|
|
171
|
+
throw new TypeError("fit must be a result returned by glmer");
|
|
172
|
+
if (options.useU !== undefined && options.reForm !== undefined) {
|
|
173
|
+
throw new Error("should specify only one of ‘use.u’ and ‘re.form’");
|
|
174
|
+
}
|
|
175
|
+
const n = simulationCount(options.nsim);
|
|
176
|
+
if (options.seed === "NA")
|
|
177
|
+
throw new Error("supplied seed is not a valid integer");
|
|
178
|
+
if (options.seed !== undefined && options.seed !== null && typeof options.seed !== "number")
|
|
179
|
+
throw new TypeError("seed must be a number or null");
|
|
180
|
+
const useU = options.useU === undefined ? undefined : logicalOption(options.useU, false);
|
|
181
|
+
const internals = glmerInternals(fit);
|
|
182
|
+
const s = internals.structure;
|
|
183
|
+
const selection = useU === true
|
|
184
|
+
? { columns: new Map(s.terms.map((term, i) => [i, new Set(term.columns.map((_, j) => j))])), completeTerms: new Set(s.terms.map((_, i) => i)) }
|
|
185
|
+
: useU === false
|
|
186
|
+
? { columns: new Map(), completeTerms: new Set() }
|
|
187
|
+
: selectedTerms(s.terms, s.groups, options.reForm);
|
|
188
|
+
const selected = selection.columns;
|
|
189
|
+
if (options.seed !== undefined && options.seed !== null)
|
|
190
|
+
setSeed(options.seed);
|
|
191
|
+
const conditionedAll = allTermsSelected(s.terms, selection.completeTerms);
|
|
192
|
+
const fittedRandom = conditionedAll ? [] : addRandomEffects(new Array(fit.nObs).fill(0), s.z, internals.b);
|
|
193
|
+
const fixedEta = conditionedAll ? internals.eta : internals.eta.map((v, i) => v - fittedRandom[i]);
|
|
194
|
+
const effects = [];
|
|
195
|
+
for (let j = 0; j < n; j++)
|
|
196
|
+
effects.push(conditionedAll ? [] : newEffects(s.terms, internals.theta, s.groups, selected, selection.completeTerms, internals.b, 1));
|
|
197
|
+
// R draws all random effects before discovering that inverse-Gaussian response simulation needs statmod.
|
|
198
|
+
if (s.family.family === "inverse.gaussian")
|
|
199
|
+
throw new Error("simulateGlmer: inverse.gaussian is not supported (R requires statmod)");
|
|
200
|
+
const sims = [];
|
|
201
|
+
sims.names = [];
|
|
202
|
+
for (let j = 0; j < n; j++) {
|
|
203
|
+
const eta = conditionedAll ? fixedEta : addRandomEffects(fixedEta, s.z, effects[j]);
|
|
204
|
+
const mu = eta.map((v) => s.family.linkinv(v));
|
|
205
|
+
if (s.family.family === "binomial") {
|
|
206
|
+
if (s.response?.length === 2) {
|
|
207
|
+
// A two-column cbind(successes, failures) response is returned as a pair of counts.
|
|
208
|
+
const size = s.size;
|
|
209
|
+
sims.push(mu.map((p, i) => {
|
|
210
|
+
const success = rbinom1(size[i], p);
|
|
211
|
+
return [success, size[i] - success];
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
// A vector response (0/1 or proportions with prior trial weights) is returned as a proportion.
|
|
216
|
+
sims.push(mu.map((p, i) => rbinom1(s.weights[i], p) / s.weights[i]));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else if (s.family.family === "poisson") {
|
|
220
|
+
sims.push(mu.map((m) => rpois1(m)));
|
|
221
|
+
}
|
|
222
|
+
else if (s.family.family === "Gamma") {
|
|
223
|
+
const shape = 1 / (fit.sigma * fit.sigma);
|
|
224
|
+
sims.push(mu.map((m) => rgamma1(shape, m / shape)));
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
sims.push(mu.map((m) => rnorm1(m, fit.sigma)));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
sims.names = Array.from({ length: n }, (_, j) => `sim_${j + 1}`);
|
|
231
|
+
return sims;
|
|
232
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import type { DataFrame } from "./modelFrame.js";
|
|
2
|
+
import type { Sample } from "../htest/types.js";
|
|
3
|
+
import { type NlsExpr } from "./nlsFormula.js";
|
|
4
|
+
/** Algorithm used to fit a nonlinear least-squares model.
|
|
5
|
+
*
|
|
6
|
+
* @category Regression
|
|
7
|
+
*/
|
|
8
|
+
export type NlsAlgorithm = "default" | "plinear" | "port";
|
|
9
|
+
/** Convergence controls for nonlinear least squares.
|
|
10
|
+
*
|
|
11
|
+
* @category Regression
|
|
12
|
+
*/
|
|
13
|
+
export interface NlsControl {
|
|
14
|
+
/** Maximum number of iterations. */
|
|
15
|
+
maxiter?: number;
|
|
16
|
+
/** Relative convergence tolerance. */
|
|
17
|
+
tol?: number;
|
|
18
|
+
/** Minimum step-length factor used by the line search. */
|
|
19
|
+
minFactor?: number;
|
|
20
|
+
/** Whether non-convergence is reported as a warning instead of an error. */
|
|
21
|
+
warnOnly?: boolean;
|
|
22
|
+
/** Positive offset used in the convergence scaling criterion. */
|
|
23
|
+
scaleOffset?: number;
|
|
24
|
+
}
|
|
25
|
+
/** Options for {@link nls}.
|
|
26
|
+
*
|
|
27
|
+
* @category Regression
|
|
28
|
+
*/
|
|
29
|
+
export interface NlsOptions {
|
|
30
|
+
/** Named initial parameter values. */
|
|
31
|
+
start?: Record<string, number>;
|
|
32
|
+
/** Nonlinear least-squares algorithm. */
|
|
33
|
+
algorithm?: NlsAlgorithm;
|
|
34
|
+
/** Non-negative observation weights, or the name of a data column. */
|
|
35
|
+
weights?: Sample | string;
|
|
36
|
+
/** Lower parameter bounds for the bounded algorithm. */
|
|
37
|
+
lower?: number | readonly number[] | Record<string, number>;
|
|
38
|
+
/** Upper parameter bounds for the bounded algorithm. */
|
|
39
|
+
upper?: number | readonly number[] | Record<string, number>;
|
|
40
|
+
/** Convergence controls. */
|
|
41
|
+
control?: NlsControl;
|
|
42
|
+
}
|
|
43
|
+
/** Internal and public fitted-model quantities.
|
|
44
|
+
*
|
|
45
|
+
* @category Regression
|
|
46
|
+
*/
|
|
47
|
+
export interface NlsModel {
|
|
48
|
+
/** Original model formula. */
|
|
49
|
+
formula: string;
|
|
50
|
+
/** Response expression label. */
|
|
51
|
+
response: string;
|
|
52
|
+
/** Parameter names in coefficient order. */
|
|
53
|
+
parameterNames: string[];
|
|
54
|
+
/** Named parameter estimates. */
|
|
55
|
+
coefficients: Record<string, number>;
|
|
56
|
+
/** Fitted response values. */
|
|
57
|
+
fitted: number[];
|
|
58
|
+
/** Observed minus fitted response values. */
|
|
59
|
+
residuals: number[];
|
|
60
|
+
/** Response values used in fitting. */
|
|
61
|
+
y: number[];
|
|
62
|
+
/** Observation weights used in fitting. */
|
|
63
|
+
weights: number[];
|
|
64
|
+
/** Original row indices retained after missing-value handling. */
|
|
65
|
+
rows: number[];
|
|
66
|
+
/** Numeric data columns retained for prediction and profiling. */
|
|
67
|
+
data: Record<string, number[]>;
|
|
68
|
+
/** Parsed expression tree used internally. @internal */
|
|
69
|
+
expression: NlsExpr;
|
|
70
|
+
/** Weighted residual sum of squares. */
|
|
71
|
+
deviance: number;
|
|
72
|
+
/** Residual standard error estimate. */
|
|
73
|
+
sigma: number;
|
|
74
|
+
/** Residual degrees of freedom. */
|
|
75
|
+
dfResidual: number;
|
|
76
|
+
/** Number of observations used in fitting. */
|
|
77
|
+
nobs: number;
|
|
78
|
+
/** Estimated parameter covariance matrix. */
|
|
79
|
+
covariance: number[][];
|
|
80
|
+
/** Estimated parameter correlation matrix. */
|
|
81
|
+
correlation: number[][];
|
|
82
|
+
/** Whether the convergence criterion was met. */
|
|
83
|
+
converged: boolean;
|
|
84
|
+
/** Number of iterations performed. */
|
|
85
|
+
iterations: number;
|
|
86
|
+
/** Algorithm used for fitting. */
|
|
87
|
+
algorithm: NlsAlgorithm;
|
|
88
|
+
/** Warnings produced during fitting. */
|
|
89
|
+
warnings: string[];
|
|
90
|
+
/** Whether the residual sum of squares is exactly zero. */
|
|
91
|
+
exactFit: boolean;
|
|
92
|
+
}
|
|
93
|
+
/** Result returned by {@link nls}.
|
|
94
|
+
*
|
|
95
|
+
* @category Regression
|
|
96
|
+
*/
|
|
97
|
+
export interface NlsResult extends NlsModel {
|
|
98
|
+
}
|
|
99
|
+
/** Result returned by {@link nlsSummary}.
|
|
100
|
+
*
|
|
101
|
+
* @category Regression
|
|
102
|
+
*/
|
|
103
|
+
export interface NlsSummaryResult {
|
|
104
|
+
/** Coefficient table with estimates, standard errors and test statistics. */
|
|
105
|
+
coefficients: {
|
|
106
|
+
/** Coefficient names in row order. */
|
|
107
|
+
rows: string[];
|
|
108
|
+
/** Summary statistic column names. */
|
|
109
|
+
cols: string[];
|
|
110
|
+
/** Rows of coefficient estimates, standard errors and test statistics. */
|
|
111
|
+
values: number[][];
|
|
112
|
+
};
|
|
113
|
+
/** Residual standard error estimate. */
|
|
114
|
+
sigma: number;
|
|
115
|
+
/** Residual and model degrees of freedom. */
|
|
116
|
+
df: [number, number];
|
|
117
|
+
/** Weighted residual sum of squares. */
|
|
118
|
+
deviance: number;
|
|
119
|
+
/** Number of observations used in fitting. */
|
|
120
|
+
nobs: number;
|
|
121
|
+
/** Warnings produced while computing the summary. */
|
|
122
|
+
warnings: string[];
|
|
123
|
+
}
|
|
124
|
+
/** Options for {@link nlsPredict}.
|
|
125
|
+
*
|
|
126
|
+
* @category Regression
|
|
127
|
+
*/
|
|
128
|
+
export interface NlsPredictOptions {
|
|
129
|
+
/** New data columns at which to evaluate the fitted expression. */
|
|
130
|
+
newdata: DataFrame;
|
|
131
|
+
}
|
|
132
|
+
/** Options for {@link nlsConfint}.
|
|
133
|
+
*
|
|
134
|
+
* @category Regression
|
|
135
|
+
*/
|
|
136
|
+
export interface NlsConfintOptions {
|
|
137
|
+
/** Confidence-interval method; profile by default. */
|
|
138
|
+
method?: "profile" | "wald";
|
|
139
|
+
/** Confidence level between zero and one. */
|
|
140
|
+
level?: number;
|
|
141
|
+
/** Parameter names for which to compute intervals; omitted means all estimable parameters. */
|
|
142
|
+
parm?: readonly string[];
|
|
143
|
+
}
|
|
144
|
+
/** Result returned by {@link nlsConfint}.
|
|
145
|
+
*
|
|
146
|
+
* @category Regression
|
|
147
|
+
*/
|
|
148
|
+
export interface NlsConfintResult {
|
|
149
|
+
/** Parameter names for the intervals. */
|
|
150
|
+
rows: string[];
|
|
151
|
+
/** Lower and upper confidence-limit labels. */
|
|
152
|
+
cols: [string, string];
|
|
153
|
+
/** Lower and upper confidence limits, one pair per parameter. */
|
|
154
|
+
values: [number, number][];
|
|
155
|
+
/** Warnings produced while computing the intervals. */
|
|
156
|
+
warnings: string[];
|
|
157
|
+
}
|
|
158
|
+
/** Result returned by {@link nlsAnova}.
|
|
159
|
+
*
|
|
160
|
+
* @category Regression
|
|
161
|
+
*/
|
|
162
|
+
export interface NlsAnovaResult {
|
|
163
|
+
/** Sequential analysis-of-variance table. */
|
|
164
|
+
table: {
|
|
165
|
+
/** Formula for each fitted model. */
|
|
166
|
+
Model: string[];
|
|
167
|
+
/** Residual degrees of freedom for each model. */
|
|
168
|
+
"Res.Df": number[];
|
|
169
|
+
/** Residual sum of squares for each model. */
|
|
170
|
+
"Res.Sum Sq": number[];
|
|
171
|
+
/** Difference in residual degrees of freedom for each comparison. */
|
|
172
|
+
"Df": (number | "NA")[];
|
|
173
|
+
/** Difference in residual sum of squares for each comparison. */
|
|
174
|
+
"Sum Sq": (number | "NA")[];
|
|
175
|
+
/** F statistic for each comparison. */
|
|
176
|
+
"F value": (number | "NA")[];
|
|
177
|
+
/** Upper-tail F-test probability for each comparison. */
|
|
178
|
+
"Pr(>F)": (number | "NA")[];
|
|
179
|
+
};
|
|
180
|
+
/** Warnings produced while comparing models. */
|
|
181
|
+
warnings: string[];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Nonlinear least-squares regression. Equivalent to R's `nls()`.
|
|
185
|
+
*
|
|
186
|
+
* Fits a user-specified nonlinear mean function by Gauss-Newton, variable projection, or bound-constrained
|
|
187
|
+
* least squares. Formula expressions are parsed safely and differentiated symbolically.
|
|
188
|
+
*
|
|
189
|
+
* @param formula Response and nonlinear mean expression.
|
|
190
|
+
* @param data Numeric data columns.
|
|
191
|
+
* @param options Starting values, algorithm, weights, and convergence controls.
|
|
192
|
+
* @returns The fitted coefficients, residuals, covariance, and convergence diagnostics.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const data = { conc: [0.02, 0.06, 0.11, 0.22, 0.56, 1.1], rate: [76, 97, 123, 159, 191, 207] };
|
|
197
|
+
* const fit = nls("rate ~ Vm * conc / (K + conc)", data, { start: { Vm: 200, K: 0.05 } });
|
|
198
|
+
* fit.coefficients;
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @category Regression
|
|
202
|
+
*/
|
|
203
|
+
export declare function nls(formula: string, data: DataFrame, options?: NlsOptions): NlsResult;
|
|
204
|
+
/**
|
|
205
|
+
* Summary of a nonlinear least-squares fit. Equivalent to R's `summary.nls()`.
|
|
206
|
+
*
|
|
207
|
+
* @param fit A fitted nonlinear model.
|
|
208
|
+
* @returns The coefficient table, residual scale, and residual degrees of freedom.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* const data = { conc: [0.02, 0.06, 0.11, 0.22, 0.56, 1.1], rate: [76, 97, 123, 159, 191, 207] };
|
|
213
|
+
* const fit = nls("rate ~ Vm * conc / (K + conc)", data, { start: { Vm: 200, K: 0.05 } });
|
|
214
|
+
* nlsSummary(fit);
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* @category Regression
|
|
218
|
+
*/
|
|
219
|
+
export declare function nlsSummary(fit: NlsResult): NlsSummaryResult;
|
|
220
|
+
/**
|
|
221
|
+
* Covariance matrix of a nonlinear least-squares fit. Equivalent to R's `vcov.nls()`.
|
|
222
|
+
*
|
|
223
|
+
* @param fit A fitted nonlinear model.
|
|
224
|
+
* @returns The covariance matrix in coefficient order.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* const data = { conc: [0.02, 0.06, 0.11, 0.22, 0.56, 1.1], rate: [76, 97, 123, 159, 191, 207] };
|
|
229
|
+
* const fit = nls("rate ~ Vm * conc / (K + conc)", data, { start: { Vm: 200, K: 0.05 } });
|
|
230
|
+
* nlsVcov(fit);
|
|
231
|
+
* ```
|
|
232
|
+
*
|
|
233
|
+
* @category Regression
|
|
234
|
+
*/
|
|
235
|
+
export declare function nlsVcov(fit: NlsResult): number[][];
|
|
236
|
+
/**
|
|
237
|
+
* Predictions from a nonlinear least-squares fit. Equivalent to R's `predict.nls()`.
|
|
238
|
+
*
|
|
239
|
+
* @param fit A fitted nonlinear model.
|
|
240
|
+
* @param options New predictor data.
|
|
241
|
+
* @returns One fitted mean per row of new data.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const data = { conc: [0.02, 0.06, 0.11, 0.22, 0.56, 1.1], rate: [76, 97, 123, 159, 191, 207] };
|
|
246
|
+
* const fit = nls("rate ~ Vm * conc / (K + conc)", data, { start: { Vm: 200, K: 0.05 } });
|
|
247
|
+
* nlsPredict(fit, { newdata: { conc: [0.1, 0.5] } });
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* @category Regression
|
|
251
|
+
*/
|
|
252
|
+
export declare function nlsPredict(fit: NlsResult, options: NlsPredictOptions): number[];
|
|
253
|
+
/**
|
|
254
|
+
* Wald or profile confidence intervals for nonlinear parameters. Equivalent to R's `confint.nls()`.
|
|
255
|
+
*
|
|
256
|
+
* @param fit A fitted nonlinear model.
|
|
257
|
+
* @param options Interval method, confidence level, and parameter names.
|
|
258
|
+
* @returns A two-column confidence interval matrix.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* const data = { conc: [0.02, 0.06, 0.11, 0.22, 0.56, 1.1], rate: [76, 97, 123, 159, 191, 207] };
|
|
263
|
+
* const fit = nls("rate ~ Vm * conc / (K + conc)", data, { start: { Vm: 200, K: 0.05 } });
|
|
264
|
+
* nlsConfint(fit, { method: "wald", level: 0.95 });
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @category Regression
|
|
268
|
+
*/
|
|
269
|
+
export declare function nlsConfint(fit: NlsResult, options?: NlsConfintOptions): NlsConfintResult;
|
|
270
|
+
/**
|
|
271
|
+
* Analysis of variance for nested nonlinear least-squares fits. Equivalent to R's `anova.nls()`.
|
|
272
|
+
*
|
|
273
|
+
* @param fits Two or more nested fits to the same observations.
|
|
274
|
+
* @returns The sequential F-test table.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* const data = { x: [1, 2, 3, 4, 5], y: [2, 3, 5, 7, 8] };
|
|
279
|
+
* const small = nls("y ~ a * x", data, { start: { a: 1 } });
|
|
280
|
+
* const large = nls("y ~ a * x + b", data, { start: { a: 1, b: 0 } });
|
|
281
|
+
* nlsAnova(small, large);
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @category Regression
|
|
285
|
+
*/
|
|
286
|
+
export declare function nlsAnova(...fits: NlsResult[]): NlsAnovaResult;
|