假设简单的线性模型:停留~饮食+吸烟+饮食:吸烟问题是在系数矩阵(边界)中传递维度名称。
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))
RGui控制台的结果:
里美元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|)"
在RGui控制台上可以看到对象(r1, r2)。对象r2是行名的字符串数组。当将R语法放入Rcaller对象时,r2不会传递给java。当不使用列表时,我单独使用对象dimnames(coeff)是相同的。
Java代码: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控制台显示" []"
我可以添加另一个例子,当使用对象列表时,只有一些对象是可见的。
我将感激你的请求
你的例子的问题是部分
lista <- list(r1 = coeff, r2 = dimnames(coeff))
在list中包含一个列表对象,即
dimnames(coeff)
本身是一个列表,RCaller返回的对象必须是一个原语(矩阵和向量)的列表。使用
更改lista的定义lista <- list(r1=coeff, r2=dimnames(coeff)[[1]], r3=dimnames(coeff)[[2]])
使lista成为一个矩阵和两个字符串向量的列表。因为我没有您使用的数据,所以我用一些随机数据复制您的示例:
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");
,我们可以处理输出:
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]);
}
,输出为:
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|)