Laplace分布用于fitdist。

huangapple go评论76阅读模式
英文:

Laplace distribution for fitdist

问题

我试图使用fitdistrplus::fitdist来拟合拉普拉斯分布。我看过@Ben Bolker在这个帖子中给出的示例。然而,我很难弄清楚拉普拉斯分布的相应函数并在R中实现它们。到目前为止,我尝试使用从这里提取的dlaplace函数:

dlaplace <- function(x, mu=0, b=1, params=list(mu, b),...){
  if(!missing(params)){
    mu <- params$mu
    b <- params$b
  }
  # 拉普拉斯分布的概率密度函数 Johnson et.al, Vol 2, chapter 24, p.164.
  d <- exp(-abs(x-mu)/b) / (2*b)
}

然后运行:

laplacefit <- fitdistrplus::fitdist(x, "laplace", 
   start=list(mu=0,b=1), method="mme")

但我得到了以下错误:

Error in fitdistrplus::fitdist(x, "laplace", start = list(mu = 0, b = 1),  :    
moment matching estimation needs an 'order' argument 
In addition: Warning messages: 
1: In fitdistrplus::fitdist(x, "laplace", start = list(mu = 0, b = 1),  :   
The dlaplace function should raise an error when names are incorrectly named 
2: In fitdistrplus::fitdist(x, "laplace", start = list(mu = 0, b = 1),  :   
The plaplace function must be defined

是否有人可以提供一个拟合拉普拉斯分布的fitdist的工作示例?

在得到有用的答案后,我使用这个文件运行了一个示例:

x <- read.table("m_trg_list.txt", header=FALSE, sep=',')
x <- as.numeric(c(x))
MASS::fitdistr(x, dlaplace, start = list(b=1, mu=0))

但我得到了以下错误:

Error in stats::optim(x = c(0.00709952, 0.0072816606, 0.0076129483, 0.0069924397,  : 
non-finite finite-difference value [1]
英文:

I am trying to apply fitdistrplus::fitdist with Laplace distribution. I have seen the example given in this post by @Ben Bolker. However, I am having a hard time figuring out the corresponding functions of the Laplace distribution and implementing them in R. So far I have tried to use dlaplace taken from here:

dlaplace &lt;- function(x, mu=0, b=1, params=list(mu, b),...){
  if(!missing(params)){
    mu &lt;- params$mu
    b &lt;- params$b
  }
  # pdf Laplace dist Johnson et.al, Vol 2, chapter 24, p.164.
  d &lt;- exp(-abs(x-mu)/b) / (2*b)
}

and run

laplacefit  &lt;-  fitdistrplus::fitdist(x, &quot;laplace&quot;, 
   start=list(mu=0,b=1), method=&quot;mme&quot;)

but I get

> Error in fitdistrplus::fitdist(x, "laplace", start = list(mu = 0, b =
> 1), : moment matching estimation needs an 'order' argument In
> addition: Warning messages: 1: In fitdistrplus::fitdist(x, "laplace",
> start = list(mu = 0, b = 1), : The dlaplace function should raise
> an error when names are incorrectly named 2: In
> fitdistrplus::fitdist(x, "laplace", start = list(mu = 0, b = 1), :
> The plaplace function must be defined

Could someone provide an working example of fitting the Laplace distribution with fitdist?

After an useful answer I am running an example with this file

x &lt;- read.table(&quot;m_trg_list.txt&quot;, header=FALSE, sep=&#39;,&#39;)
x &lt;- as.numeric(c(x))
MASS::fitdistr(x, dlaplace, start = list(b=1, mu=0))

but I get

> Error in stats::optim(x = c(0.00709952, 0.0072816606, 0.0076129483,
> 0.0069924397, : non-finite finite-difference value [1]

答案1

得分: 3

你正在尝试创建分布的所有函数,即 d,p,q,r-laplace,这是不必要的。只需将一个函数传递给 densfun 参数,而不是分布的名称:

dlaplace <- function(x, mu=0, b=1){
  if(b<=0) return(NA)
  exp(-abs(x-mu)/b) / (2*b)
}
# 从 laplace 分布生成具有 `mu = 5, b = 2` 的随机数据
mu <- 5
b <- 2
n <- 1000
x <- rexp(n, 1/b) - rexp(n, 1/b) + mu
MASS::fitdistr(x, dlaplace, start = list(b=1, mu=0))
       b            mu    
  2.02324609   4.93653582 
 (0.06398334) (0.03169579)

请注意,laplace 分布确实有封闭形式的解:

median(x)
[1] 4.936715
mean(abs(x-median(x)))
[1] 2.02316
英文:

What you are trying to do is create all the functions for a distribution ie d,p,q,r-laplace, which is not necessary. Just pass a function instead of the distribution name to the densfun argument:

dlaplace &lt;- function(x, mu=0, b=1){
  if(b&lt;=0) return(NA)
  exp(-abs(x-mu)/b) / (2*b)
}
# Generate random data from laplace with `mu = 5, b = 2`
mu &lt;- 5
b &lt;- 2
n &lt;- 1000
x &lt;- rexp(n, 1/b) - rexp(n, 1/b) + mu
MASS::fitdistr(x, dlaplace, start = list(b=1, mu=0))
       b            mu    
  2.02324609   4.93653582 
 (0.06398334) (0.03169579)

Notice that laplace does have closed form solutions:

median(x)
[1] 4.936715
mean(abs(x-median(x)))
[1] 2.02316

huangapple
  • 本文由 发表于 2023年7月3日 21:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定