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

时间:2011-07-27 15:55:46

标签: r ggplot2

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

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

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

他们似乎使用了gumbel分布,所以我在包“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)

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

3 个答案:

答案 0 :(得分:9)

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

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

enter image description here

示例2:拟合正态分布

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

enter image description here

答案 1 :(得分:4)

早些时候的会议显示,来自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))

enter image description here

答案 2 :(得分:3)

对你的代码做一个小的修改(添加一个geom),它对我来说很好。

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()
相关问题