Stata Learning From Treiman


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Stata Learning From Treiman

  1. 1. Doing analysis using stata 10.0stata作图(续)<br />Treiman<br />Ucla<br />易黠<br />于善国家中<br />2009.2.18<br />
  2. 2. 目录<br />
  3. 3. Stata, created by Statacorp, is a statistical program used by many businesses and academic institutions around the world. Most of its users work in research, especially in the field of economics and epidemiology.<br />Stata's full range of capabilities includes: <br />Why I need it?<br />Data management<br />Statistical analysis<br />Graphics<br />Simulations<br />Custom Programming<br />
  4. 4. Do Everything with -do- Files<br />capture log close<br />log using class.log,replace<br />#delimit;<br />version 10.0;<br />set more 1;<br />clear;<br />program drop _all;<br />set mem 100m;<br />*CLASS.DO (DJT initiated 5/19/99, last revised 2/4/08);<br />*This do-file creates computations for a paper on literacy in<br />China.;<br />use d:chinasurveydatachina07.dta;<br />log close;<br />
  5. 5. Document Your -do- File Exhaustively<br />the editorial review process often takes a very long time. If you have not documented your work, you may have a great deal of trouble remembering why you have done what you have done.<br />include comments summarizing the outcome of each set of commands<br />
  6. 6. 目录<br />
  7. 7. Chapter7 gssy2004case 1 curvilinear relationship<br />reg inc age agesq if good==1<br />My method:<br />gen inc1=3088.804*age-27.77605*agesq-15554.28<br />
  8. 8. 我的作图的确有问题,看看Treiman的图<br />
  9. 9. *Mark the good data.;<br />mark good if inc~=. & age>19 & age<65;<br />*Do the regression and make a predicted value.;<br />reg inc age agesq if good==1;<br />*Get the transformed coefficients.;<br />gen m=_b[_cons]-(_b[age]^2)/(4*_b[agesq]);<br />gen F=(-_b[age])/(2*_b[agesq]);<br />l m F in 1;<br />仅仅list 一个对应的m和F<br />
  10. 10. 严格的作图syntax<br />Not simple<br />lab var age "Age in 2004"<br /> lab varxinc "Expected Income in 2003"<br />. graph twoway (scatter xincage,sort connect(l) clwidth(medthick) clpattern(solid) mcolor(black) msymbol(i)), plotregion<br />> (style(none)) xlab(20(4)64) ylab(0(5000)50000) saving(ch07fig1.gph,replace)<br />定义坐标<br />Lab=label,例如将age标记为age in 2004<br />Sort connect(l)直线连接?<br />Clwidth?<br />Clpattern?<br />Mcolor?<br />ms(I)<br />Plotregion?<br />
  11. 11. 记住这个格式grtw (sc xinc age, sort connect(l) ms(i))sort前空格,tw后空格。<br />我用简化的graph tw(sc xinc age,sort connect(l))作图<br />怎么去掉这些点?只需要加上<br />ms(i)即可。<br />
  12. 12. Case 2 二分类变量、平均值、相互控制reglninceduc hrs male if good==1;<br />*Make graphs of the relationship between education and income, by sex, for those who work an average number of hours.;<br />显然大家都在用平均值来处理这种变量的交互叠加的效果。<br />*First, get the expected values evaluated at mean hours.;<br />*Get the mean hours worked, which I need below.;<br />sum hrs if good==1;<br />gen mhrs=r(mean) if good==1;<br />跟我们的做法如出一辙:都是获取相应的mean。<br />Omg~~~~这个程序写起来也不是那么顺畅。一次只能return一个mean。<br />gen xincm=_b[_cons]+_b[educ]*educ+_b[hrs]*mhrs+_b[male] if male==1 & good==1;<br />Gen xincf=_b[_cons]+_b[educ]*educ+_b[hrs]*mhrs if male==0 & good==1;<br />让你写出来这么多东西,也是一种痛苦吧。So many –b[],torture~~ <br />
  13. 13. Sum 、gen、return命令组合获取均值的方法<br />
  14. 14. graph twoway (scatter xincmeduc, sort connect(l) clwidth(medthick) clpattern(solid) mcolor(black) msymbol(i))<br />(scatter xincfeduc, sort connect(l) clwidth(medthick) clpattern(solid) mcolor(black) msymbol(O)), plotregion(style(none)) <br />legend ( label(1 "Males") label(2 "Females") cols(1) ring(0) position(11)) xlab(0(4)20) ylab(8(1)11) xtick(1(1)20) ytick(8(.25)11.5) l1("Expected ln(Income) in 2003") saving(ch07fig2.gph,replace)<br />
  15. 15. I hate to clean the syntax<br />
  16. 16. Predict命令<br />case3<br />没想到吗,随处可见的平方<br />sysuse auto, clear<br />generate weight2 = weight^2<br />regress mpg weight weight2 foreign<br />webusenewautos, clear<br />generate weight2 = weight^2<br />*Obtain out-of-sample prediction using another dataset<br />predict mpg<br />but,how stata user know that?<br />
  17. 17. Sysuse auto,cleargenerate weight2 = weight^2regress mpg weight weight2 foreign. gen mpgf=_b[_cons]+_b[weight]*weight+_b[weight2]*weight2+_b[foreign]. gen mpgd=_b[_cons]+_b[weight]*weight+_b[weight2]*weight2. grtw (sc mpgf weight, sort connect(l) ms(i)) (sc mpgd weight, sort connect(l) ms(i))<br />绘图<br />Lowess mpg weight<br />grtw (sc mpgf weight, sort connect(l) ms(i))<br /> (sc mpgd weight, sort connect(l) ms(i)),<br />legend ( label(1 "foreign") label(2 "domestic"))<br />Stata探测之方法<br />Lowess命令<br />
  18. 18. Edit the graph<br />
  19. 19. Set scheme economistgrtw (sc mpgf weight, sort connect(l) ms(i)) (sc mpgd weight, sort connect(l) ms(i)) ,legend(label(1 "foreign") label (2 "domestic")) ytitle("y=mileage") xtitle("x=weight")<br />
  20. 20. 目录<br />
  21. 21. Lowess命令——加权回归<br />Lowess is a statistical technique for plotting a smooth curve through a set of data points in a scattergram.lowess carries out a locally weighted regression of yvar on xvar, displays the graph, and optionally saves the smoothed variable.<br />A scattergram is a plot of various data points in a graph with a predictor variable as its x-axis and a criterion variable as its y-axis.<br />Lowess is a version of a locally weighted scatterplotsmoothing technique. Each smoothed value is determined by a linear polynomial taking into account the values of data within a particular span of values of the criterion variable, but giving most weight to the central value of the span, less and less weight to more distant values, and zero weight to values outside the span. The span is then moved along the x-axis and a new smoothed value computed. The size of the span is set by a tension factor determining the proportion of the data points to be included in the span.<br />Warning: lowess is computationally intensive and may therefore take a long time to run on a slow computer. Lowess calculations on 1,000 observations, for instance, require performing 1,000 regressions.<br />
  22. 22. 案例1 :手机上网频率与个人月收入之间的关系grtw (sc b2am inc,sort connect(l) ms(i)),ytitle(“y= b2am=_b[_cons]+_b[inc]*inc+_b[inc2]*inc2”) xtitle(“x=inc”)<br />
  23. 23. 案例2China 07.dta<br />grtwlowessnewsnow income<br />Lowessnewsnow income<br />
  24. 24. 其实办法都差不多<br />Ms大家都这样拿平均值开刀,没有什么神秘的地方,不同的是Treiman的do.file的帮助很大。让你完整的复现他的工作。这是很了不起的见解。<br />lawrence.C.Hamilton<br />STATISTICS WITH STATA<br />
  25. 25. Use graph combine graph to compare different x variable effect<br />Graph combine fig08_11.gph fig08_12.gph, ycommon cols(2) scale(1.25)<br />
  26. 26. 目录<br />
  27. 27. Nl命令——我不喜欢,太死板了。<br />
  28. 28. 案例1<br />nl exp2 y1 x<br />predict yhat1<br />grtw sc y1 x<br />line yhat1 x, sort<br />legend(off) ytitle(“y1=10*1.03^x+e”) xtitle(“x”)<br />
  29. 29. 案例2<br />