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) non-censored data (continuous) > # > > s1 <- NULL > s2 <- list("mean"=2) > s0 <- list("mean"=2, "sd"=1) > s3 <- function(x) + list("mean"=1.01*mean(x)) > s4 <- function(x) + list("mean"=1.01*mean(x), "sd"=sd(x)) > > f1 <- NULL > f2 <- list("sd"=3) > f3 <- function(x) + list("sd"=1.01*sd(x)) > > x <- rnorm(1000) > > #redefine normal distribution for better check > dnorm2 <- dnorm > pnorm2 <- pnorm > qnorm2 <- qnorm > rnorm2 <- rnorm > mnorm2 <- function(order, mean, sd) + ifelse(order == 1, mean, sd^2) > memp <- function(x, order) mean(x^order) > > # both NULL > mf1 <- mledist(x, "norm", start=s1, fix.arg=f1) > mf1 <- mmedist(x, "norm", start=s1, fix.arg=f1) > mf1 <- qmedist(x, "norm", start=s1, fix.arg=f1, probs=1:2/3) > mf1 <- mgedist(x, "norm", start=s1, fix.arg=f1) > > fit1 <- fitdist(x, "norm", start=s1, fix.arg=f1) > boot1 <- bootdist(fit1, niter=10) > > # both named list > mf1 <- mledist(x, "norm2", start=s2, fix.arg=f2) > mf1 <- mmedist(x, "norm2", start=s2, fix.arg=f2, order=1, memp=memp) > mf1 <- qmedist(x, "norm2", start=s2, fix.arg=f2, probs=1/3) > mf1 <- mgedist(x, "norm2", start=s2, fix.arg=f2) > > fit1 <- fitdist(x, "norm2", start=s2, fix.arg=f2) > boot1 <- bootdist(fit1, niter=10) > > # named list and NULL > mf1 <- mledist(x, "norm2", start=s0, fix.arg=f1) > mf1 <- mmedist(x, "norm2", start=s0, fix.arg=f1, order=1:2, memp=memp) > mf1 <- qmedist(x, "norm2", start=s0, fix.arg=f1, probs=1:2/3) > mf1 <- mgedist(x, "norm2", start=s0, fix.arg=f1) > > fit1 <- fitdist(x, "norm2", start=s0, fix.arg=f1) > boot1 <- bootdist(fit1, niter=10) > > # NULL and named list > mf1 <- mledist(x, "norm", start=s1, fix.arg=f2) > mf1 <- qmedist(x, "norm", start=s1, fix.arg=f2, probs=1/3) > mf1 <- mgedist(x, "norm", start=s1, fix.arg=f2) > > fit1 <- fitdist(x, "norm", start=s1, fix.arg=f2) > boot1 <- bootdist(fit1, niter=10) > > # both function > mf1 <- mledist(x, "norm2", start=s3, fix.arg=f3) > mf1 <- mmedist(x, "norm2", start=s3, fix.arg=f3, order=1, memp=memp) > mf1 <- qmedist(x, "norm2", start=s3, fix.arg=f3, probs=1/3) > mf1 <- mgedist(x, "norm2", start=s3, fix.arg=f3) > > fit1 <- fitdist(x, "norm2", start=s3, fix.arg=f3) > boot1 <- bootdist(fit1, niter=10) > > # function and NULL > mf1 <- mledist(x, "norm2", start=s4, fix.arg=f1) > mf1 <- mmedist(x, "norm2", start=s4, fix.arg=f1, order=1:2, memp=memp) > mf1 <- qmedist(x, "norm2", start=s4, fix.arg=f1, probs=1:2/3) > mf1 <- mgedist(x, "norm2", start=s4, fix.arg=f1) > > fit1 <- fitdist(x, "norm2", start=s4, fix.arg=f1) > boot1 <- bootdist(fit1, niter=10) > > # NULL and function > mf1 <- mledist(x, "norm", start=s1, fix.arg=f3) > mf1 <- qmedist(x, "norm", start=s1, fix.arg=f3, probs=1/3) > mf1 <- mgedist(x, "norm", start=s1, fix.arg=f3) > > fit1 <- fitdist(x, "norm", start=s1, fix.arg=f3) > boot1 <- bootdist(fit1, niter=10) > > # should raise error for too less parameters > try(mgedist(x, "norm", start=s2, fix.arg=f1)) $estimate mean -0.03735531 $convergence [1] 0 $value [1] 0.02598352 $hessian mean mean 184.85 $optim.function [1] "optim" $optim.method [1] "BFGS" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 18 7 $optim.message NULL $loglik [1] -1417.566 $gof [1] "CvM" Warning message: In checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : Some parameter names have no starting/fixed value but have a default value: sd. > try(fitdist(x, "norm", start=s2, fix.arg=f1)) Fitting of the distribution ' norm ' by maximum likelihood Parameters: estimate Std. Error mean -0.0310707 0.03162278 > # should raise error for too much parameters > try(mgedist(x, "norm", start=s0, fix.arg=f2)) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : A distribution parameter cannot be specified both in 'start' and 'fix.arg'. > try(fitdist(x, "norm", start=s0, fix.arg=f2)) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : A distribution parameter cannot be specified both in 'start' and 'fix.arg'. > # should raise error for NA value > try(mgedist(x, "norm", start=s1, fix.arg=list(sd=NA))) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : 'fix.arg' should not have NA or NaN values. > try(fitdist(x, "norm", start=list(sd=NA))) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : 'start' should not have NA or NaN values. > # should raise error for inconsistent parameter > try(mgedist(x, "norm", start=function(x) list("toto"=1))) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : 'start' must specify names which are arguments to 'distr'. > try(fitdist(x, "norm", fix=list(toto=2))) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : 'fix.arg' must specify names which are arguments to 'distr'. > > #test unset arguments > dbeta2<-function(x, shape1, ncp2) + dbeta(x, shape1, shape1, ncp2) > x <- rbeta(1e2, 3, 3) > try(fitdist(x, "beta2", start=list(shape1=2))) Error in checkparamlist(arg_startfix$start.arg, arg_startfix$fix.arg, : Some parameter names have no starting/fixed value: ncp2. > > dbeta3<-function(x, shape1, ncp2=0) + dbeta(x, shape1, shape1, ncp2) > fitdist(x, "beta3", start=list(shape1=2)) Fitting of the distribution ' beta3 ' by maximum likelihood Parameters: estimate Std. Error shape1 3.293974 0.4346781 Warning message: In fitdist(x, "beta3", start = list(shape1 = 2)) : The pbeta3 function must be defined > > > > # (2) censored data > # > > data(salinity) > log10LC50 <-log10(salinity) > > s1 <- NULL > s2 <- list("mean"=2) > s0 <- list("mean"=2, "sd"=1) > s3 <- function(x) list("mean"=mean(x)) > > f1 <- NULL > f2 <- list("sd"=3) > f3 <- function(x) list("sd"=sd(x)) > > fitdistcens(log10LC50, "norm", start=s1, fix.arg = f1) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 1.4702582 sd 0.2154709 > fitdistcens(log10LC50, "norm", start=s1, fix.arg = f2) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 2.966347 Fixed parameters: value sd 3 > fitdistcens(log10LC50, "norm", start=s2, fix.arg = f2) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 2.966826 Fixed parameters: value sd 3 > fitdistcens(log10LC50, "norm", start=s0, fix.arg = f1) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 1.4702237 sd 0.2154322 > fitdistcens(log10LC50, "norm", start=s3, fix.arg = f2) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 2.966347 Fixed parameters: value sd 3 > fitdistcens(log10LC50, "norm", start=s3, fix.arg = f3) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 1.628163 Fixed parameters (computed by a user-supplied function): value sd 0.5788206 > fitdistcens(log10LC50, "norm", start=s1, fix.arg = f3) Fitting of the distribution ' norm ' on censored data by maximum likelihood Parameters: estimate mean 1.628163 Fixed parameters (computed by a user-supplied function): value sd 0.5788206 > > > fit1 <- fitdistcens(log10LC50, "norm", start=s1, fix.arg = f1) > boot1 <- bootdistcens(fit1, niter = 10) > > fit1 <- fitdistcens(log10LC50, "norm", start=s3, fix.arg = f2) > boot1 <- bootdistcens(fit1, niter = 10) > > fit1 <- fitdistcens(log10LC50, "norm", start=s2, fix.arg = f3) > boot1 <- bootdistcens(fit1, niter = 10) > > # (3) non-censored data (discrete) > # > > n <- 200 > trueval <- c("size"=10, "prob"=3/4, "mu"=10/3) > x <- rnbinom(n, trueval["size"], trueval["prob"]) > > mledist(x, "nbinom") $estimate size mu 8.941108 3.554799 $convergence [1] 0 $value [1] 431.4228 $hessian size mu size 0.0953703676 -0.0002569536 mu -0.0002569536 40.2596502767 $optim.function [1] "optim" $optim.method [1] "Nelder-Mead" $fix.arg NULL $fix.arg.fun NULL $weights NULL $counts function gradient 37 NA $optim.message NULL $loglik [1] -431.4228 $vcov NULL > fitdist(x, "nbinom") Fitting of the distribution ' nbinom ' by maximum likelihood Parameters: estimate Std. Error size 8.941108 3.2381225 mu 3.554799 0.1576032 > > > # (4) non-censored data (continuous) external distributions > # > > data("endosulfan") > ATV <-endosulfan$ATV > fendo.ln <- fitdist(ATV, "lnorm") > fendo.g <- fitdist(ATV, "gamma", start=list(shape=2, scale=1), lower=0) > require("actuar") Loading required package: actuar Attaching package: 'actuar' The following objects are masked from 'package:stats': sd, var The following object is masked from 'package:grDevices': cm > fendo.ll <- fitdist(ATV, "llogis", start = list(shape = 1, scale = 500)) > fendo.P <- fitdist(ATV, "pareto", start = list(shape = 1, scale = 500)) > fendo.B <- fitdist(ATV, "burr", start = list(shape1 = 0.3, shape2 = 1, + rate = 1)) > > proc.time() user system elapsed 2.96 0.20 3.15