Wolfram Research

Function Repository Resource:

Loess

Source Notebook

Smooth noisy data using local regression

Contributed by: Jon McLoone

ResourceFunction["Loess"][data, bandwidth, x]

finds the interpolation of data at x by using local regression smoothing of bandwidth data points.

ResourceFunction["Loess"][data, Scaled[bandwidth], x]

finds the interpolation of data at x by using local regression smoothing of bandwidth fraction of data points.

ResourceFunction["Loess"][data,bandwidth,{x1,x2,}]

finds the interpolation of data at each xi by using local regression smoothing of bandwidth data points.

Details and Options

Data can be any dimensions.

Examples

Basic Examples

Loess is useful when data is noisy but has an underlying trend:

In[1]:=
data = Table[{i, Sin[i] + RandomReal[0.3]}, {i, 0, 2 \[Pi], 0.05}];
ListPlot[data]
Out[2]=

Find an estimated value for the data at x=2 using the nearest 12 data points:

In[3]:=
ResourceFunction["Loess"][data, 12, 2]
Out[3]=

Find an estimated value for the data at x=2 using the nearest 10% of the data:

In[4]:=
ResourceFunction["Loess"][data, Scaled[0.10], 2]
Out[4]=
In[5]:=
ListPlot[Table[{x, ResourceFunction["Loess"][data, 40, x]}, {x, 0, 2 \[Pi], 0.2}]]
Out[5]=

Scope

Loess can handle higher-dimensional data:

In[6]:=
data = Flatten[
   Table[{i, j, Sin[i + Sin[j]] + RandomReal[0.3]}, {i, 0, 2 \[Pi], 0.1}, {j, 0, 2 \[Pi], 0.1}], {1, 2}];
ListPlot3D[data]
Out[7]=
In[8]:=
ResourceFunction["Loess"][data, 30, {4, 4}]
Out[8]=
In[9]:=
ListPlot3D[
 Flatten[Table[{x, y, ResourceFunction["Loess"][data, 20, {x, y}]}, {x, 0, 6, 0.2}, {y, 0, 6, 0.2}], {1, 2}]]
Out[9]=

Options

By default, Loess fits straight lines to subsets of data, but you can increase the interpolation order to capture different detail:

In[10]:=
data = Table[{i, Sin[i] + RandomReal[0.3]}, {i, 0, 2 \[Pi], 0.2}];
ListPlot[data]
Out[11]=
In[12]:=
ListLinePlot[
 Table[{x, ResourceFunction["Loess"][data, Scaled[0.1], x]}, {x, 0, 2 \[Pi], 0.2}]]
Out[12]=
In[13]:=
ListLinePlot[
 Table[{x, ResourceFunction["Loess"][data, Scaled[0.1], x, InterpolationOrder -> 2]}, {x, 0, 2 \[Pi], 0.2}]]
Out[13]=

Loess fitting involves weighting the local data. Typically, points further from the estimated point are given less weight:

In[14]:=
ListLinePlot[
 Table[{x, ResourceFunction["Loess"][data, Scaled[0.2], x, "WeightsFunction" -> (1/(0.1 + Norm[#]) &)]}, {x, 0, 2 \[Pi], 0.2}]]
Out[14]=

Possible Issues

If you intend to use Loess to predict many values from the same data, then it is more efficient to find all values in a single request:

In[15]:=
data = Table[{i, Sin[i] + RandomReal[0.3]}, {i, 0, 2 \[Pi], 0.05}];
ListPlot[data]
Out[16]=

This method:

In[17]:=
xvals = Range[0, 2 \[Pi], 0.2];
ListPlot[Transpose[{xvals, ResourceFunction["Loess"][data, 40, xvals]}]]
Out[13]=

Is faster than this method:

In[18]:=
ListPlot[Table[{x, ResourceFunction["Loess"][data, 40, x]}, {x, 0, 2 \[Pi], 0.2}]]
Out[18]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

Source Metadata

License Information