Skip to content
Prev 298993 / 398506 Next

Fitting and Plotting the fitted distributions

Hello,

This is here for some days now, and I've decided to give it a try.

I've rewritten your fitfunction(), making it simpler. And include the 
gamma distribution in the list.



require(MASS)

fitfunction <- function(Type, x) list(Type=Type, Fit=fitdistr(x, Type))

fun <- function(x, data){
	data <- sort(data)
	if(x$Type == "exponential")
		y <- pexp(data, rate=x$Fit$estimate['rate'])
	else if(x$Type == "geometric")
		y <- pgeom(data, prob=x$Fit$estimate['prob'])
	else if(x$Type == "log-normal")
		y <- plnorm(data, meanlog=x$Fit$estimate['meanlog'], 
sdlog=x$Fit$estimate['sdlog'])
	else if(x$Type == "normal")
		y <- pnorm(data, mean=x$Fit$estimate['mean'], sd=x$Fit$estimate['sd'])
	else if(x$Type == "Poisson")
		y <- ppois(data, lambda=x$Fit$estimate['lambda'])
	else if(x$Type == "gamma")
		y <- pgamma(data, shape=x$Fit$estimate['shape'], 
rate=x$Fit$estimate['rate'])
	list(x=data, y=y)
}


set.seed(1)

distrList <- list("exponential", "geometric", "log-normal", "normal", 
"Poisson", "gamma")

On <- round(abs(rnorm(10000, sd=100))+5,digits=0)

storeOn <- lapply(distrList, fitfunction, x=On)
str(storeOn)
lapply(storeOn, function(x) AIC(x$Fit))

coord <- lapply(storeOn, fun, On)
color <- seq_along(distrList) + 1

plot(ecdf(On), verticals= TRUE, do.p = FALSE, lwd=2)
lapply(seq_along(coord), function(i)
	lines(coord[[i]]$x, coord[[i]]$y, col=color[i]))
legend("right", legend=distrList, col=color, lty=1, bty="n")


Hope this helps,

Rui Barradas

Em 02-07-2012 07:13, Alaios escreveu: