Friday, July 22, 2011

Random Number Generation with RCaller 2.0

Java has two standard libraries for generating random numbers. The java.lang.Math class has a random method with is used for generating uniform distributed random numbers. The second one is the java.util.Random class which has got several functions for generating random numbers. We can draw random numbers from several distribution using the probability integral transform. But R has many internal functions for random number generation from several probability distributions including the gamma, the binomial, the normal etc.


RCaller has a wrapper class, under the package statistics, for generating random number for those distributions. The class statistics. RandomNumberGenerator has these functions:


public double[] randomNormal(int n, double mean, double standardDeviation)
public double[] randomLogNormal(int n, double logmean, double logStandardDeviation) 
public double[] randomUniform(int n, double min, double max) 
public double[] randomBeta(int n, double shape1, double shape2)
public double[] randomCauchy(int n, double location, double scale) 
public double[] randomT(int n, int df) 
public double[] randomChisqyare(int n, int df)
public double[] randomF(int n, int df1, int df2)
public double[] randomPoisson(int n, double lambda) 
public double[] randomBinom(int n, int size, double p)
public double[] randomNegativeBinom(int n, int size, double p)
public double[] randomMultinomial(int n, int size, double p)
public double[] randomGeometric(int n, double p) 
public double[] randomWeibull(int n, double shape, double scale) throws 
public double[] randomHyperGeometric(int amount, int n, int m, int k) 
public double[] randomExponential(int n, double theta) throws Exception 
public double[] randomGamma(int n, double shape, double rate, double scale) 


One can see the usage of class in the Test5 class in the source of RCaller 2.0.
http://code.google.com/p/rcaller/source/browse/RCaller/src/test/Test5.java

Sunday, July 17, 2011

IP Subnet Calculator In debian.

I'm talking about console based application that able to ip subnets calculator. You have to use following command to install it;

sudo apt-get install ipcalc


How to use;

ipcalc [ip address] [mask]

You can use both format for mask part. like this:

example 1;


ismail@ismail-laptop:~/Desktop/swap$ ipcalc 172.16.40.85/24
Address: 172.16.40.85 10101100.00010000.00101000. 01010101
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 172.16.40.0/24 10101100.00010000.00101000. 00000000
HostMin: 172.16.40.1 10101100.00010000.00101000. 00000001
HostMax: 172.16.40.254 10101100.00010000.00101000. 11111110
Broadcast: 172.16.40.255 10101100.00010000.00101000. 11111111
Hosts/Net: 254 Class B, Private Internet



or



ismail@ismail-laptop:~/Desktop/swap$ ipcalc 172.16.40.85 255.255.255.0
Address: 172.16.40.85 10101100.00010000.00101000. 01010101
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 172.16.40.0/24 10101100.00010000.00101000. 00000000
HostMin: 172.16.40.1 10101100.00010000.00101000. 00000001
HostMax: 172.16.40.254 10101100.00010000.00101000. 11111110
Broadcast: 172.16.40.255 10101100.00010000.00101000. 11111111
Hosts/Net: 254 Class B, Private Internet



If you have a C class network address and you want to create 4 subNet, actually your mask is 24.

2 exp. 2 = 4

So 24-2 = 22 is your new subnet..



ismail@ismail-laptop:~/Desktop/swap$ ipcalc 172.16.40.85/22
Address: 172.16.40.85 10101100.00010000.001010 00.01010101
Netmask: 255.255.252.0 = 22 11111111.11111111.111111 00.00000000
Wildcard: 0.0.3.255 00000000.00000000.000000 11.11111111
=>
Network: 172.16.40.0/22 10101100.00010000.001010 00.00000000
HostMin: 172.16.40.1 10101100.00010000.001010 00.00000001
HostMax: 172.16.43.254 10101100.00010000.001010 11.11111110
Broadcast: 172.16.43.255 10101100.00010000.001010 11.11111111
Hosts/Net: 1022 Class B,Class B, Private Internet

About the licence of RCaller

The licence of RCaller 2.0 was changed to LGPL . That means you can use it in commercial projects without distributing the source code.

For our users who like RCaller...

Tuesday, July 12, 2011

Debugging the R output of RCaller

RCaller 2.0 has been submitted to Google Code before two or three days. Many RCaller users are testing it and except a Windows installation it seems not to be so problematic.

RCaller 2.0 receives the R outputs as XML files. If the user does not know the returned variable names or if there was a problem with the results some debugging stuff is needed.

Now the function 'getXMLFileAsString()' is implemented in RCaller, by using it, the converted R output can be investigated.

Suppose that we want to run some R code from Java and we want to have a look at the returned XML content. Have a look at these codes:



package test;

import rcaller.RCaller;

/**
 *
 * @author Mehmet Hakan Satman
 */

public class Test4 {
   
    public static void main (String[] args){
        new Test4();
    }
   
    public Test4(){
        try{
            /*
             * Creating an instance of RCaller
             */
            RCaller caller = new RCaller();
           
            /*
             * Defining the Rscript executable
             */
            caller.setRscriptExecutable("/usr/bin/Rscript");
           
            /*
             * Some R Stuff
             */
            caller.addRCode("set.seed(123)");
            caller.addRCode("x<-rnorm(10)");
            caller.addRCode("y<-rnorm(10)");
            caller.addRCode("ols<-lm(y~x)");
           
            /*
             * We want to handle the object 'ols'
             */
            caller.runAndReturnResult("ols");
           
            /*
             * Getting R results as XML
             * for debugging issues.
             */
            System.out.println(caller.getParser().getXMLFileAsString());
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
}



Because of the "set.seed()"  function of R, this code should produce the same results for all machines. The XML output is






This structure of this XML file is simple and one can see that each single variable is encapsulated within a "" and a "" tag.

Another way of handling the variable names is to use "ROutputParser.getNames()". This function simply returns an ArrayList which includes the variable names returned by R.

Friday, July 8, 2011

Rcaller 2.0 - Calling R from Java


NEWS:

2016-05-15: New Release - RCaller 3.0

2015-03-13: New Release - RCaller 2.5

2014-07-15: New Release - RCaller 2.4

2014-06-07: New Research Paper on RCaller

2014-05-15: New Release - RCaller 2.3

2014-04-15: New Release -  RCaller 2.2



I have received too many e-mails since i had first submitted the early versions of the RCaller. Some users found it usable so i was planning to develop a newer and enhanced version of this library. Now, i think, it is ready for testing. The 2.0.0 version of the RCaller is downloadable from

http://code.google.com/p/rcaller/downloads/list

with both compiled jar file and the source file with the directory structure of NetBeans 7.

The use of RCaller is changed after version 1.0 but it is still easy to implement, it does not need extra libraries, it is platform independent and compatible with the recent R versions.

Some new features in version 2.0.0 are:
1) Support for plots
2) Easier code generation
3) Enhanced interaction with R

Before anything, install the R package "Runiversal" by typing

install.packages ( "Runiversal" )

in R interactive interpreter. If installation is success, you are ready for calling R from Java. 

Let me explain them with some examples. Suppose that we have a double array with values of {1,4,3,5,6,10} and we want to show a time series plot with this. Firstly we import the needed libraries:

import java.io.File;
import rcaller.RCaller;

We are declaring the double array:

double[] numbers = new double[] {1,4,3,5,6,10};

Creating an object of Rcaller class:

RCaller caller = new RCaller();

RCaller needs the Rscript executable file (Rscript.exe in windows) which is shipped with the R. You must tell the full path of this file in RCaller like this:

caller.setRscriptExecutable("/usr/bin/Rscript");

This is the location of my Rscript file in my Ubuntu Linux. We didn't do much thing, but this code initializes the whole thing:

caller.cleanRCode();

the cleanRCode() function of RCaller class cleans the code buffer and puts some code in it. You can browse the source code if you want to know more about the initialization. Now, we can add our double array to our R code:

caller.addDoubleArray("x", numbers);

Now we have 'x' with the value of numbers[]. Now we are creating the time series plot:

File file = caller.startPlot();
            caller.addRCode("plot.ts(x)");
            caller.endPlot();

Finally we are sending the whole code to R interpreter:

caller.runOnly();

With this code, Rscript runs our code but it does not return anything. After all, if we have'nt got any errors, we can handle the generated image using

ImageIcon ii=caller.getPlot(file);

or we can show it directly using

caller.showPlot(file);

The generated plot is shown below:



The source code of entire example is given below:


package test;

import java.io.File;
import javax.swing.ImageIcon;
import rcaller.RCaller;


public class Test1 {

    public static void main(String[] args) {
        new Test1();
    }

    /*
     * Test for simple plots
     */
    public Test1() {
        try {
            RCaller caller = new RCaller();
            caller.setRscriptExecutable("/usr/bin/Rscript");
            caller.cleanRCode();

            double[] numbers = new double[]{1, 4, 3, 5, 6, 10};

            caller.addDoubleArray("x", numbers);
            File file = caller.startPlot();
            caller.addRCode("plot.ts(x)");
            caller.endPlot();
            caller.runOnly();
            ImageIcon ii = caller.getPlot(file);
            caller.showPlot(file);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}



This example shows how to send vectors to R, call R function from Java and handle the result from Java. We are generating the x and the y vectors in Java and sending an "ordinary least squares" command to R. After running process, we handle the calculated residuals, fitted values and residuals from Java. I hope the example is clear enough to understand.


package test;

import rcaller.RCaller;


public class Test2 {
   
    public static void main(String[] args){
        new Test2();
    }
   
    public Test2(){
        try{
            //Creating an instance of class RCaller
            RCaller caller = new RCaller();
           
            
            //Important. Where is the Rscript?
            //This is Rscript.exe in windows
            caller.setRscriptExecutable("/usr/bin/Rscript");
           
           
            //Generating x and y vectors
            double[] x = new double[]{1,2,3,4,5,6,7,8,9,10};
            double[] y = new double[]{2,4,6,8,10,12,14,16,18,30};
           
            //Generating R code
            //addDoubleArray() method converts Java arrays to R vectors
            caller.addDoubleArray("x", x);
            caller.addDoubleArray("y", y);
           
            //ols<-lm(y~x) is totally R Code
            //but we send the x and the y vectors from Java
            caller.addRCode("ols<-lm(y~x)");
           
            //We are running the R code but
            //we want code to send some result to us (java)
            //We want to handle the ols object generated in R side
            //
            caller.runAndReturnResult("ols");
           
           
            //We are printing the content of ols
            System.out.println("Available results from lm() object:");
            System.out.println(caller.getParser().getNames());
           
           
            //Parsing some objects of lm()
            //Residuals, coefficients and fitted.values are some elements of lm()
            //object returned by the R. We parsing those elements to use in Java
            double[] residuals = caller.getParser().getAsDoubleArray("residuals");
            double[] coefficients = caller.getParser().getAsDoubleArray("coefficients");
            double[] fitteds = caller.getParser().getAsDoubleArray("fitted_values");
           
            //Printing results
            System.out.println("Coefficients:");
            for (int i=0;i< span="">
                System.out.println("Beta "+i+" = "+coefficients[i]);
            }
           
            System.out.println("Residuals:");
            for (int i=0;i< span="">
                System.out.println(i+" = "+residuals[i]);
            }

        }catch (Exception e){
            System.out.println(e.toString());
        }
    }
   
}


I hope it works for you.