モデルBG-NBD実装を使用するためにいくつかのRコードを使用しています。Javaを使用しており、RCallerを使用してRを呼び出しています。私は本当にRに慣れていないので、何か間違ったことをしているのかもしれません。私はこのURLのコードを使用します:http ://code.google.com/p/clv-master-thesis/ これは私の動作するJavaコードです(単純なJunitテスト):
@Test
public void testRCaller(){
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("/usr/bin/Rscript");
caller.cleanRCode();
RCode code = new RCode();
code.clear();
String helper = "/dati/helper.R";
String modelNbd = "/dati/model-nbd.R";
String modelParetoNbd = "/dati/model-pareto-nbd.R";
String modelBgNbd = "/dati/model-bg-nbd.R";
String modelCbgCnbd = "/dati/model-cbg-cnbd-k.R";
code.R_source(helper);
code.R_source(modelNbd);
code.R_source(modelParetoNbd);
code.R_source(modelBgNbd);
code.R_source(modelCbgCnbd);
code.addRCode("cdData <- read.table(\"/dati/cdnow.csv\", head=T)");
code.addRCode("names(cdData)[2] <- \"x\";");
code.addRCode("bgMleFit <- bgEstimateParameters(cdData, list(r=1, alpha=2, a=1, b=2));");
code.addRCode("summary(bgMleFit);");
code.addRCode("cdBgParams <- as.list(coef(bgMleFit));");
code.addRCode("t <- 39;");
code.addRCode("cdBgCe <- bgConditionalForecast(cdData, cdBgParams, t);");
code.addRCode("(cdBgSumEstimate <- sum(cdBgCe));");
code.addRCode("(cdBgMsle <- mean((log(cdData$p2x+1)-log(cdBgCe+1))^2));");
code.addRCode("(corr <- cor(cdData$p2x, cdBgCe));");
caller.setRCode(code);
caller.runAndReturnResult("cdBgCe");
ROutputParser parser = caller.getParser();
ArrayList<String> nomi = parser.getNames();
for (String nome : nomi) {
double[] previsioni = parser.getAsDoubleArray(nome);
logger.info("Nome "+nome+" lunghezza valori "+previsioni.length);
for (int i = 0; i < previsioni.length; i++) {
logger.info("Valore "+ previsioni[i]);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
このコードを実行することにより、すべてがかなりうまく機能します。今、私はcsvファイルをデータソースとして使用したくありませんでした。DBにクエリを実行し、データをRに渡したいと思いました。Rのread.table関数は、提供されたcsvファイルの読み取りからdata.frameを構築します。つまり、私が行ったことは次のとおりです。csvを読み取るためにJavaを使用し、このコードを記述しました(csvファイルを読み取った後、わかりやすくするために、csv読み取りに関連するコードは記述しません)。
@Test
public void testRCaller(){
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("/usr/bin/Rscript");
caller.cleanRCode();
RCode code = new RCode();
code.clear();
String helper = "/dati/helper.R";
String modelNbd = "/dati/model-nbd.R";
String modelParetoNbd = "/dati/model-pareto-nbd.R";
String modelBgNbd = "/dati/model-bg-nbd.R";
String modelCbgCnbd = "/dati/model-cbg-cnbd-k.R";
code.R_source(helper);
code.R_source(modelNbd);
code.R_source(modelParetoNbd);
code.R_source(modelBgNbd);
code.R_source(modelCbgCnbd);
Map<String, Object> data = this.readCsvData();
StringBuilder userIds = new StringBuilder("ID <- c(");
long[] utenti = (long[])data.get("userIds");
int[] ordini = (int[]) data.get("ordini");
double[] tx = (double[]) data.get("tx");
double[] t = (double[])data.get("t");
int[] p2x = (int[]) data.get("p2tx");
for(int i = 0; i < utenti.length; i++){
userIds.append(utenti[i]+", ");
}
//here i check if the stringbuilder ends with, and i clean it...checkSb is simply an utility method
userIds = checkSb(userIds).append(");");
code.addIntArray("p1x", ordini);
code.addDoubleArray("tx", tx);
code.addDoubleArray("t", t);
code.addIntArray("p2x", p2x);
code.addRCode("cdData<-data.frame(ID , p1x, tx, t, p2x);");
code.addRCode("names(cdData)[2] <- \"x\";");
code.addRCode("bgMleFit <- bgEstimateParameters(cdData, list(r=1, alpha=2, a=1, b=2));");
code.addRCode("summary(bgMleFit);");
code.addRCode("cdBgParams <- as.list(coef(bgMleFit));");
code.addRCode("t <- 39;");
code.addRCode("cdBgCe <- bgConditionalForecast(cdData, cdBgParams, t);");
code.addRCode("(cdBgSumEstimate <- sum(cdBgCe));");
code.addRCode("(cdBgMsle <- mean((log(cdData$p2x+1)-log(cdBgCe+1))^2));");
code.addRCode("(corr <- cor(cdData$p2x, cdBgCe));");
caller.setRCode(code);
caller.runAndReturnResult("cdBgCe");
ROutputParser parser = caller.getParser();
ArrayList<String> nomi = parser.getNames();
for (String nome : nomi) {
double[] previsioni = parser.getAsDoubleArray(nome);
logger.info("Nome "+nome+" lunghezza valori "+previsioni.length);
for (int i = 0; i < previsioni.length; i++) {
logger.info("Valore "+ previsioni[i]);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
ご覧のとおり、read.table関数を使用しないことを除けば、コードは前のコードとまったく同じです。このコードを実行すると、エラーが発生します。最初はJavaコードにエラーがあると思いましたが、チェックしたところエラーはありませんでした。次に、Rコンソールでコードを試しました。さて、私は本当に奇妙な何かを持っています。この命令から始めましょう(int配列はcsvファイルから復元されます):
int[] ordini = (int[]) data.get("ordini");
code.addIntArray("p1x", ordini);
このJava-RCaller命令は、次のRコードを生成します:p1x <-c(..........)。より正確には、生成されたRコードは次のとおりです(準備ができています:それは巨大です):
p1x<-c(2, 1, 0, 0, 0, 7, 1, 0, 2, 0, 5, 0, 0, 0, 0, 0, 10, 1, 3, 0, 2, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 12, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 1, 0, 1, 0, 4, 0, 1, 0, 0, 0, 2, 0, 0, 3, 0, 0, 1, 0, 1, 0, 0, 0, 3, 0, 0, 1, 0, 0, 0, 7, 0, 10, 0, 0, 1, 6, 0, 0, 0, 0, 0, 2, 4, 1, 5, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 3, 0, 1, 2, 0, 1, 2, 0, 2, 1, 1, 0, 5, 2, 7, 2, 0, 4, 13, 0, 4, 4, 0, 0, 1, 0, 1, 29, 0, 3, 0, 0, 1, 0, 10, 0, 0, 13, 0, 0, 1, 0, 0, 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3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2, 0, 1, 1, 1, 0, 0, 0, 2, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 4, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 1, 0, 0, 6, 0, 0, 0, 0, 1, 0, 0, 2, 6, 0, 1, 0, 0, 0, 0, 2, 0, 7, 0, 1, 2, 1, 1, 1, 0, 0, 1, 5, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 5, 1, 0, 1, 2, 0, 0, 1, 2, 0, 1, 0, 2, 0, 1, 2, 7, 1, 2, 0, 0, 0, 5, 0, 4, 0);
Rコンソールでこの関数を実行すると、奇妙な動作が発生します。「予期しない要素が……」などのエラーが発生し、どこが間違っているのかわからない場合があります。代わりに、「read.table」を使用すると、実行されないように見えます。関数」はすべてかなりうまく機能します。ヒントを教えてください。私はどこか間違っていますか?