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*/
2731struct 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