[graalvm-users] Tightly coupling Ruby and R
Rodrigo Botafogo
rodrigo.a.botafogo at gmail.com
Fri Sep 28 08:00:03 PDT 2018
Hello...
I've being working on coupling Ruby and R for some time now (since rc1) and
I think a have managed to advance quite a bit, thanks in great part to the
help from people here, specially Chris and Stepan: thanks!! So, now,
instead of posting a question, this post is tho show where I'm at and see
if you have any suggestions, opinions, feedback, etc.
Bellow two examples of how the coupling works. The first is a scatter plot
extracted from
https://urldefense.proofpoint.com/v2/url?u=http-3A__r-2Dstatistics.co_Top50-2DGgplot2-2DVisualizations-2DMasterList-2DR-2DCode.html&d=DwIFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=V4perA7MCLoMrUH-4v2X9TEXm7WzhucqB9Quklf-V8g&s=EuFjHNgCyBdxBylfpIDxJFydO57YqQEr4t1YIYHh-YE&e=
and the second a 5th degree polynomial regression. The idea of the coupling
is to use Ruby syntax, but emulate R as closely as possible in order to
make it trivial to use for an R developer.
====================================================================
require 'cantata'
require 'ggplot'
# set options
R.options(scipen: 999) # turn-off scientific notation like 1e+48
R.theme_set(R.theme_bw) # pre-set the bw theme.
# read the R 'midwest' dataset onto the 'midwest' variable
midwest = ~:midwest
R.awt
# Scatterplot
gg = midwest.ggplot(E.aes(x: :area, y: :poptotal)) +
R.geom_point(E.aes(col: :state, size: :popdensity)) +
R.geom_smooth(method: "loess", se: false) +
R.xlim(R.c(0, 0.1)) +
R.ylim(R.c(0, 500000)) +
R.labs(subtitle: "Area Vs Population",
y: "Population",
x: "Area",
title: "Scatterplot",
caption: "Source: midwest")
puts gg
[image: image.png]
===================================================================
As another example, here is how to get a polynomial regression using the
Boston data from R package ISLR.
require 'cantata'
R.require 'MASS'
R.require 'ISLR'
lm_fit5 = R.lm(R.formula("medv ~ poly(lstat, 5)"), data: :Boston)
puts lm_fit5.summary
[This output was cut from the total print...]
Residuals:
Min 1Q Median 3Q Max
-13.5433 -3.1039 -0.7052 2.0844 27.1153
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 22.5328 0.2318 97.197 < 2e-16 ***
poly(lstat, 5)1 -152.4595 5.2148 -29.236 < 2e-16 ***
poly(lstat, 5)2 64.2272 5.2148 12.316 < 2e-16 ***
poly(lstat, 5)3 -27.0511 5.2148 -5.187 3.10e-07 ***
poly(lstat, 5)4 25.4517 5.2148 4.881 1.42e-06 ***
poly(lstat, 5)5 -19.2524 5.2148 -3.692 0.000247 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 5.215 on 500 degrees of freedom
Multiple R-squared: 0,6817, Adjusted R-squared: 0,6785
F-statistic: 214,2 on 5 and 500 DF, p-value: < 2.2e-16
===================================================================
In order to test this:
* Install GraalVM (rc6)
* Install TruffleRuby (follow the simple instructions)
* Install FastR
* Clone the git repo: https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_rbotafogo_cantata.git&d=DwIFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=V4perA7MCLoMrUH-4v2X9TEXm7WzhucqB9Quklf-V8g&s=JMJtiz_aw-o6IW_FMB8solXtUJwYRuh6nzsv02v5g_E&e=
* gem install rspec
* Use rake to run the tests ('rake -T' shows all available tasks)
Doing:
* rake specs:all -- Runs all the specs. Reading the specs shows much of
how to use the language
* rake sthda:all -- Runs a slideshow with over 80 plots
* rake islr:all -- Runs some 'labs' from the Introduction to Statistical
Learning book
--
Rodrigo Botafogo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20180928/9fd3ada1/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 65340 bytes
Desc: not available
Url : http://oss.oracle.com/pipermail/graalvm-users/attachments/20180928/9fd3ada1/attachment-0001.png
More information about the GraalVM-Users
mailing list