library(fgof)
set.seed(123)
df <- 5
location <- c(0, 1)
dispersion <- matrix(c(1, 0.6, 0.6, 2), nrow = 2)
x <- mvtnorm::rmvt(100, sigma = dispersion, df = df,
delta = location, type = "shifted")The fgof package provides goodness-of-fit tests for continuous distributions with parameters estimated from the data. Its gof.test() function compares the empirical cumulative distribution function (CDF) with the fitted CDF. It uses the multiplier bootstrap by default (simulation = "mult") and also supports the parametric bootstrap (simulation = "pb") (Kojadinovic and Yan 2012).
In this vignette, we use the multiplier bootstrap to test whether independent pairs follow a bivariate Student distribution. We fix the degrees of freedom at five and estimate two location parameters, two diagonal dispersion entries, and one correlation. See the univariate vignette for an introduction to the bootstrap procedures and p-values.
We generate 100 independent pairs from the model under test. The two coordinates within each pair can be dependent. Each row of x holds one pair, and each column corresponds to one coordinate.
Let and denote the diagonal entries of the dispersion matrix. The off-diagonal entry is , where is the correlation. With degrees of freedom, the covariance matrix is times the dispersion matrix.
library(fgof)
set.seed(123)
df <- 5
location <- c(0, 1)
dispersion <- matrix(c(1, 0.6, 0.6, 2), nrow = 2)
x <- mvtnorm::rmvt(100, sigma = dispersion, df = df,
delta = location, type = "shifted")The multiplier bootstrap uses the derivative of the joint CDF and the derivative of the log-density, also called the score. Together they account for the effect of estimating parameters on the fitted CDF. Each callback takes the parameter vector and a matrix of evaluation points. It returns one row per point and one column per estimated parameter in the order .
The callbacks below use the analytical derivatives for the Student model with fixed degrees of freedom described by Kojadinovic and Yan (2012). They call internal fgof helpers through ::: and use the same fixed degrees of freedom as the distribution wrappers.
cdf_gradient <- function(theta, points) {
fgof:::gradCdfT(theta, points, df = df)
}
score <- function(theta, points) {
fgof:::gradLogPdfT(theta, points, df = df)
}Use the internal "mymvt" distribution wrappers with dispstr = "un" for unrestricted correlation. Pass the fixed df in distr.args and the starting values for the five estimated parameters in start. The optimizer bounds keep the diagonal dispersion entries positive and the correlation inside . In two dimensions, these conditions ensure a positive definite dispersion matrix.
start <- list(param = c(mean(x[, 1]), mean(x[, 2]), 1, 1, 0))
set.seed(456)
fit <- gof.test(
x, "mymvt", nsim = 199, start = start, simulation = "mult",
derCdfWrtPar = cdf_gradient, derLogPdfWrtPar = score,
distr.args = list(df = df, dispstr = "un"),
optim.args = list(lower = c(-Inf, -Inf, 1e-4, 1e-4, -0.999),
upper = c(Inf, Inf, Inf, Inf, 0.999))
)
setNames(fit$estimate, c("mu1", "mu2", "v1", "v2", "rho")) mu1 mu2 v1 v2 rho
0.01562816 0.88137015 0.78469579 1.79831115 0.27510693
rbind(statistic = fit$statistic, p.value = fit$p.value) ksa cvma
statistic 0.05056956 0.05481997
p.value 0.83250000 0.51250000
For a point , the joint CDF gives the probability that the first coordinate is at most and the second is at most . The test compares this fitted probability with the observed fraction of pairs satisfying both inequalities. By default, the multivariate test evaluates the discrepancies at the observed pairs. The observation-based Kolmogorov–Smirnov statistic ksa uses the largest absolute discrepancy. The observation-based Cramér–von Mises statistic cvma sums the squared discrepancies.
The ksa p-value is about 0.83. At the 5% level, this test gives insufficient evidence against the bivariate Student model with five degrees of freedom.
To obtain numerical derivatives for the same model, use numDeriv::jacobian() on the CDF and log-density. Each Jacobian has the same row and column order as its analytical counterpart. As in the analytical callbacks, the degrees of freedom stay fixed.
cdf_gradient_num <- function(theta, points) {
numDeriv::jacobian(function(par) {
fgof:::pmymvt(points, par, dispstr = "un", df = df)
}, theta)
}
score_num <- function(theta, points) {
numDeriv::jacobian(function(par) {
fgof:::dmymvt(points, par, dispstr = "un", df = df, log = TRUE)
}, theta)
}
theta <- fit$estimate
c(cdf = max(abs(cdf_gradient_num(theta, x) - cdf_gradient(theta, x))),
score = max(abs(score_num(theta, x) - score(theta, x)))) cdf score
6.318580e-10 6.894871e-09
Use the numerical callbacks in a second test and reset the seed to use the same multiplier draws as in the analytical run.
set.seed(456)
fit_num <- gof.test(
x, "mymvt", nsim = 199, start = start, simulation = "mult",
derCdfWrtPar = cdf_gradient_num, derLogPdfWrtPar = score_num,
distr.args = list(df = df, dispstr = "un"),
optim.args = list(lower = c(-Inf, -Inf, 1e-4, 1e-4, -0.999),
upper = c(Inf, Inf, Inf, Inf, 0.999))
)
rbind(analytical = fit$p.value, numerical = fit_num$p.value) ksa cvma
analytical 0.8325 0.5125
numerical 0.8325 0.5125
max(abs(fit$stat.sim - fit_num$stat.sim))[1] 1.836577e-10
The p-value table compares the two runs. The maximum absolute difference between the simulated statistics gives a finer comparison when the p-values coincide.