# NOT RUN {
# }
# NOT RUN {
# Load necessary tools
data("fit_US_cities")
library(tidyverse)
x <- rcombdist(
n = 25359, dist = "lnorm",
prior = subset(fit_US_cities, (dist == "lnorm" & components == 5))$prior[[1]],
coeff = subset(fit_US_cities, (dist == "lnorm" & components == 5))$coefficients[[1]]
) # Generate data from one of the fitted functions
# Evaluate functioning of dcomdist by calculating log likelihood for all distributions
loglike <- fit_US_cities %>%
group_by(dist, components, np, n) %>%
do(loglike = sum(dcombdist(dist = .[["dist"]], x = sort(x), prior = .[["prior"]][[1]],
coeff = .[["coefficients"]][[1]], log = TRUE))) %>%
unnest(cols = loglike) %>%
mutate(NLL = -loglike, AIC = 2 * np - 2 * (loglike), BIC = log(n) * np - 2 * (loglike)) %>%
arrange(NLL)
# Evaluate functioning of mcombdist and pcombdist by calculating NMAD
#(equivalent to the Kolmogorov-Smirnov test statistic for the zeroth moment
#of the distribution) for all distributions
nmad <- fit_US_cities %>%
group_by(dist, components, np, n) %>%
do(
KS = max(abs(pempirical(q = sort(x), data = x) - pcombdist(dist = .[["dist"]],
q = sort(x), prior = .[["prior"]][[1]], coeff = .[["coefficients"]][[1]]))),
nmad_0 = nmad_test(r = 0, dist = .[["dist"]], x = sort(x), prior = .[["prior"]][[1]],
coeff = .[["coefficients"]][[1]], stat = "max"),
nmad_1 = nmad_test(r = 1, dist = .[["dist"]], x = sort(x), prior = .[["prior"]][[1]],
coeff = .[["coefficients"]][[1]], stat = "max")
) %>%
unnest(cols = c(KS, nmad_0, nmad_1)) %>%
arrange(nmad_0)
# Evaluate functioning of qcombdist pcombdist by calculating NMAD (equivalent to the Kolmogorov-
#Smirnov test statistic for the zeroth moment of the distribution) for all distributions
test <- fit_US_cities %>%
group_by(dist, components, np, n) %>%
do(out = qcombdist(pcombdist(2, dist = .[["dist"]], prior = .[["prior"]][[1]],
coeff = .[["coefficients"]][[1]], log.p = TRUE),
dist = .[["dist"]], prior = .[["prior"]][[1]], coeff = .[["coefficients"]][[1]],
log.p = TRUE
)) %>%
unnest(cols = c(out))
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab