Generalized Inverse in R - r

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)

Related

Diagonalize a matrix to compute matrix power?

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.

Converting a vector to full matrix in OpenMx

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!

Quadratic Objective with Quadratic Constraints in R: Rsolnp?

I am trying to minimize a quadratic objective with quadratic constraints, is Rolnp the way to go? I have been reading the documentation and it seems like all the examples use equations instead of vector manipulation. All of my parameters are vectors and matrices and they can be quite large. Here is my problem:
X<-([Cf]+[H])%*%[A]
Y<-([Cf]+[H]-[R])%*%[B]
I want to find H that minimizes Y%*%Dmat%*%t(Y) for a given value of X%*%Dmat%*%t(X)
Cf, R, A, Dmat and B are matrices of constants.
The values for H sohould be between 0 and 1.
Is it possible to use Rsolnp to find the vector H even though the input functions will all return other vectors?
I ended up using AUGLAG and COBYLA to solve this problem.

Rank of a matrix in R

I want to test the rank of a matrix, is there someone who can recommend a package/function in R for this?
You can try the function qr ("qr", because it performs a QR decomposition):
#define a matrix for this example
M <- matrix(data = rnorm(12), ncol = 3)
#run the function qr()
qr(M)$rank
#Alternative: load the Matrix package...
require(Matrix)
#...and run the function rankMatrix()
rankMatrix(M)[1]
http://cran.r-project.org/web/packages/Matrix/Matrix.pdf, page 101
http://cran.r-project.org/web/packages/matrixcalc/matrixcalc.pdf, page 12
You can use the Library pracma: Practical Numerical Math (Provides a large number of functions from numerical analysis and linear algebra, numerical optimization, differential equations, time series, plus some well-known special mathematical functions.).
Install it using the below command in the R console:
install.packages("pracma", repos="http://R-Forge.R-project.org")
You can use the library then :
library(pracma)
Rank(Your Matrix object)

R equivalent to Matlab re-ordering of Schur factorization?

Is there an equivalent to the MATLAB function ordschur (documentation here) in R?
The function re-orders the Schur factorization X = U*T*U' produced by the schur function and returns the reordered Schur matrix TS and the cumulative orthogonal transformation US such that X = US*TS*US'. I am particularly interested in the 'lhp' method - also described in the MATLAB documentation link.
Note that there is a function Schur in the package Matrix (see CRAN documentation here ) R which performs the Schur decomposition and eigenvalues of a square matrix. Update: This function also returns the Unitary orthogonal matrix U.
As far as I know MATLAB uses ?TRSEN function from LAPACK to perform reordering. You can look at some limited implementation here. In order to bring this functionality into R you can implement this routine by yourself.

Resources