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
|
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
################################################################################
# FUNCTION: DESCRIPTION:
# distCheck Checks consistency of distributions
################################################################################
distCheck <- function(fun = "norm", n = 1000, robust = TRUE, subdivisions = 100, ...)
{
# A function implemented by Diethelm Wuertz
# Description:
# Checks consistency of distributions
# Arguments:
# fun - a character string denoting the name of the distribution
# n - an integer specifying the number of random variates to be
# generated
# robust - a logical flag, should robust estimates be used? By
# default \code{TRUE}
# subdivisions - an integer specifying the numbers of subdivisions
# in integration __ NB: only used in one place, *not* in the other.. hmm
# ... - the distributional parameters and passed to integrate()
### FIXME (MM): add args.integrate = list() to be passed to integrate(),
### ----- and pass the others to distrib.functions
# Examples:
# .distCheck("norm", mean = 1, sd = 1)
# .distCheck("t", df = 4)
# .distCheck("exp", rate = 2)
# .distCheck("weibull", shape = 1)
# FUNCTION:
# Distribution Functions:
cat("\nDistribution Check for:", fun, "\n ")
CALL = match.call()
cat("Call: ")
cat(paste(deparse(CALL), sep = "\n", collapse = "\n"), "\n", sep = "")
dfun = match.fun(paste("d", fun, sep = ""))
pfun = match.fun(paste("p", fun, sep = ""))
qfun = match.fun(paste("q", fun, sep = ""))
rfun = match.fun(paste("r", fun, sep = ""))
# Range:
xmin = qfun(p = 0.01, ...)
xmax = qfun(p = 0.99, ...)
# Check 1 - Normalization:
NORM = integrate(dfun, lower = -Inf, upper = Inf,
subdivisions = subdivisions, stop.on.error = FALSE, ...)
cat("\n1. Normalization Check:\n NORM ")
print(NORM)
normCheck = (abs(NORM[[1]]-1) < 0.01)
# Check 2:
cat("\n2. [p-pfun(qfun(p))]^2 Check:\n ")
p = c(0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999)
P = pfun(qfun(p, ...), ...)
pP = round(rbind(p, P), 3)
print(pP)
RMSE = sd(p-P)
print(c(RMSE = RMSE))
rmseCheck = (abs(RMSE) < 0.0001)
# Check 3:
cat("\n3. r(", n, ") Check:\n", sep = "")
r = rfun(n = n, ...)
if (!robust) {
SAMPLE.MEAN = mean(r)
SAMPLE.VAR = var(r)
} else {
robustSample = MASS::cov.mcd(r, quantile.used = floor(0.95*n))
SAMPLE.MEAN = robustSample$center
SAMPLE.VAR = robustSample$cov[1,1]
}
SAMPLE = data.frame(t(c(MEAN = SAMPLE.MEAN, "VAR" = SAMPLE.VAR)),
row.names = "SAMPLE")
print(signif(SAMPLE, 3))
fun1 = function(x, ...) { x * dfun(x, ...) }
fun2 = function(x, M, ...) { x^2 * dfun(x, ...) }
MEAN = integrate(fun1, lower = -Inf, upper = Inf,
subdivisions = 5000, stop.on.error = FALSE,...)
cat(" X ")
print(MEAN)
VAR = integrate(fun2, lower = -Inf, upper = Inf,
subdivisions = 5000, stop.on.error = FALSE, ...)
cat(" X^2 ")
print(VAR)
EXACT = data.frame(t(c(MEAN = MEAN[[1]], "VAR" = VAR[[1]] - MEAN[[1]]^2)),
row.names = "EXACT ")
print(signif(EXACT, 3))
meanvarCheck = (abs(SAMPLE.VAR-EXACT$VAR)/EXACT$VAR < 0.1)
cat("\n")
# Done:
ans = list(
normCheck = normCheck,
rmseCheck = rmseCheck,
meanvarCheck = meanvarCheck)
# Return Value:
unlist(ans)
}
# ------------------------------------------------------------------------------
## (2023-10-15) GNB: Can't remove .distCheck easily since in recent versions of
## fGarch it was imported explicitly in NAMESPACE. This means that if I remove
## it, I need to ask every maintainer importing fGarch to require fGarch (>=
## 4031.90) - not impossible, but not particularly reasonable in the short term.
##
## So, keep it for now.
.distCheck <- function(...) {
if(("fGarch" %in% loadedNamespaces() && packageVersion("fGarch") < "4031.90") ||
("fExtremes" %in% loadedNamespaces() && packageVersion("fExtremes") <= "4021.83") ||
("stabledist" %in% loadedNamespaces() && packageVersion("stabledist") <= "0.7-1") ) {
## let it work with old versions of fGarch, fExtremes, stabledist
distCheck(...)
} else
stop("'.distCheck' is defunct. Use 'distCheck' instead.")
}
################################################################################
|