ONE OF THE USEFUL techniques of statistics is to look at the relationship — or correlation — between two variables. The “variables” are simply any quantities that vary — gross national product and literacy rates, murder rate and number of executions, hours spent in exercise and cholesterol levels, and so on.
The technical name for the measure of these kinds of relationships is the correlation coefficient. This technique was invented by Sir Francis Galton, a British scientist who was looking at the relationships between the heights of parents and their children as a means of discovering the laws of heredity. Karl Pearson, the great British statistician, realized that it was a general measure that could be used to investigate many kinds of relationships, and developed a formula for it. The correlation coefficient gives a numerical measure of the strength of the relationship between two variables, or varying quantities. The coefficient can range from +1.0 for a perfect positive relationship to -1.0 for a perfect negative relationship. A correlation of or near 0 means that there is no relationship, or that one variable cannot be predicted from the other.
When you are computing a correlation coefficient, it is also often useful to look at a graph or scatterplot of the relationship between the two variables. A scatterplot represents one variable on a vertical axis and the other on a horizontal axis, and contains points providing a visual illustration of the relationship. The more the points tend to form a straight line, the stronger the relationship.
I developed a program called KWIKPLOT to construct a scatterplot and compute a correlation coefficient using the T/S1000 ZX81. This program also computes a mean or average for each variable, since this was easy to include with the other computations and it can help in understanding data. These tasks are easily performed by program packages such as SPSS (Statistical Package for the Social Sciences) available on large university computers. But it was a bit more of a challenge to do it with the T/S1000 ZX81.
Data are input into Kwikplot with lines 100 to 160. The sample size is the number of pairs of observations, with each pair being made up of something like the height and weight from one case or individual. The correlation coefficient, along with the means of each of the two variables, is computed in lines 180 to 310 and printed in line 320. The range for each variable is found in lines 350 to 420. The ranges are necessary so the computer can label each axis with a scale of appropriate values. Lines 430 to 450 determine the values that will be printed in those scales. Lines 460 to 490 convert the original values of the X and Y variables to units that can be graphed on the screen. Lines 500 to 540 create a large array with 17 rows and 24 columns that corresponds to the 17 by 24 character portion of the screen where points will be graphed. These lines also fill the array with blanks. Lines 550 to 650 put an asterisk (*) in the array at the appropriate place for every point to be printed. If an asterisk is already there, these lines assign a 2, and so on. Lines 660 to 700 print the labels for the scale for the X-axis, the line making up the X-axis, and the points in the scatterplot. Line 710 prints the Y-axis. Line 720 prints the labels for the scale for the Y-axis.
The program uses variable names that are longer than one letter to help the user understand what is happening at various places. For instance, CROSS is the cross-product that is a basic part of the computation of a correlation coefficient, and XRANGE is the range of values taken by X.
An example of output is shown in Figure 1. The X-axis (vertical in this case) lists final grades in a communication theory class, with the highest possible score being 75 points. The Y-axis (horizontal in this case) lists number of absences during the semester. The lines at the top print the mean of X (MX), the mean of Y (MY), and the correlation coefficient (R). In this example, there is a moderate negative correlation of -.40 between the two variables. This correlation suggests that high numbers of absences are related to low grades, and vice versa. However, the fact that the correlation is moderate rather than strong suggests that attendance is not the only variable affecting grades. Other factors that might also influence grades are time spent reading the textbook, skill in taking notes while attending class, and time spent going over notes before a test.

A second example is shown in Figure 2. The X axis (vertical) is gas consumption in a residence measured in thousand cubic feet. The Y axis (horizontal) is a time dimension, with the numbers 1 to 24 standing for a 24-month period beginning in January. The correlation coefficient is probably not very meaningful here since the numerical values assigned to the months were arbitrary and not really measuring anything. The scatterplot shows that the relationship between gas consumption and time of the year has a great deal of regularity or lawfulness. In this case, we know that the regularity is caused by some very lawful changes in temperature as the seasons change. But in other cases that we might not understand so well, a scatterplot of this type could help us to discover new types of regularities, or laws.

