Announcement

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

  • Running ANOVA in loops

    Hi, I want to run repeated measures ANOVA on math scores and semester, but the analysis has to be done for all levels of stress (high/mid/low) and sex variables (male/female).

    Please let me know if this is possible do in a loop without having to subset manually.

    Thank you!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte student_id str6(income sex semester) float math_score
     1 "low"    "male"   "summer"  75.79707
     2 "low"    "male"   "winter"  78.42905
     3 "low"    "male"   "fall"    77.19025
     4 "low"    "male"   "summer"  86.13298
     5 "low"    "male"   "winter"  84.81785
     6 "low"    "male"   "fall"   72.808044
     7 "low"    "male"   "summer"  60.94741
     8 "low"    "male"   "winter"  93.07262
     9 "low"    "male"   "fall"    96.72986
    10 "low"    "male"   "summer"  95.45274
    11 "low"    "male"   "winter"  73.21725
    12 "low"    "male"   "fall"    68.09888
    13 "low"    "male"   "summer"  54.45422
    14 "low"    "male"   "winter" 70.273636
    15 "low"    "male"   "fall"     63.7756
    16 "low"    "male"   "summer"  73.78484
    17 "low"    "male"   "winter"  74.67471
    18 "low"    "male"   "fall"    74.49361
    19 "low"    "male"   "summer"  65.32313
    20 "low"    "male"   "winter"  71.98983
    21 "low"    "male"   "fall"    70.34425
    22 "low"    "male"   "summer"  63.78717
    23 "low"    "male"   "winter"  79.24786
    24 "low"    "male"   "fall"     82.9422
    25 "low"    "male"   "summer" 66.953354
    26 "low"    "male"   "winter"    83.132
    27 "low"    "male"   "fall"    45.36668
    28 "low"    "male"   "summer"  81.48463
    29 "low"    "male"   "winter"  85.73037
    30 "low"    "male"   "fall"    79.11235
    31 "low"    "male"   "summer"  81.41333
    32 "low"    "male"   "winter"   82.6655
    33 "low"    "male"   "fall"     62.9081
     1 "low"    "male"   "summer"  61.16171
     2 "low"    "male"   "winter"  55.96095
     3 "low"    "male"   "fall"    65.61567
     4 "low"    "male"   "summer"  57.84998
     5 "low"    "male"   "winter"  62.85389
     6 "low"    "male"   "fall"    86.14867
     7 "low"    "male"   "summer" 65.382805
     8 "low"    "male"   "winter"  78.90556
     9 "low"    "male"   "fall"    77.81376
    10 "low"    "male"   "summer"  73.22633
    11 "low"    "male"   "winter"  78.89809
    12 "low"    "male"   "fall"    67.75861
    13 "low"    "male"   "summer"  63.12998
    14 "low"    "male"   "winter" 64.939606
    15 "low"    "male"   "fall"    68.52872
    16 "low"    "male"   "summer"  82.71146
    17 "middle" "male"   "winter"  89.11002
    18 "middle" "male"   "fall"    52.11129
    19 "middle" "male"   "summer"  65.83049
    20 "middle" "male"   "winter"  68.84013
    21 "middle" "male"   "fall"   69.065605
    22 "middle" "male"   "summer"  74.18057
    23 "middle" "male"   "winter"  82.78862
    24 "middle" "male"   "fall"    67.25807
    25 "middle" "male"   "summer" 69.853386
    26 "middle" "male"   "winter"  91.95356
    27 "middle" "male"   "fall"    76.73396
    28 "middle" "male"   "summer"  60.11725
    29 "middle" "male"   "winter"  76.10474
    30 "middle" "female" "fall"    70.36514
    31 "middle" "female" "summer"  67.02353
    32 "middle" "female" "winter"  59.18249
    33 "middle" "female" "fall"     79.9767
     1 "middle" "female" "summer"  79.48432
     2 "middle" "female" "winter"  64.14967
     3 "middle" "female" "fall"    84.50146
     4 "middle" "female" "summer"  63.50959
     5 "middle" "female" "winter"  73.36041
     6 "middle" "female" "fall"    81.75935
     7 "middle" "female" "summer"  71.10308
     8 "middle" "female" "winter"  56.25582
     9 "middle" "female" "fall"    77.83181
    10 "middle" "female" "summer"  60.94855
    11 "middle" "female" "winter"  78.57565
    12 "middle" "female" "fall"     83.1645
    13 "middle" "female" "summer"   79.3857
    14 "high"   "female" "winter"  74.47901
    15 "high"   "female" "fall"    56.33062
    16 "high"   "female" "summer"  66.48232
    17 "high"   "female" "winter"  63.08831
    18 "high"   "female" "fall"    60.62999
    19 "high"   "female" "summer"  76.90691
    20 "high"   "female" "winter"  69.44049
    21 "high"   "female" "fall"    56.89666
    22 "high"   "female" "summer"  79.77268
    23 "high"   "female" "winter" 68.887505
    24 "high"   "female" "fall"    71.54644
    25 "high"   "female" "summer"  65.84627
    26 "high"   "female" "winter"  77.20901
    27 "high"   "female" "fall"    66.96027
    28 "high"   "female" "summer" 67.319885
    29 "high"   "female" "winter" 74.991455
    30 "high"   "female" "fall"    70.49643
    31 "high"   "female" "summer"  71.18354
    32 "high"   "female" "winter"   56.2911
    33 "high"   "female" "fall"    69.46329
    end

  • #2
    Well, it would be possible if you had a variable for stress.

    On the assumption that you have such a variable but didn't think to show it, the code would be
    Code:
    by stress sex: repeated_measures_anova_command

    Comment


    • #3
      Originally posted by Sonnen Blume View Post
      I want to run repeated measures ANOVA on math scores and semester, but the analysis has to be done for all levels of stress (high/mid/low) and sex variables (male/female).

      Please let me know if this is possible do in a loop without having to subset manually.
      You could also accomplish what you want in one fell swoop by including semester, stress level and sex in suitable interaction terms in a single regression model.

      Your student ID values etc. strike me as nonsensical, by the way. You might want to go back and double check things.

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        You could also accomplish what you want in one fell swoop by including semester, stress level and sex in suitable interaction terms in a single regression model.

        Your student ID values etc. strike me as nonsensical, by the way. You might want to go back and double check things.
        Yes, the data is not original; it's just a sample.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Well, it would be possible if you had a variable for stress.

          On the assumption that you have such a variable but didn't think to show it, the code would be
          Code:
          by stress sex: repeated_measures_anova_command
          Thank you Clyde. This is a interesting way to look into the data!
          Actually my intention is to do the analysis for each level of the demographic variables e.g. sex and stress, which means there will be five different sets of results are these two variables have five levels in total.
          There are many more in the original data. That's why I was thinking it might require using the loop function.

          Comment


          • #6
            Originally posted by Sonnen Blume View Post

            Thank you Clyde. This is a interesting way to look into the data!
            Actually my intention is to do the analysis for each level of the demographic variables e.g. sex and stress, which means there will be five different sets of results are these two variables have five levels in total.
            There are many more in the original data. That's why I was thinking it might require using the loop function.
            It will require a loop if you want to save the results in any way.

            What Clyde showed you will do the analysis as you want it, but the results will flash in front of your eyes, and will disappear; or more likely go into your log file.

            If you want to save or output anything in publication ready format you need a loop.

            Comment

            Working...
            X