3

There is histfit function in Matlab would plot histogram and fit the distribution by bin values. The distribution's parameters have to be estimated. How to implement histfit in r? I searched for a long time, but it has no lucky.

This post have mentioned this before, but there is no preferable solution. The sn package seems support several distribution, not so much.

I explore the data with hist function, the histogram shows gamma distribution in gerneral. But if I add up bins and show it again, the graph will show more details, and gamma distribution fails. fitdistr would fail to find parameters also. so I want to fit the data just using the coarse data from histogram. This is the question, thank you for your help.

Community
  • 1
  • 1
Readon Shaw
  • 429
  • 1
  • 4
  • 12
  • What exactly have you tried? Did you even bother to search here? Try this one: http://stackoverflow.com/questions/1497539 – Dirk Eddelbuettel Sep 18 '12 at 14:24
  • What I wanted is almost as @fmark described in [post](http://stackoverflow.com/questions/1497539). But sn package is only suite for some distributions that is not on my list. – Readon Shaw Sep 18 '12 at 14:46
  • @DirkEddelbuettel I need gamma distribution to be supported. – Readon Shaw Sep 18 '12 at 15:07
  • 1
    Please learn how to search. The **very first hit** of the query `[r] how to fit gamma distribution` returns this: http://stackoverflow.com/questions/11689595 More generally, the MASS package has a function `fitdistr` – Dirk Eddelbuettel Sep 18 '12 at 15:26
  • @DirkEddelbuettel, I have read that. fitdistr fit all samples by mle method, but I want to fit by bin values hist returns. – Readon Shaw Sep 18 '12 at 16:09

2 Answers2

1

The fitdistr function in the MASS package can be used to find parameters for a given distribution (including gamma). The function density and the logspline package (and others) can be used to estimate the density function of the data without assuming a specific distribution.

The lines and curve functions can be used to add an estimated density curve to a plotted histogram (use prob=TRUE when creating the histogram).

If you want to compare your data to a specific distribution then tools like qqplots (qqplot function or others) or visual tests (vis.test in the TeachingDemos package) will probably be better than a histogram and density plot.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • I have modified the description above, please check about that. – Readon Shaw Sep 18 '12 at 16:21
  • The `oldlogspline` function in the logspline package can fit an estimated density based on interval censored data (histogram bin counts), this will not be a gamma, but an estimated density. If you want the gamma density then you can still use `fitdistr` either with just the raw data, or create a liklihood for the gamma and interval censored data (or `optim` instead of `fitdistr`). It is not clear exactly what you want and what data you have. – Greg Snow Sep 18 '12 at 16:25
1

I have to answer it myself, package 'bda' could fit the binned data in several distributions, however it could only binning data by rounding.

Readon Shaw
  • 429
  • 1
  • 4
  • 12