Reader small image

You're reading from  The Statistics and Machine Learning with R Workshop

Product typeBook
Published inOct 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803240305
Edition1st Edition
Languages
Right arrow
Author (1)
Liu Peng
Liu Peng
author image
Liu Peng

Peng Liu is an Assistant Professor of Quantitative Finance (Practice) at Singapore Management University and an adjunct researcher at the National University of Singapore. He holds a Ph.D. in statistics from the National University of Singapore and has ten years of working experience as a data scientist across the banking, technology, and hospitality industries.
Read more about Liu Peng

Right arrow

Linear Algebra in R

The previous chapter introduced an efficient and effective reporting approach using R Markdown. Part 1 of this book essentially covered the practical aspects of getting things done using R. Part 2 of this book goes back to the fundamentals, covering two essential pillars of mathematics: linear algebra and calculus. Understanding these basics will better prepare us to appreciate and work with common mathematical operations to the point that these operations feel natural to us. Part 2 aims to develop that level of literacy, starting with a fundamental review of linear algebra with R in this chapter.

By the end of this chapter, you will have learned about the basic concepts of linear algebra, including vectors, matrices, and the system of equations. You will also be able to interpret basic notations in linear algebra and work with common matrices using R.

In this chapter, we will cover the following topics:

  • Introducing linear algebra
  • Common matrix...

Technical requirements

Introducing linear algebra

This chapter delves into one of the most important branches of mathematics: linear algebra. Linear algebra deals with linear operations of mathematical objects, including vectors, matrices, and tensors (high-dimensional matrices), the most common forms of data. For example, the typical table we use to store data in Excel consists of a series of columns. Each column is called a vector, which stores a specific number of elements and takes the form of a column instead of a row by default. A collection of these column vectors forms a matrix, a two-dimensional Excel table, or DataFrame, as we used to call it in the previous chapters. We can also view the same table as a collection of row vectors, where each vector lives in the form of a row.

Let’s put these in context. The following code snippet loads the sleep dataset and prints out the first six rows and three columns. We use A to denote this 6x3 matrix in the following exposition:

>>>...

Solving a system of linear equations

The matrix-vector multiplication operation gives rise to a system of equations. In a typical machine learning algorithm, data comes in the form of a matrix, X, and the target outcome is a vector, y. When the model that’s used is a straightforward linear model, we assume the input-output relationship as Xw = y, where w represents the vector of features/coefficients. An n x p matrix of input data multiplies a p x 1 vector, w, of features to produce, as expected, an n x 1 output vector, y. The essence of linear regress is thus to solve for the exact values in w such that the system of linear equations in Xw = y are satisfied.

The equivalence between matrix-vector multiplication and the system of linear equations may take some time to become noticeable. Let’s pause and look at this equivalence.

System of linear equations

We are already familiar with the process of calculating a matrix-vector multiplication operation. A 2x2 matrix...

Summary

In this chapter, we introduced the basics of linear algebra, including working with vectors and matrices and performing matrix-vector multiplication. We highlighted a few special matrices, such as the identity matrix, and common operations, such as transposing and inverting a matrix.

Next, we used matrix-vector multiplication to solve a system of linear equations under different settings. We introduced the geometric interpretation that corresponds to the system of linear equations, along with how to obtain the solution using matrix inverse and multiplication operations.

Lastly, we touched upon common settings of the input matrix in the machine learning context, covering both underdetermined and overdetermined systems. Developing such an understanding will be crucial when we delve into statistical modeling and machine learning in the third part of this book.

In the next chapter, we will discuss slightly more advanced concepts in matrix algebra and implementations in...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Statistics and Machine Learning with R Workshop
Published in: Oct 2023Publisher: PacktISBN-13: 9781803240305
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Liu Peng

Peng Liu is an Assistant Professor of Quantitative Finance (Practice) at Singapore Management University and an adjunct researcher at the National University of Singapore. He holds a Ph.D. in statistics from the National University of Singapore and has ten years of working experience as a data scientist across the banking, technology, and hospitality industries.
Read more about Liu Peng