[R] find parameters for a gamma distribution

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jan 5 22:59:16 CET 2005


Andrew Collier <colliera at ukzn.ac.za> writes:

> hello,
> 
> i have just started exploring R as an alternative to matlab for data analysis. so
> +far everything is _very_ promising. i have a question though regarding parameter
> +estimation. i have some data which, from a histogram plot, appears to arise from
> +a gamma distribution. i gather that you can fit the data to the distribution
> +using glm(). i am just not quite sure how this is done in practice... so here is
> +a simple example with artificial data:
> 
> d <- rgamma(100000, 20, scale = 2)
> h <- hist(d, breaks = c(seq(10, 80, 2), 100))
> 
> H <- data.frame(x = h$mids, y = h$density)
> 
> g <- glm(y ~ x, data = H, family = Gamma)
> summary(g)
> 
> Call:
> glm(formula = y ~ x, family = Gamma, data = H)
> 
> Deviance Residuals:
>     Min       1Q   Median       3Q      Max
> -3.8654  -2.0887  -0.7685   0.7147   1.4508
> 
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
> (Intercept)  30.4758    26.7258   1.140    0.262
> x             1.0394     0.6825   1.523    0.137
> 
> (Dispersion parameter for Gamma family taken to be 1.343021)
> 
>     Null deviance: 119.51  on 35  degrees of freedom
> Residual deviance: 116.28  on 34  degrees of freedom
> AIC: -260.49
> 
> Number of Fisher Scoring iterations: 7
> 
> now i suppose that the estimates parameters are:
> 
>         shape = 30.4758
>         scale = 1.0394
> 
> am i interpreting the output correctly? and, if so, why are these estimates so
> +poor? i would, perhaps naively, expected the parameters from an artificial
> +sample like this to be pretty good.
> 
> my apologies if i am doing something stupid here but my statistics capabilties
> +are rather limited!

Didn't you get a pointer to fitdistr() the last time you posted this?

To quote an old teacher of mine, what you're doing above is "incorrect
in every detail". glm() just isn't for fitting densities to histograms. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list