R Under development (unstable) (2024-07-10 r86888 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(fitdistrplus) Loading required package: MASS Loading required package: survival > > > > set.seed(1234) > nsample <- 10 > > > # (1) uniform distribution fit - no fixed value > > #manual check > dunif2 <- function(x, min, max) + dunif(x, min, max) > punif2 <- function(q, min, max) + punif(q, min, max) > > x1 <- runif(nsample, 3, 5) > > L <- function(a, b, obs) + prod(dunif(obs, min=a, max=b)) > l <- Vectorize(L, "a") > curve(l(x, b=5, obs=x1), from=1, to=3) > > f1 <- fitdist(x1, "unif") > f2 <- fitdist(x1, "unif2", start=list(min=0, max=10), lower=c(-Inf, max(x1)), + upper=c(min(x1), Inf)) > c(logLik(f1), logLik(f2)) [1] -5.322970 -5.323165 > > delta <- .2 > llsurface(x1, "unif", plot.arg = c("min", "max"), min.arg=c(1, 5-delta), + max.arg=c(3+delta, 7), main="likelihood surface for uniform") > abline(v=3, h=5, col="red", lty=2) > points(f1$estimate[1], f1$estimate[2], pch="x", col="violet") > points(f2$estimate[1], f2$estimate[2], pch="o", col="blue") > > # (2) uniform distribution fit - fixed value > > f3 <- fitdist(x1, "unif", fix.arg=list(min=2.5)) > logLik(f3) [1] -7.983315 > > llcurve(x1, "unif", plot.arg="max", min.arg = 5-delta, max.arg=7) > > f4 <- fitdist(x1, "unif", fix.arg=list(max=5.5)) > logLik(f4) [1] -9.086651 > > > # (3) four parameter beta - also known as PERT distribution > require(mc2d) Loading required package: mc2d Loading required package: mvtnorm Attaching package: 'mc2d' The following objects are masked from 'package:base': pmax, pmin > > x2 <- rpert(2*nsample, 0, 1, 2, 3) > > f1 <- fitdist(x2, "pert", start=list(min=-1, mode=0, max=10, shape=1), + lower=c(-Inf, -Inf, max(x2), 0), + upper=c(min(x2), Inf, Inf, Inf)) Warning messages: 1: In cov2cor(varcovar) : diag(V) had non-positive or NA entries; the non-finite result may be dubious 2: In sqrt(diag(varcovar)) : NaNs produced > f2 <- fitdist(x2, "pert", start=list(mode=1, shape=1), + lower=c(-Inf, 0), + upper=c(Inf, Inf), + fix.arg=list(min=min(x2)-1e-6, max=max(x2)+1e-6)) Warning messages: 1: In cov2cor(varcovar) : diag(V) had non-positive or NA entries; the non-finite result may be dubious 2: In sqrt(diag(varcovar)) : NaNs produced > > gofstat(list(f1,f2)) Goodness-of-fit statistics 1-mle-pert 2-mle-pert Kolmogorov-Smirnov statistic 0.1767997 0.3074253 Cramer-von Mises statistic 0.0706553 0.3671139 Anderson-Darling statistic 0.3920664 2.6203988 Goodness-of-fit criteria 1-mle-pert 2-mle-pert Akaike's Information Criterion 10.46162 8.877349 Bayesian Information Criterion 14.44455 10.868814 > cdfcomp(list(f1,f2)) > > proc.time() user system elapsed 2.53 0.37 2.89