Regression
r, Heat Map, & R-squared
2023-12-13
(1) r & Heat Map
r and R-squared are completely different concepts. Pearson correlation coefficient, or correlation coefficient r, calculates the correlation between two variables x and y using the following formula:
\(r_{xy}=\dfrac{\sum _{i=1}^n(x_i-\overline {x})(y_i-\overline {y})}{\sqrt{\sum _{i=1}^n(x_i-\overline {x})^2}\sqrt{\sum _{i=1}^n(y_i-\overline {y})^2}}\)
Heat Map is a representation of the correlation coefficient between independent variables in a sample dataset, displayed as varying colors on a two-dimensional plane. In Excel, it is typically presented as follows.

Step 0. Use the Boston housing data.

Step 1. Calculate the correlation coefficient between two columns (or rows) using the following formula:
=PEARSON(INDEX(housing!$A$2:$N$507,0,MATCH(HEATMAP!$A2,housing!$A$1:$N$1,0)),INDEX(housing!$A$2:$N$507,0,MATCH(HEATMAP!B$1,housing!$A$1:$N$1,0)))
The explanation for the Excel functions used here is as follows.
a) MATCH(reference_value, reference_array, 0) returns the position of the reference value within the reference array.
b) INDEX(reference_matrix, row_num, column_num) - Returns the value at a specified position in the reference matrix. If 0, returns the entire row or column.
c) PEARSON(x, y) = CORREL(x, y) calculates the correlation coefficient between the two vectors x and y.
Step 2. Using Conditional Formatting
To add conditional formatting to your spreadsheet, follow these steps:
Step 2.1 Select the range of cells that you want to apply conditional formatting to.
Step 2.2 Home-> Conditional Formatting->New Rule
Step 2.3 After setting like below, click on the "OK"

(2) R-squared
R-squared, or R2, is a commonly used statistic in statistics known as the coefficient of determination. It is used as a measure of the accuracy between observed data (y) and the model (f). It is defined as the proportion of the total variation (SStot) that is explained by the model. If R2 = 1, it means that the predictive model perfectly represents all the variation in the data.
\(R^2=1-\dfrac{SS_{res}}{SS_{tot}}\)
\(SS_{res}=\sum _i^{\ }(y_i-f_i)^2=\sum _i^{\ }e_i^2\)
\(SS_{tot}=\sum _i^{\ }(y_i-\overline {y})^2\)
The R-squared value is not actually a square, but rather a measure of expression. When the model is very poor, R-squared can have a negative value, which means that the predicted values of the model are less accurate than the mean value of y.

Note that RSQ function provided in Excel is the square of PEARSON R, so it is different from R-Squared.
(3) Adjusted R2
Adjusted R2 compensates for the increase in variables used in the regression equation, naturally improving (i.e., increasing) R2. It is defined as a compensation for the number of variables in relation to the size of the entire population.
\(R_{adj}^2=1-\dfrac{SS_{res}/df_{res}}{SS_{tot}/df_{tot}}\)
Here, df refers to the degrees of freedom used in the calculation, which is the size of the entire population. dftot is calculated as the sample size n-1, and dfres is calculated as the sample size n-p, where p represents the number of variables introduced for calculation. For example, if the equation is f=ax+b, then p would be 2 (i.e., a, b).