Announcement

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

  • Strange behvior of invt() function

    I was trying to determine how to compute confidence intervals after the ttest command, as it doesn't leave behind any scalar or macro for CI. The Stata ttest command uses the invt() function to return the inverse cumulative Student's t-distribution. The help file for the function displays:
    Code:
    [FN] Statistical functions    
    (View complete PDF manual entry)
    
    
    Function
    
        invt(df,p)
           Description:  the inverse cumulative Student's t distribution: if t(df,t) = p, then invt(df,p) = t
           Domain df:    2e-10 to 2e+17 (may be nonintegral)
           Domain p:     0 to 1
           Range:        -8e+307 to 8e+307


    However, I discovered that the function returns two different values for the same input: one when used inside the _ttest.ado file and another when used externally. Observe the following trace log:
    Code:
    . ttest price == 0
    
    One-sample t test
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
    ---------+--------------------------------------------------------------------
            - _ttest table `level' `"`xname'"' `n' `m' `s'
            = _ttest table 95 `"price"' 74 6165.256756756757 2949.495884768919
              -------------------------------------------------------------------------- begin _ttest ---
              - version 6.0
              - gettoken cmd 0 : 0
              - if "`cmd'"=="center" {
              = if "table"=="center" {
                tokenize `"`0'"'
                _center `"`1'"' `"`2'"' `"`3'"'
                }
              - else if "`cmd'"=="center2" {
              = else if "table"=="center2" {
                tokenize `"`0'"'
                _center2 `"`1'"' `"`2'"' `"`3'"'
                }
              - else if "`cmd'"=="center3" {
              = else if "table"=="center3" {
                tokenize `"`0'"'
                _center3 `"`1'"' `"`2'"' `"`3'"'
                }
              - else {
              - gettoken t1 0 : 0
              - gettoken t2 0 : 0
              - _`cmd' `"`t1'"' `"`t2'"' `0'
              = _table `"95"' `"price"'  74 6165.256756756757 2949.495884768919
                ----------------------------------------------------------------- begin _ttest._table ---
                - args level name n mean sd ztest se
                - if "`ztest'" == "" {
                = if "" == "" {
                - if (`n') == 1 | (`n') == . {
                = if (74) == 1 | (74) == . {
                  local sd = .
                  }
                - local q = invt((`n') - 1,`level'/100)
                = local q = invt((74) - 1,95/100)
                - dis `q'
                = dis 1.992997125889856
    1.9929971
                - }
                - else local q = invnormal((100+`level')/200)
                = else local q = invnormal((100+95)/200)
                - if ("`ztest'"=="2") {
                = if (""=="2") {
                  local se = `se'
                  }
                - else {
                - local se = (`sd')/sqrt(`n')
                = local se = (2949.495884768919)/sqrt(74)
                - }
                - di in smcl in gr %8s abbrev(`"`name'"',8) " {c |}" in ye _col(12) %7.0fc `n' _col(22) %
    > 9.0g `mean' _col(34) %9.0g `se' _col(46) %9.0g `sd' _col(58) %9.0g (`mean')-`q'*(`se') _col(70) %9.
    > 0g (`mean')+`q'*(`se')
                = di in smcl in gr %8s abbrev(`"price"',8) " {c |}" in ye _col(12) %7.0fc 74 _col(22) %9.
    > 0g 6165.256756756757 _col(34) %9.0g 342.8719320889989 _col(46) %9.0g 2949.495884768919 _col(58) %9.
    > 0g (6165.256756756757)-1.992997125889856*(342.8719320889989) _col(70) %9.0g (6165.256756756757)+1.9
    > 92997125889856*(342.8719320889989)
       price |      74    6165.257    342.8719    2949.496    5481.914      6848.6


    and now from the Stata command window
    Code:
    . di invt((74) - 1,95/100)
    1.6659962

    I don't understand what's happening. Does it utilize some hidden macros or globals? Such details should have been mentioned in the help file.


  • #2
    Hi Meliesa
    That is a very interesting easteregg! or bug if you prefer.
    So, for things like this, its better to contact technical support, since the problem is a programming version one.

    Bottom line, it seems that everything comes from a decision made back in Stata 6 (see line version 6)

    apparently, back in version 6, when using `invt(df, 1-alpha)` function, it would return the value under the assumption of a two-tails test. So it produces the 97.5th percentile of the t distribution
    In newer versions (no idea when that changed), behavior was modified to what we are more used to, showing the 95th percentile of the t-distribution.

    you can get the same with the following:

    Code:
    di invt((74) - 1,97.5/100)
    version 6: di invt((74) - 1,95/100)
    HTH

    Comment


    • #3
      Good catch by FernandoRios

      Documented at

      Code:
      help version
      ​​​​​​​

      Comment


      • #4
        Interesting.

        It does "fix" it with /200 in the subsequent lines.

        Comment


        • #5
          I hope Stata Corp will notice this thread and address the bug. I believe many users aren't aware that they need to attach version 6 to the invt() function.

          Comment


          • #6
            I dont think is a current "bug", but just a programming decision made around Stata 6.
            If you look at any econometric book (at least older ones), you will see that they report results as what invt() does now a days.
            At the very least, its what R does.



            Comment


            • #7
              I doubt Stata is going to make a fuss about v6: it's ancient at this point. We are at v18, after all.

              And, it's not a bug, but a programming choice at that time. You gotta know your software.

              Comment

              Working...
              X