Announcement

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

  • Using loop to form groups for mean/average generation (by group)

    Hi, I am trying to calculate the mean of a variable named gom_r by groups in rt_g1 to rt_g8. The numbers in each of these rt_g(i) is encoded from a string variable. The number "1" in column rt_g1 means the same thing as the "1" in any other rt_g(i). No observation would have the same number repeated per row.

    Aims:
    To create 34 groups for values 1-34 in variables rt_g(i).
    Then calculate the mean of the variable gom_i by each of the 34 groups.

    If I code these one by one, the codes I need look like:

    Code:
    gen group1 =.
    replace group1 = 1 if rt_g1 == 1 | rt_g2 == 1 | rt_g3 == 1 |rt_g4 == 1 | rt_g5 == 1 | rt_g6 == 1 | rt_g7 ==1 | rt_g8 ==1
    bysort group1: egen contrast1 = mean(gom_r)
    replace contrast1 =. if group1 ==.
    
    
    gen group2 =.
    replace group2 = 1 if rt_g1 == 2 | rt_g2 == 2 | rt_g3 == 2 |rt_g4 == 2 | rt_g5 == 2 | rt_g6 == 2 | rt_g7 ==1 | rt_g8 ==1
    bysort group2: egen contrast2 = mean(gom_r)
    replace contrast2 = . if group2 ==.
    Questions:
    (1) I would like to use a -loop- and/or -forvalues- command to speed up the progress. I have never used these command before, tried to adapt but cannot do fully.
    (2) I doubt the way I did was the best way to organize the dataset. I'd really appreciate any other suggestions that could achieve the same aims above.


    Thank you in advance for your advice.

    Below is an example of my data:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(gom_r rt_g1 rt_g2 rt_g3 rt_g4 rt_g5 rt_g6 rt_g7 rt_g8)
    .3333333  1  8 30  . . . . .
         .25  1  6  8 30 . . . .
    .3333333  1  2  8 10 . . . .
    .3333333  2  1  8  . . . . .
         .25  1  2  5  8 . . . .
           .  .  .  .  . . . . .
    .3333333 24  8 29  . . . . .
          .5 22  8  .  . . . 1 2
    .3333333 22  8 29  . . . 1 2
    .3333333 24 13 29  . . . . .
           1  8  .  .  . . . . .
           .  .  .  .  . . . . .
    .3333333  3  5  .  . . . . .
           1  8  .  .  . . . . .
    .3333333 24  8 29  . . . . .
          .5 22  8  .  . . . 1 2
    .3333333 23  8 28  . . . . .
           .  .  .  .  . . . . .
          .5 13 29  .  . . . . .
    .3333333  3  . 26  . . . . .
    .3333333 22  8 29  . . . 1 2
           1  8  .  .  . . . . .
           .  .  .  .  . . . . .
    .3333333  3  5  .  . . . . .
          .5  5 16  .  . . . . .
           .  .  .  .  . . . . .
          .5  8 29  .  . . . . .
    .3333333 22  5 26  . . . 1 2
          .5  8 29  .  . . . . .
          .5  8 27  .  . . . . .
          .5  8 29  .  . . . . .
           1  8  .  .  . . . . .
         .25  3  5  . 26 . . . .
    .3333333  3  . 26  . . . . .
         .25  5  8 26 16 . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
         .25 24  8 29 26 . . . .
          .5  8 16  .  . . . . .
    .3333333 22  5  .  . . . 1 2
          .5  5  8  .  . . . . .
           1  5  .  .  . . . . .
         .25 24  8 13 29 . . . .
         .25 22 24  5  8 . . 1 2
           1  5  .  .  . . . . .
           .  .  .  .  . . . . .
           1  8  .  .  . . . . .
         .25 22 24  8 26 . . 1 2
           .  .  .  .  . . . . .
          .5 22  8  .  . . . 1 2
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
         .25 22 24  8 21 . . 1 2
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
    .3333333 22  5  .  . . . 1 2
    .3333333 24  8 29  . . . . .
         .25 22  3  . 26 . . 1 2
         .25 23  8 26 16 . . . .
          .5 22 26  .  . . . 1 2
          .5 22  8  .  . . . 1 2
    .3333333 24  8 34  . . . . .
    .3333333 22  8 29  . . . 1 2
          .5 22 23  .  . . . 1 2
           1  8  .  .  . . . . .
    .3333333 22  8 29  . . . 1 2
    .3333333 23  5  8  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           1  8  .  .  . . . . .
          .5  5 16  .  . . . . .
           .  .  .  .  . . . . .
          .5  5 30  .  . . . . .
           1  5  .  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           1  8  .  .  . . . . .
          .5  5 10  .  . . . . .
          .5  8  .  .  . . . . .
          .5  8 29  .  . . . . .
    .3333333  8 13 29  . . . . .
    .3333333 22  8 26  . . . 1 2
    .3333333 23  8 16  . . . . .
           1  8  .  .  . . . . .
         .25  8 13 29 31 . . . .
          .5 23  8  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
          .5  3  8  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
           1  5  .  .  . . . . .
           .  .  .  .  . . . . .
          .5  8 29  .  . . . . .
           .  .  .  .  . . . . .
           .  .  .  .  . . . . .
    end
    label values rt_g1 imdb_g
    label values rt_g2 imdb_g
    label values rt_g3 imdb_g
    label values rt_g4 imdb_g
    label values rt_g5 imdb_g
    label values rt_g6 imdb_g
    label values rt_g7 imdb_g
    label values rt_g8 imdb_g
    label def imdb_g 1 "Action", modify
    label def imdb_g 2 "Adventure", modify
    label def imdb_g 3 "Animation", modify
    label def imdb_g 5 "Comedy", modify
    label def imdb_g 8 "Drama", modify
    label def imdb_g 13 "Horror", modify
    label def imdb_g 22 "Action & Adventure", modify
    label def imdb_g 23 "Art House & International", modify
    label def imdb_g 24 "Classics", modify
    label def imdb_g 6 "Crime", modify
    label def imdb_g 10 "Fantasy", modify
    label def imdb_g 16 "Romance", modify
    label def imdb_g 26 "Science Fiction & Fantasy", modify
    label def imdb_g 27 "Lgbtq+", modify
    label def imdb_g 29 "Mystery & Suspense", modify
    label def imdb_g 30 "Mystery & Thriller", modify
    label def imdb_g 28 "Musical & Performing Arts", modify
    label def imdb_g 34 "Faith & Spirituality", modify
    label def imdb_g 21 "Western", modify
    label def imdb_g 31 "Special Interest", modify

    The pathetic -forevalue- codes I tried:
    Code:
    forvalues i = 1/34 {
        generate group`i'=1 if rt_g`i'==`i'
    replace group`i' =. if group`i' !=`i'
    }


    Thank you all very much once again!







  • #2
    Code:
    forvalues g = 1/34{
        egen group`g' = anymatch(rt_g1-rt_g8), values(`g')
        bysort group`g': egen contrast`g' = mean(gom_r) if group`g' == 1
    }
    Question 2 cannot be answered unless we know what kind of analysis it's going to be.

    Comment


    • #3
      Thank you very much for your help Ken Chui !

      Comment


      • #4
        If interested please follow https://www.statalist.org/forums/for...ch-or-forvalue

        Comment

        Working...
        X