Theoretical numbers to superimpose on histogram

If you want superimpose the theoretical numbers at the top of an histogram based on a fitted distribution (for example, using fitdistrplus()), it is not so easy. Here is a generic function for such a purpose. It is integrated in my package HelpersMG :

modeled.hist <- function(breaks, FUN, ..., sum=1) {
  # breaks is a vector with the breaks; it can be obtained directly from hist()
  # FUN is the function to integrate the density, ex. pnorm
  # ... are the parameters of pfun
  # sum is the total numbers in the histogram; 1 for emperical frequencies
  by <- breaks[2]-breaks[1]
  xp <- c(breaks, tail(breaks, 1)+by)
  par.fun <- list(...)
  par.fun <- modifyList(list(log.p=FALSE, q=xp, lower.tail=TRUE), 
                        par.fun)
  s <- do.call(FUN, par.fun)
  # s <- pfun(xp, par.fun[1], par.fun[2], log=FALSE)
  s <- head(c(s[-1], 0)-s, length(breaks))
  return(list(x=breaks+by/2, y=s*sum))
}




n <- rnorm(100, mean=10, sd=2)
breaks <- 0:20
hist(n, breaks=breaks)

s <- modeled.hist(breaks=breaks, FUN=pnorm, mean=10, sd=2, sum=100)

points(s$x, s$y, pch=19)
lines(s$x, s$y)




n <- rlnorm(100, meanlog=2, sdlog=0.4)
b <- hist(n, ylim=c(0, 70))

s <- modeled.hist(breaks=b$breaks, FUN=plnorm, meanlog=2, sdlog=0.4, sum=100)

points(s$x, s$y, pch=19)
lines(s$x, s$y)


Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04