Stata旧笔记整理(四)

Stata旧笔记整理(四)

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

线图

line

1
2
3
sysuse sp500, clear
set scheme vg_brite, permanently
tw line close date, sort

更改线性

1
tw line close date, sort lc(dkorange) lw(thick)

connected

1
2
3
tw connected close date, sort msize(small)
# 这等价于
tw sc close date, connect(l) sort msize(small)

更改点型

1
tw connected close date, sort ms(Dh) mc(blue) msize(small)

更改线性

1
tw connected close date, sort lc(cranberry) lp(dash) lw(thick) msize(small)

同时连接多个变量

1
tw connected high low date, sort lw(thin thick) ms(Oh S) msize(vsmall vsmall)

tw tsline

1
2
tsset date
tw tsline close if date > mdy(7,1,2001), lw(thick) lc(navy)

tw tsrline

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick)

ttitle()

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) ttitle(日期)

tlabel()

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) tlabel(30apr2001 30jun2001 30aug2001 30oct2001, ang(20))

tmabel():添加小标签

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) tmlabel(1may2001 1jul2001 1sep2001 1oct2001, ang(45))

tmtick():添加小竖杠

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) tmtick(1may2001 1jul2001 1sep2001 1oct2001)

tline():添加竖线

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) tline(1may2001 1jul2001 1sep2001)

ttext():添加文本

1
tw tsrline high low if tin(01apr2001, 01oct2001), lw(thick) ttext(1035 1jul2001 "一个文本", orientation(rvertical))

线性同余法在Stata中的实现

