Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gamma distribution

    Hi all,
    I am trying to assess how well does my data fits a gamma distribution. I already used the gammafit command, and now I want to see how well does my data fits the theoretical gamma distribution. I want to know how to use the ksmirnov command to do this.
    As an example, i can create a gamma distribution and see whether it fits the gamma distribution using the ksmirnov command. Thing is I would need some help to see how to use the ksmirnov command to see whether my variable fits a theoretical gamma distribution:

    gen t = rgamma(2, 3)
    ksmirnov t = rgamma(2, 3) [is this the right way of doing this??]

  • #2
    I wouldn't spend much time on Kolmogorov-Smirnov here. It isn't good at telling you where and why something doesn't fit; it tends to be sensitive most to differences in the middle of the distribution; and yet more.

    But regardless of that your syntax is wrong, as your expression defining a single (and unrepeatable) random draw from a gamma is in no sense a definition of the cumulative distribution function. You can see that by trying it a few times

    Code:
     
    . di rgamma(2, 3)
    15.545113
    
    . di rgamma(2, 3)
    1.7015673
    Your numbers will differ, but with your example parameter valuess will often be above 1. Even if you get a result below 1, it's not a cumulative probability.

    I won't try to work out correct syntax given my antipathy to K-S. But as you have found gammafit (SSC, as you are asked to explain) its help points to other programs (on the same site), notably qgamma which gives quantile-quantile plots.

    That said, I think it's best to accept a distribution fit, but tentatively, if and only if, the fit judged graphically isn't too bad, and you can't find a better fit with another distribution. This is pretty much one of the most popular areas for distribution fitting (right-skewed distributions with a small number of parameters) and there are several other candidates.

    Comment


    • #3
      No; the -ksmirnov- command is wrong. rgamma(2,3) is a function that generates random numbers with that distribution. The right hand side of the equation in the k-smirnov command has to be the cumulative distribution function you are trying to match. The Stata distribution and density functions relating to the gamma distribution have only a shape parameter: you implement the scale parameter by applying an appropriate factor. So I think you want

      Code:
      gen t = rgamma(2, 3)
      gen x = t/3 // x WILL BE ~ Gamma(2, 1)
      ksmirnov x = gammap(2, x)
      Last edited by Clyde Schechter; 18 Feb 2015, 15:55.

      Comment


      • #4
        Thanks very much guys!

        Comment


        • #5
          I would inspect the deviation graphically. Both hangroot (SSC) and qenv (SSC) in combination with qplot (SJ) allow you to inspect the fit of a gamma distribution:


          Code:
          sysuse nlsw88, clear
          
          gammafit wage
          
          hangroot, ci name(hang, replace)
          hangroot, ci susp notheor name(susp, replace)
          
          tempname alpha beta
          
          qenvgamma wage, overall reps(20000) gen(lower upper)
          scalar `alpha' = r(alpha)
          scalar `beta' = r(beta)
          
          qplot wage lower upper, ms(oh none ..) c(. l l) lc(gs10 ..) legend(off) ///
                  ytitle("Weight, lb") trscale(`beta' * invgammap(`alpha', @)) ///
                  xtitle("Gamma quantiles")
          Click image for larger version

Name:	hang.png
Views:	1
Size:	16.3 KB
ID:	861109

          Click image for larger version

Name:	susp.png
Views:	1
Size:	12.1 KB
ID:	861110

          Click image for larger version

Name:	qenv.png
Views:	1
Size:	10.1 KB
ID:	861111
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment

          Working...
          X