Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an internal R C function from your own code

Tags:

r

I'd like to reuse R code from the stats package that simulates contingency tables to compute a simulated p.value for a chi-squared test.

When looking at the chisq.test function source code, you can see the following :

 if (simulate.p.value && all(sr > 0) && all(sc > 0)) {
        setMETH()
        tmp <- .Call(C_chisq_sim, sr, sc, B, E)
        STATISTIC <- sum(sort((x - E)^2/E, decreasing = TRUE))
        PARAMETER <- NA
        PVAL <- (1 + sum(tmp >= almost.1 * STATISTIC))/(B + 
            1)
    }

The interesting line here is the .Call call :

tmp <- .Call(C_chisq_sim, sr, sc, B, E)

What I'd like to do, if it is possible, is to use this C_chisq_sim function in my own code, but I can't manage to do it. If I try with :

tmp <- .Call(C_chisq_sim, sr, sc, B, E, PACKAGE="stats")

I get a C_chisq_sim object not found error. And if I try with :

tmp <- .Call("C_chisq_sim", sr,sc,B,E, PACKAGE="stats") 

I get an error saying that the entry point is not in the loading table.

I'd like a solution that would be cross-platform, if possible.

like image 248
juba Avatar asked Jun 25 '13 13:06

juba


1 Answers

This should do the trick I guess:

tmp <- .Call(stats:::C_chisq_sim, sr, sc, B, E, PACKAGE="stats")
like image 157
rinni Avatar answered Nov 02 '22 22:11

rinni