Last active
October 2, 2020 00:00
-
-
Save krishnanraman/8310213 to your computer and use it in GitHub Desktop.
Using R from Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 0. You must have the latest & greatest version of R, and scala 2.10.1, for all of this to work. | |
Step 1. Download and unzip the MacOS X Binary jvmr_1.0.4.tgz from here: http://cran.r-project.org/web/packages/jvmr/index.html | |
Step 2. Create a lib folder, and copy jvmr_2.10-1.0.4.jar to that folder. | |
Step 3. Start R | |
Step 4. At the R console | |
>install.packages("jvmr") | |
>library(jvmr) | |
Step 5. Start sbt version 2.10.1 in console mode like so - | |
$ sbt "++ 2.10.1" console | |
[info] Set current project to default-0844fb (in build file:/Users/kraman/workspace/jvmr/) | |
Setting version to 2.10.1 | |
[info] Set current project to default-0844fb (in build file:/Users/kraman/workspace/jvmr/) | |
[info] Starting scala interpreter... | |
[info] | |
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> | |
scala> import org.ddahl.jvmr.RInScala | |
import org.ddahl.jvmr.RInScala | |
scala> val R = RInScala() | |
R: org.ddahl.jvmr.RInScala = org.ddahl.jvmr.RInScala@511192bd | |
// zscores : See http://upload.wikimedia.org/wikipedia/commons/b/bb/Normal_distribution_and_scales.gif | |
// zscore = number of standard deviations above the mean | |
// at 3 sigma, area under curve is 99% of distribution | |
// at -3 sigma, insignificant area, almost 0% | |
// at 0, exactly 50% area | |
scala> val zscores = -3.0 to 3.0 by 0.1 | |
scala> def pvalue(zScore:Double) = R.toPrimitive[Double]("pnorm(%s,0,1)".format(zScore)) | |
pvalue: (zScore: Double)Double | |
scala> zscores foreach { z=> printf("%.3f\t%.3f\n", z, pvalue(z)) } | |
-3.000 0.001 | |
-2.900 0.002 | |
-2.800 0.003 | |
-2.700 0.003 | |
-2.600 0.005 | |
-2.500 0.006 | |
-2.400 0.008 | |
-2.300 0.011 | |
-2.200 0.014 | |
-2.100 0.018 | |
-2.000 0.023 | |
-1.900 0.029 | |
-1.800 0.036 | |
-1.700 0.045 | |
-1.600 0.055 | |
-1.500 0.067 | |
-1.400 0.081 | |
-1.300 0.097 | |
-1.200 0.115 | |
-1.100 0.136 | |
-1.000 0.159 | |
-0.900 0.184 | |
-0.800 0.212 | |
-0.700 0.242 | |
-0.600 0.274 | |
-0.500 0.309 | |
-0.400 0.345 | |
-0.300 0.382 | |
-0.200 0.421 | |
-0.100 0.460 | |
0.000 0.500 | |
0.100 0.540 | |
0.200 0.579 | |
0.300 0.618 | |
0.400 0.655 | |
0.500 0.691 | |
0.600 0.726 | |
0.700 0.758 | |
0.800 0.788 | |
0.900 0.816 | |
1.000 0.841 | |
1.100 0.864 | |
1.200 0.885 | |
1.300 0.903 | |
1.400 0.919 | |
1.500 0.933 | |
1.600 0.945 | |
1.700 0.955 | |
1.800 0.964 | |
1.900 0.971 | |
2.000 0.977 | |
2.100 0.982 | |
2.200 0.986 | |
2.300 0.989 | |
2.400 0.992 | |
2.500 0.994 | |
2.600 0.995 | |
2.700 0.997 | |
2.800 0.997 | |
2.900 0.998 | |
3.000 0.999 | |
def plot(x:List[Int], y:List[Double]) = R.apply("plot("+"c"+x.toString.substring(4)+",c"+y.toString.substring(4)+",xlab=\"\", ylab=\"\", type=\"b\", col=\"red\",main=\"graph\")") | |
val x = (1 to 100).toList | |
val y = x.map( i=> math.random * 100 + i ) | |
plot(x,y) | |
( THANKS TO Chang Su / CSL FOR MAJOR HELP WITH SBT ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment