Announcement

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

  • How to do t-test for a subset of observation

    Dear statalists,

    I have a question of how to run -ttest- for a subset of observations. I tried to add -if- after -ttest-, as the way in regression, but stata shows that -if- cannot follow -ttest-.

    Thank you very much.
    Last edited by Alex Mai; 22 Nov 2016, 05:58.

  • #2
    you don't show your command something you know you should do; the following works fine:
    Code:
    sysuse auto, clear
    ttest mpg if rep78<., by(foreign)

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      you don't show your command something you know you should do; the following works fine:
      Code:
      sysuse auto, clear
      ttest mpg if rep78<., by(foreign)
      Dear Rich,

      Thank you very much and sorry for the incomplete command in my post. Now I see that actually my mistake was to put -if- after -by()-.

      Comment


      • #4
        The "if qualifier" shall be typed before the comma.

        Please see the example below:

        Code:
        . sysuse auto
        (1978 Automobile Data)
        
        . ttest mpg, by(foreign)
        
        Two-sample t test with equal variances
        ------------------------------------------------------------------------------
           Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
        ---------+--------------------------------------------------------------------
        Domestic |      52    19.82692     .657777    4.743297    18.50638    21.14747
         Foreign |      22    24.77273     1.40951    6.611187    21.84149    27.70396
        ---------+--------------------------------------------------------------------
        combined |      74     21.2973    .6725511    5.785503     19.9569    22.63769
        ---------+--------------------------------------------------------------------
            diff |           -4.945804    1.362162               -7.661225   -2.230384
        ------------------------------------------------------------------------------
            diff = mean(Domestic) - mean(Foreign)                         t =  -3.6308
        Ho: diff = 0                                     degrees of freedom =       72
        
            Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
         Pr(T < t) = 0.0003         Pr(|T| > |t|) = 0.0005          Pr(T > t) = 0.9997
        
        . ttest mpg if trunk<13, by(foreign)
        
        Two-sample t test with equal variances
        ------------------------------------------------------------------------------
           Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
        ---------+--------------------------------------------------------------------
        Domestic |      16     24.6875    1.116985    4.467941     22.3067     27.0683
         Foreign |      14    26.21429     1.34319    5.025758     23.3125    29.11607
        ---------+--------------------------------------------------------------------
        combined |      30        25.4    .8611673    4.716808    23.63872    27.16128
        ---------+--------------------------------------------------------------------
            diff |           -1.526786    1.732872               -5.076412    2.022841
        ------------------------------------------------------------------------------
            diff = mean(Domestic) - mean(Foreign)                         t =  -0.8811
        Ho: diff = 0                                     degrees of freedom =       28
        
            Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
         Pr(T < t) = 0.1929         Pr(|T| > |t|) = 0.3858          Pr(T > t) = 0.8071
        
        . ttest mpg, if trunk <13 by(foreign)
        option if not allowed
        r(198);
        Best regards,

        Marcos

        Comment


        • #5
          That's not forbidden in itself:

          Code:
          ttest mpg , by(foreign), if rep78<.
          is legal, but forget that I told you that.

          Comment


          • #6
            Nick Cox : wow, that is really impressive! Good to learn this. I checked putting the if qualifier in the middle, of no avail.
            Best regards,

            Marcos

            Comment


            • #7
              generalizing Nick's note in #5, the rule in Stata is that options go after odd numbered commas and parts of the command are after even-numbered commas (counting 0 as even here); thus, one could take Nick's example and add ", une" and that would be fine also; as I have said previously, I do sometimes use this to advantage when trying to figure out exactly what I want - but my final do file will have only one comma (at most) per command

              Comment


              • #8
                As Rich implies, the comma is a switch between the main part of command and the options, and you can switch back and forth. I think this goes back to very early days when retyping a command was a bigger deal. Now it's best for style and clarity to do as Rich says, use at most one comma (of this kind).

                Comment

                Working...
                X