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

@stdlib/stats-lowess

v0.2.2

Published

Locally-weighted polynomial regression via the LOWESS algorithm.

Downloads

1,194

Readme

LOWESS

NPM version Build Status Coverage Status

Locally-weighted polynomial regression via the LOWESS algorithm.

Installation

npm install @stdlib/stats-lowess

Usage

var lowess = require( '@stdlib/stats-lowess' );

lowess( x, y[, opts] )

For input arrays and/or typed arrays x and y, the function returns an object holding the ordered input values x and smoothed values for y.

var x = [
    4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
    14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20,
    20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25
];
var y = [
    2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46,
    26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68,
    32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85
];

var out = lowess( x, y );
/* returns
    {
        'x': [
            4,
            4,
            7,
            7,
            ...,
            24,
            24,
            24,
            25
        ],
        'y': [
            ~4.857,
            ~4.857,
            ~13.1037,
            ~13.1037,
            ...,
            ~79.102,
            ~79.102,
            ~79.102,
            ~84.825
        ]
    }
*/

The function accepts the following options:

  • f: positive number specifying the smoothing span, i.e., the proportion of points which influence smoothing at each value. Larger values correspond to more smoothing. Default: 2/3.
  • nsteps: number of iterations in the robust fit (fewer iterations translates to faster function execution). If set to zero, the nonrobust fit is returned. Default: 3.
  • delta: nonnegative number which may be used to reduce the number of computations. Default: 1/100th of the range of x.
  • sorted: boolean indicating if the input array x is sorted. Default: false.

By default, smoothing at each value is determined by 2/3 of all other points. To choose a different smoothing span, set the f option.

var x = [
    4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
    14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20,
    20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25
];
var y = [
    2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46,
    26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68,
    32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85
];

var out = lowess( x, y, {
    'f': 0.2
});
/* returns
    {
        'x': [
            4,
            4,
            7,
            ...,
            24,
            24,
            25
        ],
        'y': [
            ~6.03,
            ~6.03,
            ~12.68,
            ...,
            ~82.575,
            ~82.575,
            ~93.028
        ]
    }
*/

By default, three iterations of locally weighted regression fits are calculated after the initial fit. To set a different number of iterations for the robust fit, set the nsteps option.

var x = [
    4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
    14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20,
    20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25
];
var y = [
    2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46,
    26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68,
    32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85
];

var out = lowess( x, y, {
    'nsteps': 20
});
/* returns
    {
        'x': [
            4,
            ...,
            25
        ],
        'y': [
            ~4.857,
            ...,
            ~84.825
        ]
    }
*/

To save computations, set the delta option. For cases where one has a large number of (x,y)-pairs, carrying out regression calculations for all points is not likely to be necessary. By default, delta is set to 1/100th of the range of the values in x. In this case, if the values in x were uniformly scattered over the entire range, the locally weighted regression would be calculated at approximately 100 points. On the other hand, for small data sets with less than 100 observations, one can safely set the option to zero so calculations are made for each data point.

var x = [
    4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
    14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20,
    20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25
];
var y = [
    2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46,
    26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68,
    32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85
];

var out = lowess( x, y, {
    'delta': 0.0
});
/* returns
    {
        'x': [
            4,
            ...,
            25
        ],
        'y': [
            ~4.857,
            ...,
            ~84.825
        ]
    }
*/

If the elements of x are sorted in ascending order, set the sorted option to true.

var x = [
    4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
    14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 20,
    20, 20, 20, 20, 22, 23, 24, 24, 24, 24, 25
];
var y = [
    2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34, 34, 46,
    26, 36, 60, 80, 20, 26, 54, 32, 40, 32, 40, 50, 42, 56, 76, 84, 36, 46, 68,
    32, 48, 52, 56, 64, 66, 54, 70, 92, 93, 120, 85
];

var out = lowess( x, y, {
    'sorted': true
});
/* returns
    {
        'x': [
            4,
            ...,
            25
        ],
        'y': [
            ~4.857,
            ...,
            ~84.825
        ]
    }
*/

Examples

var randn = require( '@stdlib/random-base-randn' );
var Float64Array = require( '@stdlib/array-float64' );
var plot = require( '@stdlib/plot-ctor' );
var lowess = require( '@stdlib/stats-lowess' );

var x;
var y;
var i;

// Create some data...
x = new Float64Array( 100 );
y = new Float64Array( x.length );
for ( i = 0; i < x.length; i++ ) {
    x[ i ] = i;
    y[ i ] = ( 0.5*i ) + ( 10.0*randn() );
}

var opts = {
    'delta': 0
};

var out = lowess( x, y, opts );
var h = plot( [ x, out.x ], [ y, out.y ] );

h.lineStyle = [ 'none', '-' ];
h.symbols = [ 'closed-circle', 'none' ];

h.view( 'stdout' );

References

  • Cleveland, William S. 1979. "Robust Locally and Smoothing Weighted Regression Scatterplots." Journal of the American Statistical Association 74 (368): 829–36. doi:10.1080/01621459.1979.10481038.
  • Cleveland, William S. 1981. "Lowess: A program for smoothing scatterplots by robust locally weighted regression." American Statistician 35 (1): 54–55. doi:10.2307/2683591.

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.