Announcement

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

  • Sample weights to twoway line graph

    Dear all,

    I am trying to plot net transfers by groups in € (y-axis) against age (x-axis) in one graph
    I defined the variables where anhhgba represents the monetary annual sum for each household, then I sorted them as follows according to age and group to get the annual average:

    Code:
    bysort age group: egen grouphhgba_mean = mean(anhhgba)                                
    twoway (line grouphhgba_mean age [aw=hhrf], sort), by(group)
    I am not sure if this is the correct way to create the graph, the command above creates two but I want only one graph in which two lines should be shown for the two groups in the same graph.
    In general, is line graph the best way to plot this or would you suggest another better way?

    Regarding the weights, I tried applying different weights (aw, pw, none) all give the same result and the same graph. I searched and it seems that weights will not change the graph unless some step is made beforehand, the post however did not explain much how this could be done. Which weights should fit into this? And how to get a weighted graph in the end? (I can shift to any other graph type not necessarily a line graph)

    For some suggestions I would be grateful!

  • #2
    Once you have calculated the means, a line plot of the means is not going to differ according to any weights: indeed specifying weights is irrelevant. Specifying weights is tolerated, but ignored.

    Other way round, if you have weights you want to use to calculate the means, you need to apply them when doing so, and egen can't help there.

    I would do something like this


    Code:
    preserve 
    
    collapse anhhgba [aw=hhrf] , by(age group) 
    
    separate anhhgba, by(group) veryshortlabel 
    
    line annhhgba?  age , sort 
    
    restore
    but the exact code does depend on what separate produces as results.

    Comment


    • #3
      Dear Nick Cox, thank you so much for your help and your quick reply! This worked very well! It seems also that aw or pw produces the same graph here. I am wondering, which line in the code above calculates the means per group? I would interpret this then as the (average) annual net transfers by groups and age?. And if this is to be made for the total sums of each of the two groups (defined by 'group') rather than using the means, how could it be adjusted?,

      Comment


      • #4
        Code:
        help collapse

        Comment


        • #5
          Thanks, that is helpful!

          Comment

          Working...
          X