@numero-uno-jpn/rstats 0.10.0 → 0.11.1
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 +37 -0
- package/README.ja.md +13 -9
- package/README.md +13 -8
- package/dist/chunks/{chunk-QJJWGNJP.js → chunk-2X7JYQBI.js} +2 -2
- package/dist/chunks/{chunk-O6PT7IVU.js → chunk-36GVER3O.js} +2 -2
- package/dist/chunks/{chunk-EWC6I75B.js → chunk-3WUXRSM6.js} +1 -1
- package/dist/chunks/{chunk-4NM37MGA.js → chunk-6I6QKTFK.js} +2 -2
- package/dist/chunks/{chunk-2XKG3DX4.js → chunk-ETOEZTGL.js} +1 -1
- package/dist/chunks/{chunk-IT354SHV.js → chunk-FVPDVRJQ.js} +2 -2
- package/dist/chunks/{chunk-QULPY6YM.js → chunk-GG4BJMJW.js} +1 -1
- package/dist/chunks/{chunk-7HPVUVZB.js → chunk-HITD7CX6.js} +1 -1
- package/dist/chunks/{chunk-LIMZKONM.js → chunk-KK4GE65D.js} +2 -2
- package/dist/chunks/chunk-LAUYSGV4.js +3 -0
- package/dist/chunks/chunk-M6GDXKKS.js +2 -0
- package/dist/chunks/{chunk-QFULZNHL.js → chunk-PHHUHR7T.js} +1 -1
- package/dist/chunks/{chunk-ENL43FNO.js → chunk-WQPFDDIY.js} +2 -2
- package/dist/chunks/chunk-X2AWQSVS.js +2 -0
- package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-ZRJQD7VS.js} +2 -2
- package/dist/data/grouping.d.ts +100 -0
- package/dist/data/grouping.js +264 -0
- package/dist/descriptive/basic.d.ts +130 -17
- package/dist/descriptive/basic.js +115 -14
- package/dist/descriptive/cov.d.ts +22 -12
- package/dist/descriptive/dataFrames.d.ts +138 -0
- package/dist/descriptive/dataFrames.js +400 -0
- package/dist/descriptive/factanal.d.ts +26 -6
- package/dist/descriptive/factanal.js +7 -3
- package/dist/descriptive/hist.d.ts +27 -6
- package/dist/descriptive/hist.js +9 -3
- package/dist/descriptive/missing.d.ts +42 -0
- package/dist/descriptive/missing.js +52 -0
- package/dist/descriptive/moments.d.ts +91 -13
- package/dist/descriptive/moments.js +66 -9
- package/dist/descriptive/omega.d.ts +28 -6
- package/dist/descriptive/omega.js +16 -4
- package/dist/descriptive/pivot.d.ts +105 -0
- package/dist/descriptive/pivot.js +347 -0
- package/dist/descriptive/quantile.d.ts +33 -5
- package/dist/descriptive/quantile.js +20 -2
- package/dist/descriptive/rank.d.ts +12 -2
- package/dist/descriptive/rank.js +2 -0
- package/dist/descriptive/table.d.ts +55 -7
- package/dist/descriptive/table.js +38 -4
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.d.ts +40 -5
- package/dist/distributions/arcsine.js +35 -4
- package/dist/distributions/beta.d.ts +12 -0
- package/dist/distributions/beta.js +12 -0
- package/dist/distributions/binomial.d.ts +18 -0
- package/dist/distributions/binomial.js +18 -0
- package/dist/distributions/bvnorm.d.ts +4 -1
- package/dist/distributions/bvnorm.js +4 -1
- package/dist/distributions/cauchy.d.ts +24 -0
- package/dist/distributions/cauchy.js +24 -0
- package/dist/distributions/chisq.d.ts +18 -0
- package/dist/distributions/chisq.js +18 -0
- package/dist/distributions/exponential.d.ts +29 -1
- package/dist/distributions/exponential.js +24 -0
- package/dist/distributions/f.d.ts +18 -0
- package/dist/distributions/f.js +18 -0
- package/dist/distributions/gamma.d.ts +20 -0
- package/dist/distributions/gamma.js +18 -0
- package/dist/distributions/geometric.d.ts +24 -0
- package/dist/distributions/geometric.js +24 -0
- package/dist/distributions/hypergeometric.d.ts +24 -0
- package/dist/distributions/hypergeometric.js +24 -0
- package/dist/distributions/invgamma.d.ts +32 -4
- package/dist/distributions/invgamma.js +27 -3
- package/dist/distributions/kumaraswamy.d.ts +31 -3
- package/dist/distributions/kumaraswamy.js +31 -3
- package/dist/distributions/laplace.d.ts +36 -4
- package/dist/distributions/laplace.js +31 -3
- package/dist/distributions/logistic.d.ts +29 -1
- package/dist/distributions/logistic.js +24 -0
- package/dist/distributions/lognormal.d.ts +29 -1
- package/dist/distributions/lognormal.js +24 -0
- package/dist/distributions/multinomial.d.ts +7 -1
- package/dist/distributions/multinomial.js +2 -0
- package/dist/distributions/negbinomial.d.ts +29 -1
- package/dist/distributions/negbinomial.js +24 -0
- package/dist/distributions/normal.d.ts +11 -1
- package/dist/distributions/normal.js +6 -0
- package/dist/distributions/pareto.d.ts +36 -4
- package/dist/distributions/pareto.js +31 -3
- package/dist/distributions/poisson.d.ts +18 -0
- package/dist/distributions/poisson.js +18 -0
- package/dist/distributions/signrank.d.ts +24 -0
- package/dist/distributions/signrank.js +24 -0
- package/dist/distributions/summary.d.ts +20 -2
- package/dist/distributions/summary.js +4 -0
- package/dist/distributions/t.d.ts +18 -0
- package/dist/distributions/t.js +18 -0
- package/dist/distributions/triangular.d.ts +36 -4
- package/dist/distributions/triangular.js +31 -3
- package/dist/distributions/tukey.d.ts +15 -1
- package/dist/distributions/tukey.js +10 -0
- package/dist/distributions/uniform.d.ts +29 -1
- package/dist/distributions/uniform.js +24 -0
- package/dist/distributions/weibull.d.ts +29 -1
- package/dist/distributions/weibull.js +24 -0
- package/dist/distributions/wilcox.d.ts +24 -0
- package/dist/distributions/wilcox.js +24 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +5 -1
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/numerics.d.ts +1 -1
- package/dist/entries/tests.js +1 -1
- package/dist/htest/agreement.d.ts +61 -23
- package/dist/htest/agreement.js +19 -9
- package/dist/htest/ansariTest.d.ts +19 -1
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +43 -10
- package/dist/htest/aov.js +17 -3
- package/dist/htest/binomTest.d.ts +12 -2
- package/dist/htest/binomTest.js +2 -0
- package/dist/htest/brunnerMunzelTest.d.ts +9 -3
- package/dist/htest/brunnerMunzelTest.js +4 -2
- package/dist/htest/chisqTest.d.ts +19 -3
- package/dist/htest/cor.d.ts +31 -3
- package/dist/htest/cor.js +9 -27
- package/dist/htest/effectSize.d.ts +70 -12
- package/dist/htest/effectSize.js +40 -7
- package/dist/htest/fisherTest.d.ts +9 -1
- package/dist/htest/fisherTest.js +2 -0
- package/dist/htest/groups.d.ts +5 -1
- package/dist/htest/ksTest.d.ts +32 -2
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +15 -4
- package/dist/htest/leveneTest.js +5 -2
- package/dist/htest/mantelhaenTest.d.ts +25 -3
- package/dist/htest/mantelhaenTest.js +15 -1
- package/dist/htest/mcnemarTest.d.ts +14 -4
- package/dist/htest/mcnemarTest.js +4 -2
- package/dist/htest/multipleComparisons.d.ts +173 -29
- package/dist/htest/multipleComparisons.js +123 -19
- package/dist/htest/normalityTests.d.ts +56 -9
- package/dist/htest/normalityTests.js +40 -6
- package/dist/htest/onewayTest.d.ts +45 -2
- package/dist/htest/onewayTest.js +15 -7
- package/dist/htest/pAdjust.d.ts +9 -1
- package/dist/htest/pAdjust.js +2 -0
- package/dist/htest/pairwise.d.ts +50 -5
- package/dist/htest/pairwise.js +25 -0
- package/dist/htest/poissonTest.d.ts +12 -2
- package/dist/htest/poissonTest.js +2 -0
- package/dist/htest/polycor.d.ts +29 -10
- package/dist/htest/power.d.ts +60 -6
- package/dist/htest/power.js +6 -0
- package/dist/htest/propTest.d.ts +13 -2
- package/dist/htest/propTest.js +2 -0
- package/dist/htest/pwr.d.ts +286 -36
- package/dist/htest/pwr.js +116 -15
- package/dist/htest/rankTests.d.ts +132 -7
- package/dist/htest/rankTests.js +30 -33
- package/dist/htest/tTest.d.ts +32 -2
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/trendTests.d.ts +29 -8
- package/dist/htest/trendTests.js +8 -4
- package/dist/htest/types.d.ts +14 -2
- package/dist/htest/varTest.d.ts +46 -6
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +23 -1
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/internal/dpq.d.ts +4 -0
- package/dist/internal/noncentral.d.ts +2 -0
- package/dist/internal/vectorize.d.ts +4 -0
- package/dist/linalg/basic.d.ts +39 -5
- package/dist/linalg/basic.js +15 -0
- package/dist/linalg/chol.d.ts +26 -5
- package/dist/linalg/chol.js +7 -0
- package/dist/linalg/eigen.d.ts +16 -7
- package/dist/linalg/eigen.js +6 -5
- package/dist/linalg/expm.d.ts +15 -5
- package/dist/linalg/expm.js +5 -3
- package/dist/linalg/kappa.d.ts +21 -1
- package/dist/linalg/kappa.js +16 -0
- package/dist/linalg/lu.d.ts +30 -6
- package/dist/linalg/lu.js +15 -3
- package/dist/linalg/matrix.d.ts +4 -0
- package/dist/linalg/norm.d.ts +16 -1
- package/dist/linalg/norm.js +9 -0
- package/dist/linalg/ops.d.ts +168 -36
- package/dist/linalg/ops.js +116 -20
- package/dist/linalg/qr.d.ts +87 -12
- package/dist/linalg/qr.js +31 -0
- package/dist/linalg/schur.d.ts +8 -2
- package/dist/linalg/schur.js +3 -1
- package/dist/linalg/solve.d.ts +42 -6
- package/dist/linalg/solve.js +22 -0
- package/dist/linalg/svd.d.ts +12 -2
- package/dist/linalg/svd.js +2 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +26 -4
- package/dist/models/anovaSequential.js +3 -1
- package/dist/models/aovEz.d.ts +29 -6
- package/dist/models/aovEz.js +5 -2
- package/dist/models/aovStrata.d.ts +12 -2
- package/dist/models/carAnova.d.ts +9 -3
- package/dist/models/carAnova.js +4 -2
- package/dist/models/coxph.d.ts +33 -7
- package/dist/models/coxph.js +8 -3
- package/dist/models/glm.d.ts +128 -21
- package/dist/models/glm.js +71 -14
- package/dist/models/glmAnova.d.ts +30 -5
- package/dist/models/glmAnova.js +15 -2
- package/dist/models/glmConfint.d.ts +16 -2
- package/dist/models/glmConfint.js +9 -1
- package/dist/models/glmFamily.d.ts +27 -5
- package/dist/models/lm.d.ts +75 -17
- package/dist/models/lm.js +22 -4
- package/dist/models/mauchly.d.ts +20 -5
- package/dist/models/mauchly.js +7 -3
- package/dist/models/modelFrame.d.ts +5 -1
- package/dist/models/modelMatrix.d.ts +18 -4
- package/dist/models/modelMatrix.js +3 -1
- package/dist/models/survival.d.ts +41 -9
- package/dist/models/survival.js +10 -4
- package/dist/numerics/deriv.d.ts +21 -6
- package/dist/numerics/deriv.js +16 -5
- package/dist/numerics/integrate.d.ts +37 -7
- package/dist/numerics/integrate.js +8 -2
- package/dist/numerics/interp.d.ts +63 -12
- package/dist/numerics/interp.js +30 -6
- package/dist/numerics/ode.d.ts +15 -5
- package/dist/numerics/ode.js +5 -3
- package/dist/numerics.js +2 -2
- package/dist/optim/optim.d.ts +32 -8
- package/dist/optim/optim.js +8 -4
- package/dist/random/beta.d.ts +8 -0
- package/dist/random/beta.js +8 -0
- package/dist/random/binomial.d.ts +16 -0
- package/dist/random/binomial.js +16 -0
- package/dist/random/continuous.d.ts +51 -0
- package/dist/random/continuous.js +51 -0
- package/dist/random/extra.d.ts +66 -6
- package/dist/random/extra.js +66 -6
- package/dist/random/gamma.d.ts +33 -0
- package/dist/random/gamma.js +33 -0
- package/dist/random/hyper.d.ts +8 -0
- package/dist/random/hyper.js +8 -0
- package/dist/random/poisson.d.ts +25 -0
- package/dist/random/poisson.js +25 -0
- package/dist/random/r2dtable.d.ts +2 -0
- package/dist/random/r2dtable.js +2 -0
- package/dist/random/rank.d.ts +16 -0
- package/dist/random/rank.js +16 -0
- package/dist/random/rng.d.ts +49 -6
- package/dist/random/rng.js +29 -2
- package/dist/random/sample.d.ts +10 -2
- package/dist/random/sample.js +5 -1
- package/dist/random/streams.d.ts +13 -0
- package/dist/random/streams.js +13 -0
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +11 -3
- package/dist/special/besselIK.js +6 -2
- package/dist/special/besselJY.d.ts +6 -2
- package/dist/special/besselJY.js +6 -2
- package/dist/special/beta.d.ts +16 -2
- package/dist/special/beta.js +16 -2
- package/dist/special/choose.d.ts +13 -5
- package/dist/special/choose.js +13 -5
- package/dist/special/erf.d.ts +8 -3
- package/dist/special/erf.js +8 -3
- package/dist/special/extra.d.ts +30 -10
- package/dist/special/extra.js +25 -9
- package/dist/special/gamma.d.ts +18 -2
- package/dist/special/gamma.js +18 -2
- package/dist/special/log1p.d.ts +16 -7
- package/dist/special/log1p.js +16 -7
- package/dist/special/psigamma.d.ts +9 -3
- package/dist/special/psigamma.js +9 -3
- package/dist/special/rCompatible.d.ts +5 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -13
- package/llms.txt +14 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-AECGWZHD.js +0 -2
- package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
package/dist/models/glm.js
CHANGED
|
@@ -247,7 +247,7 @@ export function refit(model, cols, warnings, control = {}) {
|
|
|
247
247
|
}
|
|
248
248
|
/**
|
|
249
249
|
* Fit a generalized linear model by iteratively reweighted least squares. Equivalent to R's
|
|
250
|
-
* `glm(y ~ x, family = ...)
|
|
250
|
+
* `glm(y ~ x, family = ...)`. Or `y ~ x - 1` without intercept; the predictors are given as a matrix instead of a
|
|
251
251
|
* formula. Rows with a missing value (NaN) in `x`, `y`, the weights or the offset are dropped. Every family and link
|
|
252
252
|
* of R's `stats` is available; R's warnings are returned in `warnings`. See {@link glmSummary}, {@link glmResiduals},
|
|
253
253
|
* {@link glmPredict}, {@link glmConfint} and {@link glmAnova} for the methods.
|
|
@@ -259,9 +259,11 @@ export function refit(model, cols, warnings, control = {}) {
|
|
|
259
259
|
* @example
|
|
260
260
|
* ```ts
|
|
261
261
|
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
262
|
-
* fit.coefficients; // [
|
|
263
|
-
* glmSummary(fit).pValue;
|
|
262
|
+
* fit.coefficients; // [-4.2491, 1.214]
|
|
263
|
+
* glmSummary(fit).pValue; // [0.20976, 0.18341]
|
|
264
264
|
* ```
|
|
265
|
+
*
|
|
266
|
+
* @category Generalized linear models
|
|
265
267
|
*/
|
|
266
268
|
export function glm(x, y, options = {}) {
|
|
267
269
|
const intercept = options.intercept ?? true;
|
|
@@ -394,8 +396,19 @@ function unscaledCovariance(fit) {
|
|
|
394
396
|
return chol2inv(Rr);
|
|
395
397
|
}
|
|
396
398
|
/**
|
|
397
|
-
* Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates
|
|
398
|
-
* Equivalent to R's `summary()
|
|
399
|
+
* Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates,
|
|
400
|
+
* for a `glm` fit. Equivalent to R's `summary()`.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
405
|
+
* const s = glmSummary(fit);
|
|
406
|
+
* s.estimate; // [-4.2491, 1.214]
|
|
407
|
+
* s.stdError; // [3.3878, 0.91258]
|
|
408
|
+
* s.statisticName; // "z value"
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
411
|
+
* @category Generalized linear models
|
|
399
412
|
*/
|
|
400
413
|
export function glmSummary(fit, options = {}) {
|
|
401
414
|
const warnings = new Warnings();
|
|
@@ -427,8 +440,17 @@ export function glmSummary(fit, options = {}) {
|
|
|
427
440
|
};
|
|
428
441
|
}
|
|
429
442
|
/**
|
|
430
|
-
* Residuals of a generalized linear model, one per row
|
|
431
|
-
* (
|
|
443
|
+
* Residuals of a generalized linear model, one per row, for a `glm` fit (default `"deviance"`). Equivalent to R's
|
|
444
|
+
* `residuals(fit, type)`.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
449
|
+
* glmResiduals(fit); // [-0.30644, -0.54776, 1.4436, -1.4436, 0.54776, 0.30644]
|
|
450
|
+
* glmResiduals(fit, "pearson"); // [-0.21925, -0.40231, 1.3546, -1.3546, 0.40231, 0.21925]
|
|
451
|
+
* ```
|
|
452
|
+
*
|
|
453
|
+
* @category Generalized linear models
|
|
432
454
|
*/
|
|
433
455
|
export function glmResiduals(fit, type = "deviance") {
|
|
434
456
|
const fam = familyOf(fit);
|
|
@@ -449,8 +471,16 @@ export function glmResiduals(fit, type = "deviance") {
|
|
|
449
471
|
}
|
|
450
472
|
}
|
|
451
473
|
/**
|
|
452
|
-
* Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight
|
|
453
|
-
* Equivalent to R's `hatvalues()
|
|
474
|
+
* Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight, for a
|
|
475
|
+
* `glm` fit. Equivalent to R's `hatvalues()`.
|
|
476
|
+
*
|
|
477
|
+
* @example
|
|
478
|
+
* ```ts
|
|
479
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
480
|
+
* glmHatvalues(fit); // [0.28361, 0.37762, 0.33877, 0.33877, 0.37762, 0.28361]
|
|
481
|
+
* ```
|
|
482
|
+
*
|
|
483
|
+
* @category Generalized linear models
|
|
454
484
|
*/
|
|
455
485
|
export function glmHatvalues(fit) {
|
|
456
486
|
const out = [];
|
|
@@ -478,8 +508,16 @@ function median(v) {
|
|
|
478
508
|
return s.length % 2 === 1 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
479
509
|
}
|
|
480
510
|
/**
|
|
481
|
-
* Standardized deviance residuals, for the rows of non-zero prior weight. Equivalent to R's
|
|
482
|
-
* `
|
|
511
|
+
* Standardized deviance residuals, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's
|
|
512
|
+
* `rstandard()`.
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```ts
|
|
516
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
517
|
+
* glmRstandard(fit); // [-0.36205, -0.69432, 1.7753, -1.7753, 0.69432, 0.36205]
|
|
518
|
+
* ```
|
|
519
|
+
*
|
|
520
|
+
* @category Generalized linear models
|
|
483
521
|
*/
|
|
484
522
|
export function glmRstandard(fit, options = {}) {
|
|
485
523
|
const h = glmHatvalues(fit);
|
|
@@ -491,7 +529,17 @@ export function glmRstandard(fit, options = {}) {
|
|
|
491
529
|
return Number.isFinite(s) ? s : NaN;
|
|
492
530
|
});
|
|
493
531
|
}
|
|
494
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* Cook's distances, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's `cooks.distance()`.
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* ```ts
|
|
537
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
538
|
+
* glmCooksDistance(fit); // [0.013283, 0.078894, 0.71087, 0.71087, 0.078894, 0.013283]
|
|
539
|
+
* ```
|
|
540
|
+
*
|
|
541
|
+
* @category Generalized linear models
|
|
542
|
+
*/
|
|
495
543
|
export function glmCooksDistance(fit, options = {}) {
|
|
496
544
|
const h = glmHatvalues(fit);
|
|
497
545
|
const disp = options.dispersion ?? glmDispersion(fit);
|
|
@@ -502,8 +550,17 @@ export function glmCooksDistance(fit, options = {}) {
|
|
|
502
550
|
});
|
|
503
551
|
}
|
|
504
552
|
/**
|
|
505
|
-
* Predictions of a generalized linear model with their standard errors. Equivalent to R's
|
|
506
|
-
* `predict(fit, newdata, type, se.fit = TRUE)
|
|
553
|
+
* Predictions of a generalized linear model with their standard errors, for a `glm` fit. Equivalent to R's
|
|
554
|
+
* `predict(fit, newdata, type, se.fit = TRUE)`. Aliased columns are left out, as R does.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```ts
|
|
558
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
559
|
+
* glmPredict(fit, { newdata: [7] }); // { fit: [4.2491], seFit: [3.3878] }
|
|
560
|
+
* glmPredict(fit, { newdata: [7], type: "response" }).fit; // [0.98592]
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @category Generalized linear models
|
|
507
564
|
*/
|
|
508
565
|
export function glmPredict(fit, options = {}) {
|
|
509
566
|
const fam = familyOf(fit);
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { type GlmResult } from "./glm.js";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Tests of {@link glmAnova}.
|
|
4
|
+
*
|
|
5
|
+
* @category Generalized linear models
|
|
6
|
+
*/
|
|
3
7
|
export type GlmAnovaTest = "Chisq" | "LRT" | "Rao" | "F" | "Cp";
|
|
4
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Options for {@link glmAnova}.
|
|
10
|
+
*
|
|
11
|
+
* @category Generalized linear models
|
|
12
|
+
*/
|
|
5
13
|
export interface GlmAnovaOptions {
|
|
6
14
|
/**
|
|
7
15
|
* The test: `"Chisq"` (or its synonym `"LRT"`), `"Rao"`, `"F"`, `"Cp"`, or `false` for none. Default, as R since
|
|
@@ -11,7 +19,11 @@ export interface GlmAnovaOptions {
|
|
|
11
19
|
/** Dispersion for the tests instead of the estimate of the largest model. */
|
|
12
20
|
dispersion?: number;
|
|
13
21
|
}
|
|
14
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Result of {@link glmAnova}: R's anova table.
|
|
24
|
+
*
|
|
25
|
+
* @category Generalized linear models
|
|
26
|
+
*/
|
|
15
27
|
export interface GlmAnovaResult {
|
|
16
28
|
/** Column names, such as `"Df"`, `"Deviance"`, `"Resid. Df"`, `"Resid. Dev"`, `"Pr(>Chi)"`. */
|
|
17
29
|
colNames: string[];
|
|
@@ -23,9 +35,22 @@ export interface GlmAnovaResult {
|
|
|
23
35
|
warnings: string[];
|
|
24
36
|
}
|
|
25
37
|
/**
|
|
26
|
-
* Analysis of deviance
|
|
27
|
-
*
|
|
38
|
+
* Analysis of deviance, for one `glm` fit (the sequential table, each column of `x` added in order) or for several
|
|
39
|
+
* fits of the same data (`anova(fit1, fit2, ...)`). Equivalent to R's `anova()`.
|
|
28
40
|
*
|
|
29
41
|
* @param fits One fit, or an array of fits to compare.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const y = [0, 0, 1, 0, 1, 1];
|
|
46
|
+
* const fit0 = glm([[], [], [], [], [], []], y, { family: "binomial" });
|
|
47
|
+
* const fit1 = glm([1, 2, 3, 4, 5, 6], y, { family: "binomial" });
|
|
48
|
+
* const a = glmAnova(fit1);
|
|
49
|
+
* a.colNames; // ["Df", "Deviance", "Resid. Df", "Resid. Dev", "Pr(>Chi)"]
|
|
50
|
+
* a.table[1]; // [1, 3.3618, 4, 4.956, 0.066725]
|
|
51
|
+
* glmAnova([fit0, fit1], { test: "LRT" }).table[1]; // [4, 4.956, 1, 3.3618, 0.066725]
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @category Generalized linear models
|
|
30
55
|
*/
|
|
31
56
|
export declare function glmAnova(fits: GlmResult | readonly GlmResult[], options?: GlmAnovaOptions): GlmAnovaResult;
|
package/dist/models/glmAnova.js
CHANGED
|
@@ -21,10 +21,23 @@ function rowOf(model, cols, warnings) {
|
|
|
21
21
|
/** Score statistic of the smaller model's fit against the larger model's matrix */
|
|
22
22
|
const rao = (small, large) => scoreStatistic(large.x, small.fit.workingResiduals, small.fit.workingWeights, large.intercept);
|
|
23
23
|
/**
|
|
24
|
-
* Analysis of deviance
|
|
25
|
-
*
|
|
24
|
+
* Analysis of deviance, for one `glm` fit (the sequential table, each column of `x` added in order) or for several
|
|
25
|
+
* fits of the same data (`anova(fit1, fit2, ...)`). Equivalent to R's `anova()`.
|
|
26
26
|
*
|
|
27
27
|
* @param fits One fit, or an array of fits to compare.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const y = [0, 0, 1, 0, 1, 1];
|
|
32
|
+
* const fit0 = glm([[], [], [], [], [], []], y, { family: "binomial" });
|
|
33
|
+
* const fit1 = glm([1, 2, 3, 4, 5, 6], y, { family: "binomial" });
|
|
34
|
+
* const a = glmAnova(fit1);
|
|
35
|
+
* a.colNames; // ["Df", "Deviance", "Resid. Df", "Resid. Dev", "Pr(>Chi)"]
|
|
36
|
+
* a.table[1]; // [1, 3.3618, 4, 4.956, 0.066725]
|
|
37
|
+
* glmAnova([fit0, fit1], { test: "LRT" }).table[1]; // [4, 4.956, 1, 3.3618, 0.066725]
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @category Generalized linear models
|
|
28
41
|
*/
|
|
29
42
|
export function glmAnova(fits, options = {}) {
|
|
30
43
|
const list = Array.isArray(fits) ? fits : [fits];
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { type GlmResult } from "./glm.js";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link glmConfint}.
|
|
4
|
+
*
|
|
5
|
+
* @category Generalized linear models
|
|
6
|
+
*/
|
|
3
7
|
export interface GlmConfintOptions {
|
|
4
8
|
/** Indices of the coefficients (0-based, intercept first). Default: all. */
|
|
5
9
|
parm?: readonly number[];
|
|
@@ -15,11 +19,21 @@ export interface GlmConfintOptions {
|
|
|
15
19
|
export declare function scoreStatistic(x: number[][], r: number[], v: number[], intercept: boolean, tol?: number): number;
|
|
16
20
|
/**
|
|
17
21
|
* Profile likelihood confidence intervals for the coefficients of a generalized linear model (NaN for aliased
|
|
18
|
-
* columns, and for a limit that the profile does not reach). Equivalent to R's `confint()
|
|
22
|
+
* columns, and for a limit that the profile does not reach), for a `glm` fit. Equivalent to R's `confint()`. With
|
|
19
23
|
* the exact solutions of the profile equations, where R interpolates a spline through a grid of profile points (and
|
|
20
24
|
* is up to about 5e-4 standard errors from them).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
29
|
+
* glmConfint(fit); // { lower: [-15.124, -0.065819], upper: [0.57792, 4.1592] }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @category Generalized linear models
|
|
21
33
|
*/
|
|
22
34
|
export declare function glmConfint(fit: GlmResult, options?: GlmConfintOptions): {
|
|
35
|
+
/** Lower limits, one per coefficient (R's first column, `2.5 %` by default). */
|
|
23
36
|
lower: number[];
|
|
37
|
+
/** Upper limits, one per coefficient (R's second column, `97.5 %` by default). */
|
|
24
38
|
upper: number[];
|
|
25
39
|
};
|
|
@@ -52,9 +52,17 @@ export function scoreStatistic(x, r, v, intercept, tol = 1e-11) {
|
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* Profile likelihood confidence intervals for the coefficients of a generalized linear model (NaN for aliased
|
|
55
|
-
* columns, and for a limit that the profile does not reach). Equivalent to R's `confint()
|
|
55
|
+
* columns, and for a limit that the profile does not reach), for a `glm` fit. Equivalent to R's `confint()`. With
|
|
56
56
|
* the exact solutions of the profile equations, where R interpolates a spline through a grid of profile points (and
|
|
57
57
|
* is up to about 5e-4 standard errors from them).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
62
|
+
* glmConfint(fit); // { lower: [-15.124, -0.065819], upper: [0.57792, 4.1592] }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @category Generalized linear models
|
|
58
66
|
*/
|
|
59
67
|
export function glmConfint(fit, options = {}) {
|
|
60
68
|
const level = options.level ?? 0.95;
|
|
@@ -1,15 +1,37 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Names of the families of R's stats.
|
|
3
|
+
*
|
|
4
|
+
* @category Generalized linear models
|
|
5
|
+
*/
|
|
2
6
|
export type GlmFamilyName = "gaussian" | "binomial" | "quasibinomial" | "poisson" | "quasipoisson" | "Gamma" | "inverse.gaussian" | "quasi";
|
|
3
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Names of the links of R's stats (`make.link`).
|
|
9
|
+
*
|
|
10
|
+
* @category Generalized linear models
|
|
11
|
+
*/
|
|
4
12
|
export type GlmLinkName = "identity" | "log" | "inverse" | "logit" | "probit" | "cauchit" | "cloglog" | "sqrt" | "1/mu^2";
|
|
5
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* A link: one of R's named links or a power link `mu^power` (R's `power(lambda)`; `power <= 0` is the log link).
|
|
15
|
+
*
|
|
16
|
+
* @category Generalized linear models
|
|
17
|
+
*/
|
|
6
18
|
export type GlmLink = GlmLinkName | {
|
|
19
|
+
/** The exponent lambda of the power link (R's `power(lambda)`). */
|
|
7
20
|
power: number;
|
|
8
21
|
};
|
|
9
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Variance functions of the `quasi` family.
|
|
24
|
+
*
|
|
25
|
+
* @category Generalized linear models
|
|
26
|
+
*/
|
|
10
27
|
export type QuasiVariance = "constant" | "mu(1-mu)" | "mu" | "mu^2" | "mu^3";
|
|
11
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* A family with its link (and, for `quasi`, its variance function), as R's family objects such as `quasi(link = "log", variance = "mu^2")`.
|
|
30
|
+
*
|
|
31
|
+
* @category Generalized linear models
|
|
32
|
+
*/
|
|
12
33
|
export interface GlmFamilySpec {
|
|
34
|
+
/** The family's name, such as `"binomial"`. */
|
|
13
35
|
family: GlmFamilyName;
|
|
14
36
|
/** The link. Default: the family's default (the first of its allowed links). */
|
|
15
37
|
link?: GlmLink;
|
package/dist/models/lm.d.ts
CHANGED
|
@@ -3,19 +3,28 @@ import { type QR } from "../linalg/qr.js";
|
|
|
3
3
|
import type { Sample } from "../htest/types.js";
|
|
4
4
|
import type { DataFrame } from "./modelFrame.js";
|
|
5
5
|
import { type Design, type ModelMatrixOptions } from "./modelMatrix.js";
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Options for {@link lm}.
|
|
8
|
+
*
|
|
9
|
+
* @category Linear models and ANOVA
|
|
10
|
+
*/
|
|
7
11
|
export interface LmOptions {
|
|
8
12
|
/** Include an intercept (a column of ones before the columns of `x`). Default `true`. */
|
|
9
13
|
intercept?: boolean;
|
|
10
14
|
/** Weights for weighted least squares (non-negative; rows of weight 0 are left out of the fit). */
|
|
11
15
|
weights?: Sample;
|
|
12
16
|
}
|
|
13
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* Result of {@link lm}: the fit and the quantities of R's `summary(lm)`.
|
|
19
|
+
*
|
|
20
|
+
* @category Linear models and ANOVA
|
|
21
|
+
*/
|
|
14
22
|
export interface LmResult {
|
|
15
23
|
/** Coefficients (intercept first); NaN for aliased columns. */
|
|
16
24
|
coefficients: number[];
|
|
17
25
|
/** Residuals y - fitted (rows with a missing value dropped; rows of weight 0 included). */
|
|
18
26
|
residuals: number[];
|
|
27
|
+
/** Fitted values, one per residual (R's `fitted.values`). */
|
|
19
28
|
fittedValues: number[];
|
|
20
29
|
/** Numerical rank of the model matrix. */
|
|
21
30
|
rank: number;
|
|
@@ -23,9 +32,13 @@ export interface LmResult {
|
|
|
23
32
|
dfResidual: number;
|
|
24
33
|
/** The coefficient table of the columns that are not aliased, in their order. */
|
|
25
34
|
coefTable: {
|
|
35
|
+
/** Estimates (R's `Estimate`). */
|
|
26
36
|
estimate: number[];
|
|
37
|
+
/** Standard errors (R's `Std. Error`). */
|
|
27
38
|
stdError: number[];
|
|
39
|
+
/** t statistics (R's `t value`). */
|
|
28
40
|
tValue: number[];
|
|
41
|
+
/** Two-sided p-values (R's `Pr(>|t|)`). */
|
|
29
42
|
pValue: number[];
|
|
30
43
|
};
|
|
31
44
|
/** Residual standard error. */
|
|
@@ -39,8 +52,11 @@ export interface LmResult {
|
|
|
39
52
|
* intercept, where R's `summary(lm)` has no `fstatistic`.
|
|
40
53
|
*/
|
|
41
54
|
fStatistic?: {
|
|
55
|
+
/** The F statistic. */
|
|
42
56
|
value: number;
|
|
57
|
+
/** Numerator degrees of freedom (rank minus the intercept). */
|
|
43
58
|
numdf: number;
|
|
59
|
+
/** Denominator degrees of freedom (the residual degrees of freedom). */
|
|
44
60
|
dendf: number;
|
|
45
61
|
};
|
|
46
62
|
/** p-value of the overall F test; absent with `fStatistic`. */
|
|
@@ -50,12 +66,20 @@ export interface LmResult {
|
|
|
50
66
|
/** The QR decomposition of the (weighted) model matrix. */
|
|
51
67
|
qr: QR;
|
|
52
68
|
}
|
|
53
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* Options of {@link lm} with a formula.
|
|
71
|
+
*
|
|
72
|
+
* @category Linear models and ANOVA
|
|
73
|
+
*/
|
|
54
74
|
export interface LmFormulaOptions extends ModelMatrixOptions {
|
|
55
75
|
/** Weights: an array parallel to the rows of `data`, or the name of a numeric column. */
|
|
56
76
|
weights?: Sample | string;
|
|
57
77
|
}
|
|
58
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Result of {@link lm} with a formula: the fit of the model matrix, with the formula's terms.
|
|
80
|
+
*
|
|
81
|
+
* @category Linear models and ANOVA
|
|
82
|
+
*/
|
|
59
83
|
export interface LmFormulaResult extends LmResult {
|
|
60
84
|
/** The formula. */
|
|
61
85
|
formula: string;
|
|
@@ -95,8 +119,8 @@ export interface LmFormulaResult extends LmResult {
|
|
|
95
119
|
/** The formula fit (lm, and aov without Error()) */
|
|
96
120
|
export declare function lmFormulaFit(formula: string, d: Design, aov: boolean): LmFormulaResult;
|
|
97
121
|
/**
|
|
98
|
-
* Fit a linear model from a formula. Equivalent to R's `lm(formula, data)
|
|
99
|
-
*
|
|
122
|
+
* Fit a linear model from a formula. Equivalent to R's `lm(formula, data)`. With `weights` and `contrasts`, and the
|
|
123
|
+
* quantities of `summary()`.
|
|
100
124
|
*
|
|
101
125
|
* The model matrix is that of {@link modelMatrix} (but levels without rows are dropped, as R's
|
|
102
126
|
* `drop.unused.levels`); the fit is that of the matrix form (same pivoting and tolerance), its sums of squares computed
|
|
@@ -112,12 +136,14 @@ export declare function lmFormulaFit(formula: string, d: Design, aov: boolean):
|
|
|
112
136
|
* fit.coefNames; // ["(Intercept)", "gb", "gc"]
|
|
113
137
|
* fit.coefficients; // [3, 4, 7.95]
|
|
114
138
|
* ```
|
|
139
|
+
*
|
|
140
|
+
* @category Linear models and ANOVA
|
|
115
141
|
*/
|
|
116
142
|
export declare function lm(formula: string, data: DataFrame, options?: LmFormulaOptions): LmFormulaResult;
|
|
117
143
|
/**
|
|
118
|
-
* Fit a linear model by least squares, the predictors given as a matrix
|
|
119
|
-
*
|
|
120
|
-
*
|
|
144
|
+
* Fit a linear model by least squares, the predictors given as a matrix: R's `lm(y ~ x)` (or `y ~ x - 1` without
|
|
145
|
+
* intercept, with `weights` for weighted least squares) together with `summary()`. Rows with a missing value in `x`,
|
|
146
|
+
* `y` or the weights are dropped.
|
|
121
147
|
*
|
|
122
148
|
* @param x Predictors: a matrix (array of rows, one column per predictor) or a vector (one predictor).
|
|
123
149
|
* @param y Response.
|
|
@@ -125,8 +151,8 @@ export declare function lm(formula: string, data: DataFrame, options?: LmFormula
|
|
|
125
151
|
* @example
|
|
126
152
|
* ```ts
|
|
127
153
|
* const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
|
|
128
|
-
* fit.coefficients; // [
|
|
129
|
-
* fit.rSquared;
|
|
154
|
+
* fit.coefficients; // [0.05, 1.99]
|
|
155
|
+
* fit.rSquared; // 0.99731
|
|
130
156
|
* ```
|
|
131
157
|
*/
|
|
132
158
|
export declare function lm(x: MatrixInput | Sample, y: Sample, options?: LmOptions): LmResult;
|
|
@@ -145,20 +171,39 @@ export interface LmCore {
|
|
|
145
171
|
* left out of the fit and of the counts.
|
|
146
172
|
*/
|
|
147
173
|
export declare function fitLm(Xall: number[][], Yall: number[], Wall: number[] | undefined, intercept: boolean, precise?: boolean): LmCore;
|
|
148
|
-
/**
|
|
174
|
+
/**
|
|
175
|
+
* Options for {@link confint}.
|
|
176
|
+
*
|
|
177
|
+
* @category Linear models and ANOVA
|
|
178
|
+
*/
|
|
149
179
|
export interface ConfintOptions {
|
|
150
180
|
/** Confidence level. Default `0.95`. */
|
|
151
181
|
level?: number;
|
|
152
182
|
}
|
|
153
183
|
/**
|
|
154
|
-
* Confidence intervals for the coefficients of a linear model (NaN for aliased columns)
|
|
155
|
-
* `confint()
|
|
184
|
+
* Confidence intervals for the coefficients of a linear model (NaN for aliased columns), for an `lm` fit.
|
|
185
|
+
* Equivalent to R's `confint()`.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
|
|
190
|
+
* confint(fit); // { lower: [-0.58036, 1.79994], upper: [0.68036, 2.18006] }
|
|
191
|
+
* confint(fit, { level: 0.9 }).lower; // [-0.41614, 1.84945]
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @category Linear models and ANOVA
|
|
156
195
|
*/
|
|
157
196
|
export declare function confint(fit: LmResult, options?: ConfintOptions): {
|
|
197
|
+
/** Lower limits, one per coefficient (R's first column, `2.5 %` by default). */
|
|
158
198
|
lower: number[];
|
|
199
|
+
/** Upper limits, one per coefficient (R's second column, `97.5 %` by default). */
|
|
159
200
|
upper: number[];
|
|
160
201
|
};
|
|
161
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* Options for {@link predict}.
|
|
204
|
+
*
|
|
205
|
+
* @category Linear models and ANOVA
|
|
206
|
+
*/
|
|
162
207
|
export interface PredictOptions {
|
|
163
208
|
/** `"none"` (default), `"confidence"` (for the mean response) or `"prediction"` (for a new observation). */
|
|
164
209
|
interval?: "none" | "confidence" | "prediction";
|
|
@@ -166,14 +211,27 @@ export interface PredictOptions {
|
|
|
166
211
|
level?: number;
|
|
167
212
|
}
|
|
168
213
|
/**
|
|
169
|
-
* Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals
|
|
170
|
-
*
|
|
214
|
+
* Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals, for
|
|
215
|
+
* an `lm` fit (`se.fit = TRUE`). Equivalent to R's `predict()`. Aliased columns are left out, as R does.
|
|
171
216
|
*
|
|
172
217
|
* @param newdata New predictor values, with the columns of `x` (no column of ones).
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
|
|
222
|
+
* predict(fit, [6, 7]); // { fit: [11.99, 13.98], seFit: [0.19807, 0.25338] }
|
|
223
|
+
* predict(fit, [6], { interval: "prediction" }); // { lwr: [11.119], upr: [12.861] }
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @category Linear models and ANOVA
|
|
173
227
|
*/
|
|
174
228
|
export declare function predict(fit: LmResult, newdata: MatrixInput | Sample, options?: PredictOptions): {
|
|
229
|
+
/** Predicted values, one per row of `newdata`. */
|
|
175
230
|
fit: number[];
|
|
231
|
+
/** Standard errors of the predicted means (R's `se.fit`). */
|
|
176
232
|
seFit: number[];
|
|
233
|
+
/** Lower limits of the intervals (absent with `interval: "none"`). */
|
|
177
234
|
lwr?: number[];
|
|
235
|
+
/** Upper limits of the intervals (absent with `interval: "none"`). */
|
|
178
236
|
upr?: number[];
|
|
179
237
|
};
|
package/dist/models/lm.js
CHANGED
|
@@ -229,8 +229,17 @@ export function fitLm(Xall, Yall, Wall, intercept, precise = false) {
|
|
|
229
229
|
return core;
|
|
230
230
|
}
|
|
231
231
|
/**
|
|
232
|
-
* Confidence intervals for the coefficients of a linear model (NaN for aliased columns)
|
|
233
|
-
* `confint()
|
|
232
|
+
* Confidence intervals for the coefficients of a linear model (NaN for aliased columns), for an `lm` fit.
|
|
233
|
+
* Equivalent to R's `confint()`.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
|
|
238
|
+
* confint(fit); // { lower: [-0.58036, 1.79994], upper: [0.68036, 2.18006] }
|
|
239
|
+
* confint(fit, { level: 0.9 }).lower; // [-0.41614, 1.84945]
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @category Linear models and ANOVA
|
|
234
243
|
*/
|
|
235
244
|
export function confint(fit, options = {}) {
|
|
236
245
|
const level = options.level ?? 0.95;
|
|
@@ -249,10 +258,19 @@ export function confint(fit, options = {}) {
|
|
|
249
258
|
return { lower, upper };
|
|
250
259
|
}
|
|
251
260
|
/**
|
|
252
|
-
* Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals
|
|
253
|
-
*
|
|
261
|
+
* Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals, for
|
|
262
|
+
* an `lm` fit (`se.fit = TRUE`). Equivalent to R's `predict()`. Aliased columns are left out, as R does.
|
|
254
263
|
*
|
|
255
264
|
* @param newdata New predictor values, with the columns of `x` (no column of ones).
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
|
|
269
|
+
* predict(fit, [6, 7]); // { fit: [11.99, 13.98], seFit: [0.19807, 0.25338] }
|
|
270
|
+
* predict(fit, [6], { interval: "prediction" }); // { lwr: [11.119], upr: [12.861] }
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* @category Linear models and ANOVA
|
|
256
274
|
*/
|
|
257
275
|
export function predict(fit, newdata, options = {}) {
|
|
258
276
|
const rows = newdata.length > 0 && typeof newdata[0] !== "number"
|
package/dist/models/mauchly.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { MatrixInput } from "../linalg/matrix.js";
|
|
2
2
|
import { type DDMatrix } from "./ddMatrix.js";
|
|
3
3
|
import { type DataFrame } from "./modelFrame.js";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Options for {@link mauchlyTest}.
|
|
6
|
+
*
|
|
7
|
+
* @category Linear models and ANOVA
|
|
8
|
+
*/
|
|
5
9
|
export interface MauchlyTestOptions {
|
|
6
10
|
/** Between-subjects group labels, parallel to the rows of `y` (the residuals are taken from the group means). */
|
|
7
11
|
group?: readonly (string | number)[];
|
|
@@ -12,12 +16,19 @@ export interface MauchlyTestOptions {
|
|
|
12
16
|
/** Formula over `idata` whose space is projected out. Default `"~0"` (nothing). */
|
|
13
17
|
X?: string;
|
|
14
18
|
}
|
|
15
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Result of {@link mauchlyTest}.
|
|
21
|
+
*
|
|
22
|
+
* @category Linear models and ANOVA
|
|
23
|
+
*/
|
|
16
24
|
export interface MauchlyTestResult {
|
|
17
25
|
/** Mauchly's W. */
|
|
18
26
|
statistic: number;
|
|
27
|
+
/** Name of the statistic (R's `names(statistic)`). */
|
|
19
28
|
statisticName: "W";
|
|
29
|
+
/** p-value (R's `p.value`). */
|
|
20
30
|
pValue: number;
|
|
31
|
+
/** Name of the test (R's `method`). */
|
|
21
32
|
method: "Mauchly's test of sphericity";
|
|
22
33
|
}
|
|
23
34
|
/** W = det(U) / (trace(U)/p)^p of a p x p matrix U */
|
|
@@ -34,15 +45,19 @@ export declare function mauchlyPValue(W: number, pp: number, n: number, k: numbe
|
|
|
34
45
|
export declare function contrastCrossprod(S: DDMatrix, mcols: number[][], xcols: number[][]): DDMatrix;
|
|
35
46
|
/**
|
|
36
47
|
* Mauchly's test of sphericity of the measurements of each subject (a row of `y`), on the contrasts spanned by `M`
|
|
37
|
-
* and orthogonal to `X` (both formulas over `idata`). Equivalent to R's `mauchly.test(
|
|
38
|
-
* variants (`group` for `lm(y ~ group)`, `M`, `X`, `idata`).
|
|
48
|
+
* and orthogonal to `X` (both formulas over `idata`). Equivalent to R's `mauchly.test()`. As R's
|
|
49
|
+
* `mauchly.test(lm(y ~ 1), X = ~1)` and its variants (`group` for `lm(y ~ group)`, `M`, `X`, `idata`).
|
|
39
50
|
*
|
|
40
51
|
* @param y One row per subject, one column per measurement.
|
|
41
52
|
*
|
|
42
53
|
* @example
|
|
43
54
|
* ```ts
|
|
44
55
|
* const y = [[10.5, 9.8, 8.8], [11.2, 10.1, 9.9], [9.7, 9.5, 9.0], [10.9, 10.6, 9.1], [10.1, 9.2, 9.4]];
|
|
45
|
-
* mauchlyTest(y, { X: "~1" })
|
|
56
|
+
* const r = mauchlyTest(y, { X: "~1" });
|
|
57
|
+
* r.statistic; // 0.64938
|
|
58
|
+
* r.pValue; // 0.5233
|
|
46
59
|
* ```
|
|
60
|
+
*
|
|
61
|
+
* @category Linear models and ANOVA
|
|
47
62
|
*/
|
|
48
63
|
export declare function mauchlyTest(y: MatrixInput, options?: MauchlyTestOptions): MauchlyTestResult;
|
package/dist/models/mauchly.js
CHANGED
|
@@ -65,16 +65,20 @@ export function contrastCrossprod(S, mcols, xcols) {
|
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
67
|
* Mauchly's test of sphericity of the measurements of each subject (a row of `y`), on the contrasts spanned by `M`
|
|
68
|
-
* and orthogonal to `X` (both formulas over `idata`). Equivalent to R's `mauchly.test(
|
|
69
|
-
* variants (`group` for `lm(y ~ group)`, `M`, `X`, `idata`).
|
|
68
|
+
* and orthogonal to `X` (both formulas over `idata`). Equivalent to R's `mauchly.test()`. As R's
|
|
69
|
+
* `mauchly.test(lm(y ~ 1), X = ~1)` and its variants (`group` for `lm(y ~ group)`, `M`, `X`, `idata`).
|
|
70
70
|
*
|
|
71
71
|
* @param y One row per subject, one column per measurement.
|
|
72
72
|
*
|
|
73
73
|
* @example
|
|
74
74
|
* ```ts
|
|
75
75
|
* const y = [[10.5, 9.8, 8.8], [11.2, 10.1, 9.9], [9.7, 9.5, 9.0], [10.9, 10.6, 9.1], [10.1, 9.2, 9.4]];
|
|
76
|
-
* mauchlyTest(y, { X: "~1" })
|
|
76
|
+
* const r = mauchlyTest(y, { X: "~1" });
|
|
77
|
+
* r.statistic; // 0.64938
|
|
78
|
+
* r.pValue; // 0.5233
|
|
77
79
|
* ```
|
|
80
|
+
*
|
|
81
|
+
* @category Linear models and ANOVA
|
|
78
82
|
*/
|
|
79
83
|
export function mauchlyTest(y, options = {}) {
|
|
80
84
|
const rows = y.map((r) => Array.from(r));
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { Sample } from "../htest/types.js";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* A data frame: an object of columns of equal length. `null` (and `NaN` in a numeric column) is a missing value.
|
|
4
|
+
*
|
|
5
|
+
* @category Data frames
|
|
6
|
+
*/
|
|
3
7
|
export type DataFrame = Record<string, readonly (number | string | boolean | null)[] | Float64Array>;
|
|
4
8
|
/** A variable of a model frame: numeric values, or a factor (level index per row). */
|
|
5
9
|
export type FrameVariable = {
|