@numero-uno-jpn/rstats 1.15.0 → 1.17.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 +22 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/dist/causal/mediate.d.ts +146 -0
- package/dist/causal/mediate.js +399 -0
- package/dist/causal/mediationProcedures.d.ts +231 -0
- package/dist/causal/mediationProcedures.js +240 -0
- package/dist/causal/moderation.d.ts +148 -0
- package/dist/causal/moderation.js +165 -0
- package/dist/causal.js +2 -2
- package/dist/chunks/{chunk-FD375XBW.js → chunk-23QCIPET.js} +2 -2
- package/dist/chunks/chunk-3UEYA6VG.js +2 -0
- package/dist/chunks/{chunk-SMA62635.js → chunk-3Y4DNK4O.js} +2 -2
- package/dist/chunks/{chunk-L7HJGM52.js → chunk-4BGGWQWS.js} +2 -2
- package/dist/chunks/chunk-5UFVACSV.js +2 -0
- package/dist/chunks/{chunk-5BCXHJPF.js → chunk-5W2OZAHQ.js} +2 -2
- package/dist/chunks/{chunk-PZRABNMZ.js → chunk-6MLDKHTY.js} +2 -2
- package/dist/chunks/{chunk-XVRRYHLM.js → chunk-72I7GDWL.js} +2 -2
- package/dist/chunks/chunk-BWDXJL7T.js +2 -0
- package/dist/chunks/{chunk-VO3E4Q6H.js → chunk-DD2GG4EC.js} +1 -1
- package/dist/chunks/{chunk-CIBAGTO2.js → chunk-EB45CDFA.js} +2 -2
- package/dist/chunks/chunk-EIUK6TKF.js +2 -0
- package/dist/chunks/chunk-GIIURG5X.js +2 -0
- package/dist/chunks/chunk-GLABB425.js +2 -0
- package/dist/chunks/{chunk-OO22LZ7Q.js → chunk-GOYTZ5RB.js} +2 -2
- package/dist/chunks/{chunk-HF4TT233.js → chunk-GTSK4IN4.js} +1 -1
- package/dist/chunks/{chunk-SNQABDAT.js → chunk-I2KJMF4G.js} +1 -1
- package/dist/chunks/chunk-JG4TWDQ4.js +2 -0
- package/dist/chunks/{chunk-UFJFVNC6.js → chunk-JHABMYNH.js} +1 -1
- package/dist/chunks/chunk-JV3POOET.js +2 -0
- package/dist/chunks/chunk-K3YSXDTU.js +2 -0
- package/dist/chunks/{chunk-R74QJUYU.js → chunk-KY53MB3J.js} +2 -2
- package/dist/chunks/{chunk-AGA7JCRF.js → chunk-L77QFL3C.js} +2 -2
- package/dist/chunks/chunk-LKKUUREA.js +2 -0
- package/dist/chunks/{chunk-WL3JGBLZ.js → chunk-LY2UZGUL.js} +2 -2
- package/dist/chunks/chunk-NCDPZSI2.js +2 -0
- package/dist/chunks/{chunk-6SZLUVSL.js → chunk-NCRL33PQ.js} +2 -2
- package/dist/chunks/{chunk-RHNGF7R7.js → chunk-NPWWGIFG.js} +2 -2
- package/dist/chunks/chunk-O4D43LWM.js +2 -0
- package/dist/chunks/chunk-PSQ6MLJM.js +2 -0
- package/dist/chunks/{chunk-M3HFLMXL.js → chunk-QCPGV7XH.js} +2 -2
- package/dist/chunks/{chunk-JAXYZJV7.js → chunk-RM5D7Q7W.js} +2 -2
- package/dist/chunks/{chunk-IMSXMJ43.js → chunk-RNGDVSTS.js} +2 -2
- package/dist/chunks/{chunk-3K47QLCY.js → chunk-SA2E5GVO.js} +2 -2
- package/dist/chunks/{chunk-VHMFOILD.js → chunk-T5ZALLTJ.js} +2 -2
- package/dist/chunks/{chunk-GBNGGBU3.js → chunk-TTOJDLIQ.js} +2 -2
- package/dist/chunks/{chunk-FV4ZQPRI.js → chunk-V54MBEJM.js} +2 -2
- package/dist/chunks/chunk-VZSQAGL2.js +3 -0
- package/dist/chunks/{chunk-NULJXDAR.js → chunk-XEJX5HBJ.js} +2 -2
- package/dist/chunks/chunk-XPYZOG5H.js +2 -0
- package/dist/chunks/{chunk-4EH3O466.js → chunk-XR7Y4ZV6.js} +2 -2
- package/dist/chunks/{chunk-JGZSGGMD.js → chunk-YMI33F7W.js} +2 -2
- package/dist/chunks/{chunk-MKCPS5QQ.js → chunk-ZRKXHVTJ.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 +3 -0
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +1 -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 +2 -2
- package/dist/models/betareg.d.ts +278 -0
- package/dist/models/betareg.js +588 -0
- package/dist/models/fitOptions.js +6 -4
- package/dist/models/loglin.d.ts +186 -0
- package/dist/models/loglin.js +349 -0
- package/dist/models/survreg.d.ts +43 -0
- package/dist/models/survreg.js +6 -3
- package/dist/models/tobit.d.ts +132 -0
- package/dist/models/tobit.js +172 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/random/mvrnorm.d.ts +60 -0
- package/dist/random/mvrnorm.js +237 -0
- package/dist/regression.js +3 -3
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-5JKKIVYL.js +0 -3
- package/dist/chunks/chunk-5Q5YVM7S.js +0 -2
- package/dist/chunks/chunk-AILN2NGE.js +0 -2
- package/dist/chunks/chunk-APOF7BLH.js +0 -2
- package/dist/chunks/chunk-HSVVCFEN.js +0 -2
- package/dist/chunks/chunk-KQGH6SEW.js +0 -2
- package/dist/chunks/chunk-OBTCIG5D.js +0 -2
- package/dist/chunks/chunk-P4TKSL4W.js +0 -2
- package/dist/chunks/chunk-RV4JZHTE.js +0 -2
- package/dist/chunks/chunk-U5UOVXKD.js +0 -2
- package/dist/chunks/chunk-VHBIWPB4.js +0 -2
- package/dist/chunks/chunk-YYLBBEWZ.js +0 -2
- package/dist/chunks/chunk-ZDUVFNFH.js +0 -2
- package/dist/chunks/chunk-ZUTVNRML.js +0 -2
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { type DesignContrast } from "./modelMatrix.js";
|
|
2
|
+
import type { DataFrame } from "./modelFrame.js";
|
|
3
|
+
import { type SurvregDistribution, type SurvregResult, type SurvregControl } from "./survreg.js";
|
|
4
|
+
import type { Sample } from "../htest/types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Options for {@link tobit}.
|
|
7
|
+
* @category Regression
|
|
8
|
+
*/
|
|
9
|
+
export interface TobitOptions {
|
|
10
|
+
/** Left limit: responses at or below it are left censored at it (default 0; `-Infinity` for none). */
|
|
11
|
+
left?: number;
|
|
12
|
+
/** Right limit: responses at or above it are right censored at it (default `Infinity`, none). */
|
|
13
|
+
right?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The error distribution (survreg's): `"gaussian"` (default, the classical tobit model), `"logistic"`, `"t"`,
|
|
16
|
+
* `"extreme"`, or one for positive responses (`"weibull"`, `"exponential"`, `"rayleigh"`, `"lognormal"` /
|
|
17
|
+
* `"loggaussian"`, `"loglogistic"`), where the limits are on the scale of the response.
|
|
18
|
+
*/
|
|
19
|
+
dist?: SurvregDistribution;
|
|
20
|
+
/** Degrees of freedom of the t distribution (default 4, at least 3); `parms` is the same (give one). */
|
|
21
|
+
df?: number;
|
|
22
|
+
/** R's name for `df` (tobit and survreg take the degrees of freedom of the t distribution as `parms`). */
|
|
23
|
+
parms?: number;
|
|
24
|
+
/** Case weights, one per row of `data` (positive), or the name of a column of `data`. */
|
|
25
|
+
weights?: Sample | string;
|
|
26
|
+
/** A contrast per factor of the formula (default treatment contrasts). */
|
|
27
|
+
contrasts?: Record<string, DesignContrast>;
|
|
28
|
+
/** Iteration control, as {@link survreg}'s. */
|
|
29
|
+
control?: SurvregControl;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Result of {@link tobit}: {@link survreg}'s result with the coefficients named after the formula's terms, and the
|
|
33
|
+
* censoring.
|
|
34
|
+
* @category Regression
|
|
35
|
+
*/
|
|
36
|
+
export interface TobitResult extends SurvregResult {
|
|
37
|
+
/** The formula. */
|
|
38
|
+
formula: string;
|
|
39
|
+
/** The left limit used. */
|
|
40
|
+
left: number;
|
|
41
|
+
/** The right limit used. */
|
|
42
|
+
right: number;
|
|
43
|
+
/** Observations: total, left censored, uncensored, right censored. */
|
|
44
|
+
counts: TobitCounts;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The censoring counts of a {@link tobit} fit.
|
|
48
|
+
* @category Regression
|
|
49
|
+
*/
|
|
50
|
+
export interface TobitCounts {
|
|
51
|
+
/** Observations used. */
|
|
52
|
+
total: number;
|
|
53
|
+
/** Left censored (at or below the left limit). */
|
|
54
|
+
left: number;
|
|
55
|
+
/** Uncensored. */
|
|
56
|
+
uncensored: number;
|
|
57
|
+
/** Right censored (at or above the right limit). */
|
|
58
|
+
right: number;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Tobit regression: a linear model for a response censored at a left and/or right limit, fitted by maximum likelihood
|
|
62
|
+
* with {@link survreg} on the response clipped to [left, right]. Equivalent to R's `AER::tobit()`.
|
|
63
|
+
*
|
|
64
|
+
* A response at or below `left` is left censored at `left`, at or above `right` right censored at `right`. The
|
|
65
|
+
* classical tobit model is the default (left censoring at 0, gaussian errors). `loglik` holds the log-likelihoods of
|
|
66
|
+
* the intercept-only model and of the model (McFadden's R² is `1 - loglik[1] / loglik[0]`), and `chi`, `chiDf`, `chiP`
|
|
67
|
+
* their likelihood-ratio test.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const fit = tobit("y ~ x", { y: [0, 0, 1.2, 0.4, 2.5, 0, 3.1, 1.8], x: [1, 2, 3, 2, 5, 1, 6, 4] });
|
|
71
|
+
* fit.counts; // { total: 8, left: 3, uncensored: 5, right: 0 }
|
|
72
|
+
* ```
|
|
73
|
+
* @category Regression
|
|
74
|
+
*/
|
|
75
|
+
export declare function tobit(formula: string, data: DataFrame, options?: TobitOptions): TobitResult;
|
|
76
|
+
/**
|
|
77
|
+
* Result of {@link tobitSummary}.
|
|
78
|
+
* @category Regression
|
|
79
|
+
*/
|
|
80
|
+
export interface TobitSummaryResult {
|
|
81
|
+
/** Row names of the table: the coefficients, then `Log(scale)` (when the scale is estimated). */
|
|
82
|
+
names: string[];
|
|
83
|
+
/** Estimates. */
|
|
84
|
+
estimate: number[];
|
|
85
|
+
/** Standard errors. */
|
|
86
|
+
se: number[];
|
|
87
|
+
/** Wald z statistics. */
|
|
88
|
+
z: number[];
|
|
89
|
+
/** Two-sided p-values. */
|
|
90
|
+
p: number[];
|
|
91
|
+
/** The scale. */
|
|
92
|
+
scale: number;
|
|
93
|
+
/** Log-likelihood of the model. */
|
|
94
|
+
loglik: number;
|
|
95
|
+
/** Its degrees of freedom (coefficients and scale). */
|
|
96
|
+
df: number;
|
|
97
|
+
/** Observations: total, left censored, uncensored, right censored. */
|
|
98
|
+
counts: TobitCounts;
|
|
99
|
+
/**
|
|
100
|
+
* Wald test of the coefficients other than the intercept: `b' V⁻¹ b`, on the number of coefficients minus 1 degrees
|
|
101
|
+
* of freedom (as R; without an intercept R tests all coefficients on one degree of freedom too few). `null` for the
|
|
102
|
+
* intercept-only model.
|
|
103
|
+
*/
|
|
104
|
+
wald: TobitWald | null;
|
|
105
|
+
/** Iterations. */
|
|
106
|
+
iter: number;
|
|
107
|
+
/** The distribution. */
|
|
108
|
+
dist: SurvregDistribution;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The Wald test of {@link tobitSummary}.
|
|
112
|
+
* @category Regression
|
|
113
|
+
*/
|
|
114
|
+
export interface TobitWald {
|
|
115
|
+
/** `b' V⁻¹ b` over the coefficients other than the intercept. */
|
|
116
|
+
statistic: number;
|
|
117
|
+
/** Degrees of freedom: the number of coefficients minus 1. */
|
|
118
|
+
df: number;
|
|
119
|
+
/** Upper chi-squared tail probability. */
|
|
120
|
+
p: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Summary of a {@link tobit} fit: the Wald table with the scale and the Wald test of the regression. Equivalent to
|
|
124
|
+
* `AER::summary.tobit()` in R.
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const fit = tobit("y ~ x", { y: [0, 0, 1.2, 0.4, 2.5, 0, 3.1, 1.8], x: [1, 2, 3, 2, 5, 1, 6, 4] });
|
|
128
|
+
* tobitSummary(fit).wald; // { statistic: 215.5, df: 1, p: 8.598e-49 }
|
|
129
|
+
* ```
|
|
130
|
+
* @category Regression
|
|
131
|
+
*/
|
|
132
|
+
export declare function tobitSummary(fit: TobitResult): TobitSummaryResult;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Tobit regression: AER's tobit, a formula entry point to survreg with the response censored at fixed limits.
|
|
2
|
+
// Behaviour: oracle/tobit-betareg-loglin-behaviour.md (clean room: from AER's documentation and the output of R; no
|
|
3
|
+
// source of R or of a package was read). Written for rStats.js.
|
|
4
|
+
import { pchisq } from "../distributions/chisq.js";
|
|
5
|
+
import { parseFormula } from "./formula.js";
|
|
6
|
+
import { design } from "./modelMatrix.js";
|
|
7
|
+
import { survreg } from "./survreg.js";
|
|
8
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
9
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
10
|
+
const TOBIT_KEYS = ["left", "right", "dist", "df", "parms", "weights", "contrasts", "control"];
|
|
11
|
+
const TOBIT_CONTROL_KEYS = ["maxiter", "relTolerance", "tolerChol"];
|
|
12
|
+
/**
|
|
13
|
+
* Tobit regression: a linear model for a response censored at a left and/or right limit, fitted by maximum likelihood
|
|
14
|
+
* with {@link survreg} on the response clipped to [left, right]. Equivalent to R's `AER::tobit()`.
|
|
15
|
+
*
|
|
16
|
+
* A response at or below `left` is left censored at `left`, at or above `right` right censored at `right`. The
|
|
17
|
+
* classical tobit model is the default (left censoring at 0, gaussian errors). `loglik` holds the log-likelihoods of
|
|
18
|
+
* the intercept-only model and of the model (McFadden's R² is `1 - loglik[1] / loglik[0]`), and `chi`, `chiDf`, `chiP`
|
|
19
|
+
* their likelihood-ratio test.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const fit = tobit("y ~ x", { y: [0, 0, 1.2, 0.4, 2.5, 0, 3.1, 1.8], x: [1, 2, 3, 2, 5, 1, 6, 4] });
|
|
23
|
+
* fit.counts; // { total: 8, left: 3, uncensored: 5, right: 0 }
|
|
24
|
+
* ```
|
|
25
|
+
* @category Regression
|
|
26
|
+
*/
|
|
27
|
+
export function tobit(formula, data, options = {}) {
|
|
28
|
+
checkOptions(options, TOBIT_KEYS, "tobit"); // generated: option-keys.mjs
|
|
29
|
+
checkOptions(nestedOptions(options, "control"), TOBIT_CONTROL_KEYS, "tobit", "control"); // generated: option-keys.mjs
|
|
30
|
+
const left = options.left ?? 0, right = options.right ?? Infinity;
|
|
31
|
+
if (Number.isNaN(left) || Number.isNaN(right) || !(left < right))
|
|
32
|
+
throw new RangeError("all(left < right) is not TRUE");
|
|
33
|
+
const parsed = parseFormula(formula);
|
|
34
|
+
if (parsed.response === undefined)
|
|
35
|
+
throw new TypeError("the formula must have a response");
|
|
36
|
+
const designOptions = {};
|
|
37
|
+
if (options.weights !== undefined)
|
|
38
|
+
designOptions.weights = options.weights;
|
|
39
|
+
if (options.contrasts !== undefined)
|
|
40
|
+
designOptions.contrasts = options.contrasts;
|
|
41
|
+
const d = design(parsed, data, designOptions);
|
|
42
|
+
const y = d.y;
|
|
43
|
+
if (y === undefined)
|
|
44
|
+
throw new TypeError("the response must be numeric");
|
|
45
|
+
const intercept = d.terms.intercept;
|
|
46
|
+
const x = d.x.map((row) => (intercept ? row.slice(1) : row.slice()));
|
|
47
|
+
// the clipped response as interval2 data: left censored (-Inf, left], right censored [right, Inf), exact y
|
|
48
|
+
const lo = [], hi = [];
|
|
49
|
+
let nLeft = 0, nRight = 0;
|
|
50
|
+
for (const v of y) {
|
|
51
|
+
if (v <= left) {
|
|
52
|
+
lo.push(-Infinity);
|
|
53
|
+
hi.push(left);
|
|
54
|
+
nLeft++;
|
|
55
|
+
}
|
|
56
|
+
else if (v >= right) {
|
|
57
|
+
lo.push(right);
|
|
58
|
+
hi.push(Infinity);
|
|
59
|
+
nRight++;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
lo.push(v);
|
|
63
|
+
hi.push(v);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const sOptions = { type: "interval2", time2: hi, dist: options.dist ?? "gaussian", intercept };
|
|
67
|
+
if (options.df !== undefined)
|
|
68
|
+
sOptions.df = options.df;
|
|
69
|
+
if (options.parms !== undefined)
|
|
70
|
+
sOptions.parms = options.parms;
|
|
71
|
+
if (d.frame.weights !== undefined)
|
|
72
|
+
sOptions.weights = d.frame.weights;
|
|
73
|
+
if (options.control !== undefined)
|
|
74
|
+
sOptions.control = options.control;
|
|
75
|
+
// a location-scale distribution is fitted to the response divided by a power of 2 near its size, and the result
|
|
76
|
+
// scaled back exactly: survreg's iterations fail for responses near 1e-150 (R's do not; log 29). Not for responses
|
|
77
|
+
// of ordinary size, so that the iterations stay R's
|
|
78
|
+
const dist = sOptions.dist;
|
|
79
|
+
let top = 0;
|
|
80
|
+
for (const v of [...lo, ...hi])
|
|
81
|
+
if (Number.isFinite(v))
|
|
82
|
+
top = Math.max(top, Math.abs(v));
|
|
83
|
+
const e = ["gaussian", "logistic", "t", "extreme"].includes(dist) && top > 0 ? Math.floor(Math.log2(top)) : 0;
|
|
84
|
+
const scaled = Math.abs(e) > 30;
|
|
85
|
+
const sc = scaled ? 2 ** e : 1;
|
|
86
|
+
const fit = scaled ? unscale(survreg(lo.map((v) => v / sc), undefined, x, { ...sOptions, time2: hi.map((v) => v / sc) }), sc, lo, hi) : survreg(lo, undefined, x, sOptions);
|
|
87
|
+
const names = d.colNames.slice();
|
|
88
|
+
const rowNames = [...names, ...fit.tableRowNames.slice(names.length)];
|
|
89
|
+
return {
|
|
90
|
+
...fit, coefNames: names, tableRowNames: rowNames, formula, left, right,
|
|
91
|
+
counts: { total: y.length, left: nLeft, uncensored: y.length - nLeft - nRight, right: nRight },
|
|
92
|
+
warnings: [...d.warnings, ...fit.warnings],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Summary of a {@link tobit} fit: the Wald table with the scale and the Wald test of the regression. Equivalent to
|
|
97
|
+
* `AER::summary.tobit()` in R.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const fit = tobit("y ~ x", { y: [0, 0, 1.2, 0.4, 2.5, 0, 3.1, 1.8], x: [1, 2, 3, 2, 5, 1, 6, 4] });
|
|
101
|
+
* tobitSummary(fit).wald; // { statistic: 215.5, df: 1, p: 8.598e-49 }
|
|
102
|
+
* ```
|
|
103
|
+
* @category Regression
|
|
104
|
+
*/
|
|
105
|
+
export function tobitSummary(fit) {
|
|
106
|
+
const p = fit.coefficients.length;
|
|
107
|
+
const keep = fit.coefNames.map((nm, j) => (nm === "(Intercept)" ? -1 : j)).filter((j) => j >= 0 && Number.isFinite(fit.coefficients[j]));
|
|
108
|
+
let wald = null;
|
|
109
|
+
if (keep.length > 0 && p > 1) {
|
|
110
|
+
const V = keep.map((a) => keep.map((b) => fit.var[a][b]));
|
|
111
|
+
const b = keep.map((j) => fit.coefficients[j]);
|
|
112
|
+
const sol = solveSymmetric(V, b);
|
|
113
|
+
const statistic = b.reduce((s, v, i) => s + v * sol[i], 0);
|
|
114
|
+
const df = p - 1;
|
|
115
|
+
wald = { statistic, df, p: pchisq(statistic, df, { lowerTail: false }) };
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
names: fit.tableRowNames.slice(), estimate: fit.value.slice(), se: fit.se.slice(), z: fit.z.slice(), p: fit.p.slice(),
|
|
119
|
+
scale: fit.scale[0], loglik: fit.loglik[1], df: fit.df, counts: { ...fit.counts }, wald, iter: fit.iter, dist: fit.dist,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
// Solves V s = b for a symmetric positive definite V (Cholesky); NaN when V is not.
|
|
123
|
+
function solveSymmetric(V, b) {
|
|
124
|
+
const n = b.length, L = V.map(() => new Array(n).fill(0));
|
|
125
|
+
for (let i = 0; i < n; i++)
|
|
126
|
+
for (let j = 0; j <= i; j++) {
|
|
127
|
+
let s = V[i][j];
|
|
128
|
+
for (let k = 0; k < j; k++)
|
|
129
|
+
s -= L[i][k] * L[j][k];
|
|
130
|
+
if (i === j) {
|
|
131
|
+
if (!(s > 0))
|
|
132
|
+
return new Array(n).fill(NaN);
|
|
133
|
+
L[i][i] = Math.sqrt(s);
|
|
134
|
+
}
|
|
135
|
+
else
|
|
136
|
+
L[i][j] = s / L[j][j];
|
|
137
|
+
}
|
|
138
|
+
const z = new Array(n);
|
|
139
|
+
for (let i = 0; i < n; i++) {
|
|
140
|
+
let s = b[i];
|
|
141
|
+
for (let k = 0; k < i; k++)
|
|
142
|
+
s -= L[i][k] * z[k];
|
|
143
|
+
z[i] = s / L[i][i];
|
|
144
|
+
}
|
|
145
|
+
const s = new Array(n);
|
|
146
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
147
|
+
let t = z[i];
|
|
148
|
+
for (let k = i + 1; k < n; k++)
|
|
149
|
+
t -= L[k][i] * s[k];
|
|
150
|
+
s[i] = t / L[i][i];
|
|
151
|
+
}
|
|
152
|
+
return s;
|
|
153
|
+
}
|
|
154
|
+
// The fit of y / s taken back to y: coefficients, scales and standard errors times s, log scale plus log s, the
|
|
155
|
+
// log-likelihoods minus log s per exact observation (weighted), z and p unchanged.
|
|
156
|
+
function unscale(f, s, lo, hi) {
|
|
157
|
+
const p = f.coefficients.length, ls = Math.log(s);
|
|
158
|
+
let exact = 0;
|
|
159
|
+
f.rows.forEach((r, i) => { if (lo[r] === hi[r])
|
|
160
|
+
exact += f.weights[i]; });
|
|
161
|
+
const isCoef = (j) => j < p;
|
|
162
|
+
const value = f.value.map((v, j) => (isCoef(j) ? v * s : v + ls)), se = f.se.map((v, j) => (isCoef(j) ? v * s : v));
|
|
163
|
+
const icoef = f.icoef.map((v, j) => (j === 0 ? v * s : v + ls));
|
|
164
|
+
return {
|
|
165
|
+
...f, coefficients: f.coefficients.map((v) => v * s), scale: f.scale.map((v) => v * s),
|
|
166
|
+
var: f.var.map((row, a) => row.map((v, b) => v * (isCoef(a) ? s : 1) * (isCoef(b) ? s : 1))),
|
|
167
|
+
loglik: [f.loglik[0] - exact * ls, f.loglik[1] - exact * ls], icoef, value, se,
|
|
168
|
+
table: { value, se, z: f.z.slice(), p: f.p.slice() }, linearPredictors: f.linearPredictors.map((v) => v * s),
|
|
169
|
+
score: f.score.map((v, j) => (isCoef(j) ? v / s : v)), y: f.y.map((v) => v * s),
|
|
170
|
+
lower: f.lower.map((v) => v * s), upper: f.upper.map((v) => v * s),
|
|
171
|
+
};
|
|
172
|
+
}
|