Announcement

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

  • Performing yearly t-tests on different variables in Stata

    Hello Statalist Community,

    I am working on a dataset covering the period from 2017 to 2019. My dataset includes various variables, each representing different aspects of my research. I am looking to compare the means of these variables using t-tests, but with a specific condition: the means should be calculated and compared within the same year.

    To clarify, I want to compare the mean of variable1 in 2017 with the mean of variable2 in 2017, and then do the same for 2018 and 2019. Essentially, I am looking to perform a series of t-tests where each test compares the means of two variables within the same year.

    I am unsure how to structure this analysis in Stata. Could anyone provide guidance or suggest the appropriate Stata commands to achieve this? Any help or pointers would be greatly appreciated.

    Thank you in advance for your assistance.

    Best regards
    Johanna
    Code:
      ttest dacc_noturnover == dacc_ceoturnover, unpaired

  • #2
    the answer depends heavily on what your variable names are and, particularly, on whether your data structure is wide or long - as the FAQ requests (please read the FAQ), you help people to help you by supplying an example of your data (or a realistic fake) using -dataex-; see
    Code:
    h dataex
    as well as reading the FAQ

    Comment


    • #3
      Johanna:
      I do echo Rich's advice to be more explicit about your query providing more details about your data via -dataex-.
      That said, you may mean something along the following lines:
      Code:
      . use "https://www.stata-press.com/data/r17/nlswork.dta"
      (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
      . bysort year: ttest age == grade, unpaired unequal
      
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      -> year = 68
      
      Two-sample t test with unequal variances
      ------------------------------------------------------------------------------
      Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
      ---------+--------------------------------------------------------------------
           age |   1,375    20.91927    .0576916    2.139263     20.8061    21.03245
         grade |   1,375    11.78473    .0511054    1.895041    11.68447    11.88498
      ---------+--------------------------------------------------------------------
      Combined |   2,750      16.352    .0952507    4.994987    16.16523    16.53877
      ---------+--------------------------------------------------------------------
          diff |            9.134545     .077072                 8.98342    9.285671
      ------------------------------------------------------------------------------
          diff = mean(age) - mean(grade)                                t = 118.5197
      H0: diff = 0                     Satterthwaite's degrees of freedom =  2708.59
      
          Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
       Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
      
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      <snip>
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      -> year = 88
      
      Two-sample t test with unequal variances
      ------------------------------------------------------------------------------
      Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
      ---------+--------------------------------------------------------------------
           age |   2,258    39.15412    .0644551    3.062802    39.02772    39.28052
         grade |   2,270    13.09692    .0528028    2.515766    12.99337    13.20046
      ---------+--------------------------------------------------------------------
      Combined |   4,528    26.09099    .1980637    13.32779    25.70269    26.47929
      ---------+--------------------------------------------------------------------
          diff |             26.0572    .0833222                25.89385    26.22056
      ------------------------------------------------------------------------------
          diff = mean(age) - mean(grade)                                t = 312.7281
      H0: diff = 0                     Satterthwaite's degrees of freedom =  4352.84
      
          Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
       Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment

      Working...
      X