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

@fgregg/loess

v1.4.2

Published

JavaScript implementation of the Locally-Weighted Regression package originally written in C by Cleveland, Grosse and Shyu (1992)

Downloads

11

Readme

loess

JavaScript implementation of the Locally-Weighted Regression package originally written in C by Cleveland, Grosse and Shyu (1992)

Getting started

First install the package:

npm install loess --save

Load in your data:

var data = require('./myData.json')

Instantiate a LOESS model with the data:

var Loess = require('loess')
var options = {span: 0.5, band: 0.8, degree: 1}
var model = new Loess(data, options)

Fit model by calling the .predict( ) method on the model object:

var fit = model.predict()
console.log(fit.fitted)
// do something else with fit.fitted

To fit model on a new set of points, pass a data object into .predict( )

var newData = {
  x: [1, 2, 3, 4, 5],
  x2: [6, 7, 8, 9, 10]
}

fit = model.predict(newData)

var upperLimit = fit.fitted.map((yhat, idx) => yhat + fit.halfwidth[idx])
var lowerLimit = fit.fitted.map((yhat, idx) => yhat - fit.halfwidth[idx])
// plot upperLimit and lowerLimit

Alternatively, use .grid( ) method to generate a grid of equally spaced points:

newData = model.grid([20, 20])

fit = model.predict(newData)

Usage

screenshot1

screenshot2

Find out more by visiting my demo app:

https://loess.netlify.com/


Documentation

class Loess {
  constructor (data: object, options: object) {
    // arguments
    data /*required*/ = {        
      y: [number],
      x: [number],
      x2: [number], // optional
      w: [number]  // optional
    }

    options /*optional*/ = {
      span: number, // 0 to inf, default 0.75
      band: number, // 0 to 1, default 0
      degree: [0, 1, 2] || ['constant', 'linear', 'quadratic'] // default 2
      normalize: boolean, // default true if degree > 1, false otherwise
      robust: boolean, // default false
      iterations: integer //default 4 if robust = true, 1 otherwise
    }

    // return a LOESS model object with the following properties
    this.y = data.y
    this.x = [data.x, data.x2] // predictor matrix
    this.n = this.y.length // number of data points
    this.d = this.x.length // dimension of predictors
    this.bandwidth = options.span * this.n // number of data points used in local regression
    this.options = options
  }

  predict (data: object) {
    // arguments
    data /*optional*/ = {        
      x: [number],
      x2: [number]
    } // default this.x

    return {
      fitted: [number], // fitted values for the specified data points
      halfwidth: [number] // fitted +- halfwidth is the uncertainty band
    }
  }

  grid (cuts: [integer]) {
    return {
      x_cut: [number], // equally-spaced data points
      x_cut2: [number],
      x: [number], // all combination of x_cut and x_cut2, forming a grid
      x2: [number]
    }
  }
}

Note:

  • data should be passed into the constructor function as json with keys y, x and optionally x2 and w. Values being the arrays of response, predictor variables, and observation weights.
  • If no data is supplied to .predict( ) method, default is to perform fitting on the original dataset the model is constructed with.
  • span refers to the percentage number of neighboring points used in local regression.
  • band specifies how wide the uncertainty band should be. The higher the value, the greater number of points encompassed by the uncertainty band. Setting to 0 will return only fitted values.
  • By default LOESS model will perform local fitting using the quadratic function. Overwrite this by setting the degree option to "linear" or "constant". Lower degree fitting function computes faster.
  • For multivariate data, normalize option defaults to true. This means normalization is applied before performing proximity calculation. Data is transformed by dividing the factors by their 10% trimmed sample standard deviation. Turn off this option if dealing with geographical data.
  • Set robust option to true to turn on iterative robust fitting procedure. Applicable for estimates that have non-Gaussian errors. More iterations requires longer computation time.
  • When using .grid( ), cuts refers to the number of equally spaced points required along each axis.

Credits

William S. Cleveland, Susan J. Devlin Locally Weighted Regression: An Approach to Regression Analysis by Local Fitting Journal of the American Statistical Association, Vol. 83, No. 403. (Sep., 1988), pp. 596-610.

William S. Cleveland, Eric Grosse, Ming-Jen Shyu A Package of C and Fortran Routines for Fitting Local Regression Models (20 August 1992) Source code available at http://www.netlib.org/a/dloess