Project 11: Calculating Residuals

Suppose x and y are the following arrays:

x = [2, 2, 0, 2, 4, 6]

y = [6, 8, 7, 4, 4, 1]

The scatterplot below represents 6 points formed by corresponding x and y values.

These points are: (2, 6), (2, 8), (0, 7), (2, 4), (4, 4), (6, 1)

The line of best (LSRL) is also shown.  The equation for the LSRL is: Y = -1.5x + 9

Suppose we want the residual for the point at index 5 in the arrays.  Remember the array indexes start at 0, so this point corresponds to the point (6, 1).  

Residual = actual y - predicted y

The actual y-value for the point at index 5 is 1.  

The predicted y-value for the point at index 5 is 0.     Y = -1.5(6) + 9  = 0.

Residual = 1 - 0 = 1

Graphically, the residual for this point is 1 because the actual y-coordinate is 1 unit above the predicted y-coordinate (as shown in the graph).

Similarly, the residual for the point (2, 4) would be - 2.


This means the student who is taking 2 AP classes and has 4 absences has 2 less absences than we predict based on the equation.

Project 11: Variables x, y, and index have been initialized.  There is also a working method called predictY.

x is the array of x-coordinates for the points in the dataset

y is the array of y-coordinates for the points in the dataset

Note: the length of x and y will be the same.

index is the index of the point that we are calculating the residual for (5 in the example above)

predictY(x,y,xVal) returns the predicted y-value for the given xVal by the line of best fit.

Example: if and y are as shown in the example: predictY(x,y,2) would return 6.  The predicted value for x = 2 is 6.

Task: Appropriately initialize the value 'resid' which represents the residual for point at the given index.

Example: if x and y are as shown in the example and index is 5, then 'resid' should be 1.

**If your code works for 5 test cases, you can enter your e-mail address

Universal Computational Math Methods:

pow(5,2) returns 25.0

abs(-3.0) returns 3

sqrt(49.0) returns 7.0