# Finding the Distribution Parameters

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, fit.distr\$estimate), (raw[,2]),
xlab="Observed Data", ylab="Random Gamma")
abline(0,1,col='red')

simulated = rgamma(1000, fit.distr\$estimate, fit.distr\$estimate)
hist(simulated, main=paste("Histogram of Simulated Gamma using",round(fit.distr\$estimate,3),"and",round(fit.distr\$estimate,3)),
col=8, xlab="Random Gamma Distribution Value")

( fit.distr = fitdistr(raw[,2], "normal") )
qqplot(rnorm(nrow(raw),fit.distr\$estimate, fit.distr\$estimate), (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')

``` Posted in Uncategorized

## 5 replies on “Finding the Distribution Parameters”

1. Larry says:

Where does the function fitdistr() come from?

1. Wesley says:

It is contained in the MASS package.

2. Régis says:

Hello,

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

1. Wesley says:

WordPress 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.

3. Régis says:

Hello,

Thx for the update, all it works good.

Best regards