I am a longtime user of Mathematica and, while it has taken time to learn, I love the program now. I have used many other data analysis packages and find Mathematica’s to be by far the most comprehensive. The problem I have is in communicating my results to others. Thus, I am trying to develop stable, reliable communications between Mathematica and R/Shiny. This question may be out of scope for this forum, but I have read through the RLink documentation, but it is incomplete. In particular, the author appears knows both packages fairly well, so he/she omits fundamental basic details like how to establish the link between two instances (Mathematica and R/Shiny). Can you point to some more complete instructions?

Thank you

]]>Erik.

]]>A small nit to pick: I notice that Mathematica obtains about .074 as the probability for a Poisson(5) to be greater than a Poisson(10) random variable, both from the exact result with the Skellam distribution and from the Monte Carlo simulation; from Matt’s video, R gets about 1 – 0.880 = 0.120.

I work for Maplesoft, makers of the computer algebra system Maple, and Maple gets similar values for Monte Carlo experiments. Moreover, it can also compute the exact numbers. I tried the following in Maple 17:

with(Statistics):

X := RandomVariable(Poisson(5));

Y := RandomVariable(Poisson(10));

P := Probability(X > Y);

This returns, admittedly not exactly a closed form, but an infinite summation that is an exact expression. With a bit of manipulation (see at the end of this comment), we get this value for P:

sum(5^_k * exp(-5) * GAMMA(_k + 1, 10) / GAMMA(_k + 1)^2, _k = 0 .. infinity)

where _k is the dummy variable of the summation, the single argument GAMMA function is the gamma function you would expect, and the two argument GAMMA function is the incomplete gamma function, as explained at http://www.maplesoft.com/support/help/Maple/view.aspx?path=GAMMA . The numerical value of this summation, exact to 10 decimal places, is 0.1197937523 (as obtained by running evalf on the result).

PS: This is the manipulation I performed on P to get the nice sum:

term := op(1, P):

term := simplify(term) assuming _k :: nonnegint;

P := subsop(1 = term, P);

Since you’ve been using it for a while, this may be hard. But imagine designing a similar interface from scratch, and think of all the things you would *want* to have, like breadcrumbs, tabbed notebooks, detailed running indicators, fontsize/face adjustment that isn’t circa 1994, etc.

]]>But I was a bit surprised that you complained about the interface. I think you were a bit quick to judge it. Usability-wise the notebook paradigm is the best I have seen for *interactive work*, and it’s just beginning to be picked up by other systems (MATLAB “cells”, ipython’s htmlnotebook, Sage). I always find the traditional command line of some other systems inconvenient and limiting.

Regarding palettes, their advantage is in customizability. The only ones I use are those that I made myself, and they usually do something more complicated than just insert symbols. I never open the built-in ones. Don’t try to use them to type special symbols, use the escape shortcuts instead, e.g. ESC dist ESC for \[Distributed]

]]>First, I agree completely that For loops are slow in Mathematica. As you said, we generally recommend avoiding them as more elegant and fast functional solutions usually exist. When you really want procedural loops and machine precision you should consider using Compile. If you have a C compiler like Visual Studio installed Mathematica can compile directly to C.

Here is your example using compile and compiling to C. I suspect the result is faster than what you might get in R.

f = Compile[{}, Block[{l = 0, i, k, j}, For[i = 0, i < 10^2, i = i + 1, For[j = 0, j < 10^2, j = j + 1, For[k = 0, k < 10^2, k = k + 1, l = l + 1] ]]; l ] , CompilationTarget -> "C"]; In[186]:= f[] // AbsoluteTiming Out[186]= {0.008001, 1000000}

You also commented on the failure of Probability for Poisson random variates. I agree that this could be improved since the underlying problem is fairly simple (in terms of special functions). Testing whether a poisson[5] > poisson[10] is equivalent to testing whether the difference is greater than zero. But this difference follows a SkellamDistribution[5,10]. In Mathematica the SurvivalFunction for a SkellamDistribution has a “closed-form” solution in terms of MarcumQ.

In[189]:= NProbability[x > 0, x \[Distributed] SkellamDistribution[5, 10]] Out[189]= 0.074392

To be fair and compare apples-to-apples lets run a Monte-Carlo simulation to compute the same probability. This is some unnecessarily cryptic functional code but I wanted to hint at its use and save some space.

In[191]:= AbsoluteTiming[ Mean[UnitStep[#] Unitize[#]] &[#[5] - #[10]] &[ RandomVariate[PoissonDistribution[#], 10^6] &] // N] Out[191]= {0.083005, 0.074268}

I think you hit the nail on the head with dynamic interactivity. Mathematica really excels at this and anything can be manipulated this way.

The issue for me ultimately isn’t why use Mathematica instead of R. I think they are both powerful and incredibly useful platforms. The question is why not use both?

]]>