@numero-uno-jpn/rstats 1.11.0 → 1.13.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 +20 -0
- package/README.ja.md +7 -6
- package/README.md +7 -6
- package/dist/causal/balance.d.ts +118 -0
- package/dist/causal/balance.js +264 -0
- package/dist/causal/match.d.ts +86 -0
- package/dist/causal/match.js +155 -0
- package/dist/causal/scale.d.ts +8 -0
- package/dist/causal/scale.js +26 -0
- package/dist/causal/weightit.d.ts +159 -0
- package/dist/causal/weightit.js +491 -0
- package/dist/causal.js +2 -0
- package/dist/chunks/{chunk-ZW5OCUIY.js → chunk-24UIPUSE.js} +1 -1
- package/dist/chunks/{chunk-JD6JHIVV.js → chunk-2BXB6DRK.js} +1 -1
- package/dist/chunks/chunk-2N7PIFPJ.js +2 -0
- package/dist/chunks/{chunk-IRO6MXW2.js → chunk-2SOBG73L.js} +2 -2
- package/dist/chunks/{chunk-PF6LEBKO.js → chunk-3T4TVNXS.js} +2 -2
- package/dist/chunks/chunk-5Y425VAJ.js +2 -0
- package/dist/chunks/{chunk-3K5KP3M2.js → chunk-7V6FRF32.js} +2 -2
- package/dist/chunks/chunk-7ZKPFHAO.js +2 -0
- package/dist/chunks/{chunk-MJLK2RIQ.js → chunk-AH6IVJRF.js} +1 -1
- package/dist/chunks/{chunk-W27XXELU.js → chunk-C7NEFIAH.js} +2 -2
- package/dist/chunks/{chunk-OG5FTIWF.js → chunk-CAOEREY3.js} +2 -2
- package/dist/chunks/{chunk-HIPKYBZ3.js → chunk-CBAEKGP7.js} +2 -2
- package/dist/chunks/{chunk-6VIOWV53.js → chunk-CCYGRSQW.js} +2 -2
- package/dist/chunks/{chunk-46DELNLN.js → chunk-CEY7FHMZ.js} +2 -2
- package/dist/chunks/{chunk-Z6UI6ON2.js → chunk-CKQOWTX7.js} +2 -2
- package/dist/chunks/chunk-CSZCDPF3.js +2 -0
- package/dist/chunks/chunk-DQ2MIFRQ.js +2 -0
- package/dist/chunks/{chunk-XLBSUUWY.js → chunk-DZLOU6HK.js} +2 -2
- package/dist/chunks/{chunk-S5JZMHAW.js → chunk-EWJS4VYF.js} +2 -2
- package/dist/chunks/{chunk-KICTA3JY.js → chunk-GQ4HYF4L.js} +2 -2
- package/dist/chunks/chunk-GRXOLZ6J.js +2 -0
- package/dist/chunks/{chunk-BA32XHDA.js → chunk-IIKFLH2E.js} +2 -2
- package/dist/chunks/{chunk-M4SLTLOX.js → chunk-JJGRQ22M.js} +2 -2
- package/dist/chunks/chunk-JTARV4D3.js +2 -0
- package/dist/chunks/{chunk-OWOBNS75.js → chunk-KOF2HBUV.js} +2 -2
- package/dist/chunks/{chunk-BKH5GQNA.js → chunk-LS55YUPZ.js} +2 -2
- package/dist/chunks/chunk-M3LK23YR.js +2 -0
- package/dist/chunks/{chunk-BNHMQU7V.js → chunk-NRBTDLVW.js} +2 -2
- package/dist/chunks/{chunk-HCHZ6M34.js → chunk-NWWY2WMF.js} +2 -2
- package/dist/chunks/{chunk-TNU4SMV3.js → chunk-OVJU5WCL.js} +2 -2
- package/dist/chunks/{chunk-WHKUEZQI.js → chunk-OWD2GUXJ.js} +2 -2
- package/dist/chunks/{chunk-BZBPPRSH.js → chunk-P2UT6OCU.js} +2 -2
- package/dist/chunks/{chunk-XKQSKVCL.js → chunk-P6HSXCGZ.js} +2 -2
- package/dist/chunks/{chunk-YPDFCKL4.js → chunk-QWXSJEDI.js} +2 -2
- package/dist/chunks/{chunk-QRUQEJJ5.js → chunk-RGEW3YLB.js} +2 -2
- package/dist/chunks/{chunk-BGY7Y3P4.js → chunk-ST6I33C6.js} +1 -1
- package/dist/chunks/{chunk-W2D6IDBY.js → chunk-TQ4P6W3P.js} +2 -2
- package/dist/chunks/chunk-TTBKW2KL.js +2 -0
- package/dist/chunks/{chunk-EV7ZQC6D.js → chunk-UMLJFZJ5.js} +2 -2
- package/dist/chunks/{chunk-NO675KX4.js → chunk-VO65CBCV.js} +2 -2
- package/dist/chunks/{chunk-DMT423BG.js → chunk-XRB552MP.js} +2 -2
- package/dist/chunks/{chunk-JFZOKMZA.js → chunk-XRBNTYQW.js} +2 -2
- package/dist/chunks/{chunk-Y2SS3GX2.js → chunk-Y4UWFH4Q.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/causal.d.ts +3 -0
- package/dist/entries/causal.js +4 -0
- package/dist/entries/tests.d.ts +3 -0
- package/dist/entries/tests.js +3 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/clogit.d.ts +80 -0
- package/dist/models/clogit.js +211 -0
- package/dist/models/coxph.d.ts +9 -0
- package/dist/models/coxph.js +3 -3
- package/dist/models/survfitCoxph.d.ts +95 -0
- package/dist/models/survfitCoxph.js +223 -0
- package/dist/models/survival.d.ts +9 -2
- package/dist/models/survival.js +15 -11
- package/dist/models/svyglm.d.ts +77 -0
- package/dist/models/svyglm.js +102 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +18 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries.js +2 -2
- package/package.json +6 -1
- package/dist/chunks/chunk-HPWRADJM.js +0 -2
- package/dist/chunks/chunk-JWMVP7XO.js +0 -2
- package/dist/chunks/chunk-P23KCD67.js +0 -2
- package/dist/chunks/chunk-QY4OBLXK.js +0 -2
- package/dist/chunks/chunk-SV6SLUVB.js +0 -2
package/dist/models/survival.js
CHANGED
|
@@ -184,7 +184,7 @@ function pseudoQuadratic(V, u) {
|
|
|
184
184
|
* and lie below 0.5 + sqrt(eps), the first time, or, if the curve equals 0.5 (within sqrt(eps)) there and a later such
|
|
185
185
|
* point is lower, the midpoint between that time and the time of the first lower point. NaN if there is no such point.
|
|
186
186
|
*/
|
|
187
|
-
function curveMedian(time, curve) {
|
|
187
|
+
export function curveMedian(time, curve) {
|
|
188
188
|
const tol = Math.sqrt(Number.EPSILON);
|
|
189
189
|
const first = curve.findIndex((c) => c < 0.5 + tol);
|
|
190
190
|
if (first < 0)
|
|
@@ -221,7 +221,7 @@ function curveMedian(time, curve) {
|
|
|
221
221
|
*/
|
|
222
222
|
export function survfit(time, status, options = {}) {
|
|
223
223
|
checkOptions(options, SURVFIT_KEYS, "survfit"); // generated: option-keys.mjs
|
|
224
|
-
const { group, start: startTimes, id, weights, ctype = 1, stype = 1,
|
|
224
|
+
const { group, start: startTimes, id, weights, ctype = 1, stype = 1, rmean = "common", confType: confTypeInput = "log", confInt = 0.95 } = options;
|
|
225
225
|
if (typeof confTypeInput !== "string")
|
|
226
226
|
throw new TypeError("confType must be a string");
|
|
227
227
|
const confTypes = ["log", "log-log", "plain", "none", "logit", "arcsin"];
|
|
@@ -259,8 +259,15 @@ export function survfit(time, status, options = {}) {
|
|
|
259
259
|
throw new TypeError("'weights' must be an array");
|
|
260
260
|
if (weights !== undefined && weights.length !== time.length)
|
|
261
261
|
throw new RangeError("'weights' must have the same length as 'time'");
|
|
262
|
+
// R's default: the robust (infinitesimal jackknife) variance when a case weight is not an integer
|
|
263
|
+
const robust = options.robust ?? (weights !== undefined && Array.from(weights).some((w) => w != null && Number.isFinite(w) && !Number.isInteger(w)));
|
|
262
264
|
if (typeof robust !== "boolean")
|
|
263
265
|
throw new TypeError("'robust' must be a boolean");
|
|
266
|
+
// the robust exponential-hazard curve (stype 2) has been checked with integer weights only (its restricted-mean
|
|
267
|
+
// standard error is not right with fractional ones): kept out
|
|
268
|
+
if (robust && stype === 2 && weights !== undefined && Array.from(weights).some((w) => w != null && Number.isFinite(w) && !Number.isInteger(w))) {
|
|
269
|
+
throw new RangeError("non-integer weights with stype 2 and the robust variance are not supported");
|
|
270
|
+
}
|
|
264
271
|
if (typeof rmean !== "number" && typeof rmean !== "string")
|
|
265
272
|
throw new TypeError("rmean must be a number or string");
|
|
266
273
|
if (typeof rmean === "number" && !Number.isFinite(rmean))
|
|
@@ -280,8 +287,6 @@ export function survfit(time, status, options = {}) {
|
|
|
280
287
|
throw new RangeError("weights must be finite");
|
|
281
288
|
if (w < 0)
|
|
282
289
|
throw new RangeError("weights must be non-negative");
|
|
283
|
-
if (!Number.isSafeInteger(w))
|
|
284
|
-
throw new RangeError("weights must be non-negative safe integers");
|
|
285
290
|
if (time[i] != null && typeof time[i] !== "number")
|
|
286
291
|
throw new TypeError("'time' must contain numbers");
|
|
287
292
|
if (status[i] != null && typeof status[i] !== "number")
|
|
@@ -366,9 +371,6 @@ export function survfit(time, status, options = {}) {
|
|
|
366
371
|
let totalWeight = 0;
|
|
367
372
|
for (const o of rows) {
|
|
368
373
|
totalWeight += o.weight;
|
|
369
|
-
if (!Number.isSafeInteger(totalWeight)) {
|
|
370
|
-
throw new RangeError("total case weight per group must be a safe integer");
|
|
371
|
-
}
|
|
372
374
|
}
|
|
373
375
|
nRisk = totalWeight;
|
|
374
376
|
nUnweighted = rows.length;
|
|
@@ -396,8 +398,6 @@ export function survfit(time, status, options = {}) {
|
|
|
396
398
|
nRisk = active.reduce((sum, o) => sum + o.weight, 0);
|
|
397
399
|
nUnweighted = active.length;
|
|
398
400
|
uwRisk = nUnweighted;
|
|
399
|
-
if (!Number.isSafeInteger(nRisk))
|
|
400
|
-
throw new RangeError("total case weight at risk must be a safe integer");
|
|
401
401
|
}
|
|
402
402
|
const eventRowsAtTime = atTime.filter(o => o.event);
|
|
403
403
|
const censorRows = atTime.filter(o => !o.event && (allRowsById === undefined ||
|
|
@@ -430,9 +430,13 @@ export function survfit(time, status, options = {}) {
|
|
|
430
430
|
}
|
|
431
431
|
H += inc;
|
|
432
432
|
varH += incVar;
|
|
433
|
+
// (d / n) / (n - d): n (n - d) under- or overflows for weights near 1e-200 or 1e200
|
|
434
|
+
// every row still at risk has its event: the fraction is 1 exactly (sums of non-integer weights round, and
|
|
435
|
+
// d / nRisk could otherwise come out just above or below 1)
|
|
436
|
+
const frac = e === uwRisk && uwRisk > 0 ? 1 : d / nRisk;
|
|
433
437
|
if (stype === 1) {
|
|
434
|
-
S *= 1 -
|
|
435
|
-
green +=
|
|
438
|
+
S *= 1 - frac;
|
|
439
|
+
green += frac < 1 ? (d / nRisk) / (nRisk - d) : Infinity;
|
|
436
440
|
}
|
|
437
441
|
else
|
|
438
442
|
S = Math.exp(-H);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type GlmOptions, type GlmResult } from "./glm.js";
|
|
2
|
+
import type { MatrixInput } from "../linalg/matrix.js";
|
|
3
|
+
import type { Sample } from "../htest/types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Options for {@link svyglm}.
|
|
6
|
+
*
|
|
7
|
+
* @category Generalized linear models
|
|
8
|
+
*/
|
|
9
|
+
export interface SvyglmOptions {
|
|
10
|
+
/** The sampling (inverse probability) weights, one per row, positive. */
|
|
11
|
+
weights: Sample;
|
|
12
|
+
/** The family, as {@link glm} (default `"gaussian"`). */
|
|
13
|
+
family?: GlmOptions["family"];
|
|
14
|
+
/** The link, as {@link glm}. */
|
|
15
|
+
link?: GlmOptions["link"];
|
|
16
|
+
/** Convergence tolerance, as {@link glm} (default `1e-8`). */
|
|
17
|
+
epsilon?: number;
|
|
18
|
+
/** Maximum number of iterations, as {@link glm} (default `25`). */
|
|
19
|
+
maxit?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Result of {@link svyglm}: the rows of R's `summary(svyglm(...))$coefficients` and the fit.
|
|
23
|
+
*
|
|
24
|
+
* @category Generalized linear models
|
|
25
|
+
*/
|
|
26
|
+
export interface SvyglmResult {
|
|
27
|
+
/** Column names: `"(Intercept)"`, `"x1"`, `"x2"`, ... */
|
|
28
|
+
names: string[];
|
|
29
|
+
/** Coefficients (the weighted glm's). */
|
|
30
|
+
coefficients: number[];
|
|
31
|
+
/** Standard errors, from `vcov`. */
|
|
32
|
+
se: number[];
|
|
33
|
+
/** t statistics. */
|
|
34
|
+
tValue: number[];
|
|
35
|
+
/** Two-sided p-values from the t distribution with `dfResidual` degrees of freedom. */
|
|
36
|
+
pValue: number[];
|
|
37
|
+
/** The design-based variance matrix (array of rows): sandwich HC0 times n / (n - 1). */
|
|
38
|
+
vcov: number[][];
|
|
39
|
+
/** Residual degrees of freedom of the design, n - p. */
|
|
40
|
+
dfResidual: number;
|
|
41
|
+
/** The dispersion R prints: the weighted variance of the Pearson residuals (about their weighted mean) times
|
|
42
|
+
* n / (n - 1); it does not enter the standard errors. */
|
|
43
|
+
dispersion: number;
|
|
44
|
+
/** Deviance with the weights rescaled to mean 1 (R's). */
|
|
45
|
+
deviance: number;
|
|
46
|
+
/** Null deviance with the weights rescaled to mean 1 (R's). */
|
|
47
|
+
nullDeviance: number;
|
|
48
|
+
/** Fitted means. */
|
|
49
|
+
fittedValues: number[];
|
|
50
|
+
/** Linear predictors. */
|
|
51
|
+
linearPredictors: number[];
|
|
52
|
+
/** Iterations of the glm. */
|
|
53
|
+
iter: number;
|
|
54
|
+
/** Whether the glm converged. */
|
|
55
|
+
converged: boolean;
|
|
56
|
+
/** The weighted glm (weights rescaled to mean 1). */
|
|
57
|
+
glm: GlmResult;
|
|
58
|
+
/** R's warnings, each once. */
|
|
59
|
+
warnings: string[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A generalised linear model for weighted data with design-based (sandwich) standard errors. Equivalent to
|
|
63
|
+
* `survey::svyglm()` in R. The call followed is `summary(svyglm(y ~ ..., design, family))` for the design
|
|
64
|
+
* `svydesign(ids = ~1, weights = ~w, data)`, the design of inverse probability of treatment weighting.
|
|
65
|
+
*
|
|
66
|
+
* The coefficients are those of `glm(y ~ ..., weights = w)`; the variance is the HC0 sandwich of that fit times
|
|
67
|
+
* n / (n − 1), and the tests are t tests on n − p degrees of freedom. binomial and quasibinomial give the same table
|
|
68
|
+
* (binomial warns about the non-integer successes, as R). AIC is not given (R gives NA).
|
|
69
|
+
* @category Generalized linear models
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const fit = svyglm([[1, 0.2], [0, 1.1], [1, -0.4], [0, 0.3], [1, 0.9], [0, -1.2]], [1, 0, 1, 1, 0, 0],
|
|
73
|
+
* { weights: [1.5, 2, 1.2, 3, 1.1, 2.5], family: "quasibinomial" });
|
|
74
|
+
* fit.pValue; // [0.7799833144017342, 0.5461647344319471, 0.9579412581407588]
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function svyglm(x: MatrixInput | Sample, y: Sample, options: SvyglmOptions): SvyglmResult;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// survey's svyglm for the design of inverse probability weighting, svydesign(ids = ~1, weights = w): a glm with the
|
|
2
|
+
// weights rescaled to mean 1, the sandwich variance HC0 times n / (n - 1), and t tests on n - p degrees of freedom. The
|
|
3
|
+
// behaviour is in oracle/weightit-behaviour.md, from survey's documentation and published output; Lumley (2010),
|
|
4
|
+
// "Complex Surveys: A Guide to Analysis Using R", chapter 5. Written for rStats.js (no ported code).
|
|
5
|
+
import { glm } from "./glm.js";
|
|
6
|
+
import { vcovHC } from "./sandwich.js";
|
|
7
|
+
import { pt } from "../distributions/t.js";
|
|
8
|
+
import { reported, unreported } from "../internal/warnings.js";
|
|
9
|
+
import { ldexp } from "../internal/summary.js";
|
|
10
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
11
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
12
|
+
const SVYGLM_KEYS = ["weights", "family", "link", "epsilon", "maxit"];
|
|
13
|
+
const SVYGLM_FAMILY_KEYS = ["family", "link", "variance"];
|
|
14
|
+
const SVYGLM_FAMILY_LINK_KEYS = ["power"];
|
|
15
|
+
/**
|
|
16
|
+
* A generalised linear model for weighted data with design-based (sandwich) standard errors. Equivalent to
|
|
17
|
+
* `survey::svyglm()` in R. The call followed is `summary(svyglm(y ~ ..., design, family))` for the design
|
|
18
|
+
* `svydesign(ids = ~1, weights = ~w, data)`, the design of inverse probability of treatment weighting.
|
|
19
|
+
*
|
|
20
|
+
* The coefficients are those of `glm(y ~ ..., weights = w)`; the variance is the HC0 sandwich of that fit times
|
|
21
|
+
* n / (n − 1), and the tests are t tests on n − p degrees of freedom. binomial and quasibinomial give the same table
|
|
22
|
+
* (binomial warns about the non-integer successes, as R). AIC is not given (R gives NA).
|
|
23
|
+
* @category Generalized linear models
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const fit = svyglm([[1, 0.2], [0, 1.1], [1, -0.4], [0, 0.3], [1, 0.9], [0, -1.2]], [1, 0, 1, 1, 0, 0],
|
|
27
|
+
* { weights: [1.5, 2, 1.2, 3, 1.1, 2.5], family: "quasibinomial" });
|
|
28
|
+
* fit.pValue; // [0.7799833144017342, 0.5461647344319471, 0.9579412581407588]
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function svyglm(x, y, options) {
|
|
32
|
+
checkOptions(options, SVYGLM_KEYS, "svyglm"); // generated: option-keys.mjs
|
|
33
|
+
checkOptions(nestedOptions(options, "family"), SVYGLM_FAMILY_KEYS, "svyglm", "family"); // generated: option-keys.mjs
|
|
34
|
+
checkOptions(nestedOptions(options, "family.link"), SVYGLM_FAMILY_LINK_KEYS, "svyglm", "family.link"); // generated: option-keys.mjs
|
|
35
|
+
checkOptions(nestedOptions(options, "link"), SVYGLM_FAMILY_LINK_KEYS, "svyglm", "link"); // generated: option-keys.mjs
|
|
36
|
+
const { weights, family, link, epsilon, maxit } = options;
|
|
37
|
+
if (weights === undefined)
|
|
38
|
+
throw new RangeError("'weights' must be given");
|
|
39
|
+
const w = Array.from(weights);
|
|
40
|
+
if (w.length !== y.length)
|
|
41
|
+
throw new RangeError("'weights' must have the length of 'y'");
|
|
42
|
+
if (w.some((v) => !(v > 0) || !Number.isFinite(v)))
|
|
43
|
+
throw new RangeError("'weights' must be positive and finite");
|
|
44
|
+
if (family === "quasi" || (typeof family === "object" && family !== null && family.family === "quasi")) {
|
|
45
|
+
throw new RangeError("the quasi family is not available in svyglm (its variance function is not known here)");
|
|
46
|
+
}
|
|
47
|
+
const n = w.length;
|
|
48
|
+
const mean = w.reduce((a, v) => a + v, 0) / n;
|
|
49
|
+
// survey rescales the weights to mean 1: the deviance and glm's convergence test (|change| / (|dev| + 0.1)) are then R's
|
|
50
|
+
const scaled = w.map((v) => v / mean);
|
|
51
|
+
const fit = unreported(() => glm(x, y, {
|
|
52
|
+
weights: scaled, ...(family === undefined ? {} : { family }), ...(link === undefined ? {} : { link }),
|
|
53
|
+
...(epsilon === undefined ? {} : { epsilon }), ...(maxit === undefined ? {} : { maxit }),
|
|
54
|
+
}));
|
|
55
|
+
// the variance from a fit on the columns (and a gaussian identity-link response) divided by powers of 2, exactly:
|
|
56
|
+
// a coefficient's variance can be out of range (a column near 1e300 gives about 1e-600) while its standard error is
|
|
57
|
+
// not; the scaled fit gives the standard errors, t values and p-values, the variance matrix is mapped back
|
|
58
|
+
const rows = Array.isArray(x[0]) || ArrayBuffer.isView(x[0]) ? x.map((r) => Array.from(r))
|
|
59
|
+
: Array.from(x, (v) => [v]);
|
|
60
|
+
const exponent = (vals) => {
|
|
61
|
+
let top = 0;
|
|
62
|
+
for (let i = 0; i < vals.length; i++)
|
|
63
|
+
if (Number.isFinite(vals[i]) && Math.abs(vals[i]) > top)
|
|
64
|
+
top = Math.abs(vals[i]);
|
|
65
|
+
return top > 0 ? Math.floor(Math.log2(top)) : 0;
|
|
66
|
+
};
|
|
67
|
+
const ex = (rows[0] ?? []).map((_, j) => exponent(rows.map((r) => r[j])));
|
|
68
|
+
const fam = typeof family === "object" && family !== null ? family.family : family ?? "gaussian";
|
|
69
|
+
const identity = fam === "gaussian" && (link === undefined || link === "identity") &&
|
|
70
|
+
(typeof family !== "object" || family === null || family.link === undefined || family.link === "identity");
|
|
71
|
+
const ey = identity ? exponent(y) : 0;
|
|
72
|
+
const scaledFit = ex.every((e) => e === 0) && ey === 0 ? fit : unreported(() => glm(rows.map((r) => r.map((v, j) => ldexp(v, -ex[j]))), Array.from(y, (v) => ldexp(v, -ey)), { weights: scaled, ...(family === undefined ? {} : { family }), ...(link === undefined ? {} : { link }),
|
|
73
|
+
...(epsilon === undefined ? {} : { epsilon }), ...(maxit === undefined ? {} : { maxit }) }));
|
|
74
|
+
const hc0 = unreported(() => vcovHC(scaledFit, { type: "HC0" })).vcov;
|
|
75
|
+
// the exponent of each coefficient: 2^(ey - e_j), the intercept's 2^ey
|
|
76
|
+
const ec = [...(fit.coefficients.length > ex.length ? [ey] : []), ...ex.map((e) => ey - e)];
|
|
77
|
+
const vcov = hc0.map((row, j) => row.map((v, k) => ldexp((v * n) / (n - 1), ec[j] + ec[k])));
|
|
78
|
+
const p = fit.coefficients.length;
|
|
79
|
+
const dfResidual = n - p;
|
|
80
|
+
const se = hc0.map((row, j) => ldexp(Math.sqrt((row[j] * n) / (n - 1)), ec[j]));
|
|
81
|
+
const tValue = scaledFit.coefficients.map((b, j) => b / Math.sqrt((hc0[j][j] * n) / (n - 1)));
|
|
82
|
+
const pValue = tValue.map((t) => 2 * pt(-Math.abs(t), dfResidual));
|
|
83
|
+
// the dispersion: the design-based variance of the Pearson residuals (y - mu) / sqrt(V(mu)), weighted by the
|
|
84
|
+
// rescaled weights about their weighted mean, times n / (n - 1)
|
|
85
|
+
// from the scaled fit (a gaussian response divided by 2^ey), the result times 2^(2 ey)
|
|
86
|
+
const mu = scaledFit.fittedValues, yy = scaledFit.y;
|
|
87
|
+
const variance = fit.family === "binomial" || fit.family === "quasibinomial" ? (m) => m * (1 - m)
|
|
88
|
+
: fit.family === "poisson" || fit.family === "quasipoisson" ? (m) => m
|
|
89
|
+
: fit.family === "Gamma" ? (m) => m * m : fit.family === "inverse.gaussian" ? (m) => m * m * m : () => 1;
|
|
90
|
+
const pearson = mu.map((m, i) => (yy[i] - m) / Math.sqrt(variance(m)));
|
|
91
|
+
const rbar = pearson.reduce((a, r, i) => a + scaled[i] * r, 0) / n;
|
|
92
|
+
let swr = 0;
|
|
93
|
+
for (let i = 0; i < n; i++)
|
|
94
|
+
swr += scaled[i] * (pearson[i] - rbar) ** 2;
|
|
95
|
+
const dispersion = ldexp((swr / n) * (n / (n - 1)), 2 * ey);
|
|
96
|
+
return reported("svyglm", {
|
|
97
|
+
names: fit.model.columnNames.slice(), coefficients: fit.coefficients.slice(), se, tValue, pValue, vcov, dfResidual,
|
|
98
|
+
dispersion, deviance: fit.deviance, nullDeviance: fit.nullDeviance, fittedValues: fit.fittedValues.slice(),
|
|
99
|
+
linearPredictors: fit.linearPredictors.slice(), iter: fit.iter, converged: fit.converged, glm: fit,
|
|
100
|
+
warnings: fit.warnings.slice(),
|
|
101
|
+
});
|
|
102
|
+
}
|