英文:
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 <- function(x, mu=0, b=1, params=list(mu, b),...){
if(!missing(params)){
mu <- params$mu
b <- params$b
}
# pdf Laplace dist Johnson et.al, Vol 2, chapter 24, p.164.
d <- exp(-abs(x-mu)/b) / (2*b)
}
and run
laplacefit <- fitdistrplus::fitdist(x, "laplace",
start=list(mu=0,b=1), method="mme")
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 <- read.table("m_trg_list.txt", header=FALSE, sep=',')
x <- 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 <- function(x, mu=0, b=1){
if(b<=0) return(NA)
exp(-abs(x-mu)/b) / (2*b)
}
# Generate random data from laplace with `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)
Notice that laplace does have closed form solutions:
median(x)
[1] 4.936715
mean(abs(x-median(x)))
[1] 2.02316
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论