rStats.js gives JavaScript the numerical core of R: probability distributions, random numbers that match R's
set.seed(), descriptive statistics, hypothesis tests, linear, generalized linear and (generalized) linear mixed models with
robust covariances, survival analysis, diagnostic accuracy and method comparison, linear algebra and numerical
methods. Function names and results follow R, and the functions are tested
against R (to a relative 1e-14 for distributions and 1e-12 for tests and models; see
accuracy) or against high-precision references
(where R itself is inaccurate, or has no such function).
This reference lists every function with its options, results and an example (the examples are run by the tests). The functions are grouped by subject in the menu.
t.test → tTest), with a one-line description of each.npm install @numero-uno-jpn/rstats
import { pnorm, tTest, lm } from "@numero-uno-jpn/rstats"; // everything (tree-shaken by a bundler)
import { qbeta } from "@numero-uno-jpn/rstats/distributions"; // or one area: distributions, tests,
// descriptive, linalg, numerics, special
import { lmer, glmer } from "@numero-uno-jpn/rstats/mixed"; // mixed models: only from this area
Without a bundler, https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.3.0/dist/rstats.js is one ES module with
every function except the mixed models, which are in dist/mixed.js. Keep a version in the URL (an exact one, or a
major version such as @1 from 1.0.0 on): a URL without one follows every new release, breaking changes included.
Use either dist/rstats.js or the per-area bundles (dist/tests.js, dist/mixed.js, ...) on one page: mixing them
loads the common code twice, with two random number states.
R's names, with a dot becoming camelCase: t.test → tTest, p.adjust → pAdjust. Functions of other packages
keep their names: car::Anova → Anova, tidyr::pivot_longer → pivotLonger, TOSTER::t_TOST → tTOST. psych's
ICC and irr's icc are two functions.
Where R's name is a reserved word of JavaScript or too general to export, the name says what the function computes:
| rStats.js | R |
|---|---|
variance |
var |
cronbachAlpha |
psych's alpha |
modes |
DescTools' Mode |
geometricMean |
DescTools' Gmean |
cv |
DescTools' CoefVar |
pbvnorm |
mvtnorm's pmvnorm in two dimensions |
glhtDunnett |
multcomp's glht with Dunnett's contrasts |
blandAltman |
BlandAltmanLeh's bland.altman.stats |
diagnosticTests |
epiR's epi.tests |
The methods of the models other than lm (R's summary, predict, anova, ... of a fit) carry the model's name in
front: glmSummary, glmPredict, lmerSummary, lmerAnova, glmerSummary, glmerPredict. The methods without a
prefix (predict, confint, anova) are those of lm.
pt(q, df, lower.tail = FALSE) → pt(q, df, { lowerTail: false }).glmResiduals' type, cohenES'
size, outer's fun, tabulate's nbins, propTrendTest's score, and the deriv of the functions splinefun and
splinefunH return. glmResiduals(fit, "pearson") and glmResiduals(fit, { type: "pearson" }) are the same;
giving one both ways is a RangeError.optimHess(par, fn, gr, control), which takes gr and control by position only, as R:
control is itself a plain object, which an options object after gr could not be told from.optim's control,
glm's family given as an object). pnorm(1, { lowertail: false }) throws "unknown option 'lowertail' for pnorm
(known: mean, sd, lowerTail, logP)" instead of ignoring the misspelt key. Objects whose keys are your data (data
frames, contrasts, levels) are not checked.htest fields in camelCase (statistic, parameter, pValue, confInt, estimate, method,
...), with R's names of the values in separate fields (statisticName, ...).lmer or glmer (lmerSummary, lmerAnova, lmerContest, glmerSummary,
glmerPredict, ...) need the object lmer or glmer returned itself. A copy of it (structuredClone,
{ ...fit }, JSON or postMessage) loses internal values, and passing one is a TypeError.Float64Array).NaN in a number array and null in a data frame column.lm("y ~ x + g", data), tTest("score ~ arm", data).| Error | When |
|---|---|
TypeError |
an argument of the wrong type or shape (a formula of the wrong form for the function included) |
RangeError |
a value out of range (a column that does not exist, an empty selection, an unknown choice) |
Error |
a numerical failure, or input whose result is not defined (such as too few observations) |
SyntaxError |
a formula that cannot be parsed |
warnings: R's text, each distinct warning once in
R's order, [] when there are none. Not included: the warnings of R's distribution functions about their own
precision, and those about R's argument types.setWarningHandler(handler) sets a function that receives each warning of a call
once, with the name of the function called, after the result is computed:
(message, source) => console.warn(source + ": " + message) prints them as R does.options(warn = 2).anova(fit) or glmSummary(fit).After setSeed(42), rnorm, runif, sample and the others give R's numbers after set.seed(42).