-1

I'm preparing the package in R language for the CRAN submission. However, I've to calculate the p-value for the one-sample Kolmogorov-Smirnow test using the external call to the function C_pKolmogorov2x from the stats package, i.e.

1- .Call(stats:::C_pKolmogorov2x,PACKAGE = "stats", criticalValue, n1)

During checking and building my package it causes warnings:

Foreign function call to a base package:
    .Call(stats:::C_pKolmogorov2x, ..., PACKAGE = "stats")
  Packages should not make .C/.Call/.External/.Fortran calls to a base
  package. They are not part of the API, for use only by R itself and
  subject to change without notice.

Because I've got a final value of the test statistic, to obtain the p-value I can't use ks.test, which can be applied only if we have a whole sample.

Does anybody know the other way (e.g., function from other package to use directly) to calculate this p-value using cdf for the K-S one-sample test? Or maybe the warning will not cause rejecting my package from CRAN? Thank you in advance for your help!

I've tried to found other packages intended to calculate the above-mentioned p-value. However, without success.

errenay
  • 1
  • 1
  • Are you maybe looking for `ks.test`? – Mikael Jagan Jun 03 '23 at 16:05
  • I'm puzzled. I can't find `pKolmogorov2x` anywhere in the [current R code base](https://github.com/search?q=repo%3Ar-devel%2Fr-svn+pKolmogorov2x&type=code) (the current version has calls to functions like `C_pkolmogorov_two_exact`). Can you say a little more about how you decided that this was the right call? (I do find this in some repos that link to [older versions of R](https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/ks.test.R) ...) – Ben Bolker Jun 03 '23 at 19:23
  • Yes, in the development version this call is changed. However, in the last stable version 4.3.0, the necessary function is coded as `pKolmogorov2x`. – errenay Jun 04 '23 at 03:28

1 Answers1

1

Based on the latest version of R (conveniently available on a Github mirror of the development SVN repository), the probability function for the one-sided one-sample exact test is short and coded in R — which is convenient for you, as you can't call any components of R in a CRAN package other than (1) exported/publicly available R functions or (2) C functions that are defined in R.h or other header files - from the CRAN repository policy,

CRAN packages should use only the public API. Hence they should not use entry points not declared as API in installed headers nor .Internal() nor .Call() etc calls to base packages. Also, ::: should not be used to access undocumented/internal objects in base packages (nor should other means of access be employed).

Here's the code from the link above:

pkolmogorov_one_exact <- function(q, n, lower.tail = TRUE) {
    ## Probability function for the one-sided one-sample Kolmogorov
    ## statistics, based on the formula of Birnbaum & Tingey (1951).
    j <- seq.int(from = 0, to = floor(n * (1 - q)))
    p <- q * sum(exp(lchoose(n, j)
                     + (n - j) * log(1 - q - j / n)
                     + (j - 1) * log(q + j / n)))
    if(lower.tail) 1 - p else p
}

If you need the two-sided test, things will be harder; you would have to copy the C code here (lines 403 to 472) and integrate it into your package ...

You can copy this code into your package (although you may need to be careful if your code is licensed in some way other than GPL); it would be polite but is not legally required to acknowledge where it came from.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thank you for your comment - but what about two-sided one-sample test? In this case the function `pkolmogorov_two_exact` is used with the call to the external code `.Call(C_pkolmogorov_two_exact, q, n)`. – errenay Jun 04 '23 at 03:32
  • You might be out of luck then - you might need to copy the C code to your package ... – Ben Bolker Jun 04 '23 at 20:51