Computational HW 2

Computer Assignment 2
Linear Algebra Commands
- Created by James D. Wilson, UNC Chapel Hill
- Edited by Andrew Nobel, UNC Chapel Hill
In this assignment, we will explore how to run a few simple (but incredibly useful) linear algebra
commands in R .
1. Manually creating numeric arrays: The c( ) function can be used to manually create a numeric array
in R . When using the c( ) function, numbers are entered as a list with commas between each new
entry. For example, typing x = c(1,2) will create an array x = (1, 2).
To create an array of numbers that are repeated n times, we can use the rep( , n) function. For
example, an array of 5 1’s can be obtained by typing x = rep(1, 5).
Finally, we can create a sequence of numbers using the seq(from = , to = , by = ) function. Here, the
from, to and by arguments specify where the sequence begins, ends, and by how much the sequence
increments. For example, the vector (2, 4, 6, 8) can be obtained using x = seq(2, 8, 2).
Using the c( ), rep( ) or seq( ) commands, create the following 5 arrays:
x.1 = (2, .5, 4, 2)
x.2 = (1, 1, 1, 1)
x.3 = (−1, 0, 2, 1)
x.4 = (1, −.5, .25, 3)
x.5 = (3, 6, 9, 12)
Please include the code you used to create these vectors in your final printout.
2. Forming matrices from arrays: Once arrays have been generated in R , we can create matrices by
concatenating these as rows or columns. To concatenate a group of arrays as rows, we can use the
rbind( ) command. To concatenate a group of arrays as columns, we can use the cbind( ) command.
Create two matrices X and Y from vectors x.1, . . . , x.4 using the following code:
#Concatenate the vectors
X = rbind(x.1, x.2, x.3,
#Concatenate the vectors
Y = cbind(x.1, x.2, x.3,
as rows
x.4)
as columns
x.4)
1
3. Transpose and Checking Equality: We can calculate the transpose of a matrix by using the t( )
command. For example, try typing t(X) and look at the output.
Based on our construction, t(X) = Y. We can check that this is true by using a logical equivalence
command ==. When checking the equality of two matrices, the == command will check the entry by
entry equality of the two matrices. Importantly, the two matrices must be of the same dimensions.
Verify that t(X) = Y by using the command t(X) == Y. Verify that R will return an error if you
type t(x.1) == Y because x.1 and Y are not the same dimension.
4. Matrix Multiplication: Matrix multiplication is performed in R using the %∗ % command. For
example, to obtain the product AB of two matrices A and B you can type A %∗ % B. Calculate the
empircial covariance matrix of X using the following code:
#calculate the column means of X
col.means = colMeans(X)
#create a matrix of these column means repeated across the rows
X.bar = matrix(rep(col.means,4),ncol = 4, byrow = TRUE)
#column center X
X.col.centered = X - X.bar
#calculate the empirical covariance matrix of X
sigma = 1/4 * t(X.col.centered) %*% X.col.centered
Verify that sigma is symmetric using the t( ) and == commands.
It is important to distinguish the difference between the %∗ % command and the * command in R .
The * command is used for scalar multiplication and for entry-wise multiplication of matrices.
Answer the following questions:
Questions
(a) Write out sigma, the empirical covariance matrix of X
(b) Based on sigma, what is Cov(x.1, x.3)?
(c) What is X ∗ X? Note that this is different than what one obtains when typing t(X)% ∗ %X
5. Determinants and Inverses: The determinant of a matrix can be calculated easily using the det( )
command. Similarly, the inverse of a matrix can be calculated using the solve( ) command in R . For
example, we can calculate the determinant of X by typing det(X). Also, we can find the inverse of X
by typing solve(X). Using these commands, answer the following questions:
Questions
(a) What is the determinant of X and of Y ? What linear algebra fact explains the relationship
between det(X) and det(Y )?
(b) What is the inverse of X and of Y ? What linear algebra fact explains the relationship between
X −1 and Y −1 ?
(c) What is the determinant of the covariance matrix of X, sigma? Try finding the inverse of sigma.
What error does R report? Why is this happening?
6. Eigenvalues and Eigenvectors: The eigenvalues and eigenvectors of a matrix A can be calculated
simply by typing the eigen(A) command. The output of this command will contain two components:
the eigenvectors and the eigenvalues of the matrix A. Using the eigen( ) command, answer the
following questions:
Questions
(a) What are the first and second eigenvectors of sigma?
2
(b) What are the first and second eigenvalues of sigma?
(c) Let eig.1 be the first eigenvector of sigma. Based on the equations given in class, what is the
variance of the projections of x.1, x.2, . . . , x.4 onto eig.1? What is the relationship between this
variance and the first eigenvalue? Without calculations, what do you expect the variance of the
projections of x.1, x.2, . . . , x.4 onto the second eigenvector of sigma to be?
7. Norms: The Euclidean norm || · || of a vector can be calculated in R using the norm( ) command.
For example, to calculate the norm of the a vector u, we can type norm(u). Note that ||x||2 is the
sum of squared distance of entries of x.The default in R is to use the Euclidean norm; however, one
can specify other types of norms by using the type argument in norm(u, type =). Use help(norm) for
more information. Answer the following questions:
Questions
(a) What is the length (Euclidean norm) of x.1? (Calculate this using the function
norm(as.matrix(x.1)) since the norm( ) command requires matrix type arguments)
(b) Obtain the first column centered row of X by using z.1 = X.col.centered[1,]. What is the length
of z.1?
(c) Project z.1 onto the first eigenvector of sigma. What is the sum of squared distance between z.1
and its projection (t(z.1)%∗ %eig.1%∗ %eig.1)?
3