jheer / d3-regression

Calculate statistical regressions from two-dimensional data.

Home Page:https://observablehq.com/collection/@harrystevens/d3-regression

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

d3-regression

Calculate statistical regressions from two-dimensional data. Build Status

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/d3-regression@1.2.1/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() <>

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

Linear regression

# linear(data) <>

Computes the linear regression, which takes the form y = ax + b, for the specified data points.

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]) <>

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]) <>

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]) <>

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’s 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() <>

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

Exponential regression

# exp(data) <>

Computes the exponential regression, which takes the form y = aebx, for the specified data points.

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]) <>

See linear.x().

# exp.y([y]) <>

See linear.y().

# exp.domain([domain]) <>

See linear.domain().

# d3.regressionLog() <>

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

Logarithmic regression

# log(data) <>

Computes the logarithmic regression, which takes the form y = a · ln(x) + b, for the specified data points.

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]) <>

See linear.x().

# logarithmic.y([y]) <>

See linear.y().

# log.domain([domain]) <>

See linear.domain().

# d3.regressionQuad() <>

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

Quadratic regression

# quad(data) <>

Computes the quadratic regression, which takes the form y = ax2 + bx + c, for the specified data points.

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]) <>

See linear.x().

# quad.y([y]) <>

See linear.y().

# quad.domain([domain]) <>

See linear.domain().

# d3.regressionPoly() <>

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) <>

Computes the polynomial regression, which takes the form y = anxn + ... + a1x + a0, for the specified data points.

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]) <>

See linear.x().

# poly.y([y]) <>

See linear.y().

# poly.domain([domain]) <>

See linear.domain().

# poly.order([order]) <>

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 (note, however, that d3-regression's regressionQuad is faster). 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 will likely have very 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() <>

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

Power law regression

# pow(data) <>

Computes the power law regression, which takes the form y = axb, for the specified data points.

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]) <>

See linear.x().

# pow.y([y]) <>

See linear.y().

# pow.domain([domain]) <>

See linear.domain().

# d3.regressionLoess() <>

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) <>

Computes the LOESS regression for the specified data points. 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]) <>

See linear.x().

# loess.y([y]) <>

See linear.y().

# loess.bandwidth([bandwidth]) <>

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’s current bandwidth, which defaults to .3.

About

Calculate statistical regressions from two-dimensional data.

https://observablehq.com/collection/@harrystevens/d3-regression

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:JavaScript 100.0%