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 > > > > > # (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] 20.27771 $hessian a b a 10.683955 -4.764691 b -4.764691 16.810611 $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] 20.27771 $hessian a b a 10.683955 -4.764691 b -4.764691 16.810611 $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 > > > 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.6229737 0.9281694 $convergence [1] 0 $value [1] 21.06638 $hessian meanlog sdlog meanlog 15.358336 -2.676329 sdlog -2.676329 23.992338 $optim.function [1] "optim" $optim.method [1] "BFGS" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 13 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] 21.06638 $hessian meanlog sdlog meanlog 15.356415 -2.676889 sdlog -2.676889 23.985490 $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 > > #optim() is used > mledist(log10EC50, "lnorm", optim.method="L-BFGS-B", lower=0) $estimate meanlog sdlog 0.6229875 0.9281720 $convergence [1] 0 $value [1] 21.06638 $hessian meanlog sdlog meanlog 15.358242 -2.676762 sdlog -2.676762 23.992168 $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.6229875 0.9281720 $convergence [1] 0 $value [1] 21.06638 $hessian meanlog sdlog meanlog 15.358242 -2.676762 sdlog -2.676762 23.992168 $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] 21.06638 $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] 21.06638 $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") > > 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.399337 > mledist(d, "norm", fix.arg = function(x) list(mean = 0.544))$estimate sd 1.341995 > mledist(d, "norm", fix.arg = function(x) list(mean = mean(x)))$estimate sd 1.327269 > > > proc.time() user system elapsed 1.59 0.39 1.92