R:如何使用ggplot2的stat_function绘制gumbel分布 [英] R: How to plot gumbel distribution using ggplot2's stat_function

查看:743
本文介绍了R:如何使用ggplot2的stat_function绘制gumbel分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我觉得这样比较脆弱,请耐心等待,如果我遗漏了任何东西,请随时提出问题......

我试图做一些基于以下链接的50年极端风力计算

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



他们似乎使用gumbel发行版,所以我在包evir中使用了函数gumbel以适应数据的分发,并且在包evd中作为绘图函数使用dgumbel。

 <$ (速度=样本(10:50,1000,rep = TRUE))c $ c> package(evd)
package(evir)

speeds2 < - data.frame )
gumbel(speeds2 $ speed)

然后我试着用ggplot2的stat_function ,像这样(现在除了现在我已经为loc和scale设置了虚拟值)。

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

(x,loc = loc,scale = scale,shape = 0,log = log)中的错误:

 
未使用的参数(loc = loc,scale = scale,shape = 0,log = log)

我不确定我是否以正确的方式进行此操作。任何指针都会非常感谢。

解决方案

此前的会议显示,来自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 秒时,你会得到:

 > speed2 < -  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))
dgev(x,loc = loc,scale = scale,shape = 0,log = log):
未使用的参数(loc = loc,scale = scale,shape = 0,log = log)

演示如何调用 dgumbel 函数一个特别的(表现更好的)包:

  library(VGAM)
ggplot(data = speeds2,aes(x =速度))+
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))


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.

解决方案

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))

这篇关于R:如何使用ggplot2的stat_function绘制gumbel分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