@numero-uno-jpn/rstats 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +99 -0
- package/README.ja.md +47 -14
- package/README.md +49 -15
- package/THIRD_PARTY_NOTICES.md +35 -0
- package/dist/chunks/chunk-24U4DKRY.js +2 -0
- package/dist/chunks/{chunk-J3SNI7KY.js → chunk-5K4PS7IV.js} +2 -2
- package/dist/chunks/{chunk-MVG5LUZU.js → chunk-BGVMSDVJ.js} +2 -2
- package/dist/chunks/chunk-FC2TLHZC.js +2 -0
- package/dist/chunks/{chunk-ZW52Z3GY.js → chunk-H2AP6TCR.js} +1 -1
- package/dist/chunks/chunk-IVOBWYLV.js +2 -0
- package/dist/chunks/{chunk-UIOAWLAN.js → chunk-JLSTEMSN.js} +1 -1
- package/dist/chunks/chunk-LE7TBHNI.js +2 -0
- package/dist/chunks/{chunk-ZYA4ISAT.js → chunk-LWUSYIQ4.js} +1 -1
- package/dist/chunks/{chunk-AFKYWGKN.js → chunk-MF4VBCHQ.js} +2 -2
- package/dist/chunks/{chunk-SXSJXDD3.js → chunk-MSAUU6PY.js} +2 -2
- package/dist/chunks/{chunk-XYXHHF5L.js → chunk-TOVTN5Y2.js} +1 -1
- package/dist/chunks/{chunk-JCZAB6C5.js → chunk-YC4JWUKG.js} +2 -2
- package/dist/chunks/{chunk-QNQJA6KZ.js → chunk-ZRO446BP.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +21 -5
- package/dist/distributions.js +2 -2
- package/dist/entries/distributions.d.ts +2 -1
- package/dist/entries/distributions.js +2 -1
- package/dist/entries/tests.d.ts +4 -0
- package/dist/entries/tests.js +4 -0
- package/dist/htest/brunnerMunzelTest.d.ts +28 -0
- package/dist/htest/brunnerMunzelTest.js +86 -0
- package/dist/linalg.js +2 -2
- package/dist/models/glm.d.ts +226 -0
- package/dist/models/glm.js +546 -0
- package/dist/models/glmAnova.d.ts +31 -0
- package/dist/models/glmAnova.js +146 -0
- package/dist/models/glmConfint.d.ts +25 -0
- package/dist/models/glmConfint.js +139 -0
- package/dist/models/glmFamily.d.ts +66 -0
- package/dist/models/glmFamily.js +359 -0
- package/dist/numerics.js +2 -2
- package/dist/random/lecuyer.d.ts +20 -0
- package/dist/random/lecuyer.js +71 -0
- package/dist/random/rng.d.ts +78 -11
- package/dist/random/rng.js +128 -26
- package/dist/random/sample.js +24 -7
- package/dist/random/streams.d.ts +34 -0
- package/dist/random/streams.js +75 -0
- package/dist/rstats.js +4 -3
- package/dist/special/igam.d.ts +1 -0
- package/dist/special/igam.js +1 -1
- package/dist/special/incbet.js +487 -6
- package/dist/special/incbetLargeA.d.ts +15 -0
- package/dist/special/incbetLargeA.js +200 -0
- package/dist/special/incbetTemme.d.ts +19 -0
- package/dist/special/incbetTemme.js +216 -0
- package/dist/special/loader.js +151 -118
- package/dist/special.js +2 -2
- package/dist/tests.js +4 -3
- package/package.json +1 -1
- package/dist/chunks/chunk-6R7YDHA2.js +0 -2
- package/dist/chunks/chunk-ANYGLFT4.js +0 -2
- package/dist/chunks/chunk-BI5RSUZ2.js +0 -2
- package/dist/chunks/chunk-PTPPP4TI.js +0 -2
- package/dist/chunks/chunk-YKN6OGJL.js +0 -2
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { MatrixInput } from "../linalg/matrix.js";
|
|
2
|
+
import { type QR } from "../linalg/qr.js";
|
|
3
|
+
import type { Sample } from "../htest/types.js";
|
|
4
|
+
import { Warnings, type Family, type GlmFamilyName, type GlmFamilySpec, type GlmLink } from "./glmFamily.js";
|
|
5
|
+
export type { GlmFamilyName, GlmFamilySpec, GlmLink, GlmLinkName, QuasiVariance } from "./glmFamily.js";
|
|
6
|
+
/** Options for {@link glm}. */
|
|
7
|
+
export interface GlmOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The family: a name (`"gaussian"`, the default, `"binomial"`, `"quasibinomial"`, `"poisson"`, `"quasipoisson"`,
|
|
10
|
+
* `"Gamma"`, `"inverse.gaussian"`, `"quasi"`) or an object with the link and, for `quasi`, the variance function,
|
|
11
|
+
* as R's `quasi(link = "log", variance = "mu^2")`.
|
|
12
|
+
*/
|
|
13
|
+
family?: GlmFamilyName | GlmFamilySpec;
|
|
14
|
+
/** The link (overrides the family object's): a name such as `"logit"` or `{ power: lambda }` (quasi only). */
|
|
15
|
+
link?: GlmLink;
|
|
16
|
+
/** Prior weights (non-negative). */
|
|
17
|
+
weights?: Sample;
|
|
18
|
+
/** Offset added to the linear predictor. */
|
|
19
|
+
offset?: Sample;
|
|
20
|
+
/** Starting coefficients (intercept first). */
|
|
21
|
+
start?: Sample;
|
|
22
|
+
/** Convergence tolerance on the relative change of the deviance (R's `glm.control(epsilon)`). Default `1e-8`. */
|
|
23
|
+
epsilon?: number;
|
|
24
|
+
/** Maximum number of iterations. Default `25`. */
|
|
25
|
+
maxit?: number;
|
|
26
|
+
/** Include an intercept (a column of ones before the columns of `x`). Default `true`. */
|
|
27
|
+
intercept?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** The data and settings of a fit, kept for the refits of {@link glmConfint} and {@link glmAnova}. */
|
|
30
|
+
export interface GlmModel {
|
|
31
|
+
/** Model matrix (with the column of ones), rows with a missing value dropped. */
|
|
32
|
+
x: number[][];
|
|
33
|
+
/** Column names: `"(Intercept)"`, `"x1"`, `"x2"`, ... */
|
|
34
|
+
columnNames: string[];
|
|
35
|
+
/** Response as given (a vector, or rows of successes and failures), rows with a missing value dropped. */
|
|
36
|
+
yGiven: number[] | number[][];
|
|
37
|
+
/** Prior weights as given (1 when not given). */
|
|
38
|
+
priorGiven: number[];
|
|
39
|
+
offset: number[];
|
|
40
|
+
/** Whether an offset was given. */
|
|
41
|
+
hasOffset: boolean;
|
|
42
|
+
intercept: boolean;
|
|
43
|
+
familySpec: GlmFamilySpec;
|
|
44
|
+
epsilon: number;
|
|
45
|
+
maxit: number;
|
|
46
|
+
start: number[] | undefined;
|
|
47
|
+
}
|
|
48
|
+
/** Result of {@link glm}: R's `glm` object. */
|
|
49
|
+
export interface GlmResult {
|
|
50
|
+
/** Coefficients (intercept first); NaN for aliased columns. */
|
|
51
|
+
coefficients: number[];
|
|
52
|
+
/** Fitted means, one per row (rows with a missing value dropped). */
|
|
53
|
+
fittedValues: number[];
|
|
54
|
+
linearPredictors: number[];
|
|
55
|
+
deviance: number;
|
|
56
|
+
nullDeviance: number;
|
|
57
|
+
/** AIC; NaN for the quasi families. */
|
|
58
|
+
aic: number;
|
|
59
|
+
rank: number;
|
|
60
|
+
dfResidual: number;
|
|
61
|
+
dfNull: number;
|
|
62
|
+
/** Number of iterations. */
|
|
63
|
+
iter: number;
|
|
64
|
+
converged: boolean;
|
|
65
|
+
/** Whether the step was truncated (halved) in the last iteration. */
|
|
66
|
+
boundary: boolean;
|
|
67
|
+
/** The response as the family reads it (proportions for a two-column binomial response). */
|
|
68
|
+
y: number[];
|
|
69
|
+
/** Prior weights after the response was read (multiplied by the sizes for a two-column binomial response). */
|
|
70
|
+
priorWeights: number[];
|
|
71
|
+
/** Working weights of the last least squares step (0 for rows not used). */
|
|
72
|
+
weights: number[];
|
|
73
|
+
/** Working residuals. */
|
|
74
|
+
residuals: number[];
|
|
75
|
+
/** The family's name and the link's name (`"mu^0.333"` for a power link). */
|
|
76
|
+
family: GlmFamilyName;
|
|
77
|
+
link: string;
|
|
78
|
+
/** R's warnings, each once, in order of first appearance. */
|
|
79
|
+
warnings: string[];
|
|
80
|
+
/** QR decomposition of the last weighted least squares problem (undefined for a model without columns). */
|
|
81
|
+
qr: QR | undefined;
|
|
82
|
+
model: GlmModel;
|
|
83
|
+
}
|
|
84
|
+
/** Input of the iteratively reweighted least squares (after the response has been read) */
|
|
85
|
+
interface IrlsInput {
|
|
86
|
+
x: number[][];
|
|
87
|
+
p: number;
|
|
88
|
+
y: number[];
|
|
89
|
+
w: number[];
|
|
90
|
+
offset: number[];
|
|
91
|
+
fam: Family;
|
|
92
|
+
epsilon: number;
|
|
93
|
+
maxit: number;
|
|
94
|
+
columnNames: string[];
|
|
95
|
+
start?: number[] | undefined;
|
|
96
|
+
etastart?: number[] | undefined;
|
|
97
|
+
mustart: number[];
|
|
98
|
+
/**
|
|
99
|
+
* Stop when the coefficients stop changing (max |change| <= 8 eps max |b|) instead of R's rule on the deviance,
|
|
100
|
+
* which is stationary at the estimate (used by the profile fits of glmConfint)
|
|
101
|
+
*/
|
|
102
|
+
untilCoefficients?: boolean | undefined;
|
|
103
|
+
}
|
|
104
|
+
/** Result of the iterations */
|
|
105
|
+
export interface IrlsFit {
|
|
106
|
+
coefficients: number[];
|
|
107
|
+
eta: number[];
|
|
108
|
+
mu: number[];
|
|
109
|
+
deviance: number;
|
|
110
|
+
iter: number;
|
|
111
|
+
converged: boolean;
|
|
112
|
+
boundary: boolean;
|
|
113
|
+
rank: number;
|
|
114
|
+
qr: QR | undefined;
|
|
115
|
+
/** Rows used in the last least squares step */
|
|
116
|
+
used: boolean[];
|
|
117
|
+
workingWeights: number[];
|
|
118
|
+
workingResiduals: number[];
|
|
119
|
+
}
|
|
120
|
+
/** Iteratively reweighted least squares (glm-behaviour.md, "Fitting") */
|
|
121
|
+
export declare function irls(inp: IrlsInput, warnings: Warnings): IrlsFit;
|
|
122
|
+
/** Fits a model from its stored data, with the columns `cols` of the model matrix (all by default) */
|
|
123
|
+
export declare function refit(model: GlmModel, cols: number[], warnings: Warnings, control?: {
|
|
124
|
+
epsilon?: number;
|
|
125
|
+
maxit?: number;
|
|
126
|
+
etastart?: number[];
|
|
127
|
+
start?: number[] | undefined;
|
|
128
|
+
extraOffset?: number[];
|
|
129
|
+
untilCoefficients?: boolean;
|
|
130
|
+
}): {
|
|
131
|
+
fit: IrlsFit;
|
|
132
|
+
fam: Family;
|
|
133
|
+
y: number[];
|
|
134
|
+
w: number[];
|
|
135
|
+
size: number[] | undefined;
|
|
136
|
+
offset: number[];
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Fit a generalized linear model by iteratively reweighted least squares. Equivalent to R's
|
|
140
|
+
* `glm(y ~ x, family = ...)` (or `y ~ x - 1` without intercept); the predictors are given as a matrix instead of a
|
|
141
|
+
* formula. Rows with a missing value (NaN) in `x`, `y`, the weights or the offset are dropped. Every family and link
|
|
142
|
+
* of R's `stats` is available; R's warnings are returned in `warnings`. See {@link glmSummary}, {@link glmResiduals},
|
|
143
|
+
* {@link glmPredict}, {@link glmConfint} and {@link glmAnova} for the methods.
|
|
144
|
+
*
|
|
145
|
+
* @param x Predictors: a matrix (array of rows, one column per predictor; rows of length 0 for a model with the
|
|
146
|
+
* intercept only) or a vector (one predictor).
|
|
147
|
+
* @param y Response: a vector, or for the binomial families rows of (successes, failures).
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
152
|
+
* fit.coefficients; // [intercept, slope]
|
|
153
|
+
* glmSummary(fit).pValue;
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare function glm(x: MatrixInput | Sample, y: Sample | MatrixInput, options?: GlmOptions): GlmResult;
|
|
157
|
+
/** The family object of a fit */
|
|
158
|
+
export declare function familyOf(fit: GlmResult): Family;
|
|
159
|
+
/** Pearson dispersion of summary.glm (1 for binomial and poisson) */
|
|
160
|
+
export declare function glmDispersion(fit: GlmResult, warnings?: Warnings): number;
|
|
161
|
+
/** Options for {@link glmSummary}. */
|
|
162
|
+
export interface GlmSummaryOptions {
|
|
163
|
+
/** Dispersion to use instead of the estimate (or 1 for binomial and poisson). */
|
|
164
|
+
dispersion?: number;
|
|
165
|
+
}
|
|
166
|
+
/** Result of {@link glmSummary}: the quantities of R's `summary(glm)`. */
|
|
167
|
+
export interface GlmSummary {
|
|
168
|
+
dispersion: number;
|
|
169
|
+
/** The coefficient table of the columns that are not aliased, in their order. */
|
|
170
|
+
estimate: number[];
|
|
171
|
+
stdError: number[];
|
|
172
|
+
statistic: number[];
|
|
173
|
+
pValue: number[];
|
|
174
|
+
/** `"z value"` when the dispersion is fixed (binomial, poisson), else `"t value"`. */
|
|
175
|
+
statisticName: "z value" | "t value";
|
|
176
|
+
/** (R'R)^-1 of the columns that are not aliased. */
|
|
177
|
+
covUnscaled: number[][];
|
|
178
|
+
/** covUnscaled times the dispersion. */
|
|
179
|
+
covScaled: number[][];
|
|
180
|
+
dfResidual: number;
|
|
181
|
+
/** Warnings (such as "observations with zero weight not used for calculating dispersion"). */
|
|
182
|
+
warnings: string[];
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates.
|
|
186
|
+
* Equivalent to R's `summary()` for a `glm` fit.
|
|
187
|
+
*/
|
|
188
|
+
export declare function glmSummary(fit: GlmResult, options?: GlmSummaryOptions): GlmSummary;
|
|
189
|
+
/** Kinds of residuals of {@link glmResiduals}. */
|
|
190
|
+
export type GlmResidualType = "deviance" | "pearson" | "working" | "response";
|
|
191
|
+
/**
|
|
192
|
+
* Residuals of a generalized linear model, one per row. Equivalent to R's `residuals(fit, type)` for a `glm` fit
|
|
193
|
+
* (default `"deviance"`).
|
|
194
|
+
*/
|
|
195
|
+
export declare function glmResiduals(fit: GlmResult, type?: GlmResidualType): number[];
|
|
196
|
+
/**
|
|
197
|
+
* Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight.
|
|
198
|
+
* Equivalent to R's `hatvalues()` for a `glm` fit.
|
|
199
|
+
*/
|
|
200
|
+
export declare function glmHatvalues(fit: GlmResult): number[];
|
|
201
|
+
/**
|
|
202
|
+
* Standardized deviance residuals, for the rows of non-zero prior weight. Equivalent to R's `rstandard()` for a
|
|
203
|
+
* `glm` fit.
|
|
204
|
+
*/
|
|
205
|
+
export declare function glmRstandard(fit: GlmResult, options?: GlmSummaryOptions): number[];
|
|
206
|
+
/** Cook's distances, for the rows of non-zero prior weight. Equivalent to R's `cooks.distance()` for a `glm` fit. */
|
|
207
|
+
export declare function glmCooksDistance(fit: GlmResult, options?: GlmSummaryOptions): number[];
|
|
208
|
+
/** Options for {@link glmPredict}. */
|
|
209
|
+
export interface GlmPredictOptions {
|
|
210
|
+
/** New predictor values, with the columns of `x` (no column of ones). Default: the data of the fit. */
|
|
211
|
+
newdata?: MatrixInput | Sample;
|
|
212
|
+
/** Offset for `newdata`. Default 0. */
|
|
213
|
+
newoffset?: Sample;
|
|
214
|
+
/** `"link"` (default; the linear predictor) or `"response"` (the mean). */
|
|
215
|
+
type?: "link" | "response";
|
|
216
|
+
/** Dispersion for the standard errors instead of the summary's. */
|
|
217
|
+
dispersion?: number;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Predictions of a generalized linear model with their standard errors. Equivalent to R's
|
|
221
|
+
* `predict(fit, newdata, type, se.fit = TRUE)` for a `glm` fit. Aliased columns are left out, as R does.
|
|
222
|
+
*/
|
|
223
|
+
export declare function glmPredict(fit: GlmResult, options?: GlmPredictOptions): {
|
|
224
|
+
fit: number[];
|
|
225
|
+
seFit: number[];
|
|
226
|
+
};
|