How to perform Linear Algebra in the RStudio IDE? Let’s grab some coffee and dive into it together or bookmark it as your future 101 cheat sheet.
Overview:
Key terminologies: Linear algebra, scalar, vector (orthogonal and linear independent), vector space, and matrices.
Matrices Transformation: Transpose, multiplication (with a scalar and a matrix), determinant, and inverse.
Types of matrices: square, symmetric, diagonal, identity, correlation, and covariance.
I) Key terminologies:
Linear Algebra: a branch of mathematics that deals with vectors, matrices, vector spaces, linear transformation.
Scalar (denoted c): a real single numeric number that has magnitude but no “direction”. Ex: speed.
Vector: an ordered list of scalars/numbers that has both magnitude and direction. Ex: velocity. All vectors live within a vector space.
Orthogonal vectors: Two vectors x and y are orthogonal if their dot product is equal to 0 as ⟨x, y⟩ = 𝑥1𝑦1+𝑥2𝑦2+…+𝑥𝑛𝑦𝑛 = 0.
Linear Independence: Two vectors x and y are linear independent if x ≠ cy for any scalar constant c.
Matrices: a collection of scalars/numbers. If a matrix has n rows and m columns, we will refer to it as a n×m matrix; hence, we can think of a vector as a matrix that has only one row or one column.
II) Matrices Transformation:
- Transpose: creates a new matrix with the number of columns and rows being flipped.
2. Multiplication with a scalar/number:
3. Multiplication between matrices: can only be performed if the number of rows in B and the number of columns in A are the same.
4. Determinant: a scalar value that helps find the inverse of the matrix.
5. Inverse of a matrix: fun fact, only square matrices can have an inverse; however, not all square matrices are invertible.
III) Types of Matrices:
- A square matrix: a matrix where the number of rows n equals the number of columns m. For instance, a 3*3 square matrix has 3 rows and 3 columns.
2. A symmetric matrix: a type of square matrix where the top-right triangle is the same as the bottom-left triangle.
3. A diagonal matrix: a matrix where values outside of the main diagonal have a zero value. If an n*n matrix has the diagonal elements of 1s and other elements of 0s, we call it identity matrix.
4. Correlation matrix: shows the strength and direction of the linear relationship between two variables. R syntax for returning a correlation matrix is cor(matrixName).
The coefficients are in the range [-1,1]. The value of -1 indicates a perfect linear negative relationship between two variables, whereas +1 gives us a sense of a perfect positive linear relationship between two attributes. 0 means no linear relationship exists. Last but not least, the diagonal elements in the matrix, which are the correlations of variables with themselves, always have value of 1.00.
5. Covariance matrix: show how data spread among two dimensions (direction/sign). R syntax for returning a covariance matrix is cov(matrixName).
Covariance values do not have a limit between -1 and 1 ; instead, they are in the range (-∞,+∞). For example, if two variables have a positive covariance value, that implies they move in the same direction, whereas a negative covariance gives us a sense that the variables move in opposite directions.
The value between the same variables indicates the “variance”. The non-diagonal values show the “covariance” of each variable.
In short, Covariance signifies the direction of the linear relationship between the two variables, while Correlation shows both direction and strength of the relationship.