1:伪随机数的非随机性
2:参数选取对随机数生成的影响
3:伪随机数具有自然循环的特性

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
clear
set more off
set obs 100
local m = 17
local a = 13
gen int y = 1 if _n == 1
gen t = _n
replace y = mod(`a'*y[_n-1], `m') if _n > 1
gen x = y/`m'
list x
twoway line x t //具有很强的循环性

// 随机数生成器
clear
set obs 100
local m = 2^13 - 13
local a = 7^10 - 17
gen int y = 1 if _n == 1
gen t = _n
replace y = mod(`a'*y[_n-1], `m') if _n > 1
gen x = y/`m'
list x
tw line x t //循环性减弱

// 要达到随机模拟的需要,我们需要百万以上的m和a:
clear
set obs 1000
local m = 10^12 - 11
local a = 123450987654
gen int y = 1 if _n == 1
gen t = _n
replace y = mod(`a'*y[_n-1], `m') if _n > 1
gen x = y/`m'
tw scatter x t

箱图

1
2
3
4
5
6
7
8
9
10
11
clear all
sysuse auto, clear
statsby mean = r(mean) max = r(max) min = r(min) sd = r(sd), by(rep78): sum mpg
gen meanpsd = mean + sd
gen meanmsd = mean - sd
tw sc mean rep78 || ///
rbar mean max rep78, bcolor(gs12) barw(0.2) || ///
rbar mean min rep78, bcolor(gs12) barw(0.2) || ///
rbar meanmsd meanpsd rep78, bcolor(red*0.6) barw(0.1) ||, leg(off) yti("每加仑汽油行驶英里数") xti("1978年的维修数") yla(, ang(0)) ///
note("红色的柱条:均值 ± 标准差" "透明的柱条:取值范围") scheme(s1mono)
gr export 箱图.png, replace
  • bcolor(gs12):控制柱条的颜色。

箱线图

指定变量和组别

1
2
vguse nlsw, clear
gr box wage, over(grade4)

1
gr box wage, over(grade4) nooutsides

1
gr box wage, over(union) over(grade4) nooutsides

1
gr hbox wage, over(union) over(grade4) nooutsides

1
gr box wage, over(union) over(grade4) nooutsides asyvars

1
gr box wage, nooutsides over(union) over(grade4) over(urban2)

1
gr hbox prev_exp tenure, nooutsides

1
gr hbox prev_exp tenure, nooutsides over(married)

missing:显示缺失值

1
gr hbox wage, nooutsides over(grade4) over(union) asyvars missing

nofill: 不填充交互项

1
gr box wage, nooutsides over(grade) over(collgrad) nofill

控制箱距

1
gr hbox tenure, nooutsides over(occ5, gap(300)) over(collgrad, gap(25))

1
gr hbox tenure prev_exp ttl_exp, nooutsides over(collgrad) outergap(150)

1
gr box tenure prev_exp ttl_exp, nooutsides over(collgrad) boxgap(10)

控制箱的排序

1
gr hbox tenure, nooutsides over(occ7, descending)

1
gr hbox tenure, nooutsides over(occ7, sort(occ7alpha))

sort(1):表示根据第一个y变量的均值进行排序

1
gr hbox tenure, nooutsides over(occ7, sort(1))

1
gr hbox tenure, nooutsides over(occ7, sort(1) descending)

1
gr hbox prev_exp tenure, nooutsides over(occ7, sort(1))

1
gr hbox prev_exp tenure, nooutsides over(occ7, sort(2))

1
gr hbox tenure, nooutsides over(occ7, sort(1)) over(collgrad, descending)

1
gr box wage, nooutsides over(south, relabel(1 "N&W" 2 "South"))

1
gr box prev_exp tenure ttl_exp, nooutsides

1
gr box prev_exp tenure ttl_exp, nooutsides ascategory

1
gr box prev_exp tenure ttl_exp, nooutsides ascategory yvaroptions(relabel(1 "Prev Exp" 2 "Curr Exp" 3 "Tot Exp")) xalternate

1
gr box wage, nooutsides over(occ7, label(angle(45))) over(collgrad)

1
gr box wage, nooutsides over(occ7, label(alternate)) over(collgrad)

1
gr hbox wage, nooutsides over(occ5, label(labsize(small))) over(collgrad, label(labcolor(maroon)))

1
gr hbox wage, nooutsides over(occ5, label(ticks tlw(thick) tlength(medium))) over(collgrad)

1
gr hbox wage, nooutsides over(occ5, label(labgap(huge))) over(collgrad)

1
gr box wage, nooutsides over(union) over(grade4) asyvars b1title("Education Level in Four Categories")

控制图例

1
gr box wage, over(occ7) nooutsides asyvars

nolabel选项只在有多个y变量的时候才会起作用,当使用该选项时,变量名会放在图例中

1
gr box prev_exp tenure ttl_exp, nooutsides nolabel

1
gr box prev_exp tenure ttl_exp, nooutsides showyvars

1
gr box prev_exp tenure ttl_exp, nooutsides showyvars leg(off)

1
gr box wage, nooutsides over(occ7) asyvars showyvars leg(off)

1
gr box wage, nooutsides over(occ7) asyvars leg(label(1 "Professional") label(2 "Management"))

1
gr box wage, nooutsides over(occ7) asyvars leg(r(2) colfirst pos(6))

1
gr hbox wage, nooutsides over(occ7) asyvars leg(cols(1) pos(9) stack)

1
gr box prev_exp tenure, nooutsides over(occ5) yti("Years of experience", size(vlarge) box bexpand)

1
gr box wage, nooutsides over(occ5) over(collgrad) asyvars yline(12, lw(thick) lc(maroon) lp(dash))

控制箱线的外观

1
gr box wage, over(occ5) over(collgrad) asyvars nooutsides intensity(10)

1
gr box wage, over(occ5) over(collgrad) asyvars nooutsides box(1, bcolor(sand)) box(2, bcolor(maroon))

1
gr box wage, over(occ5) over(collgrad) asyvars nooutsides box(1, bc(sand) blc(brown) blw(thick))

medtype(cline): set the median type to be a custom line

1
gr box prev_exp tenure ttl_exp, nooutsides medtype(cline) medline(lw(thick) lc(black))

1
gr box prev_exp tenure ttl_exp, nooutsides medtype(marker) medmarker(msymbol(+) msize(large))

cwhiskers : customize the whiskers

1
gr box prev_exp tenure ttl_exp, nooutsides cwhiskers lines(lw(thick) lc(black))

1
gr box prev_exp tenure ttl_exp, nooutsides alsize(20)

1
gr box prev_exp tenure ttl_exp, nooutsides capsize(5)

1
gr box prev_exp tenure ttl_exp, marker(2, msymbol(Oh) msize(vlarge))

分组箱线图

1
gr hbox wage, nooutsides over(collgrad) over(urban2) over(married) by(union) leg(r(1))

1
gr hbox ttl_exp tenure, nooutsides note("") over(urban2) over(married) by(union, missing)

1
gr hbox ttl_exp tenure, nooutsides note("") over(urban2) over( married) by(union, total missing)

1
gr hbox ttl_exp tenure, nooutsides note("") over(urban2) over( married) by(union, total missing r(1))

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×