Introduction to Fitting Data

Math 245 - Mathematical Models

Go to “Evatuation > Evaluate Notebook” to run all commands in this notebook.

q=1;

Using Mathematica to do a Least-Squares Fit

There are two commands that you will need to learn to fit data and calculate the correlation coefficient: Fit and FindFit.  Here is a summary of when to use which one:

Use Fit when you are trying to determine constants that are linear combinations of input data.  (Like y=a x+b.)  An advanced version of Fit is LinearModelFit.

Use FindFit when you are tring to determine constants that are not linear combinations of input data. (Like y=a (x^b).)  An advanced version of FindFit is NonlinearModelFit.

Unfortunately, the syntax for these commands is different, as you will see.  For the exam, you only need to concern yourself with Fit and FindFit, not with LinearModelFit or NonlinearModelFit.

Introduction to Fit

Use Fit when you are trying to determine constants that are linear combinations of input data, such as y=a (x^2)+b x+c or y=a (x^2)+b x y+c (y^2).  
(Notice how the constants a, b, and c are simply multiplying the variables involved with the data.)

Let's analyze the structure of a Fit command.  The following command finds the least squares best fit line y=a x+b.

data1={{0,1},{1,0},{3,2},{5,4}};
Fit[data1, {x,1},x]

For the Fit command, there are always three inputs: the data points, the functions the unknown constants are multiplied by, and the variables.
The data is a list of data points that looks like {(Subscript[x, i]),(Subscript[y, i])} (when y is supposed to be a function of x), or {(Subscript[x, i]),(Subscript[y, i]),(Subscript[z, i])} (when z is a function of x and y).  Data points involving more variables are also allowed; the independent variables always come first and the dependent variable is at the end.
The last input is the list of variables involved; this lets you name the independent variables to your liking.  For example, when the data points are {x,y}, the last input would be x; if the data points are {x,y,z}, the last input would be {x,y}.  
The middle input is the set of functions of which the dependent variable is supposed to be a linear function.  For example, when you hope to fit a curve of the form such as y=a (x^2)+b x+c, then the middle input would be {x^2, x, 1}; the 1 is because c is multiplied by 1.  In the case of z=a (x^2)+b x y+c (y^2), the middle input would be {x^2,x*y,y^2}.  These functions can be as involved as you like.

Example 1:  Best fit line.

Let us find the best fit line to the data from class:

data2={{1.0,3.6},{2.1,2.9},{3.5,2.2},{4.0,1.7}};
fitline2=Fit[data2, {x,1},x]

By assigning the answer to the name fitline2, we can use it later.
For instance, we can plot the data and the line to see the fit:

plot1=ListPlot[data2,PlotStyle->{PointSize[Medium]}];
plot2=Plot[fitline2,{x,0,5}];
Show[plot1,plot2]

Example 2: Best fit parabola.

Alternatively, we can find the best fit parabola to the same data (y=a (x^2)+b x+c).

curve2=Fit[data2, {x^2,x,1},x]
plot3=ListPlot[data2,PlotStyle->{PointSize[Medium]}];
plot4=Plot[curve2,{x,0,5}];
Show[plot3,plot4]

Example 3: Multiple independent variables.

Here is an example which has multiple independent variables.  Suppose that we expect w=a x+b (y^2)+c sin(z)+d, and we have data:

wlist1={9,102,324,1232,3242,5312};
xlist1={1,6,29,100,243,1643};
ylist1={4,12,123,943,3333,9124};
zlist1={123,46,9,21213,83,23,2};

The datapoints are therefore of the form {(Subscript[x, i]),(Subscript[y, i]),(Subscript[z, i]),(Subscript[w, i])}; use the Table command to create these points:

data3=Table[{xlist1[[i]],ylist1[[i]],zlist1[[i]],wlist1[[i]]},{i,6}]

Now we find the best fit:

curve3=Fit[data3, {x,y^2,Sin[z],1},{x,y,z}]

Troubleshooting.

You may get an error like the following:

fitline2=Fit[data2, {q,1},q]

This error occurs because the variable q was defined before (I defined it as 1 at the beginning of the document).  Mathematica sees that q is not a variable, so it can not be in the third part of the input.  We can fix this by removing q from Mathematica’s memory using the command Remove and re-evaluating the command.

Remove[q]
fitline2=Fit[data2, {q,1},q]

Introduction to FindFit

When you want to find a best fitting function when the relationship between the constants and the data is not as simple, use the command FindFit.  For example, you would use FindFit to determine the constants a, b, c, and d in curves of the form a x ln(b+c x) or a+b sin(c x+d).

Let’s analyze the structure of a FindFit command.  The following command finds the least squares best fit line z=c y+(x^k).  
[Note: We can not use Fit because a constant we want to find (k) is in the exponent.]

