Announcement

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

  • store difference of means with confidence intervals from ttest

    Hello everybody,

    I have run the following ttest:

    Code:
     ttest auc, by(group)
    
    Two-sample t test with equal variances
    ------------------------------------------------------------------------------
       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
    ---------+--------------------------------------------------------------------
           4 |      16    369.0653    16.94614    67.78454    332.9455    405.1852
           5 |      13    382.2868    18.02805    65.00107    343.0071    421.5666
    ---------+--------------------------------------------------------------------
    Combined |      29    374.9922    12.20094    65.70405    349.9997    399.9847
    ---------+--------------------------------------------------------------------
        diff |           -13.22151    24.85378               -64.21725    37.77424
    ------------------------------------------------------------------------------
        diff = mean(4) - mean(5)                                      t =  -0.5320
    H0: diff = 0                                     Degrees of freedom =       27
    
        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
     Pr(T < t) = 0.2995         Pr(|T| > |t|) = 0.5991          Pr(T > t) = 0.7005
    I now wish to store the difference of means (here -13.2) and confidence interval (here -64.2 to 37.8) as local variables in order to post into graphs etc. I can easily find e.g. t-statistic r(t) or p-value r(p). However, I cannot find out where difference of means, CI lower and upper limit are stored. I could not find this information via:

    Code:
    . mat list r(table)
    matrix r(table) not found
    r(111);
    I tried to calculate the difference of means myself, which did not work either:

    Code:
    . local diff=`mean(4)'-`mean(5)'
    invalid syntax
    r(198);
    And I would not know anyways how to then deduct the confidence intervals.

    Could You help me? The aim: extracting difference of means (here -13.2) and confidence interval (here -64.2 to 37.8)

    Thank You so much,
    Dorothea

  • #2
    I see two ways to get this: (1) the individual means are saved (r(mu_1) and r(mu_2)) so you could just save the difference or (2) estimate a regression which will give you, in this case, exactly the same result and the difference will be the coefficient of the group variable and that is saved and available in several ways

    forgot about the CIs - the second will give you access but the first won't (you could calculate it but the regress method is so much easier)
    Last edited by Rich Goldstein; 18 Jul 2023, 11:44.

    Comment


    • #3
      Code:
      sysuse auto, clear
      ttest price, by(foreign)
      return list
      local diff = `r(mu_2)'-`r(mu_1)'
      local lb = `diff'-`r(se)'*invt(`r(df_t)',0.975)
      local ub = `diff'+`r(se)'*invt(`r(df_t)',0.975)
      di `diff'
      di `lb'
      di `ub'
      reg price foreign
      local diff2 = r(table)[1,1]
      local lb2 = r(table)[5,1]
      local ub2 = r(table)[6,1]
      di `diff2'
      di `lb2'

      Comment


      • #4
        Thank You so much, not only this works but I even understood!
        Just a slight variation diff=`r(mu_1)'-`r(mu_2)' and I get the exact correct numbers

        Code:
        ttest auc, by(group)
        return list
        local diff = `r(mu_1)'-`r(mu_2)'
        local lb = `diff'-`r(se)'*invt(`r(df_t)',0.975)
        local ub = `diff'+`r(se)'*invt(`r(df_t)',0.975)
        di `diff'
        di `lb'
        di `ub'
        reg auc group
        local diff2 = r(table)[1,1]
        local lb2 = r(table)[5,1]
        local ub2 = r(table)[6,1]
        di `diff2'
        di `lb2'

        Comment


        • #5
          "return list" and "ereturn list" are key commands I use often to find all the goodies.

          Comment

          Working...
          X