Curvilinear Regression:  Corn Storage

In this example, we use Mathematica's ability to create Regression or ANOVA tables. This is done by replacing the Fit[] command with the Regress[] command.  Regress is found in the Statistics`LinearRegression package.

Needs["Statistics`LinearRegression`"]

First we define the data:

Clear[data, x]; data
       = {81463, 43676, 24014, 10477, 83947, 42539, 20182, 7373, 104212, 
        87433, 36952, 17953, 80581, 46134, 24807, 9028, 97936, 52217, 26415}

Now we'll plot the data, using just points, and then using a linear spline:

\(p0 = ListPlot[data, PlotStyle -> {PointSize[.02]}]; 

    p1 = ListPlot[data, PlotJoined -> True, 
        AxesLabel -> {"Quarter", "Bushels"}]; \)

Let's fit a linear model to the data (although it seems intuitively inappropriate!), then we'll display the result against the data:

g[x_] = Fit[data, {1, x}, x]\(p3 = Plot[g[x], {x, 0, 20}, DisplayFunction -> Identity]; 

    Show[p0, p3, DisplayFunction -> $DisplayFunction]; \)

If we use the Regress[] command, rather than Fit[], we'll get the regression diagnostics:

Regress[data, {1, x}, x]

Now let's fit a more appropriate model to the data, including a wave of period 4 (since this is quarterly data in a system that should cycle every year). Then we'll plot the data:

f1[x_] = Fit[data, {1, Sin[((π x)/2)]}, x]\(p10 = Plot[f1[x], {x, 0, 20}, DisplayFunction -> Identity]; 

    Show[p0, p10, DisplayFunction -> $DisplayFunction]; \)

f1[x_] = 
      Fit[data, {1, Cos[((π x)/2)], Sin[((π x)/2)]}, x]\(p10 = Plot[f1[x], {x, 0, 20}, DisplayFunction -> Identity]; 

    Show[p0, p10, DisplayFunction -> $DisplayFunction]; \)

Again, if we want to see how good a fit we've got, we should use Regress[]:

Regress[data, {1, Cos[((π x)/2)], Sin[((π x)/2)]}, x]

Here is a model including a linear term: how do they compare?

Regress[data, {1, x, Cos[((π x)/2)], Sin[((π x)/2)]}, x]