• Stars
    star
    144
  • Rank 255,590 (Top 6 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Calculate statistical regressions from two-dimensional data.

d3-regression

Calculate statistical regressions from two-dimensional data.

Stastical Regressions

Installing

If you use NPM, npm install d3-regression. Otherwise, download the latest release. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported:

<script src="https://unpkg.com/[email protected]/dist/d3-regression.min.js"></script>
<script>

const regression = d3.regressionLinear()
  .x(d => d.x)
  .y(d => d.y)
  .domain([0, 100]);

</script>

API Reference

# d3.regressionLinear() 路 Source, Example

Creates a new linear regression generator with default x- and y- accessors and a null domain.

Linear regression

# linear(data) 路 Source

Computes the linear regression, which takes the form y = ax + b, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a line represented as an array of two points, where each point is an array of two numbers representing the point's coordinates.

Also returns properties a and b, representing the equation's coefficients, and rSquared, representing the coefficient of determination. Lastly, returns a predict property, which is a function that outputs a y-coordinate given an input x-coordinate.

# linear.x([x]) 路 Source

If x is specified, sets the x-coordinate accessor, which is passed passed the current datum (d), the current index (i), and the entire data array (data). If x is not specified, returns the current x-coordinate accessor, which defaults to:

function x(d, i, data) {
  return d[0];
}

# linear.y([y]) 路 Source

If y is specified, sets the y-coordinate accessor, which is passed passed the current datum (d), the current index (i), and the entire data array (data). If y is not specified, returns the current y-coordinate accessor, which defaults to:

function y(d, i, data) {
  return d[1];
}

# linear.domain([domain]) 路 Source

If domain is specified, sets the minimum and maximum x-coordinates of the returned line to the specified array of numbers. The array must contain two elements. If the elements in the given array are not numbers, they will be coerced to numbers. If domain is not specified, returns a copy of the regression generator鈥檚 current domain.

If data is passed to the regression generator before a domain has been specified, the domain will be set to the minimum and maximum x-coordinate values of the data.

# d3.regressionExp() 路 Source, Example

Creates a new exponential regression generator with default x- and y- accessors and a null domain.

Exponential regression

# exp(data) 路 Source

Computes the exponential regression, which takes the form y = aebx, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a smooth line represented as an array of points, where each point is an array of two numbers representing the point's coordinates.

Also returns properties a and b, representing the equation's coefficients, and rSquared, representing the coefficient of determination. Lastly, returns a predict property, which is a function that outputs a y-coordinate given an input x-coordinate.

# exp.x([x]) 路 Source

See linear.x().

# exp.y([y]) 路 Source

See linear.y().

# exp.domain([domain]) 路 Source

See linear.domain().

# d3.regressionLog() 路 Source, Example

Creates a new logarithmic regression generator with default x- and y- accessors and a null domain.

Logarithmic regression

# log(data) 路 Source

Computes the logarithmic regression, which takes the form y = a 路 ln(x) + b, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a smooth line represented as an array of points, where each point is an array of two numbers representing the point's coordinates.

Also returns properties a and b, representing the equation's coefficients, and rSquared, representing the coefficient of determination. Lastly, returns a predict property, which is a function that outputs a y-coordinate given an input x-coordinate.

# log.x([x]) 路 Source

See linear.x().

# log.y([y]) 路 Source

See linear.y().

# log.domain([domain]) 路 Source

See linear.domain().

# log.base([base]) 路 Source

If base is specified, sets the base of the logarithmic regression. If base is not specified, returns the current base, which defaults to Euler's number.

# d3.regressionQuad() 路 Source, Example

Creates a new quadratic regression generator with default x- and y- accessors and a null domain.

Quadratic regression

# quad(data) 路 Source

Computes the quadratic regression, which takes the form y = ax2 + bx + c, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a smooth line represented as an array of points, where each point is an array of two numbers representing the point's coordinates.

Also returns properties a, b, and c, representing the equation's coefficients, and rSquared, representing the coefficient of determination. Lastly, returns a predict property, which is a function that outputs a y-coordinate given an input x-coordinate.

# quad.x([x]) 路 Source

See linear.x().

# quad.y([y]) 路 Source

See linear.y().

# quad.domain([domain]) 路 Source

See linear.domain().

# d3.regressionPoly() 路 Source, Example

Creates a new polynomial regression generator with default x- and y- accessors, a null domain, and an order of 3. This implementation was adapted from regression-js.

Polynomial regression

# poly(data) 路 Source

Computes the polynomial regression, which takes the form y = anxn + ... + a1x + a0, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a smooth line represented as an array of points, where each point is an array of two numbers representing the point's coordinates.

Also returns three properties: coefficients, an array representing the equation's coefficients with the intercept as the first item and nth degree coefficient as the last item; rSquared, representing the coefficient of determination; and predict, a function that outputs a y-coordinate given an input x-coordinate.

# poly.x([x]) 路 Source

See linear.x().

# poly.y([y]) 路 Source

See linear.y().

# poly.domain([domain]) 路 Source

See linear.domain().

# poly.order([order]) 路 Source

If order is specified, sets the regression's order to the specified number. For example, if order is set to 4, the regression generator will perform a fourth-degree polynomial regression. Likewise, if order is set to 2, the regression generator will perform a quadratic regression. Be careful about attempting to fit your data with higher order polynomials; though the regression line will fit your data with a high determination coefficient, it may have little predictive power for data outside of your domain.

If order is not specified, returns the regression generator's current order, which defaults to 3.

# d3.regressionPow() 路 Source, Example

Creates a new power law regression generator with default x- and y- accessors and a null domain.

Power law regression

# pow(data) 路 Source

Computes the power law regression, which takes the form y = axb, for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a smooth line represented as an array of points, where each point is an array of two numbers representing the point's coordinates.

Also returns properties a and b, representing the equation's coefficients, and rSquared, representing the coefficient of determination. Lastly, returns a predict property, which is a function that outputs a y-coordinate given an input x-coordinate.

# pow.x([x]) 路 Source

See linear.x().

# pow.y([y]) 路 Source

See linear.y().

# pow.domain([domain]) 路 Source

See linear.domain().

# d3.regressionLoess() 路 Source, Example

Creates a new LOESS regression generator with default x- and y- accessors and a bandwidth of .3. This implementation was adapted from science.js.

LOESS regression

# loess(data) 路 Source

Computes the LOESS regression for the specified data points, ignoring points with invalid values (null, undefined, NaN, Infinity).

Returns a line represented as an array of n points, where each point is an array of two numbers representing the point's coordinates.

# loess.x([x]) 路 Source

See linear.x().

# loess.y([y]) 路 Source

See linear.y().

# loess.bandwidth([bandwidth]) 路 Source

If bandwidth is specified, sets the LOESS regression's bandwidth, or smoothing parameter, to the specific number between 0 and 1. The bandwidth represents the share of the total data points that are used to calculate each local fit. Higher bandwidths produce smoother lines, and vice versa. If bandwidth is not specified, returns a copy of the regression generator鈥檚 current bandwidth, which defaults to .3.

More Repositories

1

geometric

A JavaScript library for doing geometry.
JavaScript
864
star
2

d3-geo-scale-bar

Displays automatic scale bars for projected geospatial data.
JavaScript
45
star
3

shape2path

Convert SVG shapes to SVG paths
JavaScript
24
star
4

jeezy

JavaScript. Easy.
JavaScript
23
star
5

ba64

A tiny npm module for saving Base64 encoded images that are part of data URLs to your file system.
JavaScript
10
star
6

swiftmap

A JavaScript library for making data-driven maps.
JavaScript
9
star
7

d3-marcon

Margin conventions for d3-selection.
JavaScript
8
star
8

party-time

Convert party abbreviations to full names and back again.
JavaScript
8
star
9

arraygeous

A JavaScript library for lightning fast array manipulation.
JavaScript
8
star
10

swoopy

Draw swoopy lines.
JavaScript
4
star
11

stupid-sheets

A stupid simple Google Sheets reader.
JavaScript
4
star
12

adr-election-node

Scrape data about an election from ADR
JavaScript
4
star
13

sheet2json

A Node.js package for converting each sheet in an .xls or .xlsx file to a .csv file.
JavaScript
3
star
14

quoteShare

A jQuery plugin that lets users share highlighted text on Twitter.
JavaScript
3
star
15

d3-arrow

Attach arrowheads to SVG elements.
JavaScript
3
star
16

logpct

A percent status logger for Node.js
JavaScript
2
star
17

d3-geo-compactness

A JavaScript library for computing the compactness of GeoJSON features.
JavaScript
2
star
18

jquery-scrape

Use jQuery as your selection engine on the result of an HTTP/HTTPS request.
JavaScript
2
star
19

data2grid

Add a row and a column to a set of data so you can lay it out.
JavaScript
1
star
20

inflation-scraper

Get CPI inflation from BLS.
JavaScript
1
star
21

tokenize-file

Read a file, tokenize it, and spit out a handy JSON.
JavaScript
1
star
22

boxes

u can move da boxes
JavaScript
1
star
23

nyt-1917

A node script for tweeting New York Times articles about Russia from 100 years ago.
JavaScript
1
star