Stata旧笔记整理(六)

Stata旧笔记整理(六)

之前老网站上有很多没有很好整理的笔记。之前也整理过一些,但是还有两百多篇,所以就简单汇总一下,便于检索。

a3——把日期变量转变为毫秒记的时间戳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*! 把日期时间变量转换成毫秒记的时间戳
cap prog drop a3
prog define a3
version 14.0
syntax anything(name = date) [, GENerate(string)]
gen date_czx = string(`date', "%td")
gen time_czx = tc(`=date_czx[1]' 08:00:00)-tc(1 jan 1970 08:00:00)
format time_czx %13.0f
forval i = 1/`=_N'{
replace time_czx = tc(`=date_czx[`i']' 08:00:00)-tc(1 jan 1970 08:00:00) in `i'
}
drop date_czx
if "`generate'" != ""{
ren time_czx `generate'
}
end

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
cuse 000001, c w
a3 date, gen(time)
list date time in 1/5

+---------------------------+
| date time |
|---------------------------|
1. | 1991-01-02 662774415360 |
2. | 1991-01-03 662860791808 |
3. | 1991-01-04 662947168256 |
4. | 1991-01-07 663206428672 |
5. | 1991-01-09 663379181568 |
+---------------------------+

aaplot——绘制拟合图

1
2
3
4
5
sysuse auto, clear
gen gpm = 1000 / mpg
label var gpm "Gallons per thousand miles"
* 默认线性拟合
aaplot gpm weight, name(g1)

  • lopts()表示使用tw lfit的选择项
  • aformat(), bformat(), cformat(), rsqformat() and rmseformat() tune the display format of the constant, the coefficient of the linear term, the coefficient of the quadratic term (if any), the R-square statistic (given as a percent) and the RMSE. The defaults are, respectively, %7.0g, %7.0g, %7.0g, %3.1f and the display format of yvar.
1
aaplot gpm weight, lopts(lc(blue)) aformat(%04.3f) bformat(%06.5f) rmseformat(%4.3f) name(g2)

  • qopts()表示使用tw qfit的选项
1
aaplot gpm weight, quadratic qopts(lc(pink)) name(g3)

  • both:同时绘制线性拟合和二次拟合:
1
aaplot gpm weight, both name(g4)

  • backdrop(addplot_option) provides a way to add other plots to the generated graph, but as backdrop plotted before and below all other plots. For example, many users like to draw shaded areas showing confidence intervals, but showing all data points too.
1
aaplot gpm weight, both backdrop(lfitci gpm weight, color(gs12)) name(g5)

addplot——添加新图层

  • Basic example:
    1
    2
    3
    4
    5
    sysuse auto, clear
    sc price mpg
    addplot: sc price mpg if price > 15000, ms(i) mlabel(make) leg(off)
    addplot: pcarrowi 14000 23 15500 21.5 "Expansive var"
    addplot: (line price mpg if mpg > 30) (dropline price mpg if price < 3300)

  • Working with named graphs:
    1
    2
    3
    4
    sysuse auto, clear
    sc price mpg, name(g1, replace)
    sc price weight, name(g2, replace)
    addplot g1: , title(price by mpg)

1
addplot g2: , title(price by weight)

  • Modifying a graph from file:
    1
    2
    3
    4
    5
    6
    7
    sysuse auto
    scatter price mpg
    graph save mygraph.gph
    graph drop Graph
    graph use mygraph.gph
    addplot mygraph: pcarrowi 14000 23 15500 21.5 "expensive car", legend(off)
    erase mygraph.gph

  • Addressing subgraphs in a by-graph:
    1
    2
    3
    4
    5
    sysuse auto
    scatter price mpg, by(foreign)
    addplot: , xline(21.3, lp(dash)) yline(6165, lp(dash)) norescaling
    addplot 1: , xline(19.8) yline(6072) norescaling
    addplot 2: , xline(24.8) yline(6385) norescaling

arrowplot——绘制分组趋势

  • arrowplot creates graphs showing inter-group and intra-group variation by overlaying arrows for intra-group (regression) trends on a scatter plot. This is similar to the well known Stevenson-Wolfers happiness graphs. arrowplot requires the definition of a yvar and xvar (plotted on the y- and x-axis respectively), as well as the definition of a groupvar which is the group over which the plot should be made (for example: country, region, industry).

Plot inter- and intra-industry wage versus education using NLSW survey.

1
2
3
4
5
webuse nlswork, clear
gen ind= "industry "
egen industry = concat(ind ind_code)
arrowplot ln_wage grade, groupvar(industry)
gr export arrowplot1.png, replace

Plotting inter- and intra-industry wage versus education conditonal on an individual’s union status and tenure.

1
2
arrowplot ln_wage grade, groupvar(industry) controls(union tenure)
gr export arrowplot2.png, replace

Plotting the same graph without missings, and with graph titles, labels, etc.

1
2
3
4
5
6
7
8
arrowplot ln_wage grade if ind_code!=., ///
groupvar(industry) ///
controls(union tenure) ///
title(Education and Log Wage) ///
subtitle(NLS 1968: Women 14-26) ///
xtitle(Grade of Education) ///
ytitle(Log Wage)
gr export arrowplot3.png, replace

ascii

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
. ascii
33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 ,
45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8
57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D
69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P
81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \
93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h
105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t
117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127  128 €
129  130 ‚ 131 ƒ 132 „ 133 … 134 † 135 ‡ 136 ˆ 137 ‰ 138 Š 139 ‹ 140 Œ
141  142 Ž 143  144  145 ‘ 146 ’ 147 “ 148 ” 149 • 150 – 151 — 152 ˜
153 ™ 154 š 155 › 156 œ 157  158 ž 159 Ÿ 160   161 ¡ 162 ¢ 163 £ 164 ¤
165 ¥ 166 ¦ 167 § 168 ¨ 169 © 170 ª 171 « 172 ¬ 173 ­ 174 ® 175 ¯ 176 °
177 ± 178 ² 179 ³ 180 ´ 181 µ 182 ¶ 183 · 184 ¸ 185 ¹ 186 º 187 » 188 ¼
189 ½ 190 ¾ 191 ¿ 192 À 193 Á 194 Â 195 Ã 196 Ä 197 Å 198 Æ 199 Ç 200 È
201 É 202 Ê 203 Ë 204 Ì 205 Í 206 Î 207 Ï 208 Ð 209 Ñ 210 Ò 211 Ó 212 Ô
213 Õ 214 Ö 215 × 216 Ø 217 Ù 218 Ú 219 Û 220 Ü 221 Ý 222 Þ 223 ß 224 à
225 á 226 â 227 ã 228 ä 229 å 230 æ 231 ç 232 è 233 é 234 ê 235 ë 236 ì
237 í 238 î 239 ï 240 ð 241 ñ 242 ò 243 ó 244 ô 245 õ 246 ö 247 ÷ 248 ø
249 ù 250 ú 251 û 252 ü 253 ý 254 þ 255 ÿ

asciiplot

1
2
3
4
5
6
7
8
9
ssc install asciiplot
* 绘图
asciiplot
gr export asciiplot.png, replace
* ASCII码的使用:
clear all
set obs 1
gen a = `"`=char(34)'"'
gen b = `"`=char(247)'"'

assert命令——检验一个语句的正确性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
. use census2b, clear
(Version of census2a for data validation purposes)

. list if !inrange(pop, 300000, 3e7)

+-------------------------------------------+
| state region pop medage drate |
|-------------------------------------------|
4. | Arkansas South -9 30.6 99 |
10. | Georgia South . 28.7 81 |
15. | Iowa N Cntrl 0 30 90 |
+-------------------------------------------+

. assert inrange(pop, 300000, 3e7)
3 contradictions in 50 observations
assertion is false
r(9);

.
. list if !inrange(medage, 20, 50)

+-------------------------------------------------+
| state region pop medage drate |
|-------------------------------------------------|
7. | Connecticut NE 3107576 321 84 |
+-------------------------------------------------+

. assert inrange(medage, 20, 50)
1 contradiction in 50 observations
assertion is false
r(9);

.
. list if !inrange(drate, 10, 104+145)

+---------------------------------------------+
| state region pop medage drate |
|---------------------------------------------|
9. | Florida South 9746324 34.7 1107 |
+---------------------------------------------+

. assert inrange(drate, 10, 104+145)
1 contradiction in 50 observations
assertion is false
r(9);

.
. duplicates report state

Duplicates in terms of state

--------------------------------------
copies | observations surplus
----------+---------------------------
1 | 48 0
2 | 2 1
--------------------------------------

. assert r(unique_value) == r(N)
assertion is false
r(9);

柱形图

Y变量

1
2
* Y变量
vguse nlsw.dta, clear
1
gr bar ttl_exp

1
gr bar prev_exp tenure ttl_exp

1
gr bar (median) prev_exp tenure (mean) ttl_exp

ascategory

1
gr bar prev_exp tenure ttl_exp hours, ascategory

over()

1
gr bar prev_exp tenure, over(occ5)

percentages

1
gr bar prev_exp tenure, over(occ5) percentages

多个over

1
gr hbar wage, over(occ5) over(collgrad) over(urban2)

asyvars

1
gr bar wage, over(occ5) over(union)

1
gr bar wage, over(occ5) over(union) asyvars

stack

1
gr bar wage, over(occ5) over(union) asyvars percentages stack

missing

1
gr hbar wage, over(urban3) over(union)

1
gr hbar wage, over(urban3) over(union) missing

nofill

1
gr bar wage, over(grade) over(collgrad)

1
gr bar wage, over(grade) over(collgrad) nofill

gap()/outergap()

1
gr hbar wage, over(grade4, gap(10)) outergap(200)

1
gr hbar wage, over(grade4, gap(-10))

1
gr hbar wage, over(grade4, gap(10)) over(married, gap(40)) outergap(80)

bargap()

1
2
3
4
gr hbar prev_exp ttl_exp, bargap(25) ///
over(married, gap(200)) ///
over(union, gap(400)) ///
outergap(400)

descending

1
gr hbar wage, over(occ7, gap(-25) descending) sch(vg_outc)

reverse

1
gr hbar wage, over(occ7, gap(-25) reverse) sch(vg_outc)

sort()

1
gr hbar wage, over(occ7, sort(occ7alpha))

1
gr hbar wage, over(occ7, sort(1))

1
gr hbar wage hours, over(occ7, sort(1))

1
gr hbar wage hours, over(occ7, sort(2))

1
gr hbar wage hours, over(occ7, sort(2)) over(married)

1
gr hbar (sum) wage, over(collgrad) over(occ7) asyvars stack

1
gr hbar (sum) wage, over(collgrad) over(occ7, sort((sum) wage)) asyvars stack

1
gr hbar (sum) wage, over(collgrad) over(occ7, sort((sum) wage) descending) asyvars stack

控制分类轴

1
gr hbar wage, over(union) over(smsa) over(south) missing

1
gr hbar wage, over(union, relabel(3 "missing")) over(smsa, relabel(1 "Nometro" 2 "Metro")) over(south, relabel(1 "N&W" 2 "South")) missing

1
gr hbar prev_exp tenure ttl_exp, ascategory

1
2
gr hbar prev_exp tenure ttl_exp, ascategory ///
yvaroptions(relabel(1 "Previous Exp" 2 "Current exp" 3 "Total Exp"))

yalternate

1
2
gr hbar prev_exp tenure ttl_exp, ascategory ///
yvaroptions(relabel(1 "Previous Exp" 2 "Current exp" 3 "Total Exp")) yalternate

label()

1
gr hbar wage, over(occ7, label(nolabel)) blabel(group)

1
gr bar wage, over(occ7, label(ang(45))) over(collgrad)

1
gr bar wage, over(occ7, label(alternate)) over(collgrad)

1
gr bar wage hours ttl_exp, ascategory over(collgrad) yvaroptions(label(alternate))

nolabel

1
gr bar wage hours ttl_exp, ascategory over(union) nolabel

ticks-label

1
gr bar wage, over(age3, label(ticks tlwidth(thick) tlength(medium) tpos(crossing))) over(collgrad)

label-gap

1
gr bar wage, over(age3, label(labgap(zero))) over(collgrad)

1
gr bar wage, over(age3, label(labgap(huge))) over(collgrad)

the gap between label and axis

1
gr bar wage, over(age3) over(collgrad, axis(outergap(*1.20))) asyvars b1ti("Education Level in Categories") leg(r(1))

showyvars

1
gr bar wage, over(occ7) asyvars

1
gr bar wage, over(occ7) asyvars showyvars

legend

1
2
gr bar wage, over(occ7) asyvars showyvars leg(r(2) colfirst)
gr export bar45.png, replace

exclude0

1
gr bar wage, over(occ7) asyvars exclude0

textfirst

1
gr bar wage, over(occ7) asyvars exclude0 leg(r(4) pos(1) ring(0) textfirst)

leg(stack)

1
gr bar wage, over(occ7) asyvars leg(c(1) pos(9) stack)

sch(vg_lgndc)

1
gr hbar wage, over(occ7) asyvars leg(c(1) pos(9) stack) sch(vg_lgndc)

blabel

1
gr hbar wage hours tenure, over(collgrad) blabel(name) nolabel leg(off)

1
gr hbar wage hours tenure, over(collgrad, label(nolabels)) blabel(group) nolabel leg(off)

1
gr bar tenure, over(occ5, label(nolabels)) exclude0 blabel(group) ysc(range(7.2)) over(union)

blabel(bar):在柱条上注明数值

1
gr hbar prev_exp tenure ttl_exp, blabel(bar) over(grade4)

blabel(bar)-stack

1
gr bar (sum) prev_exp tenure, stack over(grade4) blabel(bar)

blabel(total)-stack

1
gr bar (sum) prev_exp tenure, stack over(grade4) blabel(total)

1
gr hbar hours, over(occ7, label(nolabels)) blabel(group, pos(inside))

1
gr hbar hours, over(occ7, label(nolabels)) blabel(group, pos(base) gap(huge))

控制y轴

1
gr hbar wage, over(occ5) over(married) asyvars

1
gr hbar wage, over(occ5) over(married) asyvars yti("Average Wages", size(vlarge) box bexpand) yline(10, lw(thick) lcolor(red) lp(dash))

1
gr bar hours, over(occ7) asyvars yla(30(5)45, ang(20) nogrid) exclude0

1
gr bar prev_exp tenure, over(occ7) ysc(off)

1
gr hbar prev_exp tenure, over(occ7) yalternate

1
gr hbar prev_exp tenure, over(occ7) yreverse

1
gr hbar prev_exp tenure, over(occ7) xalternate yreverse

外观控制

1
gr bar wage hours ttl_exp, over(collgrad) intensity(*0.5)

1
gr bar wage hours ttl_exp, over(collgrad) bar(1, bcolor(dkgreen))

1
gr bar wage hours ttl_exp, over(collgrad) bar(1, bcolor(dkgreen) fc(ltblue) lc(blue) lw(vthick))

  • 分组绘图
1
gr bar wage, over(urban2) over(married) over(union) by(collgrad)

1
gr bar ttl_exp tenure, over(married) over(urban2) by(union, missing total)

1
gr hbar ttl_exp tenure, over(married) over(urban2) by(union, cols(1))

1
gr hbar ttl_exp tenure, over(married) over(urban2) by(union, cols(1) note("") leg(pos(3)))

1
gr hbar ttl_exp tenure, over(married) over(urban2) by(union, cols(1) note("") leg(pos(3))) leg(cols(1) stack label(1 "Total Exp") label(2 "Current Exp"))

1
gr hbar ttl_exp tenure, over(married) over(urban2) by(union, c(1) note("") leg(pos(3))) leg(c(1) stack label(1 "Total Exp") label(2 "Current Exp")) subtitle(, pos(5) ring(0) nobexpand)

1
gr bar ttl_exp tenure, over(married) over(urban2) by(union collgrad)

# Stata

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了658.8k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×