#' (Weighted) MLE of Gompertz Distribution
#'
#' Gompertz distribution is characterized by the following probability density function,
#' \deqn{f(x;\eta, b) = b\eta \exp(bx) \exp(\eta) \exp(-\eta e^{bx})}
#' where the domain is \eqn{x \in [0,\infty)} with two parameters \eqn{\eta > 0} for shape and \eqn{b > 0} for scale.
#'
#' @param x a length-\eqn{n} vector of nonnegative real numbers.
#' @param weight a length-\eqn{n} weight vector. If set as \code{NULL}, it gives an equal weight, leading to standard MLE.
#'
#' @return a named list containing (weighted) MLE of \describe{
#' \item{eta}{shape parameter \eqn{\eta}.}
#' \item{b}{scale parameter \eqn{b}.}
#' }
#'
#' @examples
#' # generate data from half normal
#' x = abs(stats::rnorm(100))
#'
#' # fit unweighted
#' Gompertz(x)
#'
#' \dontrun{
#' # put random weights to see effect of weights
#' niter = 500
#' ndata = 200
#'
#' # generate data as above and fit unweighted MLE
#' x = abs(stats::rnorm(ndata))
#' xmle = Gompertz(x)
#'
#' # iterate
#' vec.eta = rep(0,niter)
#' vec.b = rep(0,niter)
#' for (i in 1:niter){
#' # random weight
#' ww = abs(stats::rnorm(ndata))
#'
#' MLE = Gompertz(x, weight=ww)
#' vec.eta[i] = MLE$eta
#' vec.b[i] = MLE$b
#' if ((i%%10) == 0){
#' print(paste0(" iteration ",i,"/",niter," complete.."))
#' }
#' }
#'
#' # distribution of weighted estimates + standard MLE
#' opar <- par(no.readonly=TRUE)
#' par(mfrow=c(1,2))
#' hist(vec.eta, main="shape 'eta'")
#' abline(v=xmle$eta, lwd=3, col="red")
#' hist(vec.b, main="scale 'b'")
#' abline(v=xmle$b, lwd=3, col="blue")
#' par(opar)
#' }
#'
#' @author Kisung You
#' @export
Gompertz <- function(x, weight=NULL){
#############################################
# Preprocessing
x = handle_cts_nonneg("Gompertz", x)
nx = length(x)
weight = handle_weight("Gompertz", weight, nx)
maceps = 10*.Machine$double.eps
#############################################
# Optimize : DEoptim
fopt.Gompertz <- function(pars){
# parameters
eta = pars[1]
b = pars[2]
# log-likelihood
term1 = log(b) + log(eta)
term2 = (b*x + eta)
term3 = -eta*exp(b*x)
loglkd = term1+term2+term3
# return
return(-sum(loglkd*weight))
}
mylower = c(maceps, maceps)
myupper = c(1e+5, 1e+5)
sol = DEoptim::DEoptim(fopt.Gompertz, lower=mylower, upper=myupper,
control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
#############################################
# Return
output = list()
output$eta = as.double(sol[1])
output$b = as.double(sol[2])
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.