Announcement

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

  • How do I run a Ttest in Stata when I have multiple groups but I only want to compare specific groups? (More Information Below)

    I am trying to do a basic Ttest. There 6 groups based on gender, race and ethnicity. I want to run a simple ttest between group 1 and 5, but I am not sure the best way to do it. From my understanding the group variable can only take two values, and I have seen people ignoring a group based on if commands, but I have 6 groups. I don't want to have to create a new pairwise variable based on each two combinations.

  • #2
    you don't really supply enough information but, if your group is all in one variable, you want something like the following:
    Code:
    ttest var1 if inlist(group,1,5), by(group)
    clearly you should replace "var1" with whatever quantitative variable you want to test; you may also need to rename what I have called "group"; please read the FAQ (note that the above code assumes both variables are numeric; a data example using -dataex- would have reduced the need for assumptions/guesses)

    Comment


    • #3
      Originally posted by Michael Hensley View Post
      I want to run a simple ttest between group 1 and 5, but I am not sure the best way to do it.
      Two ways are shown below.

      .ÿ
      .ÿversionÿ16.1

      .ÿ
      .ÿclearÿ*

      .ÿ
      .ÿsetÿseedÿ`=strreverse("1578925")'

      .ÿ
      .ÿquietlyÿsetÿobsÿ30

      .ÿgenerateÿbyteÿgrpÿ=ÿmod(_n,ÿ6)ÿ+ÿ1

      .ÿ
      .ÿgenerateÿdoubleÿoutÿ=ÿrnormal()

      .ÿ
      .ÿ*
      .ÿ*ÿBeginÿhere
      .ÿ*
      .ÿ//ÿt-testÿ(useÿifÿknownÿheteroscedastic)
      .ÿttestÿoutÿifÿinlist(grp,ÿ1,ÿ5),ÿby(grp)ÿwelch

      Two-sampleÿtÿtestÿwithÿunequalÿvariances
      ------------------------------------------------------------------------------
      ÿÿÿGroupÿ|ÿÿÿÿÿObsÿÿÿÿÿÿÿÿMeanÿÿÿÿStd.ÿErr.ÿÿÿStd.ÿDev.ÿÿÿ[95%ÿConf.ÿInterval]
      ---------+--------------------------------------------------------------------
      ÿÿÿÿÿÿÿ1ÿ|ÿÿÿÿÿÿÿ5ÿÿÿ-.2536269ÿÿÿÿ.6935084ÿÿÿÿ1.550732ÿÿÿ-2.179115ÿÿÿÿ1.671861
      ÿÿÿÿÿÿÿ5ÿ|ÿÿÿÿÿÿÿ5ÿÿÿ-.2595319ÿÿÿÿ.3805594ÿÿÿÿ.8509566ÿÿÿ-1.316134ÿÿÿÿ.7970703
      ---------+--------------------------------------------------------------------
      combinedÿ|ÿÿÿÿÿÿ10ÿÿÿ-.2565794ÿÿÿÿ.3729116ÿÿÿÿÿ1.17925ÿÿÿ-1.100164ÿÿÿÿ.5870052
      ---------+--------------------------------------------------------------------
      ÿÿÿÿdiffÿ|ÿÿÿÿÿÿÿÿÿÿÿÿÿ.005905ÿÿÿÿ.7910621ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ-1.848549ÿÿÿÿ1.860359
      ------------------------------------------------------------------------------
      ÿÿÿÿdiffÿ=ÿmean(1)ÿ-ÿmean(5)ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿtÿ=ÿÿÿ0.0075
      Ho:ÿdiffÿ=ÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿWelch'sÿdegreesÿofÿfreedomÿ=ÿÿ7.31305

      ÿÿÿÿHa:ÿdiffÿ<ÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿHa:ÿdiffÿ!=ÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿHa:ÿdiffÿ>ÿ0
      ÿPr(Tÿ<ÿt)ÿ=ÿ0.5029ÿÿÿÿÿÿÿÿÿPr(|T|ÿ>ÿ|t|)ÿ=ÿ0.9942ÿÿÿÿÿÿÿÿÿÿPr(Tÿ>ÿt)ÿ=ÿ0.4971

      .ÿ
      .ÿ//ÿPreferredÿotherwise
      .ÿregressÿoutÿi.grp

      ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ30
      -------------+----------------------------------ÿÿÿF(5,ÿ24)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿÿ2.36
      ÿÿÿÿÿÿÿModelÿ|ÿÿ12.9967343ÿÿÿÿÿÿÿÿÿ5ÿÿ2.59934685ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0709
      ÿÿÿÿResidualÿ|ÿÿ26.4532529ÿÿÿÿÿÿÿÿ24ÿÿ1.10221887ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.3294
      -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.1898
      ÿÿÿÿÿÿÿTotalÿ|ÿÿ39.4499871ÿÿÿÿÿÿÿÿ29ÿÿ1.36034438ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ1.0499

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿgrpÿ|
      ÿÿÿÿÿÿÿÿÿÿ2ÿÿ|ÿÿ-.6343209ÿÿÿ.6639936ÿÿÿÿ-0.96ÿÿÿ0.349ÿÿÿÿ-2.004736ÿÿÿÿ.7360946
      ÿÿÿÿÿÿÿÿÿÿ3ÿÿ|ÿÿÿÿ1.08155ÿÿÿ.6639936ÿÿÿÿÿ1.63ÿÿÿ0.116ÿÿÿÿ-.2888657ÿÿÿÿ2.451965
      ÿÿÿÿÿÿÿÿÿÿ4ÿÿ|ÿÿÿ1.231445ÿÿÿ.6639936ÿÿÿÿÿ1.85ÿÿÿ0.076ÿÿÿÿ-.1389705ÿÿÿÿÿ2.60186
      ÿÿÿÿÿÿÿÿÿÿ5ÿÿ|ÿÿÿ-.005905ÿÿÿ.6639936ÿÿÿÿ-0.01ÿÿÿ0.993ÿÿÿÿÿ-1.37632ÿÿÿÿ1.364511
      ÿÿÿÿÿÿÿÿÿÿ6ÿÿ|ÿÿÿ.0439541ÿÿÿ.6639936ÿÿÿÿÿ0.07ÿÿÿ0.948ÿÿÿÿ-1.326461ÿÿÿÿÿ1.41437
      ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿÿÿÿ_consÿ|ÿÿ-.2536269ÿÿÿ.4695144ÿÿÿÿ-0.54ÿÿÿ0.594ÿÿÿÿ-1.222657ÿÿÿÿ.7154032
      ------------------------------------------------------------------------------

      .ÿtestÿ5.grp

      ÿ(ÿ1)ÿÿ5.grpÿ=ÿ0

      ÿÿÿÿÿÿÿF(ÿÿ1,ÿÿÿÿ24)ÿ=ÿÿÿÿ0.00
      ÿÿÿÿÿÿÿÿÿÿÿÿProbÿ>ÿFÿ=ÿÿÿÿ0.9930

      .ÿ
      .ÿ//ÿor
      .ÿ
      .ÿregressÿoutÿibn.grp,ÿnoconstant

      ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ30
      -------------+----------------------------------ÿÿÿF(6,ÿ24)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿÿ1.97
      ÿÿÿÿÿÿÿModelÿ|ÿÿ13.0284092ÿÿÿÿÿÿÿÿÿ6ÿÿ2.17140154ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.1101
      ÿÿÿÿResidualÿ|ÿÿ26.4532529ÿÿÿÿÿÿÿÿ24ÿÿ1.10221887ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.3300
      -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.1625
      ÿÿÿÿÿÿÿTotalÿ|ÿÿ39.4816621ÿÿÿÿÿÿÿÿ30ÿÿÿ1.3160554ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ1.0499

      ------------------------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
      -------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿgrpÿ|
      ÿÿÿÿÿÿÿÿÿÿ1ÿÿ|ÿÿ-.2536269ÿÿÿ.4695144ÿÿÿÿ-0.54ÿÿÿ0.594ÿÿÿÿ-1.222657ÿÿÿÿ.7154032
      ÿÿÿÿÿÿÿÿÿÿ2ÿÿ|ÿÿ-.8879478ÿÿÿ.4695144ÿÿÿÿ-1.89ÿÿÿ0.071ÿÿÿÿ-1.856978ÿÿÿÿ.0810823
      ÿÿÿÿÿÿÿÿÿÿ3ÿÿ|ÿÿÿ.8279229ÿÿÿ.4695144ÿÿÿÿÿ1.76ÿÿÿ0.091ÿÿÿÿ-.1411072ÿÿÿÿ1.796953
      ÿÿÿÿÿÿÿÿÿÿ4ÿÿ|ÿÿÿ.9778181ÿÿÿ.4695144ÿÿÿÿÿ2.08ÿÿÿ0.048ÿÿÿÿÿÿ.008788ÿÿÿÿ1.946848
      ÿÿÿÿÿÿÿÿÿÿ5ÿÿ|ÿÿ-.2595319ÿÿÿ.4695144ÿÿÿÿ-0.55ÿÿÿ0.586ÿÿÿÿ-1.228562ÿÿÿÿ.7094982
      ÿÿÿÿÿÿÿÿÿÿ6ÿÿ|ÿÿ-.2096728ÿÿÿ.4695144ÿÿÿÿ-0.45ÿÿÿ0.659ÿÿÿÿ-1.178703ÿÿÿÿ.7593573
      ------------------------------------------------------------------------------

      .ÿtestÿ1.grpÿ=ÿ5.grp

      ÿ(ÿ1)ÿÿ1bn.grpÿ-ÿ5.grpÿ=ÿ0

      ÿÿÿÿÿÿÿF(ÿÿ1,ÿÿÿÿ24)ÿ=ÿÿÿÿ0.00
      ÿÿÿÿÿÿÿÿÿÿÿÿProbÿ>ÿFÿ=ÿÿÿÿ0.9930

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .


      Comment


      • #4
        Hello Michael Hensley. If it is sensible and defensible to assume homogeneity of variance, then I would carry out a planed contrast that uses the error term from an omnibus ANOVA. Here's an example using the dataset Joseph generated in #3.

        Code:
        clear *
        set seed `=strreverse("1578925")'
        quietly set obs 30
        generate byte grp = mod(_n, 6) + 1
        generate double out = rnormal()
        tabstat out, statistics( count mean sd var ) by(grp)
        quietly anova out grp
        * Planned contrast of Group 1 vs Group 5
        contrast {grp 1 0 0 0 -1 0}, effects nowald
        Output from -contrast-:

        Code:
        . * Planned contrast of Group 1 vs Group 5
        . contrast {grp 1 0 0 0 -1 0}, effects nowald
        
        Contrasts of marginal linear predictions
        
        Margins      : asbalanced
        
        ------------------------------------------------------------------------------
                     |   Contrast   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                 grp |
                (1)  |    .005905   .6639936     0.01   0.993    -1.364511     1.37632
        ------------------------------------------------------------------------------

        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          Originally posted by Rich Goldstein View Post
          you don't really supply enough information but, if your group is all in one variable, you want something like the following:
          Code:
          ttest var1 if inlist(group,1,5), by(group)
          clearly you should replace "var1" with whatever quantitative variable you want to test; you may also need to rename what I have called "group"; please read the FAQ (note that the above code assumes both variables are numeric; a data example using -dataex- would have reduced the need for assumptions/guesses)
          Thanks! This works perfectly for me.

          Comment


          • #6
            I failed to point out that the result from my -contrast- in #4 is equivalent to Joseph Coveney's result from -test 5.grp- following -regress- in #3. That approach will have greater degrees of freedom than an ordinary unpaired t-test using only the two groups of interest, so should have quite a bit more power.


            PS- Does anyone else get ÿ characters instead of blank spaces when copying Joseph's code and pasting it into a DO (or text) editor?
            --
            Bruce Weaver
            Email: [email protected]
            Version: Stata/MP 18.5 (Windows)

            Comment


            • #7
              As soon as I highlight his code on the screen in my browser, that character (ASCII 255) shows up for me, and displays when I paste the material into a text editor.

              Comment

              Working...
              X