# Create a simple 3 X 2 matrix X and find its principal components and # singular values using the R function svd() # Corrected (see below) # Corrected again (extract named elements from the "results" list) # For the class # https://www.math.nyu.edu/faculty/goodman/teaching/MathFin2019/MathFinance.html # For use with Assignment 8, posted there # # The matrix X is # 1 2 # 0 1 # 1 0 X = matrix( nrow = 3, ncol = 2) # X[1,1] = 1 # X[1,2] = 2 # X[2,1] = 0 # X[2,2] = 1 # X[3,1] = 1 # X[3,2] = 0 # Credit to Leo Lin for finding a mistake in the statements commented # out above. X[1,1] = 1 X[1,2] = 2 X[2,1] = 0 X[2,2] = 2 X[3,1] = 1 X[3,2] = 0 result = svd(X) # svd() returns a named list with the elements of PCA # R has a "data structure" called "named list". # You get the element named "name" from a list object named "list" # with: list$name. sig = result$d # the singular values are named "d" U = result$u # the left principal components are the columns of "u" v = result$v # the right principal components are the columns of "v"