npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d3-regression

v1.3.10

Published

Calculate statistical regressions for two-dimensional data

Downloads

350,335

Readme

d3-regression

Calculate statistical regressions from two-dimensional data.

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(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’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() · Source, Example

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

# 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.

# 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.

# 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.

# 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.

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