Skip to content
/ stl Public

package stl implements seasonal-trend decomposition by LOESS.

License

Notifications You must be signed in to change notification settings

chewxy/stl

Repository files navigation

stl GoDoc Build Status Coverage Status Go Report Card

package stl implements Seasonal-Trend Decompositions by LOESS of time series. It is a direct implementation of Cleveland et al (1990), which was written in Fortran '77.

This package does not implement the KD-Tree indexing for nearest neighbour search for the LOESS component as implemented in the original Netlib library. Instead, a straightforwards algorithm is implemented, focusing on clarity and understandability.

There were some parts that were "inlined" and unrolled manually for performance purposes - the tricube function and local neighbour functions for example. The original more mathematical implemetnations have been left in there for future reference.

Additionally, because multiplicative models are common in time series decompositions, Box-Cox transform functions (and generator) have also been provided.

Installation

This package has very minimal dependencies. These are the listing of the dependencies:

This package supports modules

Automated testing only tests up from Go 1.8 onwards. While I'm fairly confident that this package will work on those lower versions as well

Usage

import (
	"encoding/csv"
	"os"
	"fmt"

	"github.com/chewxy/stl"
)

func main() {
	var data []float64
	f, _ := os.Open("testdata/co2.csv")
	r := csv.NewReader(f)

	r.Read() // read header
	for rec, err := r.Read(); err == nil; rec, err = r.Read() {
		// here we're ignoring errors because we know the file to be correct
		if co2, err := strconv.ParseFloat(rec[0], 64); err == nil {
			data = append(data, co2)
		}
	}

	// The main function: 
	res := stl.Decompose(data, 12, 35, stl.Additive(), stl.WithRobustIter(2), stl.WithIter(2))
	fmt.Printf("%v", res.Seasonal)
	fmt.Printf("%v", res.Trens)
	fmt.Printf("%v", res.Resid)

Licence

This package is licenced with a MIT licence. I thank Rob Hyndman for writing a very excellent guide to STL, both in the R standard lib and in principle.

About

package stl implements seasonal-trend decomposition by LOESS.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages