# Modeling count data with the Bell distribution

    library(bellreg)

    data(faults)

    # ML approach:
    mle <- bellreg(nf ~ lroll, data = faults, approach = "mle")
    summary(mle)
    #> Call:
    #> bellreg(formula = nf ~ lroll, data = faults, approach = "mle")
    #>
    #> Coefficients:
    #>               Estimate     StdErr z.value   p.value
    #> (Intercept) 0.98526380 0.33219359  2.9659  0.003018 **
    #> lroll       0.00190932 0.00049003  3.8963 9.767e-05 ***
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #>
    #> logLik = -88.96139   AIC = 181.9228

    # Bayesian approach:
    bayes <- bellreg(nf ~ lroll, data = faults, approach = "bayes", refresh = FALSE)
    summary(bayes)
    #>
    #> bellreg(formula = nf ~ lroll, data = faults, approach = "bayes",
    #>     refresh = FALSE)
    #>
    #>              mean se_mean    sd  2.5%   25%   50%   75% 97.5%    n_eff  Rhat
    #> (Intercept) 0.974   0.007 0.332 0.314 0.748 0.984 1.198 1.609 2157.921 1.001
    #> lroll       0.002   0.000 0.000 0.001 0.002 0.002 0.002 0.003 2400.936 1.000
    #>
    #> Inference for Stan model: bellreg.
    #> 4 chains, each with iter=2000; warmup=1000; thin=1;
    #> post-warmup draws per chain=1000, total post-warmup draws=4000.

    log_lik <- loo::extract_log_lik(bayes$fit)
    loo::loo(log_lik)
    #> Warning: Some Pareto k diagnostic values are too high. See help('pareto-k-diagnostic') for details.
    #>
    #> Computed from 4000 by 32 log-likelihood matrix.
    #>
    #>          Estimate   SE
    #> elpd_loo   -204.5 34.7
    #> p_loo        61.7 19.0
    #> looic       409.0 69.4
    #> ------
    #> MCSE of elpd_loo is NA.
    #> MCSE and ESS estimates assume independent draws (r_eff=1).
    #>
    #> Pareto k diagnostic values:
    #>                          Count Pct.    Min. ESS
    #> (-Inf, 0.7]   (good)     26    81.2%   376
    #>    (0.7, 1]   (bad)       4    12.5%   <NA>
    #>    (1, Inf)   (very bad)  2     6.2%   <NA>
    #> See help('pareto-k-diagnostic') for details.
    loo::waic(log_lik)
    #> Warning:
    #> 21 (65.6%) p_waic estimates greater than 0.4. We recommend trying loo instead.
    #>
    #> Computed from 4000 by 32 log-likelihood matrix.
    #>
    #>           Estimate   SE
    #> elpd_waic   -207.4 36.9
    #> p_waic        64.6 21.3
    #> waic         414.8 73.8
    #>
    #> 21 (65.6%) p_waic estimates greater than 0.4. We recommend trying loo instead.