clojuress.v1.renjin-test

clojuress.v1.renjin-test - created by notespace, Fri Feb 07 22:34:45 IST 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.

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

~~~~setup~~~~

Setup

(require '[clojuress.v1.r :as r :refer
           [r eval-r->java r->java java->r java->clj java->naive-clj clj->java
            r->clj clj->r ->code r+ colon]]
         '[clojuress.v1.require :refer [require-r]]
         '[clojuress.v1.renjin :as renjin]
         '[tech.ml.dataset :as dataset]
         '[notespace.v1.util :refer [check]]
         '[alembic.still :refer [distill]]
         '[clojuress.v1.applications.plotting :refer [plotting-function->svg]])

(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 '[tilde y x] :data df)]
  (r.base/summary fit))


Call:
.MEM$x910c5b02be7c458f(formula = (y ~ x), data = .MEM$xbfd0a178e66b42d4)

Residuals:
     Min       1Q   Median       3Q      Max
-0.50466 -0.24095  0.00264  0.23284  0.50872

Coefficients:
            Estimate   Std. Error t value    Pr(>|t|)             
(Intercept)   2.57      0.055      46.483    <0         ***       
          x -3.121      0.098     -31.969    <0         ***       
---
Signif. codes:  
0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.279 on 97 degrees of freedom
Multiple R-squared:  0.9133, Adjusted R-squared:  0.9124 
F-statistic: 1,021.9936 on 1 and 97 DF,  p-value: < 0



~~~~plotting~~~~

Plotting

(require-r '[graphics])

(plotting-function->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


clojuress.v1.renjin-test - created by notespace, Fri Feb 07 22:34:45 IST 2020.