library(fgof)
set.seed(123)
x <- rlnorm(100, meanlog = 1, sdlog = 0.5)
start <- list(meanlog = mean(log(x)), sdlog = sd(log(x)))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).
The usual one-sample p-value from stats::ks.test() assumes a fully specified null distribution. Estimating its parameters from the same data changes the null distribution of the statistic and invalidates that p-value (Zeimbekakis et al. 2024).
In this vignette, we test whether independent, positive observations follow a lognormal distribution with unknown log-scale mean and standard deviation. We use the multiplier bootstrap first, then compare it with the parametric bootstrap.
We generate 100 independent observations with meanlog = 1 and sdlog = 0.5. These values determine the simulation. For the test, we estimate both parameters from the sample. The sample mean and standard deviation of log(x) provide starting values for maximum likelihood.
library(fgof)
set.seed(123)
x <- rlnorm(100, meanlog = 1, sdlog = 0.5)
start <- list(meanlog = mean(log(x)), sdlog = sd(log(x)))Use distr = "lnorm" to select the lognormal model. The names in start match the parameters of dlnorm(). Set nsim = 199 to generate 199 bootstrap replicates for this example. The multiplier bootstrap uses derivatives of the fitted CDF and log-density to account for parameter estimation. This call uses numerical differentiation because we omit the derivative callbacks.
set.seed(456)
fit_mult <- gof.test(x, "lnorm", nsim = 199, start = start)
fit_mult$estimate meanlog sdlog
1.0451834 0.4540679
rbind(statistic = fit_mult$statistic, p.value = fit_mult$p.value) ks cvm ksa cvma
statistic 0.05775356 0.02751612 0.0587171 0.03378322
p.value 0.42750000 0.85250000 0.4075000 0.74250000
estimate contains the fitted log-scale mean and standard deviation. The table shows the observed statistics and their bootstrap p-values. Each statistic compares the empirical and fitted CDFs at a set of points.
| Discrepancy | Quantile grid | Observed values |
|---|---|---|
| Largest absolute difference (Kolmogorov–Smirnov) | ks |
ksa |
| Squared differences (Cramér–von Mises) | cvm |
cvma |
For example, the observation-based Kolmogorov–Smirnov p-value is about 0.41. At the 5% level, this test gives insufficient evidence against the lognormal model. Use gridStat = FALSE to compute only the observation-based statistics.
stats::deriv()For a positive observation , write , where and are meanlog and sdlog. The CDF is , where is the standard normal CDF. The log-density is .
R’s stats::deriv() generates code that evaluates each expression and its analytical gradient by applying derivative rules to the expression. The callbacks extract the gradient matrix, with one row per evaluation point and columns in parameter order. Documentation for stats::deriv()
parameters <- c("meanlog", "sdlog")
cdf_with_gradient <- stats::deriv(
~ pnorm((log(x) - meanlog) / sdlog),
parameters, function.arg = c("x", parameters)
)
log_density_with_gradient <- stats::deriv(
~ -((log(x) - meanlog) / sdlog)^2 / 2 - log(sdlog) - log(x) -
log(2 * pi) / 2,
parameters, function.arg = c("x", parameters)
)
cdf_gradient <- function(theta, x) {
attr(cdf_with_gradient(as.numeric(x), theta[1], theta[2]), "gradient")
}
score <- function(theta, x) {
attr(log_density_with_gradient(as.numeric(x), theta[1], theta[2]), "gradient")
}
set.seed(456)
fit_analytic <- gof.test(
x, "lnorm", nsim = 199, start = start,
derCdfWrtPar = cdf_gradient, derLogPdfWrtPar = score
)
rbind(numerical = fit_mult$p.value, analytical = fit_analytic$p.value) ks cvm ksa cvma
numerical 0.4275 0.8525 0.4075 0.7425
analytical 0.4275 0.8525 0.4075 0.7425
max(abs(fit_mult$stat.sim - fit_analytic$stat.sim))[1] 1.334752e-11
Resetting the seed uses the same multiplier draws in both fits. Comparing the simulated statistics directly shows how closely the two derivative methods agree, even when the discrete bootstrap p-values coincide.
With RTMB installed, RTMB::MakeTape() records the operations that compute the CDF or log-density at fixed evaluation points. Its jacobian() method then differentiates those outputs with respect to the fitted parameters. Use RTMB::pnorm() inside the tape so that RTMB can differentiate the standard normal CDF. RTMB on CRAN
cdf_gradient_rtmb <- function(theta, points) {
log_points <- log(as.numeric(points))
tape <- RTMB::MakeTape(function(par) {
RTMB::pnorm((log_points - par[1]) / par[2])
}, theta)
tape$jacobian(theta)
}
score_rtmb <- function(theta, points) {
log_points <- log(as.numeric(points))
tape <- RTMB::MakeTape(function(par) {
z <- (log_points - par[1]) / par[2]
-z^2 / 2 - log(par[2]) - log_points - log(2 * pi) / 2
}, theta)
tape$jacobian(theta)
}
set.seed(456)
fit_rtmb <- gof.test(
x, "lnorm", nsim = 199, start = start,
derCdfWrtPar = cdf_gradient_rtmb, derLogPdfWrtPar = score_rtmb
)
rbind(deriv = fit_analytic$p.value, RTMB = fit_rtmb$p.value) ks cvm ksa cvma
deriv 0.4275 0.8525 0.4075 0.7425
RTMB 0.4275 0.8525 0.4075 0.7425
max(abs(fit_analytic$stat.sim - fit_rtmb$stat.sim))[1] 8.326673e-17
Each callback builds a tape for the evaluation points supplied by gof.test(). This lets the CDF callback evaluate derivatives at both the observations and the quantile grid. The results compare the RTMB and stats::deriv() runs using the same multiplier draws.
To compare the two resampling methods, set simulation = "pb". The parametric bootstrap simulates samples from the fitted lognormal model and refits the model to each sample.
set.seed(789)
fit_pb <- gof.test(x, "lnorm", nsim = 199, start = start,
simulation = "pb")
fit_pb$p.value ks cvm ksa cvma
0.5675 0.8875 0.3975 0.7775
For either bootstrap method, a small p-value indicates that the observed discrepancy is large relative to the simulated discrepancies. The two methods use different resampling schemes, so their p-values can differ. Choose the test statistic before inspecting the p-values. Increase nsim when Monte Carlo variability matters for the decision.
To test a lognormal model with known sdlog = 0.5, pass distr.args = list(sdlog = 0.5) and include only meanlog in start. The fitted model and bootstrap samples then use the same fixed sdlog.
See help("gof.test", package = "fgof") for the available statistics, quantile grids, and optimizer controls.