clojisr.v1.renjin-test

clojisr.v1.renjin-test - created by notespace, Thu Jul 09 01:26:41 CEST 2020.
Table of contents

Renjin interop

Renjin is an implementation of the R language for the JVM. It supports the whole of the base R language, as well many R packages. For most packages, Renjin's support is partial and some tests fail. See the details here.

Clojisr has now basic, experimental, support for Renjin as a backend. This tutorial shows some example usage.

:setup

Setup

(require '[clojisr.v1.renjin :as renjin]
          '[clojisr.v1.r :as r :refer
            [r eval-r->java r->java java->r java->clj clj->java r->clj
             clj->r ->code r+ colon require-r]]
          '[tech.ml.dataset :as dataset]
          '[clojisr.v1.applications.plotting :refer [plot->svg]])

If we required clojisr.v1.renjin first, then the default session-type would be :renjin. But since we might be loading this namespace after doing some other things, let us make sure that we are using :renjin:

(renjin/set-as-default!) (r/discard-all-sessions)
:basic-examples

Basic examples

(require-r '[base] '[stats])
nil
(r '(+ 1 2))
[1] 3
(r.stats/median [1 2 4])
[1] 2

From plain clojure data to an R dataframe:

(-> {:x [1 2 3], :y [4 5 6]}
     r.base/data-frame)
     x y
[1,] 1 4
[2,] 2 5
[3,] 3 6
(-> {:x [1 2 3], :y [4 5 6]}
     r.base/data-frame
     r.base/rowMeans)
[1] 2.5 3.5 4.5

From a tech.ml.dataset dataset to an R dataframe:

(-> {:x [1 2 3], :y [4 5 6]}
     dataset/name-values-seq->dataset
     r.base/data-frame)
     x y
[1,] 1 4
[2,] 2 5
[3,] 3 6
(-> {:x [1 2 3], :y [4 5 6]}
     dataset/name-values-seq->dataset
     r.base/data-frame
     r.base/rowMeans)
[1] 2.5 3.5 4.5
:linear-regression

Linear regression

(let [xs (repeatedly 99 rand)
       noises (repeatedly 99 rand)
       ys (map (fn [x noise] (+ (* x -3) 2 noise)) xs noises)
       df (r.base/data-frame :x xs :y ys)
       fit (r.stats/lm '(formula y x) :data df)]
   (r.base/summary fit))

Call:
.MEM$xfd4378321f404ca3(formula = (y ~ x), data = .MEM$x04936fcc0fc84480)

Residuals:
     Min       1Q   Median       3Q      Max
-0.49664 -0.25767 -0.02435  0.25949  0.54165

Coefficients:
            Estimate   Std. Error t value    Pr(>|t|)             
(Intercept)  2.452      0.057      43.398    <0         ***       
          x -2.937        0.1     -29.434    <0         ***       
---
Signif. codes:  
0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.293 on 97 degrees of freedom
Multiple R-squared:  0.8993, Adjusted R-squared:  0.8983 
F-statistic: 866.3629 on 1 and 97 DF,  p-value: < 0
:plotting

Plotting

(require-r '[graphics])
(plot->svg
   (fn []
     (->> (repeatedly 999 rand)
          (map (fn [x] (* x x)))
          (r.graphics/hist :main "histogram" :xlab "x" :bins 100))))
histogramxFrequency0.00.20.40.60.81.0050100150200250300

clojisr.v1.renjin-test - created by notespace, Thu Jul 09 01:26:41 CEST 2020.