R and Python (RPy2): Rank-revealing QR Decomposition

I used to use R all the time until I got into Python, NumPy, and SciPy.

In general, however, R has much more sophisticated packages for statistics (I don’t think there’s too much argument here).  Unfortunately, R, in my opinion, is a cumbersome language for string manipulations or more general programming (though it is fairly capable).

You can get the best of both worlds with RPy.

I recently wanted to calculate the rank of a matrix via a QR decomposition as an alternative to computing the SVD.  I haven’t yet seen code to do this in numpy.  R, on the other hand, computes this automatically in its ‘qr’ function.  To get this running in my numpy/scipy code, I didn’t even have to worry much about matrix format conversions between the languages.   After a couple of ‘imports’ and within 5 minutes I calculated the rank in Python via the R code:

rqr = robjects.r[‘qr’]
print rqr(R)[1][0]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s