1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Linear Modeling in Python

Exercise

R-Squared

Previously, we expressed another measure of goodness, R-squared, in terms of a ratio of RSS to VAR. Multiplying top and bottom of the ratio by 1/n, the numerical equivalent form can be seen as the ratio of the variance of the residuals divided by the variance of the linear trend in the data we are modeling. This can be interpreted as a measure of how much of the variance in your data is "explained" by your model, in contrast to the spread or variance of the residuals (after you've removed the linear trend).

Here we have pre-loaded the data x_data,y_data and the model predictions y_model for the best fit model; your goal is to compute the R-squared measure to quantify how much this linear model accounts for variation in the data.

Instructions

100 XP
  • Compute the residuals, by subtracting the y_data from the y_model, and the deviations, by subtracting the y_data from the np.mean() of the y_data.
  • Compute the variance of the residuals and the variance of the deviations, using np.mean() and np.square() to each.
  • Compute the r_squared as 1 minus the ratio var_residuals / var_deviations, and print the result.