aboutsummaryrefslogtreecommitdiff
path: root/R/llhist.R
blob: 6a6d93cd26998e96917d8eee086806dc1e7506b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#' Plot the distribution of log likelihoods from multistart objects
#'
#' Produces a histogram of log-likelihoods, and an overlayed kernel density 
#' estimate. In addition, the likelihood of the original fit is shown as
#' a red vertical line.
#'
#' @param object The [multistart] object
#' @param breaks Passed to [hist]
#' @param lpos Positioning of the legend.
#' @param main Title of the plot
#' @param \dots Passed to [hist]
#' @seealso [multistart]
#' @importFrom KernSmooth bkde
#' @export
llhist <- function(object, breaks = "Sturges", lpos = "topleft", main = "", ...) {
  ll <- sapply(object, logLik)
  kde <- KernSmooth::bkde(ll)
  h <- hist(ll, freq = TRUE,
    xlim = range(kde$x),
    xlab = "", main = main,
    ylab = "Frequency of log likelihoods", breaks = breaks, ...)

  freq_factor <- h$counts[1] / h$density[1]
  lines(kde$x, freq_factor * kde$y)
  abline(v = logLik(attr(object, "orig")), col = 2)
  legend(lpos, inset = c(0.05, 0.05), bty = "n",
    lty = 1, col = c(2, 1),
    legend = c("original log likelihood",
      "kernel density estimate"))
}

Contact - Imprint