# How do I generate a QQ-Plot for data fitted using fitdistr?

I am doing the following to fit my data using an exponential function:

# Define the data
data <- c(67, 81, 93, 65, 18, 44, 31, 103, 64, 19, 27, 57, 63, 25, 22, 150,
31, 58, 93, 6, 86, 43, 17, 9, 78, 23, 75, 28, 37, 23, 108, 14, 137,
69, 58, 81, 62, 25, 54, 57, 65, 72, 17, 22, 170, 95, 38, 33, 34, 68,
38, 117, 28, 17, 19, 25, 24, 15, 103, 31, 33, 77, 38, 8, 48, 32, 48,
26, 63, 16, 70, 87, 31, 36, 31, 38, 91, 117, 16, 40, 7, 26, 15, 89,
67, 7, 39, 33, 58)

# Fit the data to a model
params = fitdistr(data, "exponential")


I got the following for the params:

> params
rate
0.019694623
(0.002087626)


I want to draw a QQ-plot to see how good the fit was. I am guessing I need to generate exponentially distributed data using the parameter generated and then use some function to draw the QQ plot but am not sure how to go about doing this. Can someone tell me how to do this in R?

Try the following code:

simdata <- qexp(ppoints(length(data)), rate = params\$estimate)
qqplot(data, simdata)


(Inspired by the base R implementation of qqnorm)

PS When using non-base R functions, you should state what library they come from. I had to Google to discover that fitdistr is from MASS.

• +1 Thank you very much. That does the trick. Sorry about not specifying the fitdistr package. I'm quite new to R so I will keep that in mind for the next time. I have accepted this as the answer but as a final note, can you please tell me if there is a numerical measure that tells me how well the data could be fitted using the distribution? Commented Jan 30, 2011 at 3:12
• In addition, is there a way to limit the upper limit of the sampling to the maximum value in the data samples? Otherwise, the Q-Q plot seems to be showing some numbers way out of the data sample range. Commented Jan 30, 2011 at 3:31
• @Legend: As to your first comment, it should really be a separate question. I can point you towards the Kolmogorov-Smirnov test (secure.wikimedia.org/wikipedia/en/wiki/Kolmogorov_Smirnov_Test), but I encourage you to ask this question, both because you may get better advice and because the answer will be more findable by others). Commented Jan 30, 2011 at 6:06
• @Legend: As to the second point, I'm not sure what you're really asking. (This usage of) a QQ plot compares the distribution of some data to a mathematical function -- so there's no "sampling" involved. The fact that the high end of the plot has values higher than those in the data shows you exactly what you are using the QQ-plot to test for: that the shape of the tail of the distribution of the observed data does not match the tail of the theoretical distribution. (Also, I don't see any "out of range" values with this code and your data from the question...how did you generate that data?) Commented Jan 30, 2011 at 6:09
• Thank You. I will look into the Kolmogorov-Smirnov test and post another question if it is not clear. Regarding my second question, the highest data point in my sample is 170 and there are 89 points. Now, if I use the function you specified, it is producing 89 points but some points are above 170 (181.38, 207.32...). While this can happen, I was just wondering if there is a way to have some upper limit so that it will sample only within a range (0,170) so that the Q-Q plot will contain equal limits on the X and Y axis. Commented Jan 30, 2011 at 6:31

You could also look Wessa P., (2008), Maximum-likelihood Exponential Distribution Fitting which has a nice, documented online execution of the problem with the R source code attached to the actual plot.