#' (Weighted) MLE of Log-Logistic Distribution
#'
#' Log-Logistic distribution is characterized by the following probability density function,
#' \deqn{f(x;\alpha,\beta) = \frac{(\beta/\alpha) (x/\alpha)^{\beta-1}}{\left(1 + (x/\alpha)^\beta \right)^2}}
#' where the domain is \eqn{x \in [0,\infty)} with scale \eqn{\alpha > 0} and shape \eqn{\beta > 0} parameters.
#'
#' @param x a length-\eqn{n} vector of values in \eqn{[0,\infty)}.
#' @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{alpha}{scale parameter \eqn{\alpha}.}
#' \item{beta}{ shape parameter \eqn{\beta}.}
#' }
#'
#' @examples
#' # generate data from half-normal
#' x = abs(stats::rnorm(100))
#'
#' # fit unweighted
#' LogLogistic(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 = LogLogistic(x)
#'
#' # iterate
#' vec.alpha = rep(0,niter)
#' vec.beta = rep(0,niter)
#' for (i in 1:niter){
#' # random weight
#' ww = abs(stats::rnorm(ndata))
#'
#' MLE = LogLogistic(x, weight=ww)
#' vec.alpha[i] = MLE$alpha
#' vec.beta[i] = MLE$beta
#' 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.alpha, main="scale 'alpha'")
#' abline(v=xmle$alpha, lwd=3, col="red")
#' hist(vec.beta, main="shape 'beta'")
#' abline(v=xmle$beta, lwd=3, col="blue")
#' par(opar)
#' }
#'
#' @author Kisung You
#' @export
LogLogistic <- function(x, weight=NULL){
#############################################
# Preprocessing
x = handle_cts_nonneg("LogLogistic", x) # nonnegative real numbers
nx = length(x)
weight = handle_weight("LogLogistic", weight, nx)
maceps = 10*.Machine$double.eps
#############################################
# Optimize : DEoptim
fopt.LogLogistic <- function(pars){
# parameters
alpha = pars[1]
beta = pars[2]
# log-likelihood
loglkd = ((beta-1)*(log(x)-log(alpha))) + (log(beta)-log(alpha)) - 2*log(1 + ((x/alpha)^beta))
# return
return(-sum(loglkd*weight))
}
sol = DEoptim::DEoptim(fopt.LogLogistic, lower=c(maceps, maceps), upper=c(1e+5,1e+5),
control=DEoptim::DEoptim.control(trace=FALSE))$optim$bestmem
#############################################
# Return
output = list()
output$alpha = as.double(sol[1])
output$beta = as.double(sol[2])
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.