楼主: 匿名
20350 212

[Stata] [Stata] 如何添加直角坐标系及极坐标系及SDAS_绘图实例 [推广有奖]

81
niuniuyiwan 在职认证  发表于 2015-8-31 09:37:48
  1. //Predictive margins with 95% confidence interval - using margins

  2. /*Requires: parmest
  3. to download this program type the following on the Stata command
  4. line (if not already loaded):*/
  5. ssc install parmest

  6. sysuse auto, clear
  7. gen kprice=price/1000
  8. regress  mpg c.kprice##c.kprice i.foreign i.rep78 turn trunk headroom

  9. margins, at(kprice=(3/15)) vsquish post

  10. return list
  11. ereturn list

  12. parmest, norestore  //saves parameters
  13. l
  14. gen kprice=3 + _n - 1
  15. l
  16. label var kprice "Price(\$1000)"

  17. twoway ///
  18. (rarea max95 min95 kprice, pstyle(ci)) ///
  19. (line estimate kprice), ///
  20. ytitle(Miles per gallon) ///
  21. title(Predictive margins with 95% confidence interval) ///
  22. xlabel(3(3)15 10.4) legend(off)
复制代码

Graph.png



82
niuniuyiwan 在职认证  发表于 2015-8-31 09:39:32
  1. //Paired Plot 2
  2. //Looking at the effect of treatment on the weight of anorexic patients.

  3. //Making up the data; based on the data found at:
  4. //http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt

  5. clear
  6. set obs 76
  7. gen treatment=mod(_n,3)+1

  8. gen w_before1=rnormal(82, 4.8)
  9. replace w_before1=rnormal(81, 5.7)  if treatment==2
  10. replace w_before1=rnormal(83, 5)    if treatment==3

  11. gen w_after1=rnormal(85.6, 8.3)
  12. replace w_after1=rnormal(81.1, 4.7) if treatment==2
  13. replace w_after1=rnormal(90, 5.4)   if treatment==3
  14. //end of making up the data

  15. label var w_before " Weight before treatment, lb"
  16. label var w_after  " Weight after treatment, lb"
  17. label define treat ///
  18.         1  "Cognitive behavioural" ///
  19.         2  "Control"               ///
  20.         3  "Family therapy"
  21.                
  22. label values treatment treat
  23.        
  24. gen byte one = 1
  25. gen byte two = 2       
  26.        
  27. twoway ///
  28. pcspike w_before one w_after two if w_before <= w_after, ///
  29. lcolor(gs12) ||                                          ///
  30. pcspike w_before one w_after two if w_before > w_after,  ///
  31. lw(*1.2) lcolor(gs2) xla(1 " before" 2 "after ")         ///
  32. xtitle("") yla(, nogrid ang(h)) ytitle("weight, lb")     ///
  33. by(treatment, row(1) note("") legend(off) noixtick)       
复制代码

Graph.png



83
niuniuyiwan 在职认证  发表于 2015-8-31 09:41:02
  1. //Paired Plot 3
  2. //Looking at the effect of treatment on the weight of anorexic patients.

  3. //Making up the data; based on the data found at:
  4. //http://users.polisci.wisc.edu/kritzer/Teaching/ps551/anorexia.txt

  5. clear
  6. set obs 76
  7. gen treatment=mod(_n,3)+1

  8. gen w_before1=rnormal(82, 4.8)
  9. replace w_before1=rnormal(81, 5.7)  if treatment==2
  10. replace w_before1=rnormal(83, 5)    if treatment==3

  11. gen w_after1=rnormal(85.6, 8.3)
  12. replace w_after1=rnormal(81.1, 4.7) if treatment==2
  13. replace w_after1=rnormal(90, 5.4)   if treatment==3
  14. //end of making up the data

  15. label var w_before " Weight before treatment, lb"
  16. label var w_after  " Weight after treatment, lb"
  17. label define treat ///
  18.         1  "Cognitive behavioural" ///
  19.         2  "Control"               ///
  20.         3  "Family therapy"
  21.                
  22. label values treatment treat
  23.        

  24. bysort treatment (w_before w_after) : gen order1 = _n - _N/2
  25.        
  26. twoway                                                   ///
  27. pcarrow w_before order1 w_after order1, pstyle(p1)       ///
  28. || scatter w_before order1, pstyle(p1) ms(o)             ///
  29. xla(none) xtitle("") yla(, ang(h)) ytitle("weight, lb")  ///
  30. by(treatment, row(1) note("") legend(off))
  31.        
复制代码

Graph.png



84
niuniuyiwan 在职认证  发表于 2015-8-31 09:42:22
  1. // Adding labels - standard error bar chart

  2. webuse assembly, clear

  3. generate upper=mean+2*std

  4. serrbar mean std date, scale(2) yline(195)              ///
  5. addplot( scatter  up date, msymbol(none) mlabel(upper))
复制代码

Graph.png



85
niuniuyiwan 在职认证  发表于 2015-8-31 09:43:38
  1. // Odds ratio graph - selecting order of bars

  2. ** requires 2 user written graphs: parmest and egenmore
  3. ** if not already installed:

  4. ssc install parmest
  5. ssc install egenmore

  6. sysuse nlsw88, clear
  7. logit union married never_married collgrad south

  8. parmest, norestore eform

  9. tab estimate, gen(kk)

  10. //order of bars
  11. input a
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5

  17. egen axis= axis( a estimate), label(parm)

  18. twoway bar  estimate axis, base(1) horizontal barw(.5) ///
  19. xline(1) ylab(, valuelabel angle(0)) ||                ///
  20. rcap min95 max95 axis, horizontal                      ///
  21. legend(order(1 "point estimate"                        ///
  22. 2 "95% conf. int.") pos(6))                            ///
  23. xtitle("odds ratio") ytitle("") scheme(color)

  24. *(Maarten L. Buis - Statalist,  14/09/2010)
