R Under development (unstable) (2025-01-06 r87534 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 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. > require("fitdistrplus") Loading required package: fitdistrplus Loading required package: MASS Loading required package: survival > > # (1) fit a user-defined continuous distribution (Gumbel) to censored data. > # > > data(fluazinam) > log10EC50 <-log10(fluazinam) > # definition of the Gumbel distribution > dgumbel <- function(x,a,b) 1/b*exp((a-x)/b)*exp(-exp((a-x)/b)) > pgumbel <- function(q,a,b) exp(-exp((a-q)/b)) > qgumbel <- function(p,a,b) a-b*log(-log(p)) > > mledist(log10EC50, "gumbel", start=list(a=0,b=2), optim.method="Nelder-Mead") $estimate a b 1.632726 1.144113 $convergence [1] 0 $value [1] 1.448408 $hessian a b a 0.7631396 -0.340335 b -0.3403350 1.200758 $optim.function [1] "optim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 65 NA $optim.message NULL $loglik [1] -20.27771 $vcov NULL > mledist(log10EC50, "gumbel", start=list(a=0,b=2)) #default NM $estimate a b 1.632726 1.144113 $convergence [1] 0 $value [1] 1.448408 $hessian a b a 0.7631396 -0.340335 b -0.3403350 1.200758 $optim.function [1] "optim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 65 NA $optim.message NULL $loglik [1] -20.27771 $vcov NULL > > #fitted coef is 1.63 1.14 , fitted loglik is -20.3 > > try(mledist(rbind(log10EC50, c("a", "b")), "gamma")) Error in checkCensoredDataFrameNAInfNan(censdata) : censdata contain NaN (not a numeric) values. > try(mledist(rbind(log10EC50, c(NA, NA)), "gamma")) Error in checkCensoredDataFrameNAInfNan(censdata) : censdata contain two NA values on the same line. > try(mledist(rbind(log10EC50, c(3, Inf)), "gamma")) Error in checkCensoredDataFrameNAInfNan(censdata) : censdata contain Inf (infinite) values. > try(mledist(rbind(log10EC50, c(3, NaN)), "gamma")) Error in checkCensoredDataFrameNAInfNan(censdata) : censdata contain NaN (not a numeric) values. > > > # (2) test optimization arguments to censored data MLE. > # > > mledist(log10EC50, "lnorm", optim.method="BFGS") $estimate meanlog sdlog 0.6229904 0.9281666 $convergence [1] 0 $value [1] 1.504742 $hessian meanlog sdlog meanlog 1.0970300 -0.1912084 sdlog -0.1912084 1.7137775 $optim.function [1] "optim" $optim.method [1] "BFGS" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 8 6 $optim.message NULL $loglik [1] -21.06638 $vcov NULL > mledist(log10EC50, "lnorm", optim.method="Nelder") $estimate meanlog sdlog 0.6230097 0.9282263 $convergence [1] 0 $value [1] 1.504742 $hessian meanlog sdlog meanlog 1.0968868 -0.1912063 sdlog -0.1912063 1.7132493 $optim.function [1] "optim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 45 NA $optim.message NULL $loglik [1] -21.06638 $vcov NULL > > #fitted coef is 0.623 0.928, fitted loglik is -21.1 > > #optim() is used > mledist(log10EC50, "lnorm", optim.method="L-BFGS-B", lower=0) $estimate meanlog sdlog 0.6229878 0.9281719 $convergence [1] 0 $value [1] 1.504742 $hessian meanlog sdlog meanlog 1.0970174 -0.1911979 sdlog -0.1911979 1.7137270 $optim.function [1] "optim" $optim.method [1] "L-BFGS-B" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 9 9 $optim.message [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH" $loglik [1] -21.06638 $vcov NULL > mledist(log10EC50, "lnorm", optim.method="BFGS", lower=0) #a simple warning $estimate meanlog sdlog 0.6229878 0.9281719 $convergence [1] 0 $value [1] 1.504742 $hessian meanlog sdlog meanlog 1.0970174 -0.1911979 sdlog -0.1911979 1.7137270 $optim.function [1] "optim" $optim.method [1] "L-BFGS-B" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 9 9 $optim.message [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH" $loglik [1] -21.06638 $vcov NULL Warning message: In mledist(log10EC50, "lnorm", optim.method = "BFGS", lower = 0) : The BFGS method cannot be used with bounds without provided the gradient. The method is changed to L-BFGS-B. > > #constrOptim() is used > mledist(log10EC50, "lnorm", optim.method="Nelder", lower=0) $estimate meanlog sdlog 0.6230097 0.9282263 $convergence [1] 0 $value [1] 1.504742 $hessian NULL $optim.function [1] "constrOptim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 45 NA $optim.message NULL $loglik [1] -21.06638 $vcov NULL > mledist(log10EC50, "lnorm", lower=0) #error $estimate meanlog sdlog 0.6230097 0.9282263 $convergence [1] 0 $value [1] 1.504742 $hessian NULL $optim.function [1] "constrOptim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 45 NA $optim.message NULL $loglik [1] -21.06638 $vcov NULL > > # (3) weighted MLE > # > xleft <- c(-1.8, -0.6, -0.1, 0.07, 0.14, 1, 1.2, 1.2, 1.2) > xright <- c(-1.8, -0.6, -0.1, 0.07, 0.14, 1, NA, NA, NA) > d <- data.frame(left = xleft, right = xright) > f1 <- mledist(d, "norm") > > #fitted coef is 0.545 1.342, fitted loglik is -12.9 > > dbis <- data.frame(left = c(-1.8, -0.6, -0.1, 0.07, 0.14, 1, 1.2), + right = c(-1.8, -0.6, -0.1, 0.07, 0.14, 1, NA)) > f2 <- mledist(dbis, "norm", weights = c(rep(1,6),3)) Warning message: In mledist(dbis, "norm", weights = c(rep(1, 6), 3)) : weights are not taken into account in the default initial values > > # f1 and f2 must give quite the same results (only starting values differ) > f1$estimate mean sd 0.5447786 1.3422448 > f2$estimate mean sd 0.5448978 1.3423076 > > # (4) test the definition of fix.arg/start.arg as functions > # if defined as functions, start.arg and fix.arg must be > # functions of pseudo data (output pseudo of cens2pseudo()) > > mledist(d, "norm", start = function(x) list(mean = 0, sd = 1))$estimate mean sd 0.5448718 1.3420260 > mledist(d, "norm", start = function(x) list(mean = mean(x), sd = 1))$estimate mean sd 0.5447851 1.3422520 > mledist(d, "norm", fix.arg = function(x) list(mean = 0))$estimate sd 1.399329 > mledist(d, "norm", fix.arg = function(x) list(mean = 0.544))$estimate sd 1.341986 > mledist(d, "norm", fix.arg = function(x) list(mean = mean(x)))$estimate sd 1.327281 > > proc.time() user system elapsed 1.06 0.10 1.15