Announcement

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

  • t-test over time

    Hi

    I am looking for a way that can make it easy to provide the results of a series of t-tests. I want to see whether two sample groups are statistically different in their means by year (2010-2020).
    Logically, I thought I can do this by running t-tests ten times by separating each sample by year. Using loops such as the foreach command is also one way to make it handy.
    Is there any other way to do this? I wonder if there is already a command in Stata for this.

  • #2
    you can use regress to test all differences at once,
    Code:
    use https://www.stata-press.com/data/r17/nlswork.dta, clear
    ttest ln_wage if year==70, by(collgrad)
    reg ln_wage i.collgrad#i.year i.year 
    di _b[1.collgrad#70.year]

    Comment


    • #3
      More generally, the statsby command provides a framework for running the same command repeatedly on different subgroups and accumulating the results..
      Code:
      help statsby

      Comment


      • #4
        Øyvind Snilsberg Thank you. It works well for me.
        William Lisowski Thanks although I have not yet understood how statsby can be used for my case.

        Comment


        • #5
          Code:
          use https://www.stata-press.com/data/r17/nlswork.dta, clear
          keep if year<=72
          ttest ln_wage if year==70, by(collgrad)
          statsby t=r(t) p=r(p), by(year) clear: ttest ln_wage, by(collgrad)
          format %9.4f t p
          list, noobs
          Code:
          . ttest ln_wage if year==70, by(collgrad)
          
          Two-sample t test with equal variances
          ------------------------------------------------------------------------------
             Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
          ---------+--------------------------------------------------------------------
                 0 |   1,557    1.477278     .009687    .3822379    1.458277    1.496279
                 1 |     129    1.866361    .0366441     .416197    1.793854    1.938867
          ---------+--------------------------------------------------------------------
          Combined |   1,686    1.507048    .0097045    .3984743    1.488014    1.526082
          ---------+--------------------------------------------------------------------
              diff |           -.3890822    .0352667               -.4582534    -.319911
          ------------------------------------------------------------------------------
              diff = mean(0) - mean(1)                                      t = -11.0326
          H0: diff = 0                                     Degrees of freedom =     1684
          
              Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
           Pr(T < t) = 0.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 1.0000
          
          . statsby t=r(t) p=r(p), by(year) clear: ttest ln_wage, by(collgrad)
          (running ttest on estimation sample)
          
                Command: ttest ln_wage, by(collgrad)
                      t: r(t)
                      p: r(p)
                     By: year
          
          Statsby groups
          ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
          .....
          
          . format %9.4f t p
          
          . list, noobs
          
            +--------------------------+
            | year          t        p |
            |--------------------------|
            |   68   -11.6224   0.0000 |
            |   69   -11.7128   0.0000 |
            |   70   -11.0326   0.0000 |
            |   71   -13.2678   0.0000 |
            |   72   -11.8362   0.0000 |
            +--------------------------+

          Comment


          • #6
            William Lisowski Thank you very much for your help.

            Comment

            Working...
            X