1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#############
# Fisher 23 #
#############
fisher23.model <- function() {
return(list(model=list(typ="Fisher 2-by-3")))
}
mutoss.fisher23.model <- function() { return(new(Class="MutossMethod",
label="Fisher's exact test in (2x3) tables",
callFunction="fisher23.model",
output=c("model"),
info="<h2></h2>
<p>(Marginal) Fisher's exact test in (2x3) tables</p>
<p></p>
<h3>Reference:</h3>
<ul>
<li>Fisher, R. A. (1922). \"<i>On the interpretation of Chi^2 from contingency tables, and the calculation of P.</i>\" Journal of the Royal Statistical Society, 85 (1):87-94.</li>
</ul>",
parameters=list(
)
)) }
fisher23.marginal <- function(data, model) {
#m <- dim(data)[3]
#result <- vector(mode="numeric",length=m)
result <- apply(data, 3, function(x) {fisher23_fast(x,2.0e-16)$rand_p} )
return(list(pValues=result))
}
fisher23_fast <- function(obs, epsilon){
# obs = observations = a 2x3 table
#build marginals = (n1., n2., n.1, n.2, n.3)
marginals <- c(sum(obs[1, ]), sum(obs[2, ]), sum(obs[, 1]), sum(obs[, 2]), sum(obs[, 3]))
n <- sum(marginals) / 2
x <- array(data=rep.int(0.0, 2*3*max(marginals)*max(marginals)), c(2, 3, max(marginals)*max(marginals)))
#build log nominator statistic
log_nom <- sum(log(gamma(marginals+1))) - log(gamma(n+1))
#build log denominator statistic
log_denom <- sum(log(gamma(obs+1)))
#compute probability of observed table
prob_table <- exp(log_nom - log_denom)
nonrand_p <- 0.0
rand_count <- 0
dim1 <- min(marginals[1], marginals[3])
#traverse all possible tables with given marginals
counter <- 0
for (k in 0:dim1)
{
for (l in max(0,marginals[1]-marginals[5]-k):min(marginals[1]-k, marginals[4]))
{
counter <- counter+1
x[1, 1, counter] <- k
x[1, 2, counter] <- l
x[1, 3, counter] <- marginals[1] - x[1, 1, counter] - x[1, 2, counter]
x[2, 1, counter] <- marginals[3] - x[1, 1, counter]
x[2, 2, counter] <- marginals[4] - x[1, 2, counter]
x[2, 3, counter] <- marginals[5] - x[1, 3, counter]
}
}
log_denom_iter <- rep.int(0.0, times=counter)
for (k in 1:counter)
{
log_denom_iter[k] <- sum(log(gamma(x[, , k]+1)))
}
prob_lauf <- exp(log_nom - log_denom_iter)
nonrand_p <- sum(prob_lauf[prob_lauf <= prob_table])
rand_count <- sum(abs(exp(prob_lauf) - exp(prob_table)) < epsilon)
u <- runif(1)
rand_p <- max(0.0, nonrand_p - u*rand_count*prob_table)
return(list(nonrand_p=nonrand_p, rand_p=rand_p, prob_table=prob_table))
}
#############
# Fisher 22 #
#############
fisher22.model <- function() {
return(list(model=list(typ="Fisher 2-by-2")))
}
mutoss.fisher22.model <- function() { return(new(Class="MutossMethod",
label="Fisher's exact test in (2x2) tables",
callFunction="fisher22.model",
output=c("model"),
info="<h2></h2>
<p>(Marginal) Fisher's exact test in (2x2) tables</p>
<p></p>
<h3>Reference:</h3>
<ul>
<li>Fisher, R. A. (1922). \"<i>On the interpretation of Chi^2 from contingency tables, and the calculation of P.</i>\" Journal of the Royal Statistical Society, 85 (1):87-94.</li>
</ul>",
parameters=list(
)
)) }
fisher22.marginal <- function(data, model) {
#m <- dim(data)[3]
#result <- vector(mode="numeric",length=m)
result <- apply(data, 3, function(x) {fisher22_fast(x,2.0e-16)$rand_p} )
return(list(pValues=result))
}
fisher22_fast <- function(obs, epsilon){
# obs = observations = a 2x2 table
#build marginals = (n1., n2., n.1, n.2)
marginals <- c(sum(obs[1, ]), sum(obs[2, ]), sum(obs[, 1]), sum(obs[, 2]))
n <- sum(obs)
x <- array(data=rep.int(0.0, 2*2*max(marginals)*max(marginals)), c(2, 2, max(marginals)*max(marginals)))
#build log nominator statistic
log_nom <- sum(log(gamma(marginals+1))) - log(gamma(n+1))
#build log denominator statistic
log_denom <- sum(log(gamma(obs+1)))
#compute probability of observed table
prob_table <- exp(log_nom - log_denom)
nonrand_p <- 0.0
rand_count <- 0
dim1 <- min(marginals[1], marginals[3])
#traverse all possible tables with given marginals
counter <- 0
for (k in (marginals[1]-marginals[4]):dim1)
{
counter <- counter+1
x[1, 1, counter] <- k
x[1, 2, counter] <- marginals[1] - k
x[2, 1, counter] <- marginals[3] - k
x[2, 2, counter] <- marginals[2] - x[2, 1, counter]
}
log_denom_iter <- rep.int(0.0, times=counter)
for (k in 1:counter)
{
log_denom_iter[k] <- sum(log(gamma(x[, , k]+1)))
}
prob_lauf <- exp(log_nom - log_denom_iter)
nonrand_p <- sum(prob_lauf[prob_lauf <= prob_table])
rand_count <- sum(abs(exp(prob_lauf) - exp(prob_table)) < epsilon)
u <- runif(1)
rand_p <- max(0.0, nonrand_p - u*rand_count*prob_table)
return(list(nonrand_p=nonrand_p, rand_p=rand_p, prob_table=prob_table))
}
|