Install via yarn add locutus and import:
import { variance } from 'locutus/r/stats/variance'.
Or with CommonJS: const { variance } = require('locutus/r/stats/variance')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
variance([1, 2, 3])
1
2
variance([1, 2, 3, 4])
1.6666666666666667
3
variance([2, 4, 6])
4
Notes
JS-safe equivalent of R’s var() for a plain vector; matrix/data-frame forms are out of scope.
Dependencies
This function uses the following Locutus functions:
exportfunctionvariance(x: unknown): number { // discuss at: https://locutus.io/r/variance/ // parity verified: R 4.4 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: JS-safe equivalent of R's var() for a plain vector; matrix/data-frame forms are out of scope. // example 1: variance([1, 2, 3]) // returns 1: 1 // example 2: variance([1, 2, 3, 4]) // returns 2: 1.6666666666666667 // example 3: variance([2, 4, 6]) // returns 3: 4
exportfunctionvariance(x) { // discuss at: https://locutus.io/r/variance/ // parity verified: R 4.4 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: JS-safe equivalent of R's var() for a plain vector; matrix/data-frame forms are out of scope. // example 1: variance([1, 2, 3]) // returns 1: 1 // example 2: variance([1, 2, 3, 4]) // returns 2: 1.6666666666666667 // example 3: variance([2, 4, 6]) // returns 3: 4
// r/_helpers/_stats (Locutus helper dependency) functiontoRNumericArray(data: unknown, functionName: string): number[] { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
functiontoRNumber(value: unknown, functionName: string): number { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integer precision`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only numeric values`) }
functionsampleVariance(values: number[]): number { if (values.length < 2) { returnNumber.NaN }
const mean = values.reduce((sum, value) => sum + value, 0) / values.length const squaredDeltaSum = values.reduce((sum, value) => { const delta = value - mean return sum + delta * delta }, 0)
return squaredDeltaSum / (values.length - 1) }
// r/stats/variance (target function module) functionvariance(x: unknown): number { // discuss at: https://locutus.io/r/variance/ // parity verified: R 4.4 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: JS-safe equivalent of R's var() for a plain vector; matrix/data-frame forms are out of scope. // example 1: variance([1, 2, 3]) // returns 1: 1 // example 2: variance([1, 2, 3, 4]) // returns 2: 1.6666666666666667 // example 3: variance([2, 4, 6]) // returns 3: 4
// r/_helpers/_stats (Locutus helper dependency) functiontoRNumericArray(data, functionName) { if (!Array.isArray(data)) { thrownewTypeError(`${functionName}() data must be an array`) }
functiontoRNumber(value, functionName) { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integer precision`) }
return numericValue }
thrownewTypeError(`${functionName}() data must contain only numeric values`) }
functionsampleVariance(values) { if (values.length < 2) { returnNumber.NaN }
const mean = values.reduce((sum, value) => sum + value, 0) / values.length const squaredDeltaSum = values.reduce((sum, value) => { const delta = value - mean return sum + delta * delta }, 0)
return squaredDeltaSum / (values.length - 1) }
// r/stats/variance (target function module) functionvariance(x) { // discuss at: https://locutus.io/r/variance/ // parity verified: R 4.4 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: JS-safe equivalent of R's var() for a plain vector; matrix/data-frame forms are out of scope. // example 1: variance([1, 2, 3]) // returns 1: 1 // example 2: variance([1, 2, 3, 4]) // returns 2: 1.6666666666666667 // example 3: variance([2, 4, 6]) // returns 3: 4
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.