Announcement

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

  • Calculate means grouped by two variables + graph

    Hi statalist,

    I have the following data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte period float(CAR CPR CR) str3 t
    1     -.02021601   .005210651    -.01500536 "t-1"
    1     -.03061243    .00918705    -.02142538 "t"  
    1     -.11210155   .009524055     -.1025775 "t+1"
    1     -.11347393    .02162349    -.09185044 "t+2"
    1     -.11637095    .00683183    -.10953912 "t+3"
    1   -.0018904363    .00448447    .002594034 "t-1"
    1    -.014493093   .006737838   -.007755255 "t"  
    1     -.09078356    .01080608    -.07997748 "t+1"
    1      -.1397632   .018221796     -.1215414 "t+2"
    1      -.1571942   .021319807    -.13587438 "t+3"
    1     .005424537 -.0004152604    .005009276 "t-1"
    1     .010793898   .006214639    .017008537 "t"  
    1     -.04457002    .01086714    -.03370288 "t+1"
    1      -.0459513   .017052412   -.028898884 "t+2"
    1     -.04517059    .02965859   -.015512004 "t+3"
    1    -.006254043  .0010318209   -.005222222 "t-1"
    1    -.019780234  .0036119604   -.016168274 "t"  
    1     -.05698282    .00162765    -.05535517 "t+1"
    1     -.05848353  .0031283556    -.05535517 "t+2"
    1     -.05106682  -.008519663    -.05958648 "t+3"
    1     -.01415645    .00543179    -.00872466 "t-1"
    1     .005807986  -.004695732   .0011122543 "t"  
    1     -.04255267   .008033036   -.034519635 "t+1"
    1     -.02918768  .0045032366    -.02468444 "t+2"
    1    -.074155495   .023411566    -.05074393 "t+3"
    2     .015609624  -.011002962    .004606662 "t-1"
    2     .025362447  -.011937443    .013425004 "t"  
    2     .007376657  -.005140465   .0022361926 "t+1"
    2       .0076771  -.005264105    .002412996 "t+2"
    2    -.012599418 -.0010738522    -.01367327 "t+3"
    2     .004836714   .003280169    .008116883 "t-1"
    2 -.000017350074    .01296515    .012947802 "t"  
    2    -.031817116   .012713633    -.01910348 "t+1"
    2    -.033317823   .014214338    -.01910348 "t+2"
    2    -.031964935    .02196741   -.009997522 "t+3"
    2     .025743484  -.012268309    .013475177 "t-1"
    2       .0375024  -.004433104    .033069298 "t"  
    2      .03093266 -.0012950723     .02963759 "t+1"
    2     .018820334   -.01604225    .002778086 "t+2"
    2    -.008723274  -.010437777    -.01916105 "t+3"
    2    -.027900886    .04784885     .01994796 "t-1"
    2     -.04683151    .05104818    .004216669 "t"  
    2     -.07213166     .0642533   -.007878363 "t+1"
    2     -.05838835    .05531978  -.0030685687 "t+2"
    2     -.05894822    .05849062 -.00045760255 "t+3"
    2    -.012607371  .0043054847   -.008301887 "t-1"
    2     -.02076536    .00561416   -.015151202 "t"  
    2     -.01978863   .007702563   -.012086067 "t+1"
    2    -.029750166   .016136214    -.01361395 "t+2"
    2    -.027605765    .01246159   -.015144173 "t+3"
    end
    I want to calculate the means of the variables CAR CPR and CR for all values that have the same value in variable t and subclassified by period 1 and period 2.

    The result should look like that:
    period t mean CAR mean CPR mean CR
    1 t-1
    1 t
    1 t+1
    1 t+2
    1 t+3
    2 t-1
    2 t
    2 t+1
    2 t+2
    2 t+3

    After that I want to put it into a graph.
    on the x-axis should be t (from t-1 to t+3)
    on the y-axis should be the means
    I want to have for each of the 3 means a line with different line patterns. CAR should be a solid line, CPR should be a dashed line and CR should be a dotted line.

    Any recommendations how to solve my problem?

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte period float(CAR CPR CR) str3 t
    1     -.02021601   .005210651    -.01500536 "t-1"
    1     -.03061243    .00918705    -.02142538 "t"  
    1     -.11210155   .009524055     -.1025775 "t+1"
    1     -.11347393    .02162349    -.09185044 "t+2"
    1     -.11637095    .00683183    -.10953912 "t+3"
    1   -.0018904363    .00448447    .002594034 "t-1"
    1    -.014493093   .006737838   -.007755255 "t"  
    1     -.09078356    .01080608    -.07997748 "t+1"
    1      -.1397632   .018221796     -.1215414 "t+2"
    1      -.1571942   .021319807    -.13587438 "t+3"
    1     .005424537 -.0004152604    .005009276 "t-1"
    1     .010793898   .006214639    .017008537 "t"  
    1     -.04457002    .01086714    -.03370288 "t+1"
    1      -.0459513   .017052412   -.028898884 "t+2"
    1     -.04517059    .02965859   -.015512004 "t+3"
    1    -.006254043  .0010318209   -.005222222 "t-1"
    1    -.019780234  .0036119604   -.016168274 "t"  
    1     -.05698282    .00162765    -.05535517 "t+1"
    1     -.05848353  .0031283556    -.05535517 "t+2"
    1     -.05106682  -.008519663    -.05958648 "t+3"
    1     -.01415645    .00543179    -.00872466 "t-1"
    1     .005807986  -.004695732   .0011122543 "t"  
    1     -.04255267   .008033036   -.034519635 "t+1"
    1     -.02918768  .0045032366    -.02468444 "t+2"
    1    -.074155495   .023411566    -.05074393 "t+3"
    2     .015609624  -.011002962    .004606662 "t-1"
    2     .025362447  -.011937443    .013425004 "t"  
    2     .007376657  -.005140465   .0022361926 "t+1"
    2       .0076771  -.005264105    .002412996 "t+2"
    2    -.012599418 -.0010738522    -.01367327 "t+3"
    2     .004836714   .003280169    .008116883 "t-1"
    2 -.000017350074    .01296515    .012947802 "t"  
    2    -.031817116   .012713633    -.01910348 "t+1"
    2    -.033317823   .014214338    -.01910348 "t+2"
    2    -.031964935    .02196741   -.009997522 "t+3"
    2     .025743484  -.012268309    .013475177 "t-1"
    2       .0375024  -.004433104    .033069298 "t"  
    2      .03093266 -.0012950723     .02963759 "t+1"
    2     .018820334   -.01604225    .002778086 "t+2"
    2    -.008723274  -.010437777    -.01916105 "t+3"
    2    -.027900886    .04784885     .01994796 "t-1"
    2     -.04683151    .05104818    .004216669 "t"  
    2     -.07213166     .0642533   -.007878363 "t+1"
    2     -.05838835    .05531978  -.0030685687 "t+2"
    2     -.05894822    .05849062 -.00045760255 "t+3"
    2    -.012607371  .0043054847   -.008301887 "t-1"
    2     -.02076536    .00561416   -.015151202 "t"  
    2     -.01978863   .007702563   -.012086067 "t+1"
    2    -.029750166   .016136214    -.01361395 "t+2"
    2    -.027605765    .01246159   -.015144173 "t+3"
    end
    
    gen time= real(cond(t=="t", "0", substr(t, -2, 2))) + 2
    lab def t 1 "t-1" 2 "t" 3 "t+1" 4 "t+2" 5 "t+3"
    lab values time t
    collapse CAR CPR CR, by(period time)
    xtset period time
    set scheme s1mono
    xtline CAR CPR CR, lp(solid dash dot) xlab(, val) xtitle("") leg(order(1 "CAR" 2 "CPR" 3 "CR") row(1)) byopts(note(""))
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.6 KB
ID:	1669935

    Last edited by Andrew Musau; 19 Jun 2022, 06:29.

    Comment

    Working...
    X