Function Repository Resource:

TrimmedLinearFit

Source Notebook

Linear model fitting discarding outliers

Contributed by: Jon McLoone

ResourceFunction["TrimmedLinearFit"][data,basis,vars]

finds a least-squares linear fit of basis to data when 5% of the outliers are discarded.

ResourceFunction["TrimmedLinearFit"][data,basis,vars, n]

finds a least-squares linear fit of basis to data when n outliers are discarded.

ResourceFunction["TrimmedLinearFit"][data,basis,vars, Scaled[n]]

finds a least-squares linear fit of basis to data when a proportion n of outliers are discarded.

Details and Options

If you discard large proportions of the data, the function may not discard the optimal choice of outliers.

Examples

Basic Examples (3) 

Find a fit ignoring a single outlier:

In[1]:=
ResourceFunction[
 "TrimmedLinearFit"][{1, 2, 3, 4, 5, 6, 100}, {1, x}, x, 1]
Out[1]=

Fit would be unduly influenced by the large outlier:

In[2]:=
Fit[{1, 2, 3, 4, 5, 6, 100}, {1, x}, x]
Out[2]=

In this case, TrimmedLinearFit is broadly equivalent to:

In[3]:=
Fit[{1, 2, 3, 4, 5, 6}, {1, x}, x]
Out[3]=

Scope (2) 

The amount of trimming can be specified as a proportion of the data, for example, discarding 30% of the data:

In[4]:=
ResourceFunction[
 "TrimmedLinearFit"][{1, 2, 3, 4, 5, 6, 100}, {1, x}, x, Scaled[0.3]]
Out[4]=

Data and models can be multidimensional:

In[5]:=
ResourceFunction[
 "TrimmedLinearFit"][{{1, 1, 1}, {1, 2, 2}, {2, 1, 2}, {2, 2, 100}}, {1, x, y}, {x, y}, 1]
Out[5]=

Publisher

Jon McLoone

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 22 March 2019

License Information