I am trying to graph cross-country distributions along with international averages using loops.
Suppose c25 is the 25th percentile of a country's trade volume, and i25 is the international average of trade volume for that year. Same goes for c50 being the 50th percentile by country, etc. See dataexample below (numbers are made up)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str3 Country int Year byte(c25 i25 c50 i50 c75) double i75 int(aloc ex) "GER" 2000 4 3 8 6 12 9 50 85 "FRA" 2000 3 3 6 6 9 9 20 56 "USA" 2000 2 3 4 6 6 9 40 29 "GER" 2001 5 6 10 12 15 15.333333333333334 88 90 "FRA" 2001 6 6 12 12 18 15.333333333333334 90 100 "USA" 2001 7 6 14 12 13 15.333333333333334 89 88 "GER" 2002 8 9 16 18 24 27 104 200 "FRA" 2002 9 9 18 18 27 27 203 490 "USA" 2002 10 9 20 18 30 27 194 190 end
Now I'd like to have a graph with the mean, sd, 25th percentile, 50th percenile, 75th percentile, and 90th percentile across countries. To do this, I have:
Code:
putpdf clear putpdf begin, pagesize(letter) local varlist c25 c50 c75 aloc ex foreach v of varlist `varlist'{ preserve local vlab: var lab `v' collapse mean = `v' (sd) sd = `v' (p25) p25 = `v' (p50) p50 = `v' (p75) p75 = `v' = `v', by(year) graph twoway (line mean year) (line sd year) (line p25 year) (line p50 year) (line p75 year), title("`v'") name(`v', replace) graph export "`v'.png", name(`v') as(png) replace putpdf paragraph, halign(center) putpdf image "`v'.png" restore } putpdf save "example.pdf", replace
For example, if I have the code below (adding the mean of i25, then I get what I want for the 25th percentile graph, but not the other ones).
Code:
putpdf clear putpdf begin, pagesize(letter) local varlist c25 c50 c75 aloc ex foreach v of varlist `varlist'{ preserve local vlab: var lab `v' collapse mean = `v' i25 (sd) sd = `v' (p25) p25 = `v' (p50) p50 = `v' (p75) p75 = `v' = `v', by(year) graph twoway (line mean year) (line sd year) (line p25 year) (line p50 year) (line p75 year) (line i25 year), title("`v'") name(`v', replace) graph export "`v'.png", name(`v') as(png) replace putpdf paragraph, halign(center) putpdf image "`v'.png" restore }
0 Response to Graphing averages using collapse mean and loops with same suffix
Post a Comment