复制代码

Graph.png



86
niuniuyiwan 在职认证  发表于 2015-8-31 09:45:06
  1. // Odds ratio graph

  2. ** requires 2 user written graphs: parmest and egenmore
  3. ** if not already installed:

  4. ssc install parmest
  5. ssc install egenmore

  6. sysuse nlsw88, clear
  7. logit union married never_married collgrad south

  8. parmest, norestore eform
  9. egen axis= axis(estimate), label(parm)

  10. twoway scatter   axis estimate, ///
  11.             xline(1) ylab(, valuelabel angle(0)) ||    ///
  12.        rcap min95 max95 axis, horizontal               ///
  13.             legend(order(1 "point estimate"            ///
  14.                          2 "95% conf. int.") pos(6))   ///
  15.             xtitle("odds ratio") ytitle("")
复制代码

Graph.png



87
niuniuyiwan 在职认证  发表于 2015-8-31 10:41:28
  1. // Table on the side of a graph

  2. sysuse auto, clear
  3. twoway (scatter mpg rep78) , graphregion( margin(r+30) )

  4. gr_edit AddTextBox added_text editor `=82+8' `=101'
  5. gr_edit added_text_new = 1
  6. gr_edit added_text_rec = 1
  7. gr_edit added_text[1].text = {}
  8. gr_edit added_text[1].text.Arrpush "Mean mpg by rep78"

  9. gr_edit AddTextBox added_text editor `=82+2' `=112'
  10. gr_edit added_text_new = 2
  11. gr_edit added_text_rec = 2
  12. gr_edit added_text[2].text = {}
  13. gr_edit added_text[2].text.Arrpush "rep78  mpg"

  14. collapse mpg, by(rep78)

  15. local z=2

  16. forvalues i=0/4 {  //rows
  17.   local ++z
  18.   gr_edit AddTextBox added_text editor `=80-(`i'*4)' `=114'
  19.   gr_edit added_text_new = `z'
  20.   gr_edit added_text_rec = `z'
  21.   gr_edit added_text[`z'].text = {}
  22.   gr_edit added_text[`z'].text.Arrpush ///
  23.   "`=rep78[`z'-2]'      `=string(mpg[`z'-2],"%8.2f")' "
  24. }
  25. exit
复制代码

Graph.png



88
niuniuyiwan 在职认证  发表于 2015-8-31 10:42:51
  1. // Table on the side of a graph

  2. sysuse auto, clear

  3. forval n = 1/20 {
  4.         loc sidetable `" `sidetable'  " `=make[`n']'  "  "'
  5.         }

  6. forval n = 1/20 {
  7.         loc sidecap `" `sidecap'  "      =price[`n']'"  "'
  8.         }
  9.        
  10. sysuse auto, clear
  11. twoway (scatter mpg rep78) ,        ///
  12. note("{bf: MAKE    }"               ///
  13. "-----------------"    `sidetable'  ///
  14. , size(medsmall) color(green)       ///
  15. position(2) margin(small)           ///
  16. justification(left))                ///
  17. caption(" {bf:   Price }"           ///
  18. "---------"    `sidecap'            ///
  19. , size(medsmall) color(midgreen)    ///
  20. position(2) margin(vsmall)          ///
  21. justification(right))
  22. exit

  23. //Based statalist Jun 9, 2011
  24. //Eric Booth
复制代码

Graph.png



89
niuniuyiwan 在职认证  发表于 2015-8-31 10:43:55
  1. // Putting Data set results in a graph

  2. clear all
  3. sysuse auto

  4. quiet: summarize mpg

  5. twoway ///
  6. scatter mpg rep78, ///
  7. text(40 3 "The mean mpg is:`=string(`=r(mean)',"%8.2f")'", placement(right)) ///
  8. text(38 3 "The sd mpg is: `=string(`=r(sd)',"%8.2f")'" ,   placement(right))

  9. exit
复制代码

Graph.png



90
niuniuyiwan 在职认证  发表于 2015-8-31 10:45:23
  1. sysuse auto, clear

  2. egen mpg1=cut(mpg), group(5) icode

  3. levelsof mpg1, local(kk)

  4. gen mean=.
  5. gen ub=.
  6. gen lb=.
  7. gen loc=.
  8. local z=1

  9. foreach i of local kk {
  10. ci for if mpg1==`i', wilson binomial

  11.   replace mean=r(mean) in `z'
  12.   replace ub=r(ub) in `z'
  13.   replace lb=r(lb) in `z'
  14.   replace loc=`i' in `z'
  15.   local ++z
  16. }

  17. label define a 0 "very poor"  1 poor  2  OK 3 good  4 vgood
  18. label value  loc a

  19. twoway (bar mean loc) (rcap ub lb loc), ///
  20. xlab(, valuelabel angle(45))            ///
  21. xtitle("Fuel Consumption")              ///
  22. ytitle(mean) legend(off)
复制代码

Graph.png



您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 18:41