 Statistikalla analysointi ja komputerissa on yhdistetty ja yhdistetty ja tehdä muiden operaatioon ja matriakseja. Mitä pitäisi yhdistää matriakseja, jos olet uusia ja kenen. Täällä on koko yhdistymä, miten matriakseja on käyttänyt. Ensimmäinen se on, että kun oletkaan hyvin basiivaa, tai basiivaa analysointi, jotta oletkaan yhdistetty, niin oletkaan kokeilla. Most econometric text, many text and structural equations modeling, for example status user manual, once you go beyond basic techniques, they switch to matrices because presenting complex models is a lot easier in this format and also it's easier to understand the ideas once you know what all this notation here means. So you need to be able to understand matrices ymmärrämää, mitä mietit. Ja ääniä, joka joskin on ääniä, on ymmärrää, että ymmärrää, mitä ääniä on. Tämä on ymmärrää ja kriitti, että ymmärrää nimenot on ymmärrää ja on ymmärrää, mitä kaikki on. Tämä on basically matrix weighted sum of squares covariancy. Sampi niin. Sitten tämän yksi asia on, että jos joku haluaa tehdä yksi alkuun, niin matriakka on käyttävä. Jos haluaisin alkuun, että modelin alkuun, ja matriakka ei ole mitään, niin voit vain ympäri matriakkaan ympäristöä, ja sitten haluat saada matriakkaan alkuun, jota tarvitsee. Jos haluat vaatia tekemään ympäristöä, että matriakkaan alkuun ei ole tärkeää, summa assumptions, understanding matrices can be useful if you end up doing a small simulation study. And finally, while you don't need to do very advanced matrix operations, sometimes just putting matrices together and taking sums of matrices is useful when you export your results from the statistical software for reproducibility. So exporting things should always be done programmatically instead of copy-paste and matrices can be helpful in some instances when you export the data. Let's take a look at what matrices are and what kind of calculations we can do with matrices. So matrix is basically a two by two array of numbers and it can be contrasted with scholars, which are single numbers. So scholar, we usually mark with a letter that is not bolded and its lower case is just a single number and then if we have a bolded number lower case that is a vector, which is basically a matrix, which is only one column or one row. So matrix has m columns and n rows, we always say they are, the m is the rows and n is the column. So you always first say the row number and then the column number. If they are m and n, number of rows and number of columns are the same, then it's called the square matrix. And if it's a square matrix, then it has a diagonal. So diagonal are the elements that are kind of like go from the top corner to the bottom corner and those are kind of like in the middle of the matrix here. So that's the diagonal. And then we have two other special areas in this matrix. We have the lower triangle here and usually correlation matrices in articles are lower triangle and sometimes for example reliability information is in diagonal or it can be just all ones because a variable correlates with itself at one and then upper triangle is typically left empty in a typical correlation matrix. So these are the basic elements of a matrix that is square and if the lower triangle or upper triangle are all the same, it's called a symmetric matrix. For example a correlation matrix is a symmetric matrix because if x correlates with y at point three, then y correlates with x at point three as well. And typically when we print out symmetric matrices, we just print out the lower triangle and possibly the diagonal here. So this is the basics of matrices. There are two special kind of matrices that you should know. One is the diagonal matrix which has entries only on the diagonal, all of diagonal entries are zeros. And this expression is useful for example if you want to present uncorrelated variables. So if you have like let's say random effects model and you want to have random effects that are uncorrelated or you have independent observations, then we would have for example independent observations, an analysis, then we would say that our error covariance matrix is diagonal, so the errors don't correlate between observations. And then we have identity matrix is all ones and identity matrix basically corresponds to the number one in scholars. So if we multiply a number with one, then we will get one, the original number, if we multiply matrix with an identity matrix, we will get the original matrix as a product. So these are these are the kind of matrices that we'll be dealing with in our econometrics books and in applied analysis. Let's take a look at some useful operations that you know. There are of course lots of matrix operations, lots of functions that you can apply to matrices, but they're only just these that you need to know to understand the basics. And knowing the determinant is only required because that is basically the equivalent of zero and you need to know that you can't divide with zero. So checking the determinant for whether some calculations are possible or not, that can be useful. So that's basically checking if your divisor is zero, this is the same as checking if determinant equals zero. Transpose addition, multiplication and inverse. So that's the things that you need to know. That's basically a plus and minus addition, multiplication, product and inverse division. So basic math skills. Let's take a look at what the transpose is first. So transpose takes an existing matrix and flips it so that the rows become columns and the columns become rows. And we use the prime symbol here to indicate that this is a transpose. And why we use transposes is that when you add matrices together or when you multiply matrices together, they need to be of certain form. So for example if you are these two matrices cannot be added together. So you can't add A and A prime because adding matrices together requires that they have the same column count and same row count. And we would need to transpose this again to make it possible to take sums of these. So transposes on a basic level, it can be understood that it's like a convenience operator. So when your matrices are not structured the right way, you can flip them so that they are and calculations become possible. And on the basic level you don't need to understand transposes any further than that. Then let's go to the actual operation. So matrix addition is simply calculating all the numbers together. So if A and B have the same dimensions and you take a sum, then the resulting matrix will contain the sums of each element of A with each element of B. So the first element of A plus B is going to be the first element of A plus first element of B and that's what there is to it. Let's take a look at the example in this data and here we define A which is a two by two matrix. We print it out to see that it is two by two, two rows, two columns and then we can generate matrix B as a sum of matrix A plus matrix A and print out so we can see that the first number here two is one plus one and so on. We can also see that calculating, taking, adding certain kind of matrices together is not possible if we define a matrix C containing one and two so that's actually a vector and it's called a row vector because it has a one row or column row vector because it has just one row and there you have it. You can't add a one by two matrix two by two matrix because they don't have the same dimensions and you would get a conformability error. So conformability error means that the matrices are not compatible for the operation that you want to carry out. That can sometimes happen and then it's useful to print out and check if they actually have the same dimensions. So that's the matrix addition and that's fairly very simple operation. Taking products of matrices is a bit more complicated but it is also a very very useful thing to know. So this is how Woolridge explains matrix multiplication and he is basically telling this in a very condensed way but let's take a step by step approach what matrix multiplication is. So it involves doing something with rows and columns and then multiplying elements together and taking a sum but to understand let's first look at matrix dimensions. So let's assume that we have a three by two matrix and two by three matrix that we want to multiply together. The first thing we need to understand is that not all matrices can be multiplied. You can take add all matrices together if your matrices are of different size they can be added. In matrix multiplication what is required is that the first matrix the column count must equal the row count of the second matrix. So these first numbers must be the same and or these like inner numbers and outer numbers they will be the dimensions of the new matrix. So when we multiply these two matrices together we get a three by three matrix and so it has nine different cells. We go from six cells and six cells to nine cells and then how do we populate the cells? Well we'll go row by row and column by column so we'll first take the first row of the first matrix and first column of the second matrix like so and we start looking at the number. So we'll take the first number of the first row and the first number of the first column we multiply them together and we add that to our matrix here. So that comes here to the first element and then we proceed to the second column of the first row and second row of the first column in the second matrix we multiply together and we add here. So this is a this is sums of products so we get sums of products of each element of the focal row and the focal column in this case we have just just two elements because this is two rows and this one has two columns. The second column which is the shift to the second column of the second matrix so that populates the second column we do the same calculation and we use the first row from the first matrix and second column from the second matrix that gives us the content and we go on like that for this full matrix. So why would this be useful and if you had to calculate all these things by hand it would take a long time and let's take a look at an application in where this is actually a useful thing to be able to do. So here is a simultaneous equations model so we have two dependent variables y1 and y2 and we have two predictors x1 and x2 and this is the model in matrix format so we have y is the y matrix the dependent variable values x is the x matrix the independent variable values beta is the matrix of regression coefficients and u is a matrix of error term values. So the xb gives the predictive values and over x beta depending on which way you write this second matrix and this is the reason why state as predict command has the b for the option xb when you want to do fitted values so it's x matrix the data matrix multiplied by the regression coefficient vector or matrix if you multiply equation models. Let's take a look at how this then how we calculate the model implied correlations between x's and y's well it is simply covariance between x and y equals covariance matrix of x multiplied by beta and how is that y is that the case if we start looking at how the numbers work let's write out the covariances. So the covariance matrix of x is symmetric we have variance of x variance of x1 and then these these covariances on on they are in the corners the upper triangle is covariances and lower triangle is the same covariances so this is a symmetric matrix then we have the regression coefficient matrix here and we multiply this together it gives us this kind of matrix so in which way is this a covariance matrix well we can take a look at we have the covariance between x1 and y is here in the first cell x y2 and x1 is here and then the covariances between y's and x2's are on the second row and if you want to verify that you actually get these same results by applying the path analysis tracing rules you're free to do so but this is just a much more convenient way of calculating this model implied covariances than trying to do this by hand or even doing it by excel because this just gives us we just tell our statistical software to multiply x with betas and that gives us the covariances instead of having to our to do this our manual calculations taking all possible paths multiplying things along the path and taking a sum of those paths and then doing that for four times for each covariance this is just a lot more convenient way of doing it okay so that is the basic of our product and now let's take a look at matrix inverse so matrix inverse is kind of like the same as dividing with the scalar so this is the inverse the idea with inversing in scalar is that if we have a divided by b that is the same as multiplying a with one divided by b which is the inverse of b and we can also write a divided by b as as a to the times b minus one which is the inverse of b and for scholars if we have a number and we divide the number with itself we get one in matrices the inverse is is kind of like dividing so if we multiply a by its inverse we get the identity matrix so identity matrix is is roughly the same as one in scholars and so this is we can if we have a times b and we want to understand what a is then we would multiply both sides of an equation with the inverse of beta or b let's take a look at an example on on how inverses could be used for and how we can use what we just learned to understand the regression analysis in in matrix four so this is again from Woolridge there is an appendix about regression analysis and how it's calculated in matrix four so we'll be taking a look at this regression model here so y is is x beta plus u so beta smaller lower case because this is a one regression model only so this is not the system of equations so we have just one set of regression coefficients x is the data matrix and u is the matrix of error term values or vector of error term values it is a vector because we have just one error term and we have values for each case y is a vector because we have just one dependent variable it has values for for each observation so let's take a look at what these equations mean so that's the model and then Woolridge explains that gives these two equations these are actually are two ways of understanding the OLS estimation criteria and how do we know what what is the meaning of of this this equation here what's the meaning of that equation here we need to first understand what is this x prime x so data matrix multiplied by its inverse and that's called a cross product matrix so what is the meaning of this cross product matrix once we understand that then it becomes much easier to understand what these equations actually tell us so the cross product matrix is here and we have the data matrix so we have k variables and in observations the first one is transposed so normally we have the observations on rows like you have in excel or in your statistical software and the variables and columns but this is transposed so the observations are on columns and then our variables are on rows and then we have the original matrix which is k variables and n observations so we need to first think about before we think of what's the meaning of the cross product matrix x prime x we need to understand the dimensions so this produces a matrix that is k by k so if we have five variables it's going to be a five by five matrix and we take our products of its value so product of the value for the first case plus the value of the first case times the value of the first case plus the value of the first case times the second case times the value of the second case plus the value of the third case times the value of the third case and so on so this is kind of like sum of squares right sum of squares for that variable and i'm going to simplify them out a bit and we're going to assume that all kaikki nämä varmastot ovat nimenomaan ympäristössä. Tämä just makes the math a bit easier, it doesn't affect the basic idea at all. So let's take a look at how, what this actually means. So that's the cross product matrix and we can start calculating and it's convenient to write x1i as like so. So we have x1 minus x1 bar, the mean of x1 plus x1 bar, the mean of x1. So we subtract the mean and we add the mean, that will not change this variable at all but will come to a convenient conclusion if we do it so. So we subtract mean and we add mean again. We can write out the product and from here these two lines actually cancel out because this is sum of all values of sum over all n and a mean-centered variable and the mean are uncorrelated so there's sum over all observations is going to be zero. So these cancel out and that's also because this is a mean of zero, the mean-centered value. So those cancel out and then this is going to be zero because we decided that our variables will be uncorrelated, will be mean-centered. So we'll just simplify the matter a bit. So we're left with this stuff here and if we divide this by n minus one and then multiply by n minus one again we can see that this is actually, this is covariance times sample size minus one. So when we have a cross-product matrix, this matrix is actually a scaled version of the covariance matrix. So how does that help us to understand these equations here? All right so we know that this is a scaled version of the covariance matrix and that we know that much. Let's take a look at what the other stuff here are. So we know that the data matrix times the regression coefficients that give us the fitted values. So that's the fitted values. The absurd values minus the fitted values is the residuals or the error term because this is the residuals because this is an estimate and then we take a product of the residuals, a matrix product with the residuals and the data matrix. This is the covariance between x and e or covariance between the residuals and the error term and we set those to be zeroes. So this equation e6, the idea here is that we have the residuals and we set them to be uncorrelated with the explanatory variables and this is like every correlation between every explanatory variable and the residual is zero and we can express all those correlations or covariances using this one simple line. So this is a very compact way of expressing this idea. So quite often when you multiply two sets of absurd values or you multiply absurd values and error terms, what you get is a scaled version of the covariance matrix. So what's the meaning of this equation here? Well x prime x times the regression coefficient is simply the model implied covariances and x prime y can be understood as the actual absurd covariances. So we can set the implied covariances to be the same as absurd covariances and that gives us the least squares results. So this is a very compact way of explaining regression analysis. Well regression is not that complicated so if you were to write down these as equations like the matrix content as well you would still end up with fairly understandable book but if you go to more complicated models then trying to write out the individual equations with our matrices is actually pretty hard and understanding the basic ideas is a lot easier from the matrices if you understand what the matrices mean. So how do we do OLS estimation then? Well one way to calculate OLS estimates and this is actually the way that your computer applies is to use these equations. So how do we get from this equation? How do we get this equation? Well we get it by multiplying both sides of equation seven with the inverse of x prime x and multiplying by inverse is basically the same as dividing so we just kind of like divide this x prime x beta with x prime x and it gives us the regression coefficients. So inverse is kind of like division and sometimes when you get an error message the matrix is not invertible then it means that this calculation here fails so you can divide and we'll take a look at what a non-invertible matrix would mean in this context. So this is actually the equation that your computer uses for the calculation. So computer does not minimize sum of squares it applies this matrix equation because it's a lot faster than trying to calculate the residuals and take their squares and try to minimize that using some optimization algorithm. No that's what the computer does for you. Now this can go wrong if there are the inverse cannot be found and to understand when inverses don't exist we need to understand the determinant and this is probably beyond trying to multiply two matrices that don't have compatible dimensions this is probably the most common problem that researchers can face. So what is the determinant? We can first take a look at scalar so every number we have the number line so we have a zero point we have negative numbers we have positive numbers and we have value 1.5 and the value 1.9 basically gives us a distance so 1.5 that's the distance from zero and zero is an important point because you cannot divide with zero so we know that from high school math dividing by zero is not possible. So how does this apply to matrices? Let's take a look at an example matrix. So our matrix is this 1, 2, 3, 2 and the matrix can be understood geometrically so we can understand that this matrix is two vectors so we have the first column and the second column and the first row gives the x values and the second column gives the y value so we can actually plot this coordinate system and then plot the first vector here so it goes up and right and the second vector goes up and right but with a different angle so that's our two vectors that this form this matrix and we can then plot there are the vectors so that they again so that they start from the end point of each other and we get this kind of geometric shape and this shape has an area so we cannot that area can be understood as the size of this matrix the same way that distance from zero can be understood as the size of the number and if this size of the matrix is zero then you can't divide or you can't invert that matrix and this size is given by the determinant so the area here is the determinant if this determinant is zero then we can't invert the matrix so we can't do our divisions with that matrix and when the when is the determinant zero it is zero when both these vectors go to the same direction for example here we are one two and two four they go to the same direction so the area is going to be zero and this if they go to the same direction it means that there is a linear dependency between the columns so we can say that one column is just the product of another column and this is the violation of the third regression assumption and so that would lead to a determinant that is zero so determinant of zero basically means that you have something that if you try to divide with is equivalent to dividing with a zero and that can be done so what you need to understand is the general concept of matrices it is has rows and columns and then there are numbers and there are just a few operations that you need to know transpose for convenience if two matrices can can't be multiplied together maybe if you transpose one maybe after that they can the same thing about adding together if you can't add together maybe transposing one would help or if you want to have a matrix that contains first the standard deviations means and then correlations maybe you need to transpose the standard deviation and core and mean vectors first before you can join them with the correlation matrix to make them the right shape then addition and multiplication and inverse are the basic operations that's like plus and minus product and division and determinant is useful to understand because that tells you when you can apply inverse or divide if you like to think about it that way