Function Repository Resource:

RegressionListPlot

Source Notebook

Display the regression line of a dataset

Contributed by: Katja Della Libera

ResourceFunction["RegressionListPlot"][{y1,y2,}]

plots points {1,y1},{2,y2}, and their line of best fit.

ResourceFunction["RegressionListPlot"][{{x1,y1},{x2,y2},}]

plots the specified points and their regression line.

ResourceFunction["RegressionListPlot"][{data1,data2,}]

plots multiple datasets and their regression lines.

Details and Options

ResourceFunction["RegressionListPlot"] accepts options for both Plot and ListPlot.
In addition, ResourceFunction["RegressionListPlot"] accepts the following options:
"Degree"1specifies the degree of the regression curves
"DisplayEquation"Falsedetermines whether to display the equation of the line of best fit
"DisplayRSquared"Falsespecifies whether to display the coefficient of determination on the plot

Examples

Basic Examples (1) 

Find and plot the correlation of a dataset:

In[1]:=
ResourceFunction[
 "RegressionListPlot"][{{2, 3}, {3, 4}, {1, 1}, {10, 4}, {13, 12}}]
Out[1]=

Scope (2) 

Generate one dataset of random numbers and one dataset of numbers correlated by a BinormalDistribution:

In[2]:=
exampleOne = {Table[RandomReal[{0, 20}, 2], 20], RandomVariate[BinormalDistribution[0.95], 30]*5 + 10};

Plot both of the examples datasets on the same plot:

In[3]:=
ResourceFunction["RegressionListPlot"][exampleOne]
Out[3]=

Options (5) 

Degree (2) 

Specify the degree of the regression function using this option:

In[4]:=
exampleTwo = {Transpose[{Range[
      40], (RandomVariate[NormalDistribution[0.5, 1], 40] + 5)^2}], RandomVariate[BinormalDistribution[0.95], 30]*7 + 15};
In[5]:=
ResourceFunction["RegressionListPlot"][exampleTwo, "Degree" -> 2]
Out[5]=

Degrees of multiple functions can be specified individually:

In[6]:=
ResourceFunction["RegressionListPlot"][exampleTwo, "Degree" -> { 4, 1}]
Out[6]=

DisplayEquation (1) 

"DisplayEquation"True will display the equation of the regression line:

In[7]:=
ResourceFunction[
 "RegressionListPlot"][{{0, 10}, {2, 8}, {1, 12}, {6, 1}, {2, 3}, {3, 4}, {4, 6} {1, 1}, {9, 5}, {9, 6}, {12, 2}, {10, 4}, {13, 12}}, "Degree" -> 2, "DisplayEquation" -> True]
Out[7]=

DisplayRSquared (2) 

"DisplayRSquared" specifies whether to display the coefficient of determination. As expected, a random dataset has a much lower coefficient of determination than a related one:

In[8]:=
exampleThree = {Table[RandomReal[{0, 20}, 2], 20], RandomVariate[BinormalDistribution[0.95], 30]*5 + 10};
In[9]:=
ResourceFunction["RegressionListPlot"][exampleThree, "DisplayRSquared" -> True]
Out[9]=

"DisplayRSquared" and "DisplayEquation" can both be set to True:

In[10]:=
ResourceFunction["RegressionListPlot"][exampleThree, "DisplayEquation" -> True, "DisplayRSquared" -> True]
Out[10]=

Neat Examples (2) 

Correlate the current eruption duration and time to next eruption of Old Faithful geyser, October 1980:

In[11]:=
ResourceFunction["RegressionListPlot"][
 ExampleData[{"Statistics", "OldFaithful"}], "DisplayEquation" -> True, "DisplayRSquared" -> True, Frame -> {{True, False}, {True, False}} , FrameLabel -> {"duration of current erruption (s)", "time to next erruptions (min)"}]
Out[11]=

Visualize how sepal length, sepal width and petal length are correlated with petal width in the famous Fisher Iris dataset:

In[12]:=
iris = {{#[[1]], #[[4]]} & /@ ExampleData[{"Statistics", "FisherIris"}], {#[[2]], #[[4]]} & /@ ExampleData[{"Statistics", "FisherIris"}], {#[[3]], #[[4]]} & /@ ExampleData[{"Statistics", "FisherIris"}]};
In[13]:=
ResourceFunction["RegressionListPlot"][iris]
Out[13]=

Version History

  • 1.0.0 – 24 December 2019

Related Resources

License Information