KWIKPLOT has limitations. With certain types of numbers — those that are more than four digits, negative numbers, or numbers between 0 and 1 — the scatterplot will not be printed or will be misleading, although the correlation coefficient and means will be correct. Also, if either X or Y has no variation, you will get a scatterplot but there will be a message saying that the correlation coefficient cannot be computed.
The program has several features that are standard for large computer program packages but that might be unusual for small computers such as the T/S1000, ZX81. It labels the X and Y axes in an easily readable fashion, and these scales change according to the value of each data set. And, as mentioned before, if more than one observation occurs at the same point, the program prints a number on the screen instead of an asterisk to show the number of cases there.
Also, KWIKPLOT is fast. You can bet that Francis Galton and Karl Pearson didn’t have anything like it when they computed the first correlation coefficients 100 years ago!
The Kwikplot Program
10 REM "KWIKPLOT"
20 DIM A$(17,25)
30 LET MX=0
40 LET MY=0
50 LET CROSS=0
60 LET MSQX=0
70 LET MSQY=0
80 DIM X(100)
90 DIM Y(100)
100 PRINT AT 10,0;"INPUT YOUR SAMPLE SIZE."
110 INPUT N
120 CLS
130 PRINT AT 10,0;"INPUT YOUR DATA WITH EACH X FOLLOWED BY ITS Y."
140 FOR I=1 TO N
150 INPUT X(I)
160 INPUT Y(I)
170 CLS
180 NEXT I
190 FOR I=1 TO N
200 LET MX=MX+X(I)
210 LET MY=MY+Y(I)
220 LET CROSS=CROSS+X(I)*Y(I)
230 LET MSQX=MSQX+(ABS X(I))**2
240 LET MSQY=MSQY+(ABS Y(I))**2
250 NEXT I
260 LET MX=MX/N
270 LET MY=MY/N
280 LET SX=SQR ((MSQX/N)-(MX)**2)
290 LET SY=SQR ((MSQY/N)-(MY)**2)
300 IF SX=0 OR SY=0 THEN GOTO 330
310 LET R=((CROSS/N)-(MX*MY))/(SX*SY)
320 PRINT "MX=";MX;" MY=";MY;" R=";R
330 IF SX=0 OR SY=0 THEN PRINT "R IS NOT COMPUTABLE"
340 PRINT
350 LET XH=X(1)
360 LET YH=Y(1)
370 FOR I=1 TO N
380 IF X(I)>XH THEN LET XH=X(I)
390 IF Y(I)>YH THEN LET YH=Y(I)
400 NEXT I
410 LET XRANGE=XH
420 LET YRANGE=YH
430 LET IX=XRANGE/16
440 LET P=YRANGE/4
450 LET Z=XH
460 FOR I=1 TO N
470 LET X(I)=INT (((X(I)/XRANGE)*16)+1.5)
480 LET Y(I)=INT (((Y(I)/YRANGE)*24)+1.5)
490 NEXT I
500 FOR I=1 TO 17
510 FOR J=1 TO 24
520 LET A$(I,J)=" "
530 NEXT J
540 NEXT I
550 FOR I=1 TO N
560 IF A$(X(I),Y(I))="8" THEN LET A$(X(I),Y(I))="9"
570 IF A$(X(I),Y(I))="7" THEN LET A$(X(I),Y(I))="8"
580 IF A$(X(I),Y(I))="6" THEN LET A$(X(I),Y(I))="7"
590 IF A$(X(I),Y(I))="5" THEN LET A$(X(I),Y(I))="6"
600 IF A$(X(I),Y(I))="4" THEN LET A$(X(I),Y(I))="5"
610 IF A$(X(I),Y(I))="3" THEN LET A$(X(I),Y(I))="4"
620 IF A$(X(I),Y(I))="2" THEN LET A$(X(I),Y(I))="3"
630 IF A$(X(I),Y(I))="*" THEN LET A$(X(I),Y(I))="2"
640 IF A$(X(I),Y(I))=" " THEN LET A$(X(I),Y(I))="*"
650 NEXT I
660 FOR I=17 TO 1 STEP -1
670 IF I=1 THEN PRINT "0";TAB 4;"▌";A$(I)
680 IF I>1 THEN PRINT INT (Z+.5);TAB 4;"▌";A$(I)
690 LET Z=Z-IX
700 NEXT I
710 PRINT TAB 4;"[illegible]"
720 PRINT TAB 5,0;TAB 10;INT ((YRANGE-3*P)+.5);TAB 16;INT ((YRANGE-2*P)+.5);TAB 22;INT ((YRANGE-P)+.5);TAB 28;INT (YRANGE)
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.