@@ -26,31 +26,16 @@ pnhyper <- function(
2626 q , ncp = 1 , upper.tail = FALSE , m , n , k , lo , hi , support , logdc
2727) {
2828
29- lims <- which(ncp == 0 | is.infinite(ncp ))
30- ones <- which(ncp == 1 )
31- ncp1 <- which(ncp == 1 )
32- res <- numeric (length(q ))
33-
34- res [lims ] <- ifelse(
35- ncp == 0 ,
36- as.numeric(if (upper.tail ) q < = lo else q > = lo ),
37- as.numeric(if (upper.tail ) q < = hi else q > = hi )
38- )
39-
40- res [ones ] <- phyper(
41- x [ones ] - upper.tail , m [ones ], n [ones ], k [ones ],
42- lower.tail = ! upper.tail
43- )
44-
45- res [- c(ones , lims )] <- mapply(
46- function (q , ncp , logdc , support ) {
47- sum(dnhyper(ncp , logdc , support )[if (upper.tail ) support > = q else support < = q ])
48- },
49- q = q [- c(ones , lims )], ncp = ncp [- c(ones , lims )], logdc = logdc [- c(ones , lims )],
50- support = support [- c(ones , lims )]
51- )
29+ if (ncp == 0 ) {
30+ return (as.numeric(if (upper.tail ) q < = lo else q > = lo ))
31+ } else if (is.infinite(ncp )) {
32+ return (as.numeric(if (upper.tail ) q < = hi else q > = hi ))
33+ } else if (ncp == 1 ) {
34+ return (phyper(q - upper.tail , m , n , k , lower.tail = ! upper.tail ))
35+ } else {
36+ return (sum(dnhyper(ncp , logdc , support )[if (upper.tail ) q < = support else support < = q ]))
37+ }
5238
53- return (res )
5439}
5540
5641# ncp.U == ncp_ci(..., lower = FALSE)
@@ -126,6 +111,8 @@ vfisher.test <- function(
126111 or <- rep(or , length.out = length(a ))
127112 }
128113
114+ alternative <- match.arg(alternative )
115+
129116 # matrix =
130117 # a, b
131118 # c, d
@@ -151,6 +138,20 @@ vfisher.test <- function(
151138 logdc : = .(list (dhyper(support [[1 ]], m , n , k , log = TRUE ))), by = rowid
152139 ]
153140
141+ if (alternative %in% c(" less" , " greater" )) {
142+ result_dt [, p.value : = pnhyper(
143+ a , or , upper.tail = alternative == " greater" , m , n , k , lo , hi , support [[1 ]], logdc [[1 ]]
144+ ), by = rowid ]
145+ } else {
146+ result_dt [or == 0 , p.value : = as.numeric(a == lo )]
147+ result_dt [is.infinite(or ), p.value : = as.numeric(a == hi )]
148+ result_dt [! (or == 0 | is.infinite(or )), p.value : = {
149+ relErr <- 1 + 10 ^ (- 7 )
150+ d <- dnhyper(or , logdc [[1 ]], support [[1 ]])
151+ sum(d [d < = d [a - lo + 1 ] * relErr ])
152+ }, by = rowid ]
153+ }
154+
154155 # x == a
155156
156157 result_dt [a == lo , estimate : = 0 ]
@@ -164,16 +165,12 @@ vfisher.test <- function(
164165
165166 if (conf.int ) {
166167
167- alternative <- match.arg(alternative )
168-
169168 if (! ((length(conf.level ) == 1L ) && is.finite(conf.level ) &&
170169 (conf.level > 0 ) && (conf.level < 1 )))
171170 stop(" 'conf.level' must be a single number between 0 and 1" )
172171
173172 sdcols <- c(sdcols , c(" ci.lo" , " ci.hi" ))
174173
175- setattr(result_dt , " conf.level" , conf.level )
176-
177174 if (alternative == " less" ) {
178175 result_dt [, ci.lo : = 0 ]
179176 result_dt [, ci.hi : = ncp_ci(a , 1 - conf.level , m , n , k , lo , hi , support [[1 ]], logdc [[1 ]], lower = FALSE ), by = rowid ]
@@ -188,68 +185,10 @@ vfisher.test <- function(
188185
189186 }
190187
191- result_dt [, .SD , .SDcols = sdcols ]
192-
193- }
188+ res <- result_dt [, .SD , .SDcols = sdcols ]
194189
190+ if (conf.int ) setattr(result_dt , " conf.level" , conf.level )
195191
196- function (x , y = NULL , workspace = 2e+05 , hybrid = FALSE , hybridPars = c(expect = 5 ,
197- percent = 80 , Emin = 1 ), control = list (), or = 1 , alternative = " two.sided" ,
198- conf.int = TRUE , conf.level = 0.95 , simulate.p.value = FALSE ,
199- B = 2000 )
200- {
201-
202- PVAL <- NULL
203- else {
204- PVAL <- switch (alternative , less = pnhyper(x , or ),
205- greater = pnhyper(x , or , upper.tail = TRUE ),
206- two.sided = {
207- if (or == 0 ) as.numeric(x == lo ) else if (or ==
208- Inf ) as.numeric(x == hi ) else {
209- relErr <- 1 + 10 ^ (- 7 )
210- d <- dnhyper(or )
211- sum(d [d < = d [x - lo + 1 ] * relErr ])
212- }
213- })
214- }
192+ return (res )
215193
216- ESTIMATE <- c(`odds ratio` = mle(x ))
217- if (conf.int ) {
218- ncp.U <- function (x , alpha ) {
219- if (x == hi )
220- return (Inf )
221- p <- pnhyper(x , 1 )
222- if (p < alpha )
223- uniroot(function (t ) pnhyper(x , t ) - alpha ,
224- c(0 , 1 ))$ root
225- else if (p > alpha )
226- 1 / uniroot(function (t ) pnhyper(x , 1 / t ) - alpha ,
227- c(.Machine $ double.eps , 1 ))$ root
228- else 1
229- }
230- ncp.L <- function (x , alpha ) {
231- if (x == lo )
232- return (0 )
233- p <- pnhyper(x , 1 , upper.tail = TRUE )
234- if (p > alpha )
235- uniroot(function (t ) pnhyper(x , t , upper.tail = TRUE ) -
236- alpha , c(0 , 1 ))$ root
237- else if (p < alpha )
238- 1 / uniroot(function (t ) pnhyper(x , 1 / t , upper.tail = TRUE ) -
239- alpha , c(.Machine $ double.eps , 1 ))$ root
240- else 1
241- }
242- CINT <- switch (alternative , less = c(0 , ncp.U(x ,
243- 1 - conf.level )), greater = c(ncp.L(x , 1 - conf.level ),
244- Inf ), two.sided = {
245- alpha <- (1 - conf.level )/ 2
246- c(ncp.L(x , alpha ), ncp.U(x , alpha ))
247- })
248- attr(CINT , " conf.level" ) <- conf.level
249- }
250- RVAL <- c(RVAL , list (conf.int = if (conf.int ) CINT , estimate = ESTIMATE ,
251- null.value = NVAL ))
252- }
253- structure(c(RVAL , alternative = alternative , method = METHOD ,
254- data.name = DNAME ), class = " htest" )
255194}
0 commit comments