Skip to content

Commit 5f38646

Browse files
guitargeekdpiparo
authored andcommitted
[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 caf87c6 commit 5f38646

2 files changed

Lines changed: 20 additions & 69 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: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@
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

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

@@ -185,20 +189,16 @@ struct GoFTStress {
185189
Double_t A2 = goft.AndersonDarlingTest("t");
186190
Double_t pvalueAD = goft.AndersonDarlingTest();
187191

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
192+
// R: library(goftest); ad.test(x, "pnorm", mean=300, sd=50)$statistic
193+
Double_t expectedA2 = 1.09849;
194+
193195
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
194196

195197
Double_t Dn = goft.KolmogorovSmirnovTest("t");
196198
Double_t pvalueKS = goft.KolmogorovSmirnovTest();
197199

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

203203
result += PrintResultKS(nsmps, Dn, expectedDn, pvalueKS);
204204
return result;
@@ -225,21 +225,17 @@ struct GoFTStress {
225225
Double_t A2 = goft.AndersonDarlingTest("t");
226226
Double_t pvalueAD = goft();
227227

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
228+
// R: library(goftest); ad.test(x, "pexp", rate=1.54)$statistic
229+
Double_t expectedA2 = 0.54466;
230+
233231
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
234232

235233
// Double_t Dn = goft->KolmogorovSmirnovTest("t");
236234
Double_t Dn = goft(ROOT::Math::GoFTest::kKS, "t");
237235
Double_t pvalueKS = goft.KolmogorovSmirnovTest();
238236

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

244240
result += PrintResultKS(nsmps, Dn, expectedDn, pvalueKS);
245241
return result;
@@ -271,22 +267,18 @@ Int_t UnitTest5() {
271267
Double_t A2 = goft(ROOT::Math::GoFTest::kAD, "t");
272268
Double_t pvalueAD = goft.AndersonDarlingTest();
273269

270+
// R: library(goftest); ad.test(x, "plnorm", meanlog=5, sdlog=2)$statistic
274271
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
279272

280273
Int_t result = PrintResultAD1Sample(A2, expectedA2, pvalueAD);
281274

282275
Double_t Dn = goft.KolmogorovSmirnovTest("t");
283276
// Double_t pvalueKS = goft->KolmogorovSmirnovTest();
284277
Double_t pvalueKS = goft(ROOT::Math::GoFTest::kKS);
285278

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

292284
return result;
@@ -446,42 +438,6 @@ Int_t UnitTest5() {
446438
return EXIT_FAILURE;
447439
}
448440
}
449-
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)