header

Homework 15

In this lab, you will write an application that will read paired data from a text file and calculate the correlation coefficient. You will write a method to read the input and another method to calculate the correlation coefficient.

Output Specifications

Display the correlation coefficient for paired data in a text file. Here is a sample run:

Name of the input file: PearsonRData.txt

r = 0.8827

Input Specifications

The data is read from an input file whose name is specified by the user. Each line in the file contains two double values. Both values correspond to the same subject in a statistical study. The first value is the value of the explanatory variable (x) and the second value is the value of the response variable (y). The exact number of lines in the file is not known ahead of time but you may assume that there will never be more than one hundred lines. Here is a sample file:

700 84.2
600 88.2
450 69.8
660 91.2
750 95.3

Process Specifications

Calculate the Pearson product moment correlation coefficient (r) using this formula:

Pearson R Correlation Formula

The summation symbol indicates that the value you are to use in the formula is the sum of the indicated values. You will need the sum of the x values, the sum of the y values, the sum of the squares of the x values (x2), the sum of the squares of the y values (y2), and the sum of the products of the paired x and y values (xy). The variable n is the number of paired values in the data set. Here are the needed sums based on the data given above:

Sums

Your application is to find these sums and use them to calculate the correlation coefficient.

Miscellaneous Specifications

Your application should include a value-returning method whose purpose is to read the paired data into a two-dimensional array and return the number of data pairs found in the file.

Your application should include a value-returning method whose sole purpose is to calculate the correlation coefficient. The parameters to this method are a two-dimensional array containing the paired data and the number of data pairs in the two-dimensional array.

The file Hmwk15.java contains the main program and declarations for the other two methods. Your job is to write the code for these other two methods.

Submitting Your Work

Submit your completed Hmwk15.java application as an attachment to an email message whose subject is "Hmwk15".