Skip to content

Commit ee84df2

Browse files
committed
[math] Drop R interface from stressGoFTest
The `#ifdef ROOT_HAS_R` blocks in UnitTest3/4/5 used the R interface at runtime to overwrite the hardcoded expected A2 and Dn values with freshly computed ones. The hardcoded values already match what R produces on the same generated sample, so this added a build-time dependency on the R interface (and the `-DROOT_HAS_R` define) for no real benefit. Remove the runtime R calls, the `TRInterface.h` include and the private `R_ADTest` / `R_KSTest` helpers. Each hardcoded reference value now carries an inline `// R: ...` comment quoting the exact `ad.test` / `ks.test` call it was derived from, and the file header documents how to reproduce them by dumping the sample and running R on it. Also drop the now-unused `if(r)` block from the test CMakeLists, since no other test in this directory uses the R interface. This is done in proparation to removing the deprecated R interface.
1 parent 2c052eb commit ee84df2

2 files changed

Lines changed: 18 additions & 67 deletions

File tree

math/mathcore/test/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ if(mathmore)
4545
list(APPEND Libraries MathMore)
4646
endif()
4747

48-
if(r)
49-
add_definitions(-DROOT_HAS_R)
50-
list(APPEND Libraries RInterface)
51-
endif()
52-
5348
#---Build and add all the defined test in the list---------------
5449
foreach(file ${TestSource})
5550
get_filename_component(testname ${file} NAME_WE)

math/mathcore/test/stressGoFTest.cxx

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212

1313
#include "TRandom3.h"
1414

15-
#ifdef ROOT_HAS_R
16-
#include "TRInterface.h"
17-
#endif
18-
1915
#include <iostream>
2016

2117
#include <cassert>
2218

