Search code examples
rmodel-fittingfitdistrplus

Fitting truncnorm using fitdistrplus


I am trying to fit a truncated normal distribution to some data. However, I have been running into the following error:

<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data,     gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth,     lower = lower, upper = upper, ...): non-finite finite-difference value [1]>
Error in fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8,  : 
  the function mle failed to estimate the parameters, 
                with the error code 100

I'm not sure what's going wrong - I've read that in some cases there can be problems fitting if the initial guesses are wrong or higher than the actual values, but I've tried a number of different start values and none seem to work.

Here is a small sample of my data, and the code I used to get the error:

library(fitdistrplus)
library(truncnorm)
testData <- c(3.2725167726, 0.1501345235, 1.5784128343, 1.218953218, 1.1895520932, 
              2.659871271, 2.8200152609, 0.0497193249, 0.0430677458, 1.6035277181, 
              0.2003910167, 0.4982836845, 0.9867184303, 3.4082793339, 1.6083770189, 
              2.9140912221, 0.6486576911, 0.335227878, 0.5088426851, 2.0395797721, 
              1.5216239237, 2.6116576364, 0.1081283479, 0.4791143698, 0.6388625172, 
              0.261194346, 0.2300098384, 0.6421213993, 0.2671907741, 0.1388568942, 
              0.479645736, 0.0726750815, 0.2058983462, 1.0936704833, 0.2874115077, 
              0.1151566887, 0.0129750118, 0.152288794, 0.1508512023, 0.176000366, 
              0.2499423442, 0.8463027325, 0.0456045486, 0.7689214668, 0.9332181529, 
              0.0290242892, 0.0441181842, 0.0759601229, 0.0767983979, 0.1348839304
)

fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8, sd = 0.9))

Solution

  • The problem is that the mle estimator provides increasingly negative estimates for the parameter mean as the lower bound a tends to zero (note that the latter must not be specified within the start parameter, but within fix.arg):

    fitdist(testData, "truncnorm", fix.arg=list(a=-.5),
            start = list(mean = mean(testData), sd = sd(testData)))
    fitdist(testData, "truncnorm", fix.arg=list(a=-.2),
            start = list(mean = mean(testData), sd = sd(testData)))
    fitdist(testData, "truncnorm", fix.arg=list(a=-.15),
            start = list(mean = mean(testData), sd = sd(testData)))
    

    One possibility to prevent large negative values for mean is to use a lower bound for the optimisation:

    fitdist(testData, "truncnorm", fix.arg=list(a=0),
            start = list(mean = mean(testData), sd = sd(testData)),
            optim.method="L-BFGS-B", lower=c(0, 0))
    

    However, this alters the estimation procedure; in fact you are imposing additional constraints on the parameters and might obtain different answers with different lower bounds.