为什么R计算在我的虚拟机中不一致?

我试图build立一个新的虚拟机与R和下面的包作为R server运行到我的计算。

  #this is how I install my R-packages function install_packages(){ folder='dir.create(Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)' packages='install.packages(c("Rserve","fArma","fGarch","tseries","MASS","lattice","gtools","gmodels","gplots","HiddenMarkov", "xts", "PerformanceAnalytics"), Sys.getenv("R_LIBS_USER"), repos = "http://cran.rstudio.com")' echo "$folder" >> ./install_packages.R echo "$packages" >> ./install_packages.R sudo /usr/bin/R CMD BATCH install_packages.R rm -f ./install_packages.R } 

如果我从主机上向这个新的虚拟机发出一个呼叫(使用mvn clean package ),它会给我一个奇怪的错误:

 Running com.company.documentengine.statistics.JensensAlphaTest Oct 28, 2015 2:17:45 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB INFO: PostgreSQL JDBC Driver Registered Oct 28, 2015 2:17:45 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB INFO: test Database connection confirmed for user postgres Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 17.971 sec <<< FAILURE! - in com.company.documentengine.statistics.JensensAlphaTest testCalculate(com.company.documentengine.statistics.JensensAlphaTest) Time elapsed: 8.821 sec <<< FAILURE! java.lang.AssertionError: Calculation wrong. expected:<0.039801296645998546> but was:<NaN> at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:553) at com.company.documentengine.statistics.JensensAlphaTest.testCalculate(JensensAlphaTest.java:40) 

现在,如果我使用新的虚拟机进行同样的调用,而且还安装了所有这些软件包,那么一切正常。

 ------------------------------------------------------- TESTS ------------------------------------------------------- Running com.company.documentengine.statistics.JensensAlphaTest Oct 28, 2015 1:23:13 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB INFO: PostgreSQL JDBC Driver Registered Oct 28, 2015 1:23:13 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB INFO: test Database connection confirmed for user postgres Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 17.465 sec - in com.company.documentengine.statistics.JensensAlphaTest Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 21.423s [INFO] Finished at: Wed Oct 28 13:23:20 UTC 2015 [INFO] Final Memory: 18M/362M [INFO] ------------------------------------------------------------------------ 

我真的很困惑这个,请谁能给我一些build议/想法!

编辑

我试图debugging我的testing,看看我犯了什么错误,但仍然没有线索。 现在我知道至less我的问题是… 请看我的debugging比较 。 这是两种情况下使用的所有软件包的比较。

Java代码

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestContext.class}) @ActiveProfiles(profiles = {"test"}) public class JensensAlphaTest { @Autowired private TestSeriesManager testSeriesManager; @Test public void testCalculate() throws Exception { PriceSeries<PriceSeriesDatum> dax = testSeriesManager.getDax(); PriceSeries<PriceSeriesDatum> sDax = testSeriesManager.getSDax(); InterestRateSeries<InterestRateDatum> euribor = testSeriesManager.getEuribor(); LocalDate asOfDate = LocalDate.of(2014, 10, 1); JensensAlpha jensensAlpha = new JensensAlpha(dax, sDax, euribor, asOfDate); double eps = 1e-15; /* here is the inconsistent part */ double actualValue = jensensAlpha.calculate(Period.SINCE_INCEPTION, ReturnsType.DAILY_DISCRETE); double expectedValue = 0.039801296645998546; assertEquals("Calculation wrong.", expectedValue, actualValue, eps); } } 

这是所谓的方法:

public double calculate(Period period,ReturnsType returnsType){

 NavigableMap<LocalDate, Double> returnSeries = returnsType.getReturnSeries(series); NavigableMap<LocalDate, Double> returnBenchmark = returnsType.getReturnSeries(benchmark); NavigableMap<LocalDate, Double> returnRiskFree = returnsType.getReturnSeries(riskFree); LocalDate startDate = period.getStartDate(returnSeries); NavigableMap<LocalDate, Double> cutReturnSeries = StatisticsUtils.getMapSince(startDate, returnSeries); NavigableMap<LocalDate, Double> cutBenchmarkReturnSeries; NavigableMap<LocalDate, Double> cutRiskFreeReturnSeries; try { cutBenchmarkReturnSeries = StatisticsUtils.getMapSince(startDate, returnBenchmark); cutRiskFreeReturnSeries = StatisticsUtils.getMapSince(startDate, returnRiskFree); } catch (IllegalArgumentException e) { throw new NotEnoughDataException( "This error can occur when the price series is short (only a few returns), so the benchmark is not" + " updated for the taken first date of the series.", e); } REXPS4[] inputClasses = {RexpParser.createREXPS4Class(cutReturnSeries), RexpParser.createREXPS4Class(cutBenchmarkReturnSeries), RexpParser.createREXPS4Class(cutRiskFreeReturnSeries)}; RScript script = RScript.fromFileName("JensensAlpha.R"); REXPS4 resultClass = script.execute(inputClasses); try { return resultClass.getAttribute("value").asDouble(); } catch (REXPMismatchException e) { throw new RScriptException("Exception while getting results from the R script.", e); } 

}

而执行方法:

 @Override public REXPS4 execute(REXPS4[] inputClasses) { RConnection c = RConnectionSingleton.INSTANCE.getRConnection(); try { int inputClassNumber = 1; for (REXPS4 inputClass : inputClasses) { c.assign("inputClass" + inputClassNumber, inputClass); inputClassNumber++; } c.eval(code); /* the resultClass is wrong only when I connect to my vm */ return (REXPS4) c.get("resultClass", null, true); } catch (REngineException e) { throw new ScriptExecutionException("Exception while trying to execute the RScript.", e); } } 

只是为了让你知道我的问题发生了什么。 问题是时区。 我不知道为什么,但是R或我们用于计算的一些软件包需要时区相同。

我位于德国(时区CET是+1 UTC ),我正在设置我的虚拟机使用UTC,从而出现问题。 哦,我真的很高兴解决这个问题(连续3天工作!),但现在一切都很好! 非常感谢我的同事@Ralf提示!

其他相关问题: 1,2,3 。 我希望这可以帮助别人! 🙂