| 1 |
/* |
| 2 |
* Mathlib : A C Library of Special Functions |
| 3 |
* Copyright (C) 1998 Ross Ihaka |
| 4 |
* Copyright (C) 2000--2007 The R Core Team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, a copy is available at |
| 18 |
* http://www.r-project.org/Licenses/ |
| 19 |
* |
| 20 |
* SYNOPSIS |
| 21 |
* |
| 22 |
* #include <Rmath.h> |
| 23 |
* double ptukey(q, rr, cc, df, lower_tail, log_p); |
| 24 |
* |
| 25 |
* DESCRIPTION |
| 26 |
* |
| 27 |
* Computes the probability that the maximum of rr studentized |
| 28 |
* ranges, each based on cc means and with df degrees of freedom |
| 29 |
* for the standard error, is less than q. |
| 30 |
* |
| 31 |
* The algorithm is based on that of the reference. |
| 32 |
* |
| 33 |
* REFERENCE |
| 34 |
* |
| 35 |
* Copenhaver, Margaret Diponzio & Holland, Burt S. |
| 36 |
* Multiple comparisons of simple effects in |
| 37 |
* the two-way analysis of variance with fixed effects. |
| 38 |
* Journal of Statistical Computation and Simulation, |
| 39 |
* Vol.30, pp.1-15, 1988. |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "NUM2.h" |
| 43 |
|
| 44 |
#define R_Q_P01_boundaries(p, _LEFT_, _RIGHT_) \ |
| 45 |
if (log_p) { \ |
| 46 |
if (p > 0.0) \ |
| 47 |
return undefined; \ |
| 48 |
if (p == 0.0) /* upper bound*/ \ |
| 49 |
return lower_tail ? _RIGHT_ : _LEFT_; \ |
| 50 |
if (isundef (p)) /* cannot occur*/ \ |
| 51 |
return lower_tail ? _LEFT_ : _RIGHT_; \ |
| 52 |
} else { /* !log_p */ \ |
| 53 |
if (p < 0.0 || p > 1.0) \ |
| 54 |
return undefined; \ |
| 55 |
if (p == 0.0) \ |
| 56 |
return lower_tail ? _LEFT_ : _RIGHT_; \ |
| 57 |
if (p == 1.0) \ |
| 58 |
return lower_tail ? _RIGHT_ : _LEFT_; \ |
| 59 |
} |
| 60 |
|
| 61 |
#define R_D_Lval(p) (lower_tail ? (p) : (0.5 - (p) + 0.5)) |
| 62 |
#define R_DT_qIv(p) (log_p ? (lower_tail ? exp(p) : - expm1(p)) : R_D_Lval(p)) |
| 63 |
|
| 64 |
#define R_D__0 (log_p ? undefined : 0.0) /* 0 */ |
| 65 |
#define R_D__1 (log_p ? 0.0 : 1.0) /* 1 */ |
| 66 |
#define R_DT_0 (lower_tail ? R_D__0 : R_D__1) /* 0 */ |
| 67 |
#define R_DT_1 (lower_tail ? R_D__1 : R_D__0) /* 1 */ |
| 68 |
#define R_D_val(x) (log_p ? log(x) : (x)) |
| 69 |
#define R_D_Clog(p) (log_p ? log1p(-(p)) : (0.5 - (p) + 0.5)) /* [log](1-p) */ |
| 70 |
#define R_DT_val(x) (lower_tail ? R_D_val(x) : R_D_Clog(x)) |
| 71 |
|
| 72 |
static double wprob(double w, double rr, double cc) |
| 73 |
{ |
| 74 |
/* wprob() : |
| 75 |
|
| 76 |
This function calculates probability integral of Hartley's |
| 77 |
form of the range. |
| 78 |
|
| 79 |
w = value of range |
| 80 |
rr = no. of rows or groups |
| 81 |
cc = no. of columns or treatments |
| 82 |
ir = error flag = 1 if pr_w probability > 1 |
| 83 |
pr_w = returned probability integral from (0, w) |
| 84 |
|
| 85 |
program will not terminate if ir is raised. |
| 86 |
|
| 87 |
bb = upper limit of legendre integration |
| 88 |
iMax = maximum acceptable value of integral |
| 89 |
nleg = order of legendre quadrature |
| 90 |
ihalf = int ((nleg + 1) / 2) |
| 91 |
wlar = value of range above which wincr1 intervals are used to |
| 92 |
calculate second part of integral, |
| 93 |
else wincr2 intervals are used. |
| 94 |
C1, C2, C3 = values which are used as cutoffs for terminating |
| 95 |
or modifying a calculation. |
| 96 |
|
| 97 |
M_1_SQRT_2PI = 1 / sqrt(2 * pi); from abramowitz & stegun, p. 3. |
| 98 |
M_SQRT2 = sqrt(2) |
| 99 |
xleg = legendre 12-point nodes |
| 100 |
aleg = legendre 12-point coefficients |
| 101 |
*/ |
| 102 |
#define nleg 12 |
| 103 |
#define ihalf 6 |
| 104 |
|
| 105 |
/* looks like this is suboptimal for double precision. |
| 106 |
(see how C1-C3 are used) <MM> |
| 107 |
*/ |
| 108 |
/* const double iMax = 1.; not used if = 1*/ |
| 109 |
constexpr static double C1 = -30.; |
| 110 |
constexpr static double C2 = -50.; |
| 111 |
constexpr static double C3 = 60.; |
| 112 |
constexpr static double bb = 8.; |
| 113 |
constexpr static double wlar = 3.; |
| 114 |
constexpr static double wincr1 = 2.; |
| 115 |
constexpr static double wincr2 = 3.; |
| 116 |
constexpr static double xleg [ihalf] = { |
| 117 |
0.981560634246719250690549090149, |
| 118 |
0.904117256370474856678465866119, |
| 119 |
0.769902674194304687036893833213, |
| 120 |
0.587317954286617447296702418941, |
| 121 |
0.367831498998180193752691536644, |
| 122 |
0.125233408511468915472441369464 |
| 123 |
}; |
| 124 |
constexpr static double aleg [ihalf] = { |
| 125 |
0.047175336386511827194615961485, |
| 126 |
0.106939325995318430960254718194, |
| 127 |
0.160078328543346226334652529543, |
| 128 |
0.203167426723065921749064455810, |
| 129 |
0.233492536538354808760849898925, |
| 130 |
0.249147045813402785000562436043 |
| 131 |
}; |
| 132 |
double pr_w, rinsum, xx; |
| 133 |
longdouble blb, bub, einsum, elsum; |
| 134 |
|
| 135 |
const double qsqz = w * 0.5; |
| 136 |
|
| 137 |
/* if w >= 16 then the integral lower bound (occurs for c=20) */ |
| 138 |
/* is 0.99999999999995 so return a value of 1. */ |
| 139 |
|
| 140 |
if (qsqz >= bb) |
| 141 |
return 1.0; |
| 142 |
|
| 143 |
/* find (f(w/2) - 1) ^ cc */ |
| 144 |
/* (first term in integral of hartley's form). */ |
| 145 |
// djwm: pnorm(q,m,s,1,0) = NUMgaussP((q-m)/s) |
| 146 |
// pr_w = 2 * pnorm(qsqz, 0.,1., 1,0) - 1.; /* erf(qsqz / M_SQRT2) */ |
| 147 |
pr_w = 2 * NUMgaussP (qsqz) - 1.0; |
| 148 |
/* if pr_w ^ cc < 2e-22 then set pr_w = 0 */ |
| 149 |
if (pr_w >= exp(C2 / cc)) |
| 150 |
pr_w = pow(pr_w, cc); |
| 151 |
else |
| 152 |
pr_w = 0.0; |
| 153 |
|
| 154 |
/* if w is large then the second component of the */ |
| 155 |
/* integral is small, so fewer intervals are needed. */ |
| 156 |
|
| 157 |
const double wincr = ( w > wlar ? wincr1 : wincr2 ); |
| 158 |
|
| 159 |
/* find the integral of second term of hartley's form */ |
| 160 |
/* for the integral of the range for equal-length */ |
| 161 |
/* intervals using legendre quadrature. limits of */ |
| 162 |
/* integration are from (w/2, 8). two or three */ |
| 163 |
/* equal-length intervals are used. */ |
| 164 |
|
| 165 |
/* blb and bub are lower and upper limits of integration. */ |
| 166 |
|
| 167 |
blb = qsqz; |
| 168 |
const double binc = (bb - qsqz) / wincr; |
| 169 |
bub = blb + binc; |
| 170 |
einsum = 0.0; |
| 171 |
|
| 172 |
/* integrate over each interval */ |
| 173 |
|
| 174 |
const double cc1 = cc - 1.0; |
| 175 |
for (double wi = 1.0; wi <= wincr; wi ++) { |
| 176 |
elsum = 0.0; |
| 177 |
const double a = double (0.5 * (bub + blb)); |
| 178 |
|
| 179 |
/* legendre quadrature with order = nleg */ |
| 180 |
|
| 181 |
const double b = double (0.5 * (bub - blb)); |
| 182 |
|
| 183 |
for (integer jj = 1; jj <= nleg; jj ++) { |
| 184 |
integer j; |
| 185 |
if (ihalf < jj) { |
| 186 |
j = (nleg - jj) + 1; |
| 187 |
xx = xleg [j-1]; |
| 188 |
} else { |
| 189 |
j = jj; |
| 190 |
xx = -xleg [j-1]; |
| 191 |
} |
| 192 |
const double c = b * xx; |
| 193 |
const double ac = a + c; |
| 194 |
|
| 195 |
/* if exp(-qexpo/2) < 9e-14, */ |
| 196 |
/* then doesn't contribute to integral */ |
| 197 |
|
| 198 |
const double qexpo = ac * ac; |
| 199 |
if (qexpo > C3) |
| 200 |
break; |
| 201 |
|
| 202 |
const double pplus = 2 * NUMgaussP (ac); // djmw: 2 * pnorm(ac, 0., 1., 1,0); |
| 203 |
const double pminus= 2 * NUMgaussP (ac - w); // djmw: 2 * pnorm(ac, w, 1., 1,0); |
| 204 |
|
| 205 |
/* if rinsum ^ (cc-1) < 9e-14, */ |
| 206 |
/* then doesn't contribute to integral */ |
| 207 |
|
| 208 |
rinsum = pplus * 0.5 - pminus * 0.5; |
| 209 |
if (rinsum >= exp (C1 / cc1)) { |
| 210 |
rinsum = aleg [j-1] * exp(-(0.5 * qexpo)) * pow(rinsum, cc1); |
| 211 |
elsum += rinsum; |
| 212 |
} |
| 213 |
} |
| 214 |
elsum *= 2.0 * b * cc * NUM1_sqrt2pi; |
| 215 |
einsum += elsum; |
| 216 |
blb = bub; |
| 217 |
bub += binc; |
| 218 |
} |
| 219 |
|
| 220 |
/* if pr_w ^ rr < 9e-14, then return 0 */ |
| 221 |
pr_w += (double) einsum; |
| 222 |
if (pr_w <= exp(C1 / rr)) |
| 223 |
return 0.0; |
| 224 |
|
| 225 |
pr_w = pow(pr_w, rr); |
| 226 |
if (pr_w >= 1.0) // 1 was iMax was eps |
| 227 |
return 1.0; |
| 228 |
return pr_w; |
| 229 |
} /* wprob() */ |
| 230 |
|
| 231 |
static double ptukey(double q, double rr, double cc, double df, int lower_tail, int log_p) |
| 232 |
{ |
| 233 |
/* function ptukey() [was qprob() ]: |
| 234 |
|
| 235 |
q = value of studentized range |
| 236 |
rr = no. of rows or groups |
| 237 |
cc = no. of columns or treatments |
| 238 |
df = degrees of freedom of error term |
| 239 |
ir [0] = error flag = 1 if wprob probability > 1 |
| 240 |
ir [1] = error flag = 1 if qprob probability > 1 |
| 241 |
|
| 242 |
qprob = returned probability integral over [0, q] |
| 243 |
|
| 244 |
The program will not terminate if ir [0] or ir [1] are raised. |
| 245 |
|
| 246 |
All references in wprob to Abramowitz and Stegun |
| 247 |
are from the following reference: |
| 248 |
|
| 249 |
Abramowitz, Milton and Stegun, Irene A. |
| 250 |
Handbook of Mathematical Functions. |
| 251 |
New York: Dover publications, Inc. (1970). |
| 252 |
|
| 253 |
All constants taken from this text are |
| 254 |
given to 25 significant digits. |
| 255 |
|
| 256 |
nlegq = order of legendre quadrature |
| 257 |
ihalfq = int ((nlegq + 1) / 2) |
| 258 |
eps = max. allowable value of integral |
| 259 |
eps1 & eps2 = values below which there is |
| 260 |
no contribution to integral. |
| 261 |
|
| 262 |
d.f. <= dhaf: integral is divided into ulen1 length intervals. else |
| 263 |
d.f. <= dquar: integral is divided into ulen2 length intervals. else |
| 264 |
d.f. <= deigh: integral is divided into ulen3 length intervals. else |
| 265 |
d.f. <= dlarg: integral is divided into ulen4 length intervals. |
| 266 |
|
| 267 |
d.f. > dlarg: the range is used to calculate integral. |
| 268 |
|
| 269 |
M_LN2 = log(2) |
| 270 |
|
| 271 |
xlegq = legendre 16-point nodes |
| 272 |
alegq = legendre 16-point coefficients |
| 273 |
|
| 274 |
The coefficients and nodes for the legendre quadrature used in |
| 275 |
qprob and wprob were calculated using the algorithms found in: |
| 276 |
|
| 277 |
Stroud, A. H. and Secrest, D. |
| 278 |
Gaussian Quadrature Formulas. |
| 279 |
Englewood Cliffs, |
| 280 |
New Jersey: Prentice-Hall, Inc, 1966. |
| 281 |
|
| 282 |
All values matched the tables (provided in same reference) |
| 283 |
to 30 significant digits. |
| 284 |
|
| 285 |
f(x) = .5 + erf(x / sqrt(2)) / 2 for x > 0 |
| 286 |
|
| 287 |
f(x) = erfc( -x / sqrt(2)) / 2 for x < 0 |
| 288 |
|
| 289 |
where f(x) is standard normal c. d. f. |
| 290 |
|
| 291 |
if degrees of freedom large, approximate integral |
| 292 |
with range distribution. |
| 293 |
*/ |
| 294 |
#define nlegq 16 |
| 295 |
#define ihalfq 8 |
| 296 |
|
| 297 |
/* const double eps = 1.0; not used if = 1 */ |
| 298 |
constexpr static double eps1 = -30.0; |
| 299 |
constexpr static double eps2 = 1.0e-14; |
| 300 |
constexpr static double dhaf = 100.0; |
| 301 |
constexpr static double dquar = 800.0; |
| 302 |
constexpr static double deigh = 5000.0; |
| 303 |
constexpr static double dlarg = 25000.0; |
| 304 |
constexpr static double ulen1 = 1.0; |
| 305 |
constexpr static double ulen2 = 0.5; |
| 306 |
constexpr static double ulen3 = 0.25; |
| 307 |
constexpr static double ulen4 = 0.125; |
| 308 |
constexpr static double xlegq [ihalfq] = { |
| 309 |
0.989400934991649932596154173450, |
| 310 |
0.944575023073232576077988415535, |
| 311 |
0.865631202387831743880467897712, |
| 312 |
0.755404408355003033895101194847, |
| 313 |
0.617876244402643748446671764049, |
| 314 |
0.458016777657227386342419442984, |
| 315 |
0.281603550779258913230460501460, |
| 316 |
0.950125098376374401853193354250e-1 |
| 317 |
}; |
| 318 |
constexpr static double alegq [ihalfq] = { |
| 319 |
0.271524594117540948517805724560e-1, |
| 320 |
0.622535239386478928628438369944e-1, |
| 321 |
0.951585116824927848099251076022e-1, |
| 322 |
0.124628971255533872052476282192, |
| 323 |
0.149595988816576732081501730547, |
| 324 |
0.169156519395002538189312079030, |
| 325 |
0.182603415044923588866763667969, |
| 326 |
0.189450610455068496285396723208 |
| 327 |
}; |
| 328 |
|
| 329 |
if (isundef (q) || isundef (rr) || isundef (cc) || isundef (df)) |
| 330 |
return undefined; |
| 331 |
|
| 332 |
if (q <= 0.0) |
| 333 |
return R_DT_0; |
| 334 |
|
| 335 |
/* df should be > 1 */ |
| 336 |
/* there should be at least two values */ |
| 337 |
|
| 338 |
if (df < 2 || rr < 1 || cc < 2) { |
| 339 |
return undefined; |
| 340 |
} |
| 341 |
|
| 342 |
// if (isundef (q) { return R_DT_1; } |
| 343 |
|
| 344 |
if (df > dlarg) |
| 345 |
return R_DT_val(wprob(q, rr, cc)); |
| 346 |
|
| 347 |
/* calculate leading constant */ |
| 348 |
|
| 349 |
const double f2 = df * 0.5; |
| 350 |
/* lgammafn(u) = log(gamma(u)) */ |
| 351 |
double f2lf = ((f2 * log(df)) - (df * NUMln2)) - NUMlnGamma (f2); //lgammafn(f2); |
| 352 |
const double f21 = f2 - 1.0; |
| 353 |
|
| 354 |
/* integral is divided into unit, half-unit, quarter-unit, or */ |
| 355 |
/* eighth-unit length intervals depending on the value of the */ |
| 356 |
/* degrees of freedom. */ |
| 357 |
|
| 358 |
const double ff4 = df * 0.25; |
| 359 |
double ulen; |
| 360 |
if (df <= dhaf) |
| 361 |
ulen = ulen1; |
| 362 |
else if (df <= dquar) |
| 363 |
ulen = ulen2; |
| 364 |
else if (df <= deigh) |
| 365 |
ulen = ulen3; |
| 366 |
else |
| 367 |
ulen = ulen4; |
| 368 |
|
| 369 |
f2lf += log (ulen); |
| 370 |
|
| 371 |
/* integrate over each subinterval */ |
| 372 |
|
| 373 |
double otsum, ans = 0.0; |
| 374 |
|
| 375 |
for (integer i = 1; i <= 50; i++) { |
| 376 |
otsum = 0.0; |
| 377 |
|
| 378 |
/* legendre quadrature with order = nlegq */ |
| 379 |
/* nodes (stored in xlegq) are symmetric around zero. */ |
| 380 |
|
| 381 |
const double twa1 = (2 * i - 1) * ulen; |
| 382 |
|
| 383 |
for (integer jj = 1; jj <= nlegq; jj ++) { |
| 384 |
double t1; |
| 385 |
integer j; |
| 386 |
if (ihalfq < jj) { |
| 387 |
j = jj - ihalfq - 1; |
| 388 |
t1 = f2lf + f21 * log (twa1 + xlegq [j] * ulen) - (xlegq [j] * ulen + twa1) * ff4; |
| 389 |
} else { |
| 390 |
j = jj - 1; |
| 391 |
t1 = f2lf + f21 * log (twa1 - xlegq [j] * ulen) + (xlegq [j] * ulen - twa1) * ff4; |
| 392 |
} |
| 393 |
|
| 394 |
/* if exp(t1) < 9e-14, then doesn't contribute to integral */ |
| 395 |
if (t1 >= eps1) { |
| 396 |
const double qsqz = ( ihalfq < jj ? |
| 397 |
q * sqrt ((xlegq [j] * ulen + twa1) * 0.5) : |
| 398 |
q * sqrt ((-(xlegq [j] * ulen) + twa1) * 0.5) ); |
| 399 |
|
| 400 |
/* call wprob to find integral of range portion */ |
| 401 |
|
| 402 |
const double wprb = wprob (qsqz, rr, cc); |
| 403 |
const double rotsum = wprb * alegq [j] * exp (t1); |
| 404 |
otsum += rotsum; |
| 405 |
} |
| 406 |
/* end legendre integral for interval i */ |
| 407 |
/* L200: */ |
| 408 |
} |
| 409 |
|
| 410 |
/* if integral for interval i < 1e-14, then stop. |
| 411 |
* However, in order to avoid small area under left tail, |
| 412 |
* at least 1 / ulen intervals are calculated. |
| 413 |
*/ |
| 414 |
if (i * ulen >= 1.0 && otsum <= eps2) |
| 415 |
break; |
| 416 |
|
| 417 |
/* end of interval i */ |
| 418 |
/* L330: */ |
| 419 |
|
| 420 |
ans += otsum; |
| 421 |
} |
| 422 |
|
| 423 |
if (otsum > eps2) { /* not converged */ |
| 424 |
Melder_throw (U"Not converged"); |
| 425 |
} |
| 426 |
if (ans > 1.) |
| 427 |
ans = 1.; |
| 428 |
return R_DT_val(ans); |
| 429 |
} |
| 430 |
/* |
| 431 |
* Mathlib : A C Library of Special Functions |
| 432 |
* Copyright (C) 1998 Ross Ihaka |
| 433 |
* Copyright (C) 2000--2005 The R Core Team |
| 434 |
* based in part on AS70 (C) 1974 Royal Statistical Society |
| 435 |
* |
| 436 |
* This program is free software; you can redistribute it and/or modify |
| 437 |
* it under the terms of the GNU General Public License as published by |
| 438 |
* the Free Software Foundation; either version 2 of the License, or |
| 439 |
* (at your option) any later version. |
| 440 |
* |
| 441 |
* This program is distributed in the hope that it will be useful, |
| 442 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 443 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 444 |
* GNU General Public License for more details. |
| 445 |
* |
| 446 |
* You should have received a copy of the GNU General Public License |
| 447 |
* along with this program; if not, a copy is available at |
| 448 |
* http://www.r-project.org/Licenses/ |
| 449 |
* |
| 450 |
* SYNOPSIS |
| 451 |
* |
| 452 |
* double qtukey(p, rr, cc, df, lower_tail, log_p); |
| 453 |
* |
| 454 |
* DESCRIPTION |
| 455 |
* |
| 456 |
* Computes the quantiles of the maximum of rr studentized |
| 457 |
* ranges, each based on cc means and with df degrees of freedom |
| 458 |
* for the standard error, is less than q. |
| 459 |
* |
| 460 |
* The algorithm is based on that of the reference. |
| 461 |
* |
| 462 |
* REFERENCE |
| 463 |
* |
| 464 |
* Copenhaver, Margaret Diponzio & Holland, Burt S. |
| 465 |
* Multiple comparisons of simple effects in |
| 466 |
* the two-way analysis of variance with fixed effects. |
| 467 |
* Journal of Statistical Computation and Simulation, |
| 468 |
* Vol.30, pp.1-15, 1988. |
| 469 |
*/ |
| 470 |
|
| 471 |
/* qinv() : |
| 472 |
* this function finds percentage point of the studentized range |
| 473 |
* which is used as initial estimate for the secant method. |
| 474 |
* function is adapted from portion of algorithm as 70 |
| 475 |
* from applied statistics (1974) ,vol. 23, no. 1 |
| 476 |
* by odeh, r. e. and evans, j. o. |
| 477 |
* |
| 478 |
* p = percentage point |
| 479 |
* c = no. of columns or treatments |
| 480 |
* v = degrees of freedom |
| 481 |
* qinv = returned initial estimate |
| 482 |
* |
| 483 |
* vmax is cutoff above which degrees of freedom |
| 484 |
* is treated as infinity. |
| 485 |
*/ |
| 486 |
|
| 487 |
static double qinv(double p, double c, double v) |
| 488 |
{ |
| 489 |
constexpr static double p0 = 0.322232421088; |
| 490 |
constexpr static double q0 = 0.993484626060e-01; |
| 491 |
constexpr static double p1 = -1.0; |
| 492 |
constexpr static double q1 = 0.588581570495; |
| 493 |
constexpr static double p2 = -0.342242088547; |
| 494 |
constexpr static double q2 = 0.531103462366; |
| 495 |
constexpr static double p3 = -0.204231210125; |
| 496 |
constexpr static double q3 = 0.103537752850; |
| 497 |
constexpr static double p4 = -0.453642210148e-04; |
| 498 |
constexpr static double q4 = 0.38560700634e-02; |
| 499 |
constexpr static double c1 = 0.8832; |
| 500 |
constexpr static double c2 = 0.2368; |
| 501 |
constexpr static double c3 = 1.214; |
| 502 |
constexpr static double c4 = 1.208; |
| 503 |
constexpr static double c5 = 1.4142; |
| 504 |
constexpr static double vmax = 120.0; |
| 505 |
|
| 506 |
const double ps = 0.5 - 0.5 * p; |
| 507 |
const double yi = sqrt (log (1.0 / (ps * ps))); |
| 508 |
double t = yi + (((( yi * p4 + p3) * yi + p2) * yi + p1) * yi + p0) |
| 509 |
/ (((( yi * q4 + q3) * yi + q2) * yi + q1) * yi + q0); |
| 510 |
if (v < vmax) |
| 511 |
t += (t * t * t + t) / v / 4.0; |
| 512 |
double q = c1 - c2 * t; |
| 513 |
if (v < vmax) |
| 514 |
q += -c3 / v + c4 * t / v; |
| 515 |
return t * (q * log (c - 1.0) + c5); |
| 516 |
} |
| 517 |
|
| 518 |
/* |
| 519 |
* Copenhaver, Margaret Diponzio & Holland, Burt S. |
| 520 |
* Multiple comparisons of simple effects in |
| 521 |
* the two-way analysis of variance with fixed effects. |
| 522 |
* Journal of Statistical Computation and Simulation, |
| 523 |
* Vol.30, pp.1-15, 1988. |
| 524 |
* |
| 525 |
* Uses the secant method to find critical values. |
| 526 |
* |
| 527 |
* p = confidence level (1 - alpha) |
| 528 |
* rr = no. of rows or groups |
| 529 |
* cc = no. of columns or treatments |
| 530 |
* df = degrees of freedom of error term |
| 531 |
* |
| 532 |
* ir(1) = error flag = 1 if wprob probability > 1 |
| 533 |
* ir(2) = error flag = 1 if ptukey probability > 1 |
| 534 |
* ir(3) = error flag = 1 if convergence not reached in 50 iterations |
| 535 |
* = 2 if df < 2 |
| 536 |
* |
| 537 |
* qtukey = returned critical value |
| 538 |
* |
| 539 |
* If the difference between successive iterates is less than eps, |
| 540 |
* the search is terminated |
| 541 |
*/ |
| 542 |
|
| 543 |
|
| 544 |
static double qtukey(double p, double rr, double cc, double df, int lower_tail, int log_p) { |
| 545 |
constexpr static double eps = 0.0001; |
| 546 |
constexpr int maxiter = 50; |
| 547 |
|
| 548 |
if (isundef (p) || isundef (rr) || isundef (cc) || isundef (df)) |
| 549 |
return undefined; |
| 550 |
|
| 551 |
/* df should be > 1 ; there should be at least two values */ |
| 552 |
if (df < 2.0 || rr < 1.0 || cc < 2.0) |
| 553 |
return undefined; |
| 554 |
|
| 555 |
//R_Q_P01_boundaries (p, 0.0, ML_POSINF); |
| 556 |
R_Q_P01_boundaries (p, 0.0, undefined); |
| 557 |
p = R_DT_qIv (p); /* lower_tail,non-log "p" */ |
| 558 |
|
| 559 |
/* Initial value */ |
| 560 |
|
| 561 |
double x0 = qinv (p, cc, df); |
| 562 |
|
| 563 |
/* Find prob(value < x0) */ |
| 564 |
|
| 565 |
double valx0 = ptukey (x0, rr, cc, df, /*LOWER*/true, /*LOG_P*/false) - p; |
| 566 |
|
| 567 |
/* Find the second iterate and prob(value < x1). */ |
| 568 |
/* If the first iterate has probability value */ |
| 569 |
/* exceeding p then second iterate is 1 less than */ |
| 570 |
/* first iterate; otherwise it is 1 greater. */ |
| 571 |
double x1; |
| 572 |
if (valx0 > 0.0) |
| 573 |
x1 = ( x0 > 1.0 ? x0 - 1.0 : 0.0 ); // djmw: fmax2 (0.0, x0 - 1.0); |
| 574 |
else |
| 575 |
x1 = x0 + 1.0; |
| 576 |
double valx1 = ptukey (x1, rr, cc, df, /*LOWER*/true, /*LOG_P*/false) - p; |
| 577 |
|
| 578 |
/* Find new iterate */ |
| 579 |
|
| 580 |
double ans = 0.0; |
| 581 |
for (integer iter = 1 ; iter < maxiter; iter ++) { |
| 582 |
ans = x1 - ((valx1 * (x1 - x0)) / (valx1 - valx0)); |
| 583 |
valx0 = valx1; |
| 584 |
|
| 585 |
/* New iterate should be >= 0 */ |
| 586 |
|
| 587 |
x0 = x1; |
| 588 |
if (ans < 0.0) { |
| 589 |
ans = 0.0; |
| 590 |
valx1 = -p; |
| 591 |
} |
| 592 |
/* Find prob(value < new iterate) */ |
| 593 |
|
| 594 |
valx1 = ptukey (ans, rr, cc, df, /*LOWER*/true, /*LOG_P*/false) - p; |
| 595 |
x1 = ans; |
| 596 |
|
| 597 |
/* If the difference between two successive */ |
| 598 |
/* iterates is less than eps, stop */ |
| 599 |
|
| 600 |
const double xabs = fabs (x1 - x0); |
| 601 |
if (xabs < eps) |
| 602 |
return ans; |
| 603 |
} |
| 604 |
|
| 605 |
/* The process did not converge in 'maxiter' iterations */ |
| 606 |
Melder_warning (U"Maximum number of iterations exceeded."); |
| 607 |
return ans; |
| 608 |
} |
| 609 |
|
| 610 |
double NUMinvTukeyQ (double p, double cc, double df, double rr) { |
| 611 |
return qtukey (p, rr, cc, df, 0, 0); |
| 612 |
} |
| 613 |
|
| 614 |
double NUMtukeyQ (double q, double cc, double df, double rr) { |
| 615 |
return ptukey (q, rr, cc, df, 0, 0); |
| 616 |
} |