0

I was trying to use RinRuby in my rails app... and there are two ways I can write the same code. 1) Using R.eval statements

R.eval "integrand <- function(q) {((-log(#{n}) + q*log(q) + (1-q)*log((1-q)/(#{n}-1)))^2)*(q^#{a}*(1-q)^#{b}*1/beta(#{a},#{b}))}"
R.eval "integ <- integrate(integrand, lower = 0, upper = 1)$val"
R.eval "eig <- (log(#{n}) - (#{b}/(#{a}+#{b}))*log(#{n}-1) - digamma(#{a}+#{b}+1) + (#{a}*digamma(#{a}+1)+#{b}*digamma(#{b}+1))/(#{a}+#{b}))"
R.eval "r_output <- (integ - eig)"
val = R.pull "r_output"

and 2)Using EOF wrap around the code

r = RinRuby.new(:echo => false)
r.eval <<-EOF
  integrand <- function(q) {((-log(#{n}) + q*log(q) + (1-q)*log((1-q)/(#{n}-1)))^2)*(q^#{a}*(1-q)^#{b}*1/beta(#{a},#{b}))}
  integ <- integrate(integrand, lower = 0, upper = 1)$val
  eig <- (log(#{n}) - (#{b}/(#{a}+#{b}))*log(#{n}-1) - digamma(#{a}+#{b}+1) + (#{a}*digamma(#{a}+1)+#{b}*digamma(#{b}+1))/(#{a}+#{b}))
  r_output <- (integ - eig)
EOF
val = r.pull "r_output"

Is one form expected to be faster than the other? I wanted to optimize my code so I thought of using the second one. The first form also echo's the RinRuby statements upon execution but they are turned off in second one. So I thought that would optimize the code but that is not the case, infact the second is taking more time ... can anyone help.

Bob
  • 561
  • 1
  • 6
  • 18
  • If there are any faster alternatives to RinRuby for scientific computing in ruby that would help. – Bob Oct 05 '14 at 16:51
  • Bob, you might want to look at the selected answer [here](http://stackoverflow.com/questions/9287265/r-statistical-package-gem-for-a-rails-application). – Cary Swoveland Oct 05 '14 at 18:13
  • Thanks @CarySwoveland. I read it and it seems like RSRuby > RServe > RinRuby in terms of speed. This is in particular necessary when using R and Ruby for web development such as using rails framework. Though, are there other libraries than R that you think might be even more scalable- either because of a great gem or because the library is itself good? – Bob Oct 05 '14 at 18:41
  • I cannot answer that question; perhaps someone else can. – Cary Swoveland Oct 05 '14 at 18:53
  • I checked RSRuby way way way faster than RinRuby. One can directly write R code in RSRuby as well e.g. by using the construct r.eval(r.parse(:text => "3+5"))... Though I do not know about variable assignment... – Bob Oct 06 '14 at 15:45
  • Glad to hear it's working for you, Bob. btw, I've never used R and am not likely to do so in future... – Cary Swoveland Oct 06 '14 at 15:54

0 Answers0