R:如何使用 ggplot2 的 stat_function 绘制gumbel分布

发布于 2024-11-26 18:19:21 字数 871 浏览 0 评论 0原文

如果这相当脆弱,请耐心等待,如果我遗漏了任何内容,请随时提出问题...

我正在尝试根据以下链接进行一些 50 年极端风力计算

http://www.wasp.dk/Products/weng/ExtremeWinds.htm

他们似乎使用分布,所以我使用包“evir”中的函数gumbel来拟合数据的分布,并使用包“evd”中的函数dgumbel作为绘图函数。

package("evd")
package("evir")

speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
gumbel(speeds2$speed)

然后我尝试使用 ggplot2 的 stat_function 来绘制它,就像这样(除了现在我已经为 loc 和 scale 输入了虚拟值。

library(ggplot2)
ggplot(data=speeds2, aes(x=speed)) + 
  stat_function(fun=dgumbel, args=list(loc=1, scale=0.5))

我收到以下错误:

Error in dgev(x, loc = loc, scale = scale, shape = 0, log = log) : 
  unused argument(s) (loc = loc, scale = scale, shape = 0, log = log)

我不确定我是否以正确的方式这样做。任何指针都会是非常感谢。

Please bear with me if this is rather tenuous, and feel free to ask questions if I have left anything out...

I'm attempting to do some 50 year extreme wind calculations based on the following link

http://www.wasp.dk/Products/weng/ExtremeWinds.htm

They seem to use the gumbel distribution, so I have used function gumbel in package "evir" to fit the distribution to the data, and function dgumbel in package "evd" as the plotting function.

package("evd")
package("evir")

speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
gumbel(speeds2$speed)

I have then tried to plot this using ggplot2's stat_function, like so (except for now I have put in dummy values for loc and scale.

library(ggplot2)
ggplot(data=speeds2, aes(x=speed)) + 
  stat_function(fun=dgumbel, args=list(loc=1, scale=0.5))

I get the following error:

Error in dgev(x, loc = loc, scale = scale, shape = 0, log = log) : 
  unused argument(s) (loc = loc, scale = scale, shape = 0, log = log)

I am unsure if I am doing this the right way. Any pointers would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-12-03 18:19:22

这是我编写的一个通用函数,用于简化具有拟合密度和经验密度的数据绘制。

# FUNCTION TO DRAW HISTOGRAM OF DATA WITH EMPIRICAL AND FITTED DENSITITES
# data  = values to be fitted
# func  = name of function to fit (e.g., 'norm', 'gumbel' etc.)
# start = named list of parameters to pass to fitting function 
hist_with_density = function(data, func, start = NULL){
    # load libraries
    library(VGAM); library(fitdistrplus); library(ggplot2)

    # fit density to data
    fit   = fitdist(data, func, start = start)
    args  = as.list(fit$estimate)
    dfunc = match.fun(paste('d', func, sep = ''))

    # plot histogram, empirical and fitted densities
    p0 = qplot(data, geom = 'blank') +
       geom_line(aes(y = ..density..,colour = 'Empirical'),stat = 'density') +
       stat_function(fun = dfunc, args = args, aes(colour = func))  +
       geom_histogram(aes(y = ..density..), alpha = 0.4) +
       scale_colour_manual(name = '', values = c('red', 'blue')) + 
       opts(legend.position = 'top', legend.direction = 'horizontal')
    return(p0)  
}

以下是如何使用它的两个示例
示例 1:拟合 Gumbel

data1 = sample(10:50,1000,rep=TRUE)
(hist_with_density(data1, 'gumbel', start = list(location = 0, scale = 1)))

在此处输入图像描述

示例 2:拟合正态分布

data2 = rnorm(1000, 2, 1)
(hist_with_density(data2, 'norm'))

在此处输入图像描述

Here is a generic function that I wrote to simplify plotting of data with fitted and empirical densities.

# FUNCTION TO DRAW HISTOGRAM OF DATA WITH EMPIRICAL AND FITTED DENSITITES
# data  = values to be fitted
# func  = name of function to fit (e.g., 'norm', 'gumbel' etc.)
# start = named list of parameters to pass to fitting function 
hist_with_density = function(data, func, start = NULL){
    # load libraries
    library(VGAM); library(fitdistrplus); library(ggplot2)

    # fit density to data
    fit   = fitdist(data, func, start = start)
    args  = as.list(fit$estimate)
    dfunc = match.fun(paste('d', func, sep = ''))

    # plot histogram, empirical and fitted densities
    p0 = qplot(data, geom = 'blank') +
       geom_line(aes(y = ..density..,colour = 'Empirical'),stat = 'density') +
       stat_function(fun = dfunc, args = args, aes(colour = func))  +
       geom_histogram(aes(y = ..density..), alpha = 0.4) +
       scale_colour_manual(name = '', values = c('red', 'blue')) + 
       opts(legend.position = 'top', legend.direction = 'horizontal')
    return(p0)  
}

Here are two examples of how you would use it
Example 1: Fit a Gumbel

data1 = sample(10:50,1000,rep=TRUE)
(hist_with_density(data1, 'gumbel', start = list(location = 0, scale = 1)))

enter image description here

Example 2: Fit a Normal Distribution

data2 = rnorm(1000, 2, 1)
(hist_with_density(data2, 'norm'))

enter image description here

拥醉 2024-12-03 18:19:22

早些时候的会议显示,gumbel 调用的参数估计值接近 24 和 11。

library(evd)
library(ggplot2)
 speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
 ggplot(data=speeds2, aes(x=speed), geom="density") + 
   stat_function(fun=dgumbel, args=list(loc=24, scale=11))

如果您仅使用 1 和 0.5 的参数,您会得到一条直线。仅加载 evd 可以防止与 evir 中与 dgumbel 相关的函数发生冲突。当您第二次加载 evir 时,您会得到:

> speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
> ggplot(data=speeds2, aes(x=speed), geom="density") + 
+   stat_function(fun=dgumbel, args=list(loc=24, scale=11))
Error in dgev(x, loc = loc, scale = scale, shape = 0, log = log) : 
  unused argument(s) (loc = loc, scale = scale, shape = 0, log = log)

演示如何在特定(表现更好)包中调用 dgumbel 函数:

library(VGAM)
ggplot(data = speeds2, aes(x = speed)) + 
   stat_function(fun = VGAM::dgumbel, args = list(location = 24, scale = 11))

我认为 Ramnath 的建议是添加经验 '密度'很好,但我更喜欢使用 geom_histogram:

ggplot(data=speeds2, aes(x=speed)) + geom_histogram(aes(y = ..density..) , binwidth=5 ) + 
                            stat_function(fun=dgumbel, args=list(loc=24, scale=11))

在此处输入图像描述

Earlier session showed that the parameter estimates from the gumbel call were near 24 and 11.

library(evd)
library(ggplot2)
 speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
 ggplot(data=speeds2, aes(x=speed), geom="density") + 
   stat_function(fun=dgumbel, args=list(loc=24, scale=11))

If you only used the parameters of 1 and 0.5, you got a straight flat line. Loading only evd prevents conflicts with the dgumbel-related functions in evir. When you load evir second you get:

> speeds2 <- data.frame(speed=sample(10:50,1000,rep=TRUE))
> ggplot(data=speeds2, aes(x=speed), geom="density") + 
+   stat_function(fun=dgumbel, args=list(loc=24, scale=11))
Error in dgev(x, loc = loc, scale = scale, shape = 0, log = log) : 
  unused argument(s) (loc = loc, scale = scale, shape = 0, log = log)

Demonstrating how to make a call to a dgumbel function in a particular (better behaved) package:

library(VGAM)
ggplot(data = speeds2, aes(x = speed)) + 
   stat_function(fun = VGAM::dgumbel, args = list(location = 24, scale = 11))

I think Ramnath's suggestion to add the empiric 'density' is good but I prefer to use geom_histogram:

ggplot(data=speeds2, aes(x=speed)) + geom_histogram(aes(y = ..density..) , binwidth=5 ) + 
                            stat_function(fun=dgumbel, args=list(loc=24, scale=11))

enter image description here

分开我的手 2024-12-03 18:19:22

通过对代码进行少量修改(添加几何图形),它对我来说效果很好。

library(evd)
speeds2 <- data.frame(speed = sample(10:50, 1000, rep = TRUE))

ggplot(data = speeds2, aes(x = speed)) + 
  stat_function(fun = dgumbel, args = list(loc = 1, scale = 0.5)) +
  geom_histogram()

With a small, modification to your code (adding a geom) it works fine for me.

library(evd)
speeds2 <- data.frame(speed = sample(10:50, 1000, rep = TRUE))

ggplot(data = speeds2, aes(x = speed)) + 
  stat_function(fun = dgumbel, args = list(loc = 1, scale = 0.5)) +
  geom_histogram()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文