Linear Algebra in R programming

Habbee
4 min readFeb 10, 2022

--

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.

Coffee cup
Image by author.

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.

Create a vector a in R, printed in a “row” format.

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.

The dot product is 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.

Create a 3-by-3 matrix A in R, using byrow=TRUE (printed horizontally).
Create a 3-by-3 matrix A in R, using byrow=FALSE (printed vertically).

II) Matrices Transformation:

  1. Transpose: creates a new matrix with the number of columns and rows being flipped.
Matrix A is transposed using the syntax t(A).

2. Multiplication with a scalar/number:

Using the “*” symbol to multiply a matrix with c.

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.

Using the “%*%” symbol to multiply a matrix with a matrix.

4. Determinant: a scalar value that helps find the inverse of the matrix.

Find the determinant of matrix A by using the syntax det(A).

5. Inverse of a matrix: fun fact, only square matrices can have an inverse; however, not all square matrices are invertible.

Find the inverse of a matrix using the syntax solve(A).

III) Types of Matrices:

  1. 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.
A square matrix.

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.

Use the syntax diag(A) to print the diagonal elements of a diagonal 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.

Image by author.

--

--