Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to send to R a big function,it has only three variables but output is quite big.So when i "translate" that code from mathematica to look like R code and input it in REvaluate i get the popup window thats telling me that java(tm) platform se binary has stopped working.I succeeded the similar thing with same amount of code length but with this one I can't and I can't figure out what's wrong.

Here is the code that's making trouble:

ClearAll[sigmasqr, a, ll, sll];
s = FinancialData["^gspc", "FractionalChange", {"2005/01/01", "2009/12/31"}][[All, 2]];

R[t_] := s[[t]]

sigmasqr[1, w_, a_, b_] = Variance[s]

sigmasqr[t_, w_, a_, b_] := w + a R[t - 1]^2 + b sigmasqr[t - 1, w, a, b]

ll[t_, w_, a_, b_] := -.5 (Log[2 \[Pi]] + Log[sigmasqr[t, w, a, b]] + R[t]^2/
 sigmasqr[t, w, a, b]) // N

sll = Sum[ll[t, w, a, b], {t, 2, 100}];

So sll is a log likelihood sum for 100 returns,now I want to create a function that can be used in R's optim from this sll.Here is the code:

REvaluate["a1=function(w,a,b){" <> StringReplace[ToString[sll//InputForm], {"[" -> "(", "]" -> ")", 
"L" -> "l", "*^" -> "*10^"}] <> "}"]

And here is where problem starts.. I found that the troublesome part starts from part 48 of the sum

    REvaluate["a1=function(w,a,b){" <> StringReplace[ToString[sll[[1;;47]]//InputForm], {"[" -> "(", "]" -> ")", 
"L" -> "l", "*^" -> "*10^"}] <> "}"]

If you check you will see that it runs perfectly,but now if you run:

    REvaluate["a1=function(w,a,b){" <> StringReplace[ToString[sll[[48]]//InputForm], {"[" -> "(", "]" -> ")", 
"L" -> "l", "*^" -> "*10^"}] <> "}"]

you will get a java platform stop working message.Is there a solution to this problem? I should say that I pasted the troublesome part in R to check did I forgat to change something in the form of code but,no it's working in R.

share|improve this question
1  
What makes you think that you are constructing a valid R function with this string? I looked at it, and it contains e.g. Mathematica Log functions etc. Besides, this is most definitely not the recommended way to construct an R function with RLink. What you should be doing is to transfer data to R, and then construct the R function to operate on that data. –  Leonid Shifrin Apr 10 at 23:32
    
Well that's why I have the replace string command so it actually uses log in R.Like I said I would like to construct an optimizer for my classmates who don't know much about R,that's my motivation.For this purpose FindMinimum and NMinimize are too slow,and I don't know how or even can I speed them up.Probably with compile but I dont know c,so this was my solution... –  Milos Cipovic Apr 11 at 0:03
    
You can check that it actually works in the example given.Thanks for responding by the way. –  Milos Cipovic Apr 11 at 0:20
    
I am looking at it, and I do reproduce the problem, although I am doing this a bit differently. Will look more into this tomorrow, and see if I can think of something, unless someone else here does this first. –  Leonid Shifrin Apr 11 at 0:31
    
Thanks a lot,I was afraid that i was asking something not so bright but since I got attention of the great Leonid Shifrin I think I'm good then :) –  Milos Cipovic Apr 11 at 0:44

2 Answers 2

up vote 5 down vote accepted

Short Answer

R cannot handle expressions that are nested very deeply. You will have to find a way to simplify your expressions.

Long Answer

I can reproduce the crash on V9 under Windows 7 64-bit with a simpler setup. Start with these definitions:

Needs["RLink`"]
InstallR[];

expr[depth_] := "function(x){"~~Nest["1+("~~#~~")"&, "x", depth]~~"}"

This will work:

REvaluate[expr[48]]

But this will not:

(* WARNING: this causes JLink/Java to crash!! *)
REvaluate[expr[49]]

And neither will this (after restarting JLink/RLink):

(* WARNING: this causes JLink/Java to crash!! *)
com`wolfram`links`rlink`RLinkInit`getRExecutor[]@eval[expr[50]]

If I evaluate the following expression (expr[50]) directly in Rgui, I get a runtime exception:

function(x){
1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(
1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(
1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(
1+(1+(1+(1+(1+(x)))))))))))))))))))))))))))))
)))))))))))))))))))))
}

# Error: contextstack overflow at line 1

My guess is that this is not a bug in RLink directly -- I suspect that the JRI native library has a very nasty problem trying to handle such error conditions.

share|improve this answer
    
Hmmm,I was short one pair of brackets when I was checking in R firs time,it would save us both the trouble.Thanks for your effort.I will post this problem in R community,and see is there a fix. –  Milos Cipovic Apr 11 at 11:19
    
+1. Thanks for this, your answer puts me to shame. I should've figured that out! A small excuse is that I didn't have the R installed on my current machine, and was running out of time at the moment. I was also thinking about JRI, but again, didn't have the time to check. JRI was useful as a short-cut to get cross-platformness for free, when implementing RLink, but it has its price. –  Leonid Shifrin Apr 13 at 13:13
    
But, this should clearly be considered a bug (I mean the crash), even if not an easy-to-fix one, since the end user could not care less which part of RLink is faulty. I will file it. This will probably not be the highest priority one, but if I ever get the time to deal with all RLink bugs wholesale, I'll see what can be done for it. Perhaps, if the problem is localized enough, it can be fixed by JRI maintainers. Or, I'll have to learn R's C bindings properly, which might not be a bad thing either. –  Leonid Shifrin Apr 13 at 13:21
    
Well I was actually trying to bring solvers from R to work in Mathematica which would be awesome (in example above they would be used in maximizing the log likelihood function),I will put an hold on this for a while.By the way congrats on building RLink,so simple and so useful ... –  Milos Cipovic Apr 14 at 23:32
    
Milos: FYI, if you address a comment to someone other than the poster of a response, you should address them using "@" syntax. For example, your previous comment was directed to Leonid, so you should include @LeonidShifrin in the comment text. He will receive a notification now because of this comment. –  WReach Apr 15 at 2:26

Ok,just reporting that an easy way around this problem is using ExpandAll command in mathematica,so you get rid of nested parentheses

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.