We will now extend the method of least squares to equations with multiple independent variables of form
As in Method of Least Squares, we express this line in the form
Given a set of n points (x11, …, x1k, y1), … , (xn1, …, xnk, yn), our objective is to find a line of the above form which best fits the points. As in the simple regression case, this means finding the values of the bj coefficients for which the sum of the squares, expressed as follows, is minimum:
where ŷi is the y-value on the best fit line corresponding to x, …, xik.
Definition 1: The best fit line is called the (multiple) regression line
Theorem 1: The regression line has form
where the coefficients bm are the solutions to the following k equations in k unknowns.
Click here for a proof of Theorem 1 (using calculus).
Observation: We can use either the population or sample formulas for covariance (as long as we stick to one or the other). Thus, we can use the Excel function COVAR for the population covariance (or COVARIANCE.P in Excel 2010/2013) or COVARIANCE.S for the sample covariance in Excel 2010/2013 (or the supplemental function COVARS), although as we will see below there are ways of calculating all the covariances together. Note too that where j = m
Example 1: A jeweler prices diamonds on the basis of quality (with values from 0 to 8, with 8 being flawless and 0 containing numerous imperfections) and color (with values from 1 to 10, with 10 being pure white and 1 being yellow). Based on the price per carat (in hundreds of dollars) of the following 11 diamonds weighing between 1.0 and 1.5 carats, determine the relationship between quality, color and price.
Figure 1 – Data for Example 1
As described above, we need to solve the following equations:
where x1 = quality and x2 = color, which for our problem yields the following equations (using the sample covariances that can be calculated via the COVAR function as described in Basic Concepts of Correlation):
For this example, finding the solution is quite straightforward: b1 = 4.90 and b2 = 3.76. Thus the regression line takes the form
Using the means found in Figure 1, the regression line for Example 1 is
(Price – 47.18) = 4.90 (Color – 6.00) + 3.76 (Quality – 4.27)
Price = 4.90 ∙ Color + 3.76 ∙ Quality + 1.75
Thus, the coefficients are b0 = 1.75, b1 = 4.90 and b2 = 3.76.
Observation: The fact that coefficient b1 is larger than b2 doesn’t mean that it plays a stronger role in the prediction described by the regression line. If, however, we standardize all the variables that are used to create the regression line, then indeed the coefficients that have a larger absolute value do have a greater influence in the prediction defined by the regression line. Note that if we do this the intercept will be zero.
Observation: With only two independent variables, it is relatively easy to calculate the coefficients for the regression line as described above. With more variables, this approach becomes tedious, and so we now define a more refined method.
Definition 2: Given m random variables x1, x2, …, xm and a sample x1j, x2j, …, xnj of size n for each random variable xj, the covariance matrix is an m × m array of form [cij] where cij = cov(xi, xj). The correlation matrix is an m × m array of form [cij] where cij is the correlation coefficient between xi and xj.
The sample covariance matrix is the covariance matrix where the cij refer to the sample covariances and the population covariance matrix is the covariance matrix where the cij refer to the population covariances.
Since the corresponding sample and population correlation matrices are the same, we refer to them simply as the correlation matrix.
Property 0: If X is the n × m array [xij] and x̄ is the 1 × m array [x̄j], then the sample covariance matrix S and the population covariance matrix Σ have the following property:
Example 2: Find the regression line for the data in Example 1 using the covariance matrix.
The approach is described in Figure 2.
Figure 2 – Creating the regression line using the covariance matrix
The sample covariance matrix for this example is found in the range G6:I8. Since we have 3 variables, it is a 3 × 3 matrix. In general, the covariance matrix is a (k+1) × (k+1) matrix where k = the number of independent variables. The sample covariance matrix can be created in Excel, cell by cell using the COVARIANCE.S or COVARS function. Alternatively, using Property 0, it can be created by highlighting the range G6:I8 and using the following array formula:
(see Matrix Operations for more information about these matrix operations). The sample covariance matrix can also be created using the following supplemental array function (as described below):
Note that the linear equations that need to be solved arise from the first 2 rows (in general, the first k rows) of the covariance matrix, which we have repeated in the range G12:I13 of Figure 2.
Solving this system of linear equations is equivalent to solving the matrix equation AX = C where X is the k × 1 column vector consisting of the bj, C = the k × 1 column vector consisting of the constant terms and A is the k × k matrix consisting of the coefficients of the bi terms in the above equations. Using the techniques of Matrix Operations and Simultaneous Linear Equations, the solution is given by X = A-1C. For this example the solution A-1C is located in the range K16:K17, and can be calculated by the array formula:
Thus b1 is the value in cell K16 (or G20) and b2 is the value in cell K17 (or G21). The value of the coefficient b0 (in cell G19) is found using the following Excel formula:
Real Statistics Excel Support: The Real Statistics Resources Pack provides the following supplemental array functions:
COV(R1, b) = the covariance matrix for the sample data contained in range R1, organized by columns. If R1 is a k × n array (i.e. variables, each with a sample of size n), then COV(R1) must be a k × k array.
COVP(R1, b) = the population covariance matrix for the data contained in range R1. The result is the same as COV(R1) except that entries use the population version of covariance (i.e. division by n instead of n – 1).
CORR(R1, b) = the correlation matrix for the data contained in range R1.
If b = TRUE (default) then any row in R1 which contains a blank or non-numeric cell is not used, while if b = FALSE then correlation/covariance coefficients are calculated pairwise (by columns) and so any row which contains non-numeric data for either column in the pair is not used to calculate that coefficient value.
The Real Statistics Resource Pack also contains a Matrix Operations data analysis tool which includes similar functionality.
Observation: Let R1 be a k × n range which contains only numeric value, let R2 be a 1 × n range containing the means of the columns in R1 and let R3 be a 1 × n range containing the standard deviations of the columns in R1. Then
COV(R1) = MMULT(TRANSPOSE(R1-R2),R1-R2)/(ROWS(R1)–1)
CORR(R1) = MMULT(TRANSPOSE((R1-R2)/R3),(R1-R2)/R3)/(ROWS(R1)–1)