data4={{0.1,1,10},{1,0,23},{3,2,50},{5,4,100}};
FindFit[data4,c*y+x^k, {c,k},{x,y}]

For the FindFit command, there are always four inputs: the data points, the function we wish to fit, the constants to be found, and the variables.
   Just as in Fit, the data is a list of data points that looks like {(Subscript[x, i]),(Subscript[y, i])} (when y is supposed to be a function of x), or {(Subscript[x, i]),(Subscript[y, i]),(Subscript[z, i])} (when z is a function of x and y).  Data points involving more variables are also allowed; the independent variables always come first and the dependent variable is at the end.
   The second input is the function involving the independent variables and the constants that need to be found; this is different from what was required in Fit since the relationship between the constants and variables can be more complicated. This function can be as involved as you like.
   The third input is now the list of constants to be found, so that Mathematica can distinguish between these and the independent variables in the function you provide.
   Just as in Fit, the last input is the list of variables involved; this lets you name the independent variables to your liking.  For example, when the data points are {x,y}, the last input would be x; if the data points are {x,y,z}, the last input would be {x,y}.  
   Unlike as in Fit, the output of FindFit is a list of rules which explains what the values of the constants are.

Example 4: Non-linear example.

Suppose we have some x and y values that we expect to fit to a curve of the form y=C (x^k).  The way to do this in Mathematica is:

(* First define all the required datapoints *)
xlist2={1,2,3,4,5,6};
ylist2={0.5,3,9,21,42,72};
data5=Table[{xlist2[[i]],ylist2[[i]]},{i,6}];

(* This line of code does the linear fit *)
FindFit[data5,c*x^k, {c,k},{x}]

That means that the data should fit the curve y=(0.334263)x^(2.99898), which we can verify by plotting it against the original data:

curve=(0.334263)x^(2.99898);
plot5=ListPlot[data5,PlotStyle->{PointSize[Medium]},PlotRange->{0,80}];
plot6=Plot[curve,{x,0,7}];
Show[plot5,plot6]

Calculating (R^2) in Mathematica

How good of a fit was the best fit line from Example 1?  Recall, the data and fit was

data2={{1.0,3.6},{2.1,2.9},{3.5,2.2},{4.0,1.7}};
fitline2=Fit[data2, {x,1},x]

We calculate (R^2) by using the command LinearModelFit.  This command uses the same syntax as Fit; however, the output looks different.

model2=LinearModelFit[data2,{x,1},x]

After evaluation, there is a large amount of information stored in model2.  Among other things is the (R^2) value:

model2["RSquared"]

Also available is the list of residuals, which you could use to create a residual plot if desired.

residuals=model2["FitResiduals"]

As always, if you want more information about the command LinearModelFit, go to “Help > Documentation Center” and search for that command.

Example 5. Multiple Linear Regression

If instead there are multiple independent variables, we use LinearModelFit as we used Fit earlier.  Suppose that we expect w=a x+b y+c z+d, using the data from Example 3 above.  Then we find the model as follows

data7=Table[{xlist1[[i]],ylist1[[i]],zlist1[[i]],wlist1[[i]]},{i,6}]
model7=LinearModelFit[data7,{x,y,z,1},{x,y,z}]

And calculate (R^2) as before.

model7["RSquared"]

Non-linear models

In order to access additional information about non-linear models, use the command NonlinearModelFit instead of LinearModelFit.  NonlinearModelFit has the same syntax as FindFit.

Comprehension Questions

For the following data, determine 
(a) the best fit line 
(b) the best fit parabola
(c) the best fit function of the form y=C (x^k)
(d) the best fit function of the form y=C (e^(k x))
and then plot all these best fit functions on the same set of axes.  
Which one do you feel is the best fit of them all?

xlist3={1,3,6,8,9,10,11,14,15,24,27,33,36,42,43,48,51,56,58}
ylist3={7,10,55,92,106,102,93,115,135,234,267,301,301,322,333,371,392,395,378}

The list weatherData2 in the code below is the NYC temperature data we analyzed in class.  Fit this data to the function a+b*Sin[c x+d].  Compare the result to the answer given in class when we assumed values for c and d.

<<"Units`";
weatherData=WeatherData["KLGA", "MeanTemperature", {{2006, 1, 1}, {2008, 12, 31}, "Week"},"DateValue"];
weatherData2=Table[{(DateDifference[{2006,1,1},weatherData[[i,1]]]/365) ,ConvertTemperature[weatherData[[i,2]],Celsius,Fahrenheit]},{i,Length[weatherData]}];
graph1=ListPlot[weatherData2]

Answer to last question (Double click arrow at right to reveal)

(*  
ans=FindFit[weatherData2,a+b*Sin[c x+d], {a,b,c,d},{x}]
graph2=Plot[57.20670984722384`+21.37718639108152`*Sin[6.327995693879408` x-8.30774637570208`],{x,0,3}];
Show[{graph1,graph2}]
*)