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.
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)))
Example 2: Fit a Normal Distribution
data2 = rnorm(1000, 2, 1)
(hist_with_density(data2, 'norm'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With