Hello everyone! So... I am trying to learn how to correctly use the 'apply' function while learning something about the Wishart distribution.
R has a function called 'rWishart' where you can sample random positive-definite matrices and store them in lists. From the official documentation I took this example:
I kind of understand what's happening here. R is an array and there are matrices here. In mR the code is saying to take the mean of average of all the 5 matrices. I thought that by doing the following I could get the eigenvalues of said matrices using the 'eigen' function
But it gives me this error
Does anyone have any idea of how it should be done using apply? This is relatively straight-forward using a for-loop but I would really like to learn how to do it with 'apply'. The endgoal would be to not only obtain the eigenvalues but also to average them so the first eigenvalue of the first matrix plus the first eigenvalue for the second matrix plus the third eigen value of the third matrix, etc... and then the average of that, so in the end I can end up with the averages of all the eigenvalues.
But first things first. Any ideas of how to do this with 'apply'?
Thank-you!
R has a function called 'rWishart' where you can sample random positive-definite matrices and store them in lists. From the official documentation I took this example:
Code:
R <- rWishart(3,5,diag(5))
mR <- apply(R, 1:2, mean)
Code:
mR <- apply(R, 1:2, eigen)
Code:
Error in FUN(newX[, i], ...) : non-square matrix in 'eigen'
But first things first. Any ideas of how to do this with 'apply'?
Thank-you!