2.5 Computer tasks

This Chapter’s computer tasks are short and sweet, as the focus has primarily been on the mathematics. Tasks for later chapters will be more challenging.

  1. Let’s consider some basic matrix computations in R. First, we show how to do matrix multiplication and addition
##      [,1] [,2]
## [1,]    3    1
## [2,]    1    6
##      [,1] [,2]
## [1,]    5    6
## [2,]    2    8
##      [,1] [,2]
## [1,]   17   26
## [2,]   17   54
##      [,1] [,2]
## [1,]    8    7
## [2,]    3   14
## [1] 2 2

Multiplication of a matrix by a scalar is easy - but be careful if you use the * for two square matrices, as R will do element-wise multiplication

##      [,1] [,2]
## [1,]    9    3
## [2,]    3   18
##      [,1] [,2]
## [1,]   15    6
## [2,]    2   48

Note that R won’t let you multiply matrices that are not conformable (i.e. not the right shape).

The usual Euclidean inner product is just matrix multiplication

##      [,1]
## [1,]   71

The inverse, determinant, and trace of a matrix are computed as follows:

##             [,1]        [,2]
## [1,]  0.35294118 -0.05882353
## [2,] -0.05882353  0.17647059
## [1] 17
## [1] 9

Note that numerical errors will start to appear quite quickly. For example, the following should return the identity matrix. The result is very close to the identity, but not exactly equal to it. With larger matrices, numerical errors can be worse and appear alarmingly quickly.

##              [,1] [,2]
## [1,] 1.000000e+00    0
## [2,] 5.551115e-17    1
  1. Solve the linear system for \(\mathbf x\) using R.

\[\left(\begin{array}{ccc} 3&2&1\\2&1&3\\ 1&3&2\end{array}\right) \mathbf x=\left(\begin{array}{c} 1\\1\\ 1\end{array}\right)\]

  1. Consider the iris dataset. Let \(\mathbf X\) be the 4 numerical variables
  • Compute the sample mean vector, the sample covariance matrix, and the sample correlation matrix for the four numerical variables using the in built R commands colMeans, ’cov, and cor.

  • Compute the centering matrix for \({\bf H}={\bf I}_n-n^{-1}{\bf 1}_n {\bf 1}_n^\top\) using \(n=150\) (the number of data points in the iris dataset), and check compute the column means of \(\mathbf H\mathbf X\) are all zero (or close - there will be numerical error). Compute the sample covariance and correlation matrices using \(\mathbf H\).

  • Check the properties of the centering matrix (you can ignore 7.) given in Section 2.4

  • What does the following command do?

Thus you’ll see that it usually isn’t worth computing the centering matrix when doing things in practice. We use \(\mathbf H\) in the description of the methods as it makes the mathematics easier to write down.

  • Compute the covariance matrix of \(\mathbf X\) directly (ie, don’t use the cov command - but do check your answer with cov).