# Data transformation to fit gamma distribution in R

I'm having trouble to fit and simulate a gamma distribution using the fitdistr function from the MASS package:

My data is daily rainfall, so i 'm adding 1 to all points to get rid the 0's:

require(MASS)
rainfall = scan('daily_rainfall_january.txt', '')

rainfall = rainfall + 1
# sometimes i get warnings here (NA produced)
fit = fitdistr(rainfall, 'gamma')


Now i want so simulate a January rainfall: (plot shows the accumulated rainfall over N simulations)

1 - Blue and red: output from 1000 rgamma simulations

2 - Dark line: 30 year climatology mean (monthly accumulated rainfall)

shape = fit$$estimate['shape'] rate = fit$$estimate['rate']
rainfall.simul = rgamma(31, shape = shape, rate = rate)


Now, how do i transform back rainfall.simul? Is there a better way to handle the 0's problem?

• It seems unlikely a Gamma would fit daily rainfall records (except by accident for a sufficiently small dataset): there will be too many days in which no rain at all occurred. These true zeros have zero probability no matter what the parameters may be. Could you share with us your reasons for fitting a distribution? What do you hope to accomplish by that?
– whuber
Commented Sep 16, 2013 at 20:56
• To estimate the probability density for the daily rainfall forecast...Is it bad to transform the data in this case? I have nearly 11 years of daily data (~ 4500 points). I added '0.1' instead of '1' to the series, then ran a simualtion for each month: for some places/months, i got a pretty good fit (compared against a 30 year climatology mean). Commented Sep 16, 2013 at 21:06
• Fitting a univariate distribution won't really help you run a realistic simulation, because rainfall has such strong temporal correlation (on several scales) and seasonality. Instead of trying to model all that parametrically, why not use the historical data more directly? Incidentally, reproducing a mean in your simulation tells you next to nothing about the goodness of the fit.
– whuber
Commented Sep 16, 2013 at 21:11
• What do you mean by more directly? I'm using a gamma to simulate each month, not the whole year, which would be clearly absurd. Commented Sep 16, 2013 at 21:14
• Your simulations therefore (according to your description) exhibit none of the important time patterns present in the actual rainfall record. I don't want to recommend anything because I don't know what you're doing with these simulations, but--just as an example--you could draw a random value for a given date from the values for the same date during the 30 preceding years. For the next date you could flip a coin and depending on its outcome choose a value for that date from the same year or else from another random year. That's easy to do and produces a far more realistic simulation.
– whuber
Commented Sep 16, 2013 at 21:19