2319
/*N.B.: The tests' expected values (expectedDn and expectedA2) were computed on Pcphsft54.cern.ch i386 GNU/Linux computer (slc4_ia32_gcc34)
2420
2521
LM. (16/9/14) Expected values for AD2 test have been computed with R kSamples package
22+
23+
For the 1-sample tests (UnitTest3, UnitTest4, UnitTest5) the expected A2 and Dn
24+
values quoted next to each test were obtained by running the corresponding R
25+
commands (base R's ks.test and the goftest package's ad.test) on the very same
26+
sample that the C++ code generates. To regenerate them, dump `sample` to a text
27+
file from the test and run the R commands quoted in the inline comments on
28+
`x <- scan("sample.txt")`.
2629
*/
2730
struct GoFTStress {
2831

@@ -185,20 +188,16 @@ struct GoFTStress {
185188
Double_t A2 = goft.AndersonDarlingTest("t");
186189
Double_t pvalueAD = goft.AndersonDarlingTest();
187190

188-
Double_t expectedA2 = 1.09849; // value computed using R below
189-
#ifdef ROOT_HAS_R
190-
Double_t rA2 = R_ADTest(sample,"\"pnorm\", mean=300, sd=50");
191-
if (rA2 != -999) expectedA2 = rA2;
192-
#endif
191+
// R: library(goftest); ad.test(x, "pnorm", mean=300, sd=50)$statistic
192+
Double_t expectedA2 = 1.09849;
193+
193194
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
194195

195196
Double_t Dn = goft.KolmogorovSmirnovTest("t");
196197
Double_t pvalueKS = goft.KolmogorovSmirnovTest();
197198

199+
// R: ks.test(x, "pnorm", mean=300, sd=50)$statistic
198200
Double_t expectedDn = 0.0328567;
199-
#ifdef ROOT_HAS_R
200-
expectedDn = R_KSTest(sample,"\"pnorm\", mean=300, sd=50");
201-
#endif
202201

203202
result += PrintResultKS(nsmps, Dn, expectedDn, pvalueKS);
204203
return result;
@@ -225,21 +224,17 @@ struct GoFTStress {
225224
Double_t A2 = goft.AndersonDarlingTest("t");
226225
Double_t pvalueAD = goft();
227226

228-
Double_t expectedA2 = 0.54466; // value computed using R below
229-
#ifdef ROOT_HAS_R
230-
Double_t rA2 = R_ADTest(sample,"\"pexp\", rate=1.54");
231-
if (rA2 != -999) expectedA2 = rA2;
232-
#endif
227+
// R: library(goftest); ad.test(x, "pexp", rate=1.54)$statistic
228+
Double_t expectedA2 = 0.54466;
229+
233230
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
234231

235232
// Double_t Dn = goft->KolmogorovSmirnovTest("t");
236233
Double_t Dn = goft(ROOT::Math::GoFTest::kKS, "t");
237234
Double_t pvalueKS = goft.KolmogorovSmirnovTest();
238235

236+
// R: ks.test(x, "pexp", rate=1.54)$statistic
239237
Double_t expectedDn = 0.021343;
240-
#ifdef ROOT_HAS_R
241-
expectedDn = R_KSTest(sample,"\"pexp\", rate=1.54");
242-
#endif
243238

244239
result += PrintResultKS(nsmps, Dn, expectedDn, pvalueKS);
245240
return result;
@@ -271,22 +266,18 @@ Int_t UnitTest5() {
271266
Double_t A2 = goft(ROOT::Math::GoFTest::kAD, "t");
272267
Double_t pvalueAD = goft.AndersonDarlingTest();
273268

269+
// R: library(goftest); ad.test(x, "plnorm", meanlog=5, sdlog=2)$statistic
274270
Double_t expectedA2 = 0.458346;
275-
#ifdef ROOT_HAS_R
276-
Double_t rA2 = R_ADTest(sample,"\"plnorm\", meanlog=5, sdlog=2");
277-
if (rA2 != -999) expectedA2 = rA2;
278-
#endif
279271

280272
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
281273

282274
Double_t Dn = goft.KolmogorovSmirnovTest("t");
283275
// Double_t pvalueKS = goft->KolmogorovSmirnovTest();
284276
Double_t pvalueKS = goft(ROOT::Math::GoFTest::kKS);
285277

278+
// R: ks.test(x, "plnorm", meanlog=5, sdlog=2)$statistic
286279
Double_t expectedDn = 0.0214143;
287-
#ifdef ROOT_HAS_R
288-
expectedDn = R_KSTest(sample,"\"plnorm\", meanlog=5, sdlog=2");
289-
#endif
280+
290281
result += PrintResultKS(nsmps, Dn, expectedDn, pvalueKS);
291282

292283
return result;
@@ -447,41 +438,6 @@ Int_t UnitTest5() {
447438
}
448439
}
449440

450-
private:
451-
452-
// function to test using R interface
453-
#ifdef ROOT_HAS_R
454-
ROOT::R::TRInterface &R = ROOT::R::TRInterface::Instance();
455-
double R_KSTest(const std::vector<double> & sample, TString testDist) {
456-
R["x"] = sample;
457-
R << "result = ks.test(x," + testDist + ")";
458-
R << "pval = result$p.value";
459-
R << "stat = result$statistic";
460-
double pvalue = R["pval"];
461-
double tstat = R["stat"];
462-
if (GoFTStress::fgDebugLevel >= GoFTStress::kStandardDebug) {
463-
std::cout << "R KS result : Dn = " << tstat << " pvalue = " << pvalue << std::endl;
464-
}
465-
return tstat;
466-
}
467-
double R_ADTest(const std::vector<double> & sample, TString testDist) {
468-
R << "ret = library(\"goftest\", logical.return = TRUE)";
469-
bool ok = R["ret"];
470-
if (!ok) {
471-
return -999;
472-
}
473-
R["x"] = sample;
474-
R << "result = ad.test(x," + testDist + ")";
475-
R << "pval = result$p.value";
476-
R << "stat = result$statistic";
477-
double pvalue = R["pval"];
478-
double tstat = R["stat"];
479-
if (GoFTStress::fgDebugLevel >= GoFTStress::kStandardDebug) {
480-
std::cout << "R AD result : A2 = " << tstat << " pvalue = " << pvalue << std::endl;
481-
}
482-
return tstat;
483-
}
484-
#endif
485441
};
486442

487443

0 commit comments

Comments
 (0)