This is a brief description on one way to determine the distribution of given data. There are several ways to accomplish this in R especially if one is trying to determine if the data comes from a normal distribution. Rather than focusing on hypothesis testing and determining if a distribution is actually the said distribution this example shows one simple approach to determine the parameters of a distribution. I’ve found this useful when I’m given a dataset and I need to generate more of the same type of data for testing and simulation purposes. The example below uses data that originated from a gamma (1,2). Here we can see how well the data fits the distribution.

raw = t( matrix(c(
1, 0.4789,
1, 0.1250,
2, 0.7048,
2, 0.2482,
2, 1.1744,
2, 0.2313,
2, 0.3978,
2, 0.1133,
2, 0.1008,
1, 0.7850,
2, 0.3099,
1, 2.1243,
2, 0.3615,
2, 0.2386,
1, 0.0883), nrow=2
) )
( fit.distr = fitdistr(raw[,2], "gamma") )
qqplot(rgamma(nrow(raw),fit.distr$estimate[1], fit.distr$estimate[2]), (raw[,2]),
xlab="Observed Data", ylab="Random Gamma")
abline(0,1,col='red')
simulated = rgamma(1000, fit.distr$estimate[1], fit.distr$estimate[2])
hist(simulated, main=paste("Histogram of Simulated Gamma using",round(fit.distr$estimate[1],3),"and",round(fit.distr$estimate[2],3)),
col=8, xlab="Random Gamma Distribution Value")
( fit.distr = fitdistr(raw[,2], "normal") )
qqplot(rnorm(nrow(raw),fit.distr$estimate[1], fit.distr$estimate[2]), (raw[,2]))
abline(0,1,col='red')
( fit.distr = fitdistr(raw[,2], "lognormal") )
qqplot(rlnorm(nrow(raw),fit.distr$estimate, fit.distr$sd), (raw[,2]))
abline(0,1,col='red')
( fit.distr = fitdistr(raw[,2], "exponential") )
qqplot(rexp(nrow(raw),fit.distr$estimate), (raw[,2]))
abline(0,1,col='red')

### Like this:

Like Loading...

## Larry

/ April 10, 2013Where does the function fitdistr() come from?

## Wesley

/ April 10, 2013It is contained in the MASS package.

## Régis

/ April 11, 2013Hello,

I can’t launch the function correctly i have an error message :Erreur dans raw < -t(matrix(c(1, 0.4789, 1, 0.125, 2, 0.7048, 2, 0.2482, :

comparaison (3) possible seulement pour les types liste et atomique

Secondly how did you find the parameters?

Best regarfs

## Wesley

/ April 11, 2013WordPress did a strange thing with the “<-” value and put a space between the two characters. I updated the post and used a “=” symbol instead.

As far a the parameters go, if you look at the fit.distr$estimate you will see an array of values. For one parameter distributions (e.g. exponential) it will only contain one value. For two parameters distributions (e.g. beta) it will contain the two values. These values are the parameters based on the data.

## Régis

/ April 11, 2013Hello,

Thx for the update, all it works good.

Best regards