Finding Regression Models in Mathematica

Introduction and first example

We will use the FindFit command to find regression models.  Suppose we have the following data:

MyData={{2,3}, {3,5},{4,11}, {5,23}, {6, 18},{7,42},{8, 45}}

We need to give the FindFit command several things as input: the data itself, the general formula, a list of the constants in the formula, and finally, the variable in the formula.  For example, if we want a linear model, we enter the following:

LinearConstants = FindFit[MyData, a*x + b, {a,b}, x]

As output, we get values for the constants a and b in the linear formula a x + b.

We can now define a function using the constants:

LinearModel[x_]:= a*x + b/.LinearConstants

We can plot the linear model with the data on the same set of axes using the Show command together with ListPlot and Plot.

Show[ListPlot[MyData], Plot[LinearModel[x], {x, 0, 8}], PlotRange->{0, 50}]

More examples

To find a logarithmic model, we enter the following

LogarithmicConstants = FindFit[MyData, a + b*Log[x] , {a,b}, x]

Again, we now define a function using these constants:

LogarithmicModel[x_]:= a +b*Log[x]/.LogarithmicConstants

And we plot the model with the data:

Show[ListPlot[MyData], Plot[LogarithmicModel[x], {x, 0, 8}], PlotRange->{0, 50}]

To find and plot an exponential model:

ExpConstants = FindFit[MyData, a * b^x , {a,b}, x]
ExpModel[x_]:=  a * b^x /.ExpConstants;
Show[ListPlot[MyData], Plot[ExpModel[x], {x, 0, 8}], PlotRange->{0, 50}]

To find and plot a logistic model (with some additional data points, say):

NewData={{2,3}, {3,5},{4,11}, {5,23}, {6, 18},{7,42},{8, 45}, {9, 50}, {10, 50}};LogisticConstants = FindFit[NewData, c/(1+a * b^x) , {a,b,c}, x]
LogisticModel[x_]:=  c/(1+a * b^x) /.LogisticConstants;
Show[ListPlot[NewData], Plot[LogisticModel[x], {x, 0, 8}], PlotRange->{0, 50}]