-1

Assume simple linear model : Stay ~ Diet + Smoke + Diet:Smoke The problem is to pass the dimension names in the matrix of coefficients (borders).

skrypt in R: (syntaks.txt)

BaseFrame=read.csv("DietSmoke.txt")

modd<-glm(Stay ~ Diet + Smoke + Diet:Smoke , BaseFrame, family = gaussian) 

coeff=summary(modd)$coef

lista<-list(r1=coeff,r2=dimnames(coeff))

results on RGui console:

lista
$r1

              Estimate Std. Error       t value  Pr(>|t|)
(Intercept)   3.666667e+00   2.441159  1.502019e+00 0.1538496
DietB         1.833333e+00   2.989797  6.131965e-01 0.5489321
DietC         2.666667e+00   3.452321  7.724273e-01 0.4518611
SmokeY        5.333333e+00   3.452321  1.544855e+00 0.1432147
DietB:SmokeY -5.166667e+00   4.566991 -1.131306e+00 0.2756896
DietC:SmokeY  2.756121e-15   4.882319  5.645107e-16 1.0000000

$r2
$r2[[1]]

[1] "(Intercept)" "DietB"   "DietC"   "SmokeY" "DietB:SmokeY" "DietC:SmokeY"

$r2[[2]]

[1] "Estimate"   "Std. Error" "t value"    "Pr(>|t|)" 

On the RGui console objects (r1, r2) are seen. Object r2 is string array of row names .When'll put R syntax to Rcaller object r2 is not passed to the java. When instead of a list I put alone the object dimnames(coeff) is the same .

Java code:

RCaller caller = new RCaller();
caller.setRscriptExecutable("C:\\R\\R-3.1.2\\bin\\Rscript.exe");
RCode code = new RCode();
code.addRCode(syntaks);
caller.setRCode(code);
caller.runAndReturnResult("lista");
System.out.println(caller.getParser().getNames()); 

Eclipse console shows " []"

I can add that another examples when used list of objects only some objects are seen.

I will be gratefull for the request.

1 Answers1

0

The problem with your example is that the part

lista <- list(r1 = coeff, r2 = dimnames(coeff))

includes a list object in list, that is,

dimnames(coeff)

is a list itself and the returned object in RCaller must be a list of primitives (Matrices and vectors). Changing definition of lista with

lista <- list(r1=coeff, r2=dimnames(coeff)[[1]], r3=dimnames(coeff)[[2]])

make lista a list of a matrix and two string vectors. Since I don't have the data you use, I replicate your example with some random data:

    RCaller caller = RCaller.create();
    RCode code = RCode.create();

    code.addDoubleArray("Stay", getRandomDouble(30));
    code.addDoubleArray("Diet", getRandomDouble(30));
    code.addDoubleArray("Smoke", getRandomDouble(30));
    code.addRCode("BaseFrame <- as.data.frame(cbind(Stay,Diet,Smoke))");
    code.addRCode("modd <- glm(Stay ~ Diet + Smoke + Diet:Smoke, BaseFrame, family = gaussian)");
    code.addRCode("coeff <- summary(modd)$coef");
    code.addRCode("lista<-list(r1 = coeff, r2 = dimnames(coeff)[[1]], r3 = dimnames(coeff)[[2]])");
    caller.setRCode(code);
    caller.runAndReturnResult("lista");

and we can handle the output by:

    System.out.println("r1:");
    double[][] data = caller.getParser().getAsDoubleMatrix("r1");
    for (int i = 0; i < data.length; i++){
        for (int j = 0; j < data[0].length; j++){
            System.out.print(data[i][j] + " ");
        }
        System.out.println();
    }

    System.out.println("r2:");
    String[] names = caller.getParser().getAsStringArray("r2");
    for (int i = 0; i < names.length; i++){
        System.out.println(names[i]);
    }

    System.out.println("r3:");
    names = caller.getParser().getAsStringArray("r3");
    for (int i = 0; i < names.length; i++){
        System.out.println(names[i]);
    }

and the output is:

r1:
59.2591703775371 0.12534103762444 0.0438737218457358 -0.0042978710538064 
18.7562448759218 0.3291208843117 0.31313594046636 0.00547569333255683 
3.1594368046245 0.38083586791087 0.140110783132699 -0.784899882587023 
0.00398304744186597 0.706419085801913 0.889652249263241 0.439606023811926 
r2:
(Intercept)
Diet
Smoke
Diet:Smoke
r3:
Estimate
Std. Error
t value
Pr(>|t|)
jbytecode
  • 681
  • 12
  • 29