Function Repository Resource:

# FittedModelPlot

Plot fitted models together with their raw data

Contributed by: Lukas Lang
 ResourceFunction["FittedModelPlot"][FittedModel[…]] plots the raw data and the fit described by the FittedModel[…] expression. ResourceFunction["FittedModelPlot"][FittedModel[…],lims] plots the fit over the range given by lims. ResourceFunction["FittedModelPlot"][{FittedModel[…],FittedModel[…]},…] plots several fit results. ResourceFunction["FittedModelPlot"][{…,w[FittedModel[…],…],…},…] plots the FittedModel[…] with features defined by the symbolic wrapper w. ResourceFunction["FittedModelPlot"][data,funcs,x] computes the fit from LinearModelFit[data,funcs,x]. ResourceFunction["FittedModelPlot"][data,form,params,x] computes the fit from NonlinearModelFit[data,form,params,x]. ResourceFunction["FittedModelPlot"][{data1,data2,…},…,x] computes the fits for several datai. ResourceFunction["FittedModelPlot"][data,…,{x,xmin,xmax}] plots the fit from xmin to xmax. ResourceFunction["FittedModelPlot"][{…,w[datai,…],…},…] plots datai with features defined by the symbolic wrapper w.

## Details and Options

ResourceFunction["FittedModelPlot"] uses Plot and ListPlot to plot the data.
When provided with the raw data, ResourceFunction["FittedModelPlot"] effectively uses LinearModelFit or NonlinearModelFit to compute the best fits.
ResourceFunction["FittedModelPlot"] accepts all options of Plot, ListPlot and NonlinearModelFit, with the following additions and changes:
 "ErrorBands" False whether to plot the error bands of the fits LegendFunction Automatic how to generate the plot legend
The bands plotted by "ErrorBands"True are effectively given by the "MeanPredictionBands" property of the fitted models.
With the default setting , the PlotLegends setting is used as it is given.
With the setting LegendFunctionfunc, the PlotLegends setting is effectively given by func[{fit1,fit2,}], where the fiti are the FittedModel[] expressions of the individual curves.
ResourceFunction["FittedModelPlot"] supports the full range of wrappers of Plot and ListPlot and will use them to style the plotted curves and points accordingly.
In ResourceFunction["FittedModelPlot"][FittedModel[],lims], lims can be any of the specifications for the horizontal PlotRange supported by ListPlot.
In ResourceFunction["FittedModelPlot"][,{x,xmin,xmax}] and ResourceFunction["FittedModelPlot"][,{x,lim}], the limit specifications can similarly be any specification supported by ListPlot.
The first argument of ResourceFunction["FittedModelPlot"] can also be an Association, with the keys being used for the legend.
Labeled and Callout type data wrappers are, by default, attached to the data points. Use position specifications of the form {Fit,pos} to indicate they should be attached to the fit line instead.

## Examples

### Basic Examples (4)

Fit data using LinearModelFit and plot the result:

 In[1]:=
 Out[3]=

Plot two fits:

 In[4]:=
 Out[8]=

Style the plot and add a legend:

 In[9]:=
 Out[13]=

Supply the data to FittedModelPlot directly:

 In[14]:=
 Out[16]=

### Scope (2)

Specify labels and callouts via wrappers, and specify that the callout should be attached to the fit line:

 In[17]:=
 Out[19]=

Plot the fit over a wider range:

 In[20]:=
 Out[22]=

### Options (5)

#### ErrorBands (3)

With the default setting "ErrorBands"False, no error bands are plotted:

 In[23]:=
 Out[25]=

Add error bands to the plot:

 In[26]:=
 Out[28]=

The setting for ConfidenceLevel affects the error bands:

 In[29]:=
 Out[31]=

#### LegendFunction (2)

With the default setting , the legend is effectively generated from the PlotLegends setting:

 In[32]:=
 Out[33]=

Give a function to compute the legend from the FittedModel[…] expressions:

 In[34]:=
 Out[35]=

### Properties and Relations (1)

FittedModelPlot can be recreated using a combination of ListPlot and Plot:

 In[36]:=
 Out[38]=
 In[39]:=
 Out[39]=

### Neat Examples (2)

Generate some noisy data for three normal distributions:

 In[40]:=

Plot the data together with their fits:

 In[41]:=
 Out[41]=

Lukas Lang

## Version History

• 2.0.0 – 11 November 2020
• 1.0.0 – 25 September 2020

## Author Notes

Changes from version 1.0.0 to 1.1.0:

Fixed the discrepancy between the plot markers used in the legend and in the plot.
Removed the example using style wrappers due to a bug in ListPlot failing to apply those styles to PlotMarkers.
Ensured that Labeled and Callout wrappers are only applied once, and added a way to control to which part they should be attached.