Regression Analysis Using Maple

 

Overview:

 

we will use Maple here for several purposes:

 

1)  to graph data and to graph curves

2)  to perform computations generating "least squares fits"

3)  to gain an understanding of what a least squares fit is

 

 

Part One   Plotting Data with Maple

 

By "data"  we mean isolated points as opposed to continuous curves.  For example, we wish to plot the following data:

 

{ (0,1), (1,2), (2,5), (3,10), (5,26)  }

 

We will be working with this data for awhile so first we will break it up into two parts, the x values and the y values:

 

>x_val:=[0 , 1, 2, 3, 5 ];                note the use of [        ]  s

>y_val:=[1 , 2, 5, 10, 26];

 

to plot them, we will build a two dimensional array C as follows:

 

>> for i from 1 to 5 do C[i]:=[x_val[i],y_val[i]];od;

 

We next generate a plot but do not yet display it on the screen. We store this plot in a structure which we call  our_data_plot   as follows:

 

> our_data_plot:=plot([seq(C[i],i=1..5)],style=point):

 

Note the colon, :, at the end, as opposed to a semicolon. This suppresses output.

 

We can now easily see what this looks like on the screen by using the display function from Maple:

 

>display(our_data_plot);

 

You should see a parabolic looking set of points appear in red dots.

 

Next lets plot a regular parabola for comparison, say, y = x2+2.  This is much easier because it’s a continuous graph, vs discrete in the earlier case.  For reasons that will be clear shortly, we will do this in two steps:

 

   >parab_graph:=plot(x^2 + 2, x=0..5,color=green,thickness=2 ):    note colon again

  > display(parab_graph);

 

You should see a nice, smooth parabola drawn in green, fairly thick.

 

Finally, lets superimpose the two graphs:

 

> display( {parab_graph,  our_data_plot } );    note the  curly braces   {      }  which are needed anytime you plot more than one thing at once .

 

 

Part Two  Using Maple to Determine "Best Fit" Curves

 

This is pretty easy since Maple does the bulk of the work:

 

1) open the Stat library

 

            >with(stats);

 

2) have the x coordinates of your data in an array x_val  , as above in this page

and the y coordinates in  an array  y_val  also as above. Put them together into an array A:

              >A:=[x_val,y_val] ;

 

3)  Now get Maple to do the rest. (You might want to just Cut and Paste from here)

 

> quad_fit:=fit[leastsquare[[x,y],y=a*x^2+b*x+c]](A);

 

that's it! Stand back and read the results.

 

4) To see how well it worked, plot the data and the curve. First,

 

 > plot(rhs(quad_fit),x=0..4);

 

will plot just the curve. Then follow the instructions in Part One and you can get both the data points and the curve on the same plot and see how well the software has done in finding the best curve. (the "rhs" takes the equation stored in quad_fit and extracts just the "right hand side" of it, which is all we need to plot it, in case you were wondering)