I computed the vectorized form of an implied variance-covariance matrix using mxAlgebra in OpenMx. Now, I need to reconstruct this to a matrix in order to run the model. OpenMx has the function vech2full which does what I want for half-vectorized matrices, but I have a full vectorized matrix.
Thanks for your help!
Related
I am trying to calculate P^100 where P is my transition matrix. I want to do this by diagonalizing P so that way we have P = Q*D*Q^-1.
Of course, if I can get P to be of this form, then I can easily calculate P^100 = Q*D^100*Q^-1 (where * denotes matrix multiplication).
I discovered that if you just do P^5 that all you'll get in return is a matrix where each of your entries of P were raised to the 5th power, rather than the fifth power of the matrix (P*P*P*P*P).
I found a question on here that asks how to check if a matrix is diagonalizable but not how to explicitly construct the diagonalization of a matrix. In MATLAB it's super easy but well, I'm using R and not MATLAB.
The eigen() function will compute eigenvalues and eigenvectors for you (the matrix of eigenvectors is Q in your expression, diag() of the eigenvalues is D).
You could also use the %^% operator in the expm package, or functions from other packages described in the answers to this question.
The advantages of using someone else's code are that it's already been tested and debugged, and may use faster or more robust algorithms (e.g., it's often more efficient to compute the matrix power by composing powers of two of the matrix rather than doing the eigenvector computations). The advantage of writing your own method is that you'll understand it better.
I have 2 two dissimilarity matrices. One with observed data comparing among 111 sites and another generated using a null model.
I would like to use the adnois function in vegan to test whether the observed dissimilarities differ significantly from those expected by the null model. However the adonis function will only take one dissimilarity matrix on the left side of the formula.
Does anyone have any idea how to model this test?
Thanks
The answer to this problem was:
meanjac <- function(x) mean(vegdist(x, method='jaccard', diag=TRUE))
test <- oecosimu(x, nestfun=meanjac, method="r1", nsimul = 10^3, statistic='adonis')
which passes a function to get the mean of jaccard dissimilarity matrix to oecosimu, which then uses the 'r1' method to generate null community matrices by randomly shuffling the binary community matrix but assigning the probability of species occupancies based on their observed occupancy and comparing this to the observed dissimilarity matrix.
Thanks Jari for pointing me in the right direction...
According to the results I am getting ( I do not see that in the API), hclust works by using each row of a given matrix as a vector. Is there any way to work it so that it works with columns instead?
Besides, does dist work the same or does dist work with columns?
You can always apply hclust to transposed matrix:
# If you have observations matrix
m <- matrix(1:100, nrow=20)
hc <- hclust(dist(t(m)))
Besides, does dist work the same or does dist work with columns?
General convention is variables in columns, observations in rows and that's how dist works:
dist package:stats R Documentation
Distance Matrix Computation
Description:
This function computes and returns the distance matrix computed by
using the specified distance measure to compute the distances
between the rows of a data matrix.
Update
hclust works by using each row of a given matrix as a vector.
Actually internal implementation of hclust shouldn't matter. You pass as an argument dissimilarity structure produced by dist, and I am almost sure, that all metrics implemented in dist produce proper symmetrical distance matrix.
The R function dist "computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix".
However, I want the distance measure to be computed between the columns of a data matrix, not the rows! How can I do that?
Do I need to rotate the matrix. If so, how? If not, should I use a different function?
Maybe you can use R function t?
t(x) will transpose matrix x.
I can use ginv function from MASS library to get Moore-Penrose Generalisied Inverse of a matrix.
m <- matrix(1:9, 3, 3)
library(MASS)
ginv(m)
In SAS we do have more than one function to get a generalized inverse of a matrix. SVD can be used to find the generalized inverse but again this is a Moore-Penrose. I wonder if there any function in R to get a generalized inverse of a matrix (which is not unique) other than Moore-Penrose Generalisied Inverse. Thanks in advance for your help and time.
Edit
A generalized inverse of a matrix A is defined as any matrix G that
satisfies the equation AGA = A.
This G is not a Moore-Penrose Generalisied Inverse so it is not unique.
Most of the time you don't really want the inverse of a matrix, because the end result can be ruined by rounding errors by the time you're done.
It's more typical to create the LU decomposition using partial pivoting and scaling. Use it to perform forward/back substitution on right-hand-side vector to get the solution. This is especially helpful if you have multiple RHS vectors, because you can apply it repeatedly.
You need the Matrix package to do this.
Yes true, it's a great inconvenience R packages are no longer available. Alternatively you can use the pracma package.
And your Moore-Penrose Generalisied Inverse:
pinv(m)