Announcement

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

  • Help needed with twoway graphing with a criterion

    Hello

    I'm having some challenges in making a twoway line graph that has a criterion. Y variable = a respondents score in a survey (q2_score). X variable = when the respondent answered the survey ("hour"). The criterion i want is to show it based on different demographics like gender and age. I've looked into it and this is the options i've tried:

    graph twoway (line q2_score hour) if gender == 1 --> 1 = man. When i submit this i get exactly the same line graph i would get if i just typed "graph twoway (line q2_score hour)".

    I also typed the code:

    graph twoway (line q2_score hour), by gender. This gives me the two line graphs next to eachother by gender, but the line graphs are unchanged, or just the same as if i would type "(line q2_score hour)" as shown below.

    Click image for larger version

Name:	Picture1.png
Views:	1
Size:	700.9 KB
ID:	1698269


    Anyone know how to fix this such that the graphs are based on the criterion/criteria?

    My other option is to make datasets based exclusively on the different ranges of age and one for man and one for woman, but there are also other parameters i would like to use as criterions, and i really hope there is an easier way to do it than to make 10's of different datasets.

    Tor

  • #2
    The qualifier

    Code:
    if gender == 1
    should make no difference to a graph if and only if there are no other values of gender for which the other variables are non-missing.

    The graph you show is even more bizarre.

    Hence I am puzzled too and have nothing to suggest in the absence of a data example that shows the problem reproducibly.

    Comment


    • #3
      I am probably explaining the problem very poorly. I've provided a dataexample of the variables that are involved.

      dataex gender q2_score hour
      clear
      input long gender float(q2_score hour)
      1 2.689252 16
      0 2.689252 16
      1 2.671615 17
      0 2.689252 16
      0 2.6821506 12
      0 2.643651 19
      0 2.6976414 13
      0 2.6976414 13
      0 2.6821506 12
      1 2.6312926 20
      0 2.6549544 22
      1 2.607183 18
      1 2.674876 15
      0 2.689252 16
      1 2.671615 17
      1 2.702945 11
      0 2.671615 17
      1 2.702945 11
      1 2.669734 10
      1 2.6976414 13
      0 2.6549544 22
      1 2.689252 16
      1 2.689252 16
      0 2.632322 7
      0 2.737732 14
      1 2.638225 9
      0 2.669734 10
      0 2.689252 16
      1 2.643651 19
      0 2.6976414 13
      0 2.671615 17
      1 2.737732 14
      0 2.638225 9
      0 2.6549544 22
      0 2.700603 8
      1 2.631273 21
      0 2.671615 17
      0 2.6549544 22
      0 2.6821506 12
      0 2.671615 17
      0 2.689252 16
      0 2.700603 8
      1 2.669734 10
      1 2.689252 16
      0 2.700603 8
      1 2.671615 17
      1 2.674876 15
      0 2.638225 9
      0 2.685225 0
      0 2.702945 11
      1 2.702945 11
      1 2.6821506 12
      0 2.6976414 13
      0 2.689252 16
      0 2.6549544 22
      0 2.697556 23
      1 2.669734 10
      1 2.607183 18
      0 2.6976414 13
      0 2.638225 9
      1 2.689252 16
      0 2.669734 10
      0 2.702945 11
      1 2.737732 14
      0 2.669734 10
      1 2.737732 14
      0 2.638225 9
      1 2.6821506 12
      0 2.702945 11
      0 2.643651 19
      0 2.6976414 13
      0 2.638225 9
      1 2.737732 14
      1 2.702945 11
      0 2.6976414 13
      0 2.632322 7
      0 2.6549544 22
      1 2.638225 9
      1 2.702945 11
      0 2.638225 9
      1 2.638225 9
      1 2.6821506 12
      0 2.643651 19
      0 2.6821506 12
      0 2.674876 15
      1 2.689252 16
      1 2.674876 15
      0 2.6976414 13
      1 2.689252 16
      1 2.6976414 13
      0 2.689252 16
      0 2.6821506 12
      1 2.737732 14
      0 2.643651 19
      1 2.638225 9
      1 2.689252 16
      0 2.671615 17
      0 2.702945 11
      0 2.689252 16
      0 2.700603 8
      end
      label values gender label_gender
      label def label_gender 0 "Mann", modify
      label def label_gender 1 "Kvinne", modify
      [/CODE]

      Btw, just saw that 1 = woman, and not man, as I wrote in #1.

      Does this dataexample provide any clarity to the issue?

      Comment


      • #4
        Thanks for the data example. I ran this code on your data example


        Code:
        line q2_score hour, name(G1, replace) sort 
        
        line q2_score hour if gender == 1 , name(G2, replace) sort 
        
        line q2_score hour if gender == 0, sort || line q2_score hour if gender == 1, sort legend(order(1 "Mann" 2 "Kvinne")) name(G3, replace)
        
        line q2_score hour, by(gender) sort name(G4, replace)
        noting that the sort option is needed in each case and that by gender is illegal.


        I can't reproduce either of the problems reported in #1.

        Comment


        • #5
          Thanks, Nick. Your code made me realize where the error lies (other than my own stupidity); The variable q2_score is an average of what all the respondents score is within different time intervals. It was created by the code you helped me with some days ago:

          egen q2_score = mean(cond(q2 < ., inlist(q2 , 3), .)), by(hour)

          The issue is that the variable q2_score does not take into account f.ex gender and by default cant be used to see the difference between genders or other demographic differences, such as age. The way i need to construct the variable is such that it considers demographic aspects as well.

          How would I go about changing "egen q2_score = mean(cond(q2 < ., inlist(q2 , 3), .)), by(hour)" such that it incorporates gender?

          Said in "human" terms the code would be: generate q2_score_women, where q2_score is the mean of the values of q2 that are equal to 3 and the values of gender that are equal to 1, based on the different time intervals in the variable "hour". The same would go for men just that the 1 in gender is changed to 0.

          Comment


          • #6
            Hi again. After alot of trial and error/searching it seems like the code below worked (in case someone else has the same problem):

            egen q2_score_women = mean(cond(q2 < ., inlist(q2 , 3), .)), by(hour), if gender == 1
            egen q2_score_men = mean(cond(q2 < ., inlist(q2 , 3), .)), by(hour), if gender == 0

            I cross referenced this by taking an exclusive dataset with only men and women and got the same numbers.

            With these new variables one can type the code for getting the different graphs.

            Comment


            • #7
              Code:
              egen q2_score = mean(cond(q2 < ., q2 == 3, .)), by(hour gender)
              holds the same information. Two separate variables aren't obviously advantageous as each is missing when the other is non-missing.
              Last edited by Nick Cox; 24 Jan 2023, 02:19.

              Comment


              • #8
                Thats a good point, thanks again, Nick. Also, i tried the code, but i got an error in the "cond term"; ";q2 invalid name". But, it worked when i typed:

                egen q2_score = mean(cond(q2 < ., inlist(q2, 3), .)), by(hour gender)
                Last edited by Tor Haug Anonsen; 24 Jan 2023, 02:39.

                Comment


                • #9
                  I think you used an early version of #8 with typos. I fixed it 2 minutes later. Sorry about that


                  inlist(q2, 3) isn't wrong but q2 == 3 is more idiomatic in my view.

                  Comment

                  Working...
                  X