Due: Tuesday, Nov 2, end of your lab session

Lab 6: Visualizing Regression

Today you will build a useful piece of software out of various related components. By combining your linear regression code from last week's exercise, and some new graphics routines you will write today, you will build a tool for consuming a dataset from a text file and producing a visualization of linear regression analysis on that dataset.

Preliminaries

Set the language level to Intermediate Student. Procure your linear regression code, and copy it into a file Lab6.ss. Download the necessary teachpacks from this page.

Use the following data file to test your work: discus.txt. The data gives the gold metal distance for discus throws in the Olympic Games from 1900 to 1992. The data is strongly linear. The x values are years since 1900 and the y values are distances in meters.


Part 1: Graphics

The first component is a visual tool for linear regression. You will write a function

;; linreg-image: dataset -> image

that, given a dataset, will produce a plot of the data overlaid with the best-fit line as determined by the analysis, as well as the text of the linear equation.

Your visual representation of linear regression analysis must include, at a minimum,

You may also include r2 if your code computes it.

You may break this problem into smaller pieces as you see fit. Here is a skeleton of a possible implementation, which you may use if you like:

In this lab, you should complete at least functions axes and plot-point. Make sure to include contracts, purpose statements and tests for all the functions you write.

[Note: Functions axes and plot-point should be finished by the end of the lab session. To receive full credit, you should also get started on the other helper functions. Turn in all your work at the end of the lab session according to these submission instructions.]


Part 2: File Input

[Note: If you have reached this far, well done! Part 2 will be part of this week's homework, but you can get started on it in the lab.]

Write a function

;; filename->dataset: string -> dataset

to produce a dataset from a file. The input argument to this function will be a string representing the full path to a data file.

Recall that the teachpack cs151-io.ss provides the following function:

;; read-from-file: string -> list

The function read-from-file takes one argument: a string which is the name of a data file. That file is read into a list, token by token. The value that results from a read-from-file is like any other Racket list and is ready to be used as such.

Use read-from-file as the basis for function filename->dataset.


Part 3: Putting it Together

[Note: Part 3 will be part of this week's homework.]

Finally, you will put the pieces together into a function

;; main: string string -> bool

which will consume the names of an input data file and an output data file, respectively, read the data in from the former and save an image to the latter.

The teachpack save-image.ss provides

;; save-image: img string symbol -> bool

Its first argument is the image to save, the second is the filename to be saved, and the third is the filetype to save. The save-image teachpack can save images in three formats: png, xbm, and xpm. For this exercise, use 'png for the filetype. The function returns true when the save operation was successful.

For example, assuming some-img is an image, you can say

(save-image some-img "~\Desktop\some-img.png" 'png)

The function save-image should allow you to implement main as sketched above.


Samples

As stated above, your image must include axes, datapoints, the best-fit line, and the text of the linear equation, as follows:
An image like this is sufficient for the purposes of this exercise.

If you are somewhat more ambitious, you can enrich the appearance of your image with various features, as in the following illustration:
(These images were produces with Excel, but could just as well have been produced by Racket programs.) This amount of detail is probably impossible in the lab time, but it should give you some ideas. Furthermore, you should know that you could produce such detail given enough time.


Hand in your work

To receive full credit, you should complete functions axes and plot-point of Part 1 and have made a good start on other helper functions of Part 1. This week's homework will include all parts of this lab.
Save all your files and submit all your work according to the submission instructions.


Material designed by Adam Shaw.