example for fitdistr() in r

The fitdistr() function in R is used to fit probability distributions to data.

Here is an example of how to use fitdistr() to fit a normal distribution to a sample data:

main.r
# create sample data
data <- rnorm(100, mean = 10, sd = 2)

# load the MASS package
library(MASS)

# fit the normal distribution to the data
fit <- fitdistr(data, "normal")

# view the estimated parameters
fit$estimate
219 chars
12 lines

In the above code, we first create a sample data using the rnorm() function with a mean of 10 and a standard deviation of 2.

We then load the MASS package which contains the fitdistr() function.

Lastly, we use fitdistr() to fit the normal distribution to the data and store the result in the fit object. We can then view the estimated parameters of the normal distribution using fit$estimate.

Note that fitdistr() can be used to fit a variety of different distributions including the gamma distribution, exponential distribution, and Weibull distribution.

gistlibby LogSnag