Least squares - 13 - Multiple linear regression - Matrix form and an example — Transcript
Full transcript
- 0:09to help us work with these multiple
- 0:11linear
- 0:12models efficiently we need to use some
- 0:15matrix notation we will look at that in
- 0:17this video the first step here is that
- 0:20we're going to remove the intercept from
- 0:22consideration this is not as bad as it
- 0:25sounds the intercept is seldom of
- 0:27interest and we can always recover it if
- 0:29we want to know it we remove it by first
- 0:32stating the regular regression model
- 0:34then in the second line we restate the
- 0:36regression model at the point which we
- 0:38know the model will pass through the
- 0:40averages thirdly we subtract the second
- 0:43line from the first line this creates a
- 0:47new regression model where our variables
- 0:48are in their centered form in other
- 0:50words instead of X I we use X I minus
- 0:54the mean of all X values instead of Y I
- 0:56we use Y I minus the mean of the Y's
- 0:59recall and centering is a process that
- 1:01simply shifts the variable but maintains
- 1:04all of the relationships that it has
- 1:06when we build our model with the
- 1:08centered x and y variables or as we
- 1:11sometimes say deviation variables we now
- 1:13have a model where the intercept is 0
- 1:15and does not need to be estimated notice
- 1:18from the second line over here we can
- 1:20always recover that intercept value back
- 1:22if we really are interested in it now
- 1:25sometimes you will see me build
- 1:27regression models with the intercept in
- 1:28it especially in the designed
- 1:30experiments part of the course so you
- 1:32really don't have to do this step but it
- 1:35does help for interpretation later on so
- 1:38now we have a model without an intercept
- 1:40but we have K parameters beta 1 beta 2
- 1:43up to beta K and each of them are
- 1:46multiplied by their corresponding X
- 1:47variable right at the end we have an
- 1:50error epsilon I and all of that is added
- 1:53up in a linear combination to give you a
- 1:55value of y I we can express this
- 1:58observation as a linear combination in
- 2:00vector form here we have a vector X and
- 2:03a vector beta and this is for the I
- 2:05thought servation so we collect our
- 2:08vector X the input data and the vector
- 2:11beta and we multiply them using regular
- 2:14matrix multiplication ad the epsilon
- 2:16value to the end and then we get our Y I
- 2:18value there are K values of X multiplied
- 2:22by their corresponding K coefficients of
- 2:24beta and we add
- 2:26to get our single y value and we will
- 2:29call that vector X I to emphasize that
- 2:32that is the row vector for the I thought
- 2:34servation of X now if we have multiple
- 2:38observations of X then we can stack
- 2:40those vectors in a matrix and form
- 2:43capital matrix X Row 1 is the first
- 2:46observation x 1 Row 2 for the next
- 2:49observation x subscript 2 and so on up
- 2:52to the last row x subscript n I've also
- 2:56replaced the beta's now with bees to
- 2:59emphasize that we're going to estimate
- 3:01the bee values rather than us knowing
- 3:03what the population model is so we have
- 3:05a matrix of X observations n rows and K
- 3:09columns there are n rows for every
- 3:11observation and K columns for the K
- 3:14input variables multiply that here by
- 3:17our vector b1 b2 up to be K so K entries
- 3:21of B and then we add a vector of errors
- 3:24at the end I've emphasized the
- 3:27dimensions of each of the vectors and
- 3:28matrices over here now the objective
- 3:31function for multiple linear regression
- 3:33is exactly the same as regular least
- 3:35squares minimize the sum of squares of
- 3:38the errors if we take that vector of
- 3:41errors over here lowercase e the sum of
- 3:44squares of the errors can easily be
- 3:46shown to be e transpose e well that
- 3:48vector e can be written as Y minus
- 3:51matrix X times vector B if we sub that
- 3:55in we get the second row over here and
- 3:57if we expand that matrix vector
- 3:59transpose we can use some basic rules of
- 4:02matrix vector algebra to get this
- 4:04revised objective function we've seen
- 4:07before that we can minimize the
- 4:08subjective function by setting the
- 4:10partial derivative of it with respect to
- 4:13the search variables to 0 this means the
- 4:16partial derivatives with respect to each
- 4:18of the entries in vector B are set equal
- 4:20to 0 this will give us K equations in K
- 4:24unknowns we have K partial derivative
- 4:27equations and there are K values of B b1
- 4:30b2 up to BK now these K linear equations
- 4:34in K unknowns can be solved very
- 4:36compactly using what is probably the
- 4:38best-known
- 4:39formula in least-squares that the
- 4:41entries in vector B are equal to X
- 4:43transpose X inverse multiplied by X
- 4:46transpose Y don't forget that formula
- 4:49you're going to see it reoccurring
- 4:51several times in this module as well as
- 4:53in the next module on designed
- 4:55experiments and it's time for an example
- 4:58now where we've got two vectors X 1 and
- 5:00X 2 shown here on the screen let's use
- 5:03these to predict the vector Y the first
- 5:06step we're going to have to take is to
- 5:07Center the vectors X and the vector Y
- 5:09will do that and the results are shown
- 5:12here but before we go ahead and even
- 5:14solve this equation what do you expect
- 5:17the slope to be for X 1 as it relates to
- 5:20Y and what do you expect the slope to be
- 5:22for X 2 as it relates to Y examine the
- 5:25data and write down your answer keep it
- 5:27aside and we'll verify it later now
- 5:30let's go solve this mathematically and
- 5:32try to verify our intuition the first
- 5:35step we need to do is assemble those two
- 5:37individual vectors into a matrix capital
- 5:40X the first column will represent the
- 5:43entries for variable X 1 the second
- 5:45column in matrix X represents the second
- 5:48variable X subscript 2 notice that we
- 5:51have n observations 6 in this case so
- 5:54there must be six rows in the X matrix
- 5:56and two columns one for each of the
- 5:58input variables X is always an N by K
- 6:01matrix and the Y vector is always an N
- 6:04by 1 vector of the output values these
- 6:07are assembled now as shown here on the
- 6:09screen so we are ready to go calculate
- 6:12vector B the two coefficients of our
- 6:15regression model X transpose X inverse
- 6:17multiplied by X transpose Y let's go
- 6:20calculate the first part X transpose X
- 6:23that's shown over here then we can go
- 6:26calculate X transpose Y separately shown
- 6:29over here you're welcome to use any
- 6:31software tool to do this but here on the
- 6:34screen are the our instructions in case
- 6:36you wanted to try it over there but we
- 6:38actually need the inverse of X transpose
- 6:40X and you can also do that in our with
- 6:43this line of code so now we have X
- 6:45transpose X inverse and we can go
- 6:47multiply it by X transpose Y
- 6:50I want to point out that X transpose X
- 6:53will always be a K by K matrix K being
- 6:56the number of variables in our model in
- 6:58this case K is 2 it is going to be an
- 7:01important point later on that the
- 7:03entries in X transpose X represent the
- 7:06covariance of the X variables with each
- 7:08other for example the entry in the one
- 7:11two position in this matrix is the same
- 7:14as the entry in the to one position
- 7:15that's because the covariance of
- 7:18variable X 1 with variable X 2 is the
- 7:21same as the covariance of variable X 2
- 7:24with variable X 1 you'll recall that
- 7:26from an earlier video and that that
- 7:28should be the same number the entries on
- 7:30the diagonal are also covariances but
- 7:33they're covariances of the variables
- 7:35with themself so we simply call that
- 7:36variance in this case the 55.5 over here
- 7:40is directly proportional to the variance
- 7:43of the x one variable the 62 is
- 7:45proportional to the variance of the X 2
- 7:48variable because the diagonal entries in
- 7:51X transpose X are variances they should
- 7:53always be positive variances are by
- 7:56definition positive values and the off
- 7:58diagonal elements in X transpose X will
- 8:01always be symmetrical once you've
- 8:02calculated the upper diagonal part you
- 8:05can just copy the values over to the
- 8:07lower diagonal part now here's a
- 8:09question I want to leave for advanced
- 8:11students what will the x transpose x
- 8:13matrix look like when the variables x1
- 8:15and x2 and so on are entirely
- 8:18uncorrelated with each other what will
- 8:20you observe in those off diagonal
- 8:22entries you'll see the answer to that
- 8:25coming up in the next module but you
- 8:27should be able to guess what it is now
- 8:29now if x transpose x represents
- 8:32variances and covariances what do you
- 8:34think the entries in X transpose Y
- 8:36represents
- 8:38if you guess that it's the covariance of
- 8:40the entries of X with Y you write the
- 8:43first entry in the X transpose Y result
- 8:45is the covariance of variable x1 with Y
- 8:49and the second entry is the covariance
- 8:52of X 2 with Y now take a look at the
- 8:55signs do they match your expectations
- 8:57what you wrote down earlier look back at
- 9:00the raw data and make sure at least that
- 9:03the sign information matches your
- 9:05expectation remember right at the start
- 9:08of the section on linear models we said
- 9:10that for covariances at the very least
- 9:12you can interpret the sign of that
- 9:14result the magnitude of the value isn't
- 9:16so useful and those results that we see
- 9:19there the 36.5 and the -36 seem to make
- 9:23sense the positive sign for the first
- 9:25entry indicates that the first X
- 9:27variable is positively correlated with
- 9:30the Y variable and we see that in the
- 9:32raw data is that first column increases
- 9:34the column of Y also increases the
- 9:37second entry in X transpose Y is
- 9:39negative indicating that that column in
- 9:41X has a negative correlation with Y and
- 9:44that's also observed in the raw data
- 9:46there
- 9:48now one final thing to mention before we
- 9:50end this video is that you should never
- 9:52calculate your least squares model using
- 9:54this X transpose X inverse formula
- 9:56multiplied by X transpose Y for real
- 9:59general data it works in some specific
- 10:02cases very well and that's why we will
- 10:04use it later on but in general there's
- 10:06very poor numerical conditioning with
- 10:08calculating the inverse in this way
- 10:10sophisticated least squares packages
- 10:13such as R will calculate the linear
- 10:15model using the QR decomposition instead
- 10:18it's a similar type of linear algebra
- 10:20step and interested students should go
- 10:22investigate that in their own time the
- 10:25our software package uses the QR
- 10:27decomposition behind-the-scenes and not
- 10:29this inverse and since we're talking
- 10:31about R let's go see now how R solves
- 10:34this example problem use the LM command
- 10:38and say why is regressed on two
- 10:40variables x1 plus x2 try that out and
- 10:44use the summary command on that model
- 10:46result to see if it matches the output
- 10:49that you calculated with the X transpose
- 10:51X inverse X transpose Y equation there
- 10:54is one last entry we need to go
- 10:56calculate and that is the standard error
- 10:57standard error you will recall is equal
- 11:00to the objective function of the least
- 11:01squares model divided by n minus K and
- 11:04then we take the square root so in other
- 11:07words standard error is calculated as e
- 11:09transpose e divided by n minus K and
- 11:12then we take the square root once we
- 11:14have that error vector it is easy to go
- 11:16calculate the standard error in the next
- 11:19video we're going to look at the
- 11:20important topic of interpreting the
- 11:22multiple linear regression coefficients
About this transcript
This page contains the full transcript of Least squares - 13 - Multiple linear regression - Matrix form and an example by K Dunn, generated from the public captions YouTube serves with the video. The transcript has 1,983 words across 273 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.