[R] unable to call R t-test from Java

O'Brien, Laura lobrien at icoria.com
Wed Jul 20 16:41:41 CEST 2005


Hello,

My colleague and I would like to write Java code that invokes R to do a simple TTest.  I've included my sample java code below.  I tried various alternatives and am unable to pass a vector to the TTest method.  In my investigation, I tried to call other R methods that take vectors and also ran into various degrees of failure.   Any insight you can provide or other Web references you can point me to would be appreciated.

Thank you,
Laura O'Brien
Application Architect



---------------------------  code   ------------------------------


package org.omegahat.R.Java.Examples;

import org.omegahat.R.Java.ROmegahatInterpreter;
import org.omegahat.R.Java.REvaluator;


public class JavaRCall2
{

    /**
     * want to see if I can eval a t.test command like what I would run in the
     * R command line
     */

    static public void runTTestByEval_cores(REvaluator e, ROmegahatInterpreter interp)
    {
        /* produces a core */
        System.err.println("eval a t.test");
        Object value = e.eval("t.test (c(1,2,3), c(4,5,6))");
        if (value != null)
            interp.show(value);
    }

    
    /**
     * want to see if I can eval anything that takes a vector, e.g. mean, 
     * like what I would run in the R command line
     */

    static public void runMeanByEval_works(REvaluator e, ROmegahatInterpreter interp)
    {
        System.err.println("\r\n  evaluation string mean command");
        Object value = e.eval("mean(c(1,2,3))");
        if(value != null) 
        {
            interp.show(value ); 
            System.err.println("\r\n");
        }    
    }

/**
 *  if I pass mean a org.omegahat.Environment.DataStructures.numeric what do I get?  NaN
 */
 
    static public void runMeanByNumericList_nan(REvaluator e, ROmegahatInterpreter interp)
    {    
         Object[] funArgs = new Object[1];
         // given argument is not numeric or logical
         
         org.omegahat.Environment.DataStructures.numeric rList1 = new org.omegahat.Environment.DataStructures.numeric(3);       
    
         double[] dList = new double[3];
         dList[0] = (double) 1.1;
         dList[1] = (double) 2.2; 
         dList[2] = (double) 3.3;
         rList1.setData(dList, true);
         System.err.println(rList1.toString());

         funArgs[0] = rList1 ;
         
         System.err.println("\r\n Calling mean and passing an omegahat vector");

    
         Object value =  e.call("mean", funArgs); 
         if(value != null) 
         {
         interp.show(value ); 
         System.err.println("\r\n");
         }

    }
    
    /**
     * let's run some tests on the vector passed in and see what R thinks I'm handing it
     * 
     * it returns 
     * isnumeric:     false
     * mode:          list
     * length:        2
     */

    public static void runTestsOnOmegahatNumeric(REvaluator e, ROmegahatInterpreter interp)
    {
        Object[] funArgs = new Object[1];
        // given argument is not numeric or logical
         
        org.omegahat.Environment.DataStructures.numeric rList1 = new org.omegahat.Environment.DataStructures.numeric(3);       
    
        double[] dList = new double[3];
        dList[0] = (double) 1.1;
        dList[1] = (double) 2.2; 
        dList[2] = (double) 3.3;
        rList1.setData(dList, true);
        System.err.println(rList1.toString());

        funArgs[0] = rList1 ;
         
        System.err.println("\r\n Calling isnumeric and passing an omegahat vector");
   
        Object value =  e.call("is.numeric", funArgs); 
        if(value != null) 
        {
            interp.show(value ); 
            System.err.println("\r\n");
        }

        // mode is list

        System.err.println("\r\n Calling mode and passing an omegahat vector");
   
        value =  e.call("mode", funArgs); 
        if(value != null) 
        {
            interp.show(value ); 
            System.err.println("\r\n");
        }

        System.err.println("\r\n Calling length and passing an omegahat vector");
        System.err.println("\r\n");
        System.err.println("INTERESTING:  thinks the length is 2!");

   
        value =  e.call("length", funArgs); 
        if(value != null) 
        {
            interp.show(value ); 
            System.err.println("\r\n");
        }

    }

    /**
     * run the mean on a java array.  Does it also return NAN?  
     * It returns a different value than the mean -- 2.19999... instead of 2.2
     */

    static public void runMeanOnJavaArray(REvaluator e, ROmegahatInterpreter interp)
    {
        Object[] funArgs = new Object[1];
        double[] d = { 1.1, 2.2, 3.3};
        funArgs[0] = d;
        System.err.println("\r\n Calling mean of (1.1, 2.2, 3.3) and passing a java array\r\n");
        System.err.println("INTERSTING:  thinks the mean is 2.19999... it should be 2.2 \r\n");
   
        Object value =  e.call("mean", funArgs); 
        if(value != null) 
        {
            interp.show(value ); 
            System.err.println("\r\n");
        }
    }

    /**
     *    This is what I really want! 
     */

    static public void runTTestOnJavaArray_cores(REvaluator e, ROmegahatInterpreter interp)
    {
        Object[] funArgs = new Object[2];
        double[] d0 = { 1.1, 2.2, 3.3};
        double[] d1 = { 9.9, 8.8, 7.7};

        funArgs[0] = d0;
        funArgs[1] = d1;
        
         System.err.println("\r\n Calling t.test and passing a java array");
   
         Object value =  e.call("t.test", funArgs); 
         if(value != null) 
         {
         interp.show(value ); 
         System.err.println("\r\n");
         }
        
    }

    /**
     * 
     */
    static public void main(String[] args) 
    {
        ROmegahatInterpreter interp = new ROmegahatInterpreter(ROmegahatInterpreter.fixArgs(args), false);
        REvaluator e = new REvaluator();

        Object[] funArgs;
        String[] objects;
        Object value;
        int i;

        /** GOAL:  pass a vector of numbers into a t-test
        */
         
            // unfortunately it core dumps
            runTTestOnJavaArray_cores(e, interp);   


        /*         since I've been unable to get that work, 
        *         I try various evaluation commands to see if I 
        *         can get any sort of R commands that take vectors to work
        *         in any sort of fashion
        */
          

        //
        //   SAMPLE eval based calls
        //
          
        //  can I successfully invoke a t.test command similar to what I can
        //  do via the command line, e.g. t.test (c(1,2,3), c(4,5,6))
        //  NO -- this core dumps
        // runTTestByEval_cores(e, interp);
            
        // can I eval anything that takes a vector -- let's try the mean method
        // yes this works!
            //runMeanByEval_works(e, interp);
            
          
        //
        //    SAMPLE org.omegahat.Environment.DataStructures.numeric 
        //

        // this returns Nan
          
        // runMeanByNumericList_nan(e, interp);

        // given that the above returns NaN, what does R think I'm passing it for a vector?
        // let's run some tests
        runTestsOnOmegahatNumeric(e, interp);
          
        // now let's try some tests on a java array instead of an omegahat numeric
        runMeanOnJavaArray(e, interp);
    }
}




More information about the R-help mailing list