Wolfram Blog
Jon McLoone

Why Would a Mathematica User Care about R?

May 22, 2013 — Jon McLoone, International Business & Strategic Development

The benefits of linking from Mathematica to other languages and tools differ from case to case. But unusually, in the case of the new RLink in Mathematica 9, I think the benefits have very little to do with R, the language. The real benefit, I believe, is in the connection it makes to the R community.

When we first added the MathLink libraries for C, there were real benefits in farming out intensive numerical work (though Mathematica performance improvements over the years and development of the compiler have greatly reduced the occasions where that would be worth the effort). Creating an Excel link added an alternative interface paradigm to Mathematica that wasn’t available in the Mathematica front end. But in the case of R, it isn’t immediately obvious that it does many things that you can’t already do in Mathematica or many that it does significantly better.

However, with RLink I now have immediate access to the work of the R community through the add-on libraries that they have created to extend R into their field. A great zoo of these free libraries fill out thousands of niches–sometimes popular, sometimes obscure–but lots of them. There are over 4,000 right here and more elsewhere. At a stroke, all of them are made immediately available to the Mathematica environment, interpreted through the R language runtime.

Let’s look at a simple example. While Mathematica supports FisherRatioTest, it doesn’t know the exact Fisher test. (This is a hypothesis test where the null hypothesis is that rows and columns in a contingency table with fixed marginals are independent.)

<< RLink`; InstallR[];

ExactFisherTest[data_] :=   RFunction["function(x){fisher.test(x)}"][data][[1, 1, 1]]

Well, now it does.

ExactFisherTest[{{1, 2, 1, 0}, {3, 3, 6, 1}, {10, 10, 14, 9}, {6, 7,     12, 11}}]

0.782685

Finding the right library is more work than phoning Tank, and I skipped over any error checking. But the only complicated bit was extracting the p-value from the result (the “[[1,1,1]]” part) because RFunction returns an RObject that contains additional metadata, which, this time, I didn’t care about.

I can now use this just like any built-in function.

I can plot it:

DiscretePlot[  ExactFisherTest[{{1, 2, 1, 0}, {3, 3, 6, 1}, {10, 10, 14, 9}, {6, 7,      12, x}}],  {x, 0, 20, 1}]

DiscretePlot of ExactFisherTest

I can manipulate it:

Manipulate[  DiscretePlot[   ExactFisherTest[{{1, 2, 1, 0}, {3, 3, 6, 1}, {10, 10, 14, 9}, {6, 7,       y, x}}], {x, 0, 20, 1}],  {y, 12, 20, 1}]

Manipulate of a DiscretePlot of ExactFisherTest

And I can use it with libraries from other languages in a similar way:

<< JLink`; jran = JavaNew["java.util.Random"]; JavaRandom[max_] := max (jran@nextFloat[]);

ExactFisherTest[{{1, 2, 1, 0}, {3, 3, 6, 1}, {10, 10, 14, 9}, {6, 7,     12, Round[JavaRandom[20]]}}]

0.964371

The future is always hard to predict. When I started here (many) years ago, general linking to FORTRAN seemed like the most important thing, but no one ever asks me about that any more–C and Java linking are the most popular. Links to some specific libraries (BLAS/LAPACK, GMP, and others) have ended up being core infrastructure components in Mathematica. Whether RLink finds extensive use in Mathematica‘s future features, or remains a more or less stand-alone added chunk of functionality within Mathematica‘s infrastructure, is not yet clear.

There are also issues that need to be considered. R code isn’t going to handle symbolic arguments or high-precision numbers, so, for robustness, you will want to type-check more carefully than you might with Mathematica code. You don’t always have the elegant design and quality of Mathematica. Some of it is quite raw, while some of it is excellent. But design and quality take time and resources, and so it will be quite a while before we fill out Mathematica to fill every one of these niches, and through RLink they are available right now.

A big chunk of extra functionality just became a part of the Mathematica ecosystem.

Download this post as a Computable Document Format (CDF) file.

Leave a Comment

12 Comments


Szabolcs

Rather than saying that it makes a connection to the R community, it’s probably better to say that it opens the gate to several libraries which have R interfaces. This is why it’s absolutely essential to be able to use an external R installation, not just the one that is auto-downloaded by Mathematica. Murray has a very good point.

Here’s an example where I found it quite useful in practice: https://github.com/szhorvat/IGraphR

Posted by Szabolcs    May 22, 2013 at 11:22 pm
Jon McLoone

You can use a stand-alone R distribution on both Windows and Linux.

However you are correct that this limitation exists for users of OS X. Improvements to R Link, such as this, are under consideration for the next release.

Posted by Jon McLoone    May 23, 2013 at 10:07 am
telefunkenvf14

Your opening paragraph begs the question: What prevents the MMA community from being more like the R community?

(call me if you’d like some ideas…)

— Anticipating any reply might mention these —-

Demonstrations Project — Sure, it’s a nice place to show off programming chops, but it does little to foster any sense of community. Why isn’t there a way to rate demonstrations? (so it’s easier to find great demonstrations) Why isn’t it possible to fav demonstrations (and be able to manage those favorites—and perhaps be notified of updates to them—via Wolfram Portal)? Why not make it easy to contact demonstrations authors? (suggest extensions, improvements, bug fixes, pitch an idea, or just to heap praise—-all this could be easily incorporated into Wolfram Portal, no?)

WRI Library — Needs help. Tons of valuable information, but for the most part it’s a mess of seemingly abandoned projects and outdated marketing materials. When the Wolfram Portal was announced, I was positive it would have a way to interact with the Library…

Posted by telefunkenvf14    May 24, 2013 at 12:13 am
    Jon McLoone

    These are all great points, and I know that most of them have been discussed here. I haven’t been part of the ‘community’ team, so I can’t comment on when we might expect these features, but there is a plan, the first step of which is in beta now at http://wolfram.com/community

    Posted by Jon McLoone    May 29, 2013 at 6:25 am
Selva Rajan

Despite having access to MM9 at work, I continue to use R because
no software can beat the wealth of libs available for R. I wouldn’t recommend shackling R into mathematica environment With R studio, R coding , lib install and update has become so convinient that it would be an overkill to use MM for accessing R libs

Posted by Selva Rajan    May 28, 2013 at 12:30 am
topdiablo3

Your opening paragraph begs the question: What prevents the MMA community from being more like the R community?

Posted by topdiablo3    June 26, 2013 at 3:10 am


Leave a comment

Loading...

Or continue as a guest (your comment will be held for moderation):