


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
R code and output for calculating the correlation matrix, eigenvalues, and eigenvectors of a given matrix x. The analysis is divided into six parts: creating the square plot, calculating the correlation matrix and variance-covariance matrix, determining the determinant using the product of eigenvalues, finding the eigenvectors and eigenvalues, and finally, calculating the square root of the dot product of the mean vector and the inverse of the variance-covariance matrix.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



#Square plot par(pty = "s") #Set up some dummy values for plot a1<-c(-1,1) a2<-c(-1,1) plot(x = a1, y = a2, type = "n", main = "Problem 1", xlab = "X", , ylab = "Y") grid(nx = NULL, ny = NULL, col = "gray", lty = "dotted") abline(h = 0, lty = 1, lwd = 2) abline(v = 0, lty = 1, lwd = 2) arrows(x0 = 0, y0 = 0, x1 = 2/sqrt(5), y1 = 1/sqrt(5), col = 2, lty = 1) arrows(x0 = 0, y0 = 0, x1 = 1/sqrt(5), y1 = -2/sqrt(5), col = 2, lty = 1) -1.0 -0.5 0.0 0.5 1. -1. -0.
Problem 1 X Y
> # NOTES: If you have problems seeing what a function is doing, > # type and run help(function) where "function" is replaced > # with the function name. > X<-matrix(data =c(2, 2, 3, 1, 2, 4, 3, 5, 2, 6, 3, 4, 4, 6, 8), nrow=5, ncol=3) > X [,1] [,2] [,3] [1,] 2 4 3 [2,] 2 3 4 [3,] 3 5 4 [4,] 1 2 6 [5,] 2 6 8 > ####### > #Part a > R<-cor(x = X) #Also could do just cor(X) > R [,1] [,2] [,3] [1,] 1.0000000 0.6708204 -0. [2,] 0.6708204 1.0000000 0. [3,] -0.3535534 0.3162278 1. > sigma.hat<-var(x = X) > sigma.hat [,1] [,2] [,3] [1,] 0.50 0.75 -0. [2,] 0.75 2.50 1. [3,] -0.50 1.00 4. > # diag( ) finds the diagonal values > sum(diag(R)) [1] 3 > sum(diag(sigma.hat)) [1] 7 > ####### > #Part b > # Determinant using the product of the eigenvalues > # eigen( ) has two components - the values and the vectors > prod(eigen(R)$values)
[1,] 1.046255e- [2,] 3.930233e- [3,] -9.864885e- > ####### > #Part d > sum(eigen(sigma.hat)$values) [1] 7 > sum(diag(sigma.hat)) [1] 7 > prod(eigen(sigma.hat)$values) [1] 0. > ####### > #Part e > #rbind combines into one row the results of the mean functions > # If you have problems seeing what the function is doing, examine the individual mean functions first > mu.hat<-rbind(mean(X[,1]), mean(X[,2]), mean(X[,3])) > #Another way to get the mu.hat vector - this tells R to "apply" the function "mean" to each column (MARGIN=2) of the X matrix > mu.hat<-apply(X = X, MARGIN = 2, FUN = mean) > #t() finds the transpose > sqrt(t(mu.hat)%%mu.hat) [,1] [1,] 6. > #solve() finds an inverse of a matrix > sqrt(t(mu.hat)%%solve(sigma.hat)%%mu.hat) [,1] [1,] 5. > sqrt(t(X[2,]-mu.hat)%%solve(sigma.hat)%%(X[2,]-mu.hat)) [,1] [1,] 0. > ####### > #Part f > a<-c(2, -1, 3) > t(a)%%X[1,] [,1] [1,] 9