简体   繁体   English

R Optim() 函数:截断对数正态最大似然估计(求解 mu 和 sd)

[英]R Optim() function: Truncated Log-normal Maximum Likelihood Estimation (solve for mu and sd)

I'm attempting to fit a truncated log-normal distribution in R. In the below x is list of integers.我试图在 R 中拟合截断的对数正态分布。在下面的 x 是整数列表。 I believe the function Optim() is likely to be the best way to do this, as below.我相信函数 Optim() 可能是最好的方法,如下所示。

log.lklh.lnorm <- function(x, mu, sd, input_min, input_max){-sum(log(dlnorm(x, meanlog = mu, sdlog = sd, log = FALSE)/((plnorm(input_max, meanlog = mu, sdlog = sd, lower.tail = TRUE, log.p = FALSE)) - (plnorm(input_min, meanlog = mu, sdlog = sd, lower.tail = TRUE, log.p = FALSE)))))}


optim(par = c(0,1,1,100000), log.lklh.lnorm)

I used excel solver to solve (ie find the maximum value of this sum) for mu and sd (subject to the input max and min).我使用excel求解器来解决(即找到这个总和的最大值)mu和sd(取决于输入的最大值和最小值)。 However, i cant seem to replicate this in R. I've tried various versions of the above code, including:但是,我似乎无法在 R 中复制它。我尝试了上述代码的各种版本,包括:

  • Set input min and input max to certain values, such as 1 and 100,000 respectively.将输入最小值和输入最大值分别设置为特定值,例如 1 和 100,000。
  • Order of Optim() calls, ie input x, par and fn order) Optim() 调用顺序,即输入 x、par 和 fn 顺序)

Any help is greatly appreciated, thanks!非常感谢任何帮助,谢谢!

Dave戴夫

I think you want to optimize a function of just two parameters.我认为您想优化只有两个参数的函数。 So, we could do:所以,我们可以这样做:

x <- c(1,2,3,3,3,4,4,5,5,6,7)

log.lklh.lnorm <- function(x, mu, sd, input_min, input_max){-sum(log(dlnorm(x, meanlog = mu, sdlog = sd, log = FALSE)/((plnorm(input_max, meanlog = mu, sdlog = sd, lower.tail = TRUE, log.p = FALSE)) - (plnorm(input_min, meanlog = mu, sdlog = sd, lower.tail = TRUE, log.p = FALSE)))))}


f <- function(y) {
  log.lklh.lnorm(x,y[1],y[2],1,100000)    
}

optim(par = c(3,1), f)

Notes in response to further questions in the comments:针对评论中进一步问题的注释:

  1. What is the function of par = c(3,1) ? par = c(3,1)什么? This has two functions.这有两个功能。 (1) It is the initial point (guess). (1) 是初始点(猜测)。 (2) It determines the number of parameters to optimize for. (2) 它决定了要优化的参数数量。 In this case, this is 2. It is noted that you can (and should) calculate very good initial values.在这种情况下,这是 2。请注意,您可以(并且应该)计算出非常好的初始值。 You can get very good estimates for μ and σ just by calculating the average and sample standard deviation.只需计算平均值和样本标准差,您就可以得到非常好的 μ 和 σ 估计值。 (In statistics this is sometimes called method of moments ) This will make the task of optim much easier. (在统计学中,这有时被称为method of moments )这将使optim任务变得更加容易。 So instead of par=c(3,1) we really should do: par = c(mean(x),sd(x)) .因此,我们真的应该这样做,而不是par=c(3,1)par = c(mean(x),sd(x))

  2. What does y[1]/y[2] do?. y[1]/y[2]做什么? The optim function organizes all parameters to optimize for, as a single vector . optim函数将所有要优化的参数组织为单个向量 So I represented (μ,σ) by a single vector y (of length 2).所以我用单个向量y (长度为 2)表示 (μ,σ)。 In the function f , this array is unpacked and passed on to log.lklh.lnorm as individual arguments.在函数f ,这个数组被解包并作为单独的参数传递给log.lklh.lnorm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM