Stata旧笔记整理(八)

Stata旧笔记整理(八)

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

ciplot

  • ciplot produces a display of means and confidence intervals. Means are shown by point symbols and intervals by capped bars. ci is used for the calculations. aweights and fweights are allowed;
1
2
webuse citytemp, clear
ciplot heatdd

1
ciplot heatdd cooldd

1
ciplot heatdd, by(division)

1
ciplot heatdd cooldd, by(division) xla(, ang(45))

1
ciplot heatdd cooldd, by(division) hor

clickout——列出当前文件夹中的所有文件,提供点击打开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
. cd "/Users/mr.cheng/Desktop"
/Users/mr.cheng/Desktop

. * 列出当前文件夹中的所有文件,提供点击打开

. clickout
tododolist.do
coefplot.do
weibo.ado
.DS_Store
clickout.do
charlist.do
prov.dta
ToDoList.md
colorscatter.do

collapse命令——根据原始数据集按照一定的规则创建一个统计量构成的数据集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
clear all
webuse college, clear
* 创建一个包含每年gpa的25%分位数的数据集(注意这里需要使用fw作为权重)
collapse (p25) gpa [fw=number], by(year)
* 创建一个包含每年gpa和hour的均值和中位数,并把两者的中位数分别记为medgpa和medhour,注意这里同样需要使用number作为权重。
webuse college, clear
collapse (mean) gpa hour (mean) medgpa = gpa medhour = hour [fw = number], by(year)
* 创建一个包含每年gpa和hour的数量和最小值的数据集,并把两者的最小值分别记为mingpa和minhour
webuse college, clear
collapse (count) gpa hour (min) mingpa = gpa minhour = hour [fw = number], by(year)
* 创建一个包含每年观测值的百分数,分母为去掉缺失值的总观测值的数量
webuse college, clear
collapse (percent) gpa hour [fw = number], by(year)
webuse college, clear
replace gpa = . in 3
collapse (percent) gpa hour [fw = number], by(year)
* 忽略所有的含有缺失值的观测值计算均值:
webuse collapse, clear
replace gpa = . in 2/4
collapse (mean) gpa hour [fw = number], by(year) cw

confirm file:检查某个文件是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
confirm file d:/temp.txt
/*如果该文件不存在就会返回一个错误码,为了避免程序发现该文件不存在而停下来,
我们在代码前面加一个cap*/
cap confirm file d:/temp.txt
/*然后我们可以用_rc来提取这一error code*/
cap confirm file d:/temp.txt
while _rc{
cap confirm file d:/temp.txt
}
/*千万不要运行上述程序,因为Stata会不停地检查文件是否存在
一个解决办法是每次检查完让stata休息一会儿*/
cap confirm file d:/temp.txt
while _rc{
sleep 1000 /*暂停1000毫秒*/
cap confirm file d:/temp.txt
}
cap confirm file d:/temp.txt
local i 1
while _rc & `i'<10{
di "这个文件不存在"
local i=`i'+1
sleep 1000 /*暂停1000毫秒*/
cap confirm file d:/temp.txt
}

contract命令

根据给定变量产生一个频数和百分比的数据集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78

. list

+--------------------------+
| rep78 foreign _freq |
|--------------------------|
1. | 1 Domestic 2 |
2. | 2 Domestic 8 |
3. | 3 Domestic 27 |
4. | 4 Domestic 9 |
5. | 5 Domestic 2 |
|--------------------------|
6. | . Domestic 4 |
7. | 3 Foreign 3 |
8. | 4 Foreign 9 |
9. | 5 Foreign 9 |
10. | . Foreign 1 |
+--------------------------+

包括频数为0的

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
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78, zero

. list

+--------------------------+
| rep78 foreign _freq |
|--------------------------|
1. | 1 Domestic 2 |
2. | 2 Domestic 8 |
3. | 3 Domestic 27 |
4. | 4 Domestic 9 |
5. | 5 Domestic 2 |
|--------------------------|
6. | . Domestic 4 |
7. | 1 Foreign 0 |
8. | 2 Foreign 0 |
9. | 3 Foreign 3 |
10. | 4 Foreign 9 |
|--------------------------|
11. | 5 Foreign 9 |
12. | . Foreign 1 |
+--------------------------+

把frequency命名为count

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78, freq(count)

. list

+--------------------------+
| rep78 foreign count |
|--------------------------|
1. | 1 Domestic 2 |
2. | 2 Domestic 8 |
3. | 3 Domestic 27 |
4. | 4 Domestic 9 |
5. | 5 Domestic 2 |
|--------------------------|
6. | . Domestic 4 |
7. | 3 Foreign 3 |
8. | 4 Foreign 9 |
9. | 5 Foreign 9 |
10. | . Foreign 1 |
+--------------------------+

删除含有缺失值的观测值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78, freq(count) nomiss

. list

+--------------------------+
| rep78 foreign count |
|--------------------------|
1. | 1 Domestic 2 |
2. | 2 Domestic 8 |
3. | 3 Domestic 27 |
4. | 4 Domestic 9 |
5. | 5 Domestic 2 |
|--------------------------|
6. | 3 Foreign 3 |
7. | 4 Foreign 9 |
8. | 5 Foreign 9 |
+--------------------------+

产生累积频数序列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78, cfreq(cumfreq)

. list

+------------------------------------+
| rep78 foreign _freq cumfreq |
|------------------------------------|
1. | 1 Domestic 2 2 |
2. | 2 Domestic 8 10 |
3. | 3 Domestic 27 37 |
4. | 4 Domestic 9 46 |
5. | 5 Domestic 2 48 |
|------------------------------------|
6. | . Domestic 4 52 |
7. | 3 Foreign 3 55 |
8. | 4 Foreign 9 64 |
9. | 5 Foreign 9 73 |
10. | . Foreign 1 74 |
+------------------------------------+

产生百分比和累积百分比序列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
. sysuse auto, clear
(1978 Automobile Data)

. contract foreign rep78, cfreq(cumfreq) percent(percentage) cpercent(cumpercent)

. list, abb(10)

+--------------------------------------------------------------+
| rep78 foreign _freq cumfreq percentage cumpercent |
|--------------------------------------------------------------|
1. | 1 Domestic 2 2 2.70 2.70 |
2. | 2 Domestic 8 10 10.81 13.51 |
3. | 3 Domestic 27 37 36.49 50.00 |
4. | 4 Domestic 9 46 12.16 62.16 |
5. | 5 Domestic 2 48 2.70 64.86 |
|--------------------------------------------------------------|
6. | . Domestic 4 52 5.41 70.27 |
7. | 3 Foreign 3 55 4.05 74.32 |
8. | 4 Foreign 9 64 12.16 86.49 |
9. | 5 Foreign 9 73 12.16 98.65 |
10. | . Foreign 1 74 1.35 100.00 |
+--------------------------------------------------------------+

cpyxplot——散点图数组

1
2
3
sysuse auto, clear
cpyxplot for \rep mpg turn length
gre cpyxplot

cvxhull

1
2
3
4
5
6
7
* ssc install cvxhull
sysuse auto, clear
gen foreign1 = foreign
label def for 1 For 0 Dom
label val foreign1 for
cvxhull mpg weight, group(foreign) hulls(2) ///
scat(mlab(foreign1) mlabpos(c) msym(i) ysc(r(0, 60)))

datax

1
2
3
4
5
6
7
8
qui which dataex
if _rc !=0 {
ssc install dataex, replace
}

webuse2 nlswork.dta, clear
xtset idcode year
dataex idcode year ln_wage in 1/20

did3——创建双重差分法的表格

1
net install did3.pkg, from("https://bitbucket.org/keithk/kk-adofiles/raw/3668170c07edef8ad9f18af25b3e2a39673c62ee/")

用法

1
did3 结果变量 处理变量 时期变量, 选择项

示例

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
. use "http://fmwww.bc.edu/repec/bocode/c/CardKrueger1994.dta", clear
. did3 fte treated t, l(varname)

Y: fte (Output: Full Time Employment)
Row: treated (New Jersey = 1; Pennsylvania = 0)
Column: t (Feb. 1992 = 0; Nov. 1992 = 1)
Check if row {0|1|.}: OK) Check if column {0|1|.}: OK)

Mean of fte if treated==1 & t==0: 17.06518 (.4833988)*** n=326
Mean of fte if treated==0 & t==0: 19.94872 (1.322493)*** n=78
Mean of fte if treated==1 & t==1: 17.57266 (.4907463)*** n=320
Mean of fte if treated==0 & t==1: 17.54221 (.9013452)*** n=77
Difference of fte (by treated) if t==0: -2.883534 (1.180528)** n=404
Difference of fte (by treated) if t==1: .0304485 (1.093943) n=397
Difference of fte (by t) if treated==1: .5074722 (.688807) n=646
Difference of fte (by t) if treated==0: -2.40651 (1.604303) n=155
Difference-in-Difference: 2.913982 (1.610513)* n=801
Asterisks indicate if mean is different from zero at the 10*, 5**, and 1*** percent level.)

t=0 t=1 Difference
treated=1 17.06518 17.57266 .5074722
r1 .4833988 .4907463 .688807
r1
treated=0 19.94872 17.54221 -2.40651
r1 1.322493 .9013452 1.604303
r1
Difference -2.883534 .0304485 2.913982
r1 1.180528 1.093943 1.610513
r1

dirlist——将某类型的文件名存储为宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
. cd "/Users/mr.cheng/Documents/我的项目/Web_data_source"
/Users/mr.cheng/Documents/我的项目/Web_data_source

. dirlist *.png

. ret list

macros:
r(nfiles) : "592"
r(fsizes) : "10918 90579 326511 196704 64009 27412 .."
r(ftimes) : "17:19:30 16:25:14 16:27:29 17:30:16 13.."
r(fdates) : ". . . . . . . . . . . . . . . . . . . .."
r(fnames) : "1.png 45度线.png Mandelbrot集合.png St.."

. * Mac OS X: Spaces in the filespec should be preceded by an escape character "\".

dirtools——一个非常好用的工具包

ldo/ldta/lgph/lmata/lall/leps/lpdf/lsmclltex/lado/ldir/

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
. cd "/Users/mr.cheng/Documents/我的项目/Web_data_source"
/Users/mr.cheng/Documents/我的项目/Web_data_source

. ldo
[view] [edit] [do] PSM_Stata操作实例.do
[view] [edit] [do] Stata_emoji.do

Click [here] for other links

. ldta
[des] [describe] [use] 000001.dta
[des] [describe] [use] china_map.dta
[des] [describe] [use] ldw_exper.dta
[des] [describe] [use] temp.dta
[des] [describe] [use] 历任基金经理.dta

Click [here] for other links

. lgph

Click [here] for other links

. lmata

Click [here] for other links

. leps

Convert all files to PDF

Click [here] for other links

. lpdf
[open] [print] [tops] A_Gentle_Introduction_to_Stata_4ed.pdf
[open] [print] [tops] A_Visual_Guide_to_Stata_Graphics(3rd_ed).pdf
[open] [print] [tops] An_Introduction_to_Stata_Programming.pdf
[open] [print] [tops] Options,_Futures_and_Other_Derivatives_Solution_Manual_9th_Edition_John_C._Hull.pdf
[open] [print] [tops] R_for_stata_user.pdf
[open] [print] [tops] ggplot2_cn.pdf
[open] [print] [tops] ggplot2_en.pdf
[open] [print] [tops] txcj1.pdf
[open] [print] [tops] 金融时间序列及其特征(一):资产收益率.pdf

Convert all files to PS

Click [here] for other links

. lsmcl

Click [here] for other links

. ltex

Click [here] for other links

distinct——寻找奇异值

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
62
63
. sysuse auto, clear
(1978 Automobile Data)

. distinct

| Observations
| total distinct
--------------+----------------------
make | 74 74
price | 74 74
mpg | 74 21
rep78 | 69 5
headroom | 74 8
trunk | 74 18
weight | 74 64
length | 74 47
turn | 74 18
displacement | 74 31
gear_ratio | 74 36
foreign | 74 2

. distinct, max(10)

| Observations
| total distinct
--------------+----------------------
rep78 | 69 5
headroom | 74 8
foreign | 74 2

. distinct make-headroom

| Observations
| total distinct
----------+----------------------
make | 74 74
price | 74 74
mpg | 74 21
rep78 | 69 5
headroom | 74 8

. distinct make-headroom, missing abbrev(6)

| Observations
| total distinct
--------+----------------------
make | 74 74
price | 74 74
mpg | 74 21
rep78 | 74 6
head~m | 74 8

. distinct foreign rep78, joint

Observations
total distinct
69 8

. distinct foreign rep78, joint missing

Observations
total distinct
74 10

函数、宏、标量与矩阵

导论

1
2
3
4
5
clear all
use census2c, clear
list
list, separator(0) // 不绘制分割线,默认五行分隔一次
sum pop* //查看所有开头为pop的变量的统计量
  • if var < . 可以用来排除所有的缺失值;
  • 如果所有的参数都缺失,missing(x1,x2,x3···xn)会返回1,否则将会返回0;

字符串型转数值型:real()/destring/encode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
. encode state, gen(stateid)

. d state stateid

storage display value
variable name type format label variable label
------------------------------------------------------------------------------
state str13 %-13s State
stateid long %13.0g stateid State

. gen real = real("1")

. d real

storage display value
variable name type format label variable label
------------------------------------------------------------------------------
real float %9.0g

. gen string = "1"

. destring string, replace
string: all characters numeric; replaced as byte

数值型转字符串型:strofreal()/tostring/decode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
. tostring pop, gen(popstring)
popstring generated as str9

. d pop popstring

storage display value
variable name type format label variable label
------------------------------------------------------------------------------
pop double %8.1f 1980 Population, '000
popstring str9 %9s 1980 Population, '000

. decode stateid, gen(statename)

. d statename

storage display value
variable name type format label variable label
------------------------------------------------------------------------------
statename str13 %13s State

. gen stringid = strofreal(1)

inlist()/inrange()函数

  • inlist(z,a,b,...): 1 if z is a member of the remaining arguments; otherwise, 0

    1
    2
    use census2c, clear
    gen byte newengland = inlist(state, "Connecticut", "Maine", "Massachusetts", "New Hampshire", "Rhode Island", "Vermont")
  • inrange(z,a,b):1 if it is known that a < z < b; otherwise, 0

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    . list state pop if inrange(pop, 5000, 9999), sep(0)

    +------------------------+
    | state pop |
    |------------------------|
    3. | Indiana 5490.2 |
    7. | Massachusetts 5737.0 |
    8. | Michigan 9262.1 |
    13. | New Jersey 7364.8 |
    +------------------------+
  • reldif(x, y): the “relative” difference |x-y|/(|y|+1); 0 if both arguments are the same type of extended missing value; missing if only one argument is missing or if the two arguments are two different types of missing.

substr()函数

  • substr(s,n1,n2):提取从n1开始,长度为n2的子字符串
1
2
3
4
5
6
substr("abcdef",2,3) = "bcd"
substr("abcdef",-3,2) = "de"
substr("abcdef",2,.) = "bcdef"
substr("abcdef",-3,.) = "def"
substr("abcdef",2,0) = ""
substr("abcdef",15,2)= ""

missing()函数

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
. use census2c, clear
(1980 Census data for NE and NC states)

. replace pop = . in 5/8
(4 real changes made, 4 to missing)

. gen smallpop = 0

. replace smallpop = 1 if pop < 5000 & !missing(pop)
(11 real changes made)

. gen largepop = 0

. replace largepop = 1 if pop >= 5000 & !missing(pop)
(6 real changes made)

. list pop smallpop largepop in 1/10, separator(0)

+-------------------------------+
| pop smallpop largepop |
|-------------------------------|
1. | 3107.6 1 0 |
2. | 11426.5 0 1 |
3. | 5490.2 0 1 |
4. | 2913.8 1 0 |
5. | . 0 0 |
6. | . 0 0 |
7. | . 0 0 |
8. | . 0 0 |
9. | 4076.0 1 0 |
10. | 4916.7 1 0 |
+-------------------------------+
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
. use census2c, clear
(1980 Census data for NE and NC states)

. replace pop = . in 5/8
(4 real changes made, 4 to missing)

. gen spop = (pop <= 5000)

. gen lpop = (pop > 5000)

. list pop spop lpop in 1/10, separator(0)

+-----------------------+
| pop spop lpop |
|-----------------------|
1. | 3107.6 1 0 |
2. | 11426.5 0 1 |
3. | 5490.2 0 1 |
4. | 2913.8 1 0 |
5. | . 0 1 |
6. | . 0 1 |
7. | . 0 1 |
8. | . 0 1 |
9. | 4076.0 1 0 |
10. | 4916.7 1 0 |
+-----------------------+

. gen sspop = (pop <= 5000) if pop < .
(4 missing values generated)

. gen llpop = (pop > 5000) if pop < .
(4 missing values generated)

. list pop sspop llpop in 1/10, separator(0)

+-------------------------+
| pop sspop llpop |
|-------------------------|
1. | 3107.6 1 0 |
2. | 11426.5 0 1 |
3. | 5490.2 0 1 |
4. | 2913.8 1 0 |
5. | . . . |
6. | . . . |
7. | . . . |
8. | . . . |
9. | 4076.0 1 0 |
10. | 4916.7 1 0 |
+-------------------------+

cond()函数

  • cond(x,a,b[,c]) =
    • a if x is true and nonmissing,
    • b if x is false, and c if x = missing;
    • a if c is not specified and x = missing

recode命令/recode()函数:记录离散和连续的值

  • 下面的命令
1
2
3
replace newcode = 5 if oldcode == 2
replace newcode = 8 if oldcode == 3
replace newcode = 12 if inlist(oldcode, 5, 6, 7)
  • 等价于
1
recode oldcode (2 = 5) (3 = 8) (5/7 = 12), gen(newcode)

recode()函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
. use census2c, clear
(1980 Census data for NE and NC states)

. gen popbrack = recode(pop, 3000, 7000, 12000, 17000)

. list pop popbrack in 1/10, separator(0)

+--------------------+
| pop popbrack |
|--------------------|
1. | 3107.6 7000 |
2. | 11426.5 12000 |
3. | 5490.2 7000 |
4. | 2913.8 3000 |
5. | 2363.7 3000 |
6. | 1124.7 3000 |
7. | 5737.0 7000 |
8. | 9262.1 12000 |
9. | 4076.0 7000 |
10. | 4916.7 7000 |
+--------------------+

floor()函数和ceil()函数

  • floor(): 小于参数的最大整数
  • ceil(): 大于参数的最小整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
. use census2c, clear
(1980 Census data for NE and NC states)

. gen floor = floor(pop)

. gen ceil = ceil(pop)

. gen diff = ceil - floor

. list floor pop ceil diff in 1/5, separator(0)

+--------------------------------+
| floor pop ceil diff |
|--------------------------------|
1. | 3107 3107.6 3108 1 |
2. | 11426 11426.5 11427 1 |
3. | 5490 5490.2 5491 1 |
4. | 2913 2913.8 2914 1 |
5. | 2363 2363.7 2364 1 |
+--------------------------------+

_n/_N

1
2
3
4
5
6
7
8
9
10
11
12
13
. clear all

. cd "~/Desktop/itsp"
/Users/mr.cheng/Desktop/itsp

. use census2c, clear
(1980 Census data for NE and NC states)

. gsort region -pop

. by region: gen totpop = sum(pop)

. by region: list region totpop if _n == _N
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
----------------------------------------------------------------------
-> region = NE

+-------------------+
| region totpop |
|-------------------|
9. | NE 49135.28 |
+-------------------+

----------------------------------------------------------------------
-> region = N Cntrl

+--------------------+
| region totpop |
|--------------------|
12. | N Cntrl 58865.67 |
+--------------------+


. list region pop totpop, sepby(region)

+------------------------------+
| region pop totpop |
|------------------------------|
1. | NE 17558.1 17558.07 |
2. | NE 11863.9 29421.97 |
3. | NE 7364.8 36786.79 |
4. | NE 5737.0 42523.83 |
5. | NE 3107.6 45631.4 |
6. | NE 1124.7 46756.06 |
7. | NE 947.2 47703.22 |
8. | NE 920.6 48623.83 |
9. | NE 511.5 49135.28 |
|------------------------------|
10. | N Cntrl 11426.5 11426.52 |
11. | N Cntrl 10797.6 22224.15 |
12. | N Cntrl 9262.1 31486.23 |
13. | N Cntrl 5490.2 36976.45 |
14. | N Cntrl 4916.7 41893.14 |
15. | N Cntrl 4705.8 46598.9 |
16. | N Cntrl 4076.0 50674.87 |
17. | N Cntrl 2913.8 53588.68 |
18. | N Cntrl 2363.7 55952.36 |
19. | N Cntrl 1569.8 57522.18 |
20. | N Cntrl 690.8 58212.95 |
21. | N Cntrl 652.7 58865.67 |
+------------------------------+

. gen smallpop = 0

. replace smallpop = 1 if pop <= 5000 & !missing(pop)
(13 real changes made)

. gen largepop = 0

. replace largepop = 1 if pop > 5000 & !missing(pop)
(8 real changes made)

. gen popsize = smallpop + 2 * largepop

. label var popsize "人口规模代码"

. label define popsize 1 "< 5百万" 2 "> 5百万"

. label val popsize popsize

. bysort region popsize: egen meanpop2 = mean(pop)

. list region popsize state meanpop2, sepby(region popsize)

+----------------------------------------------+
| region popsize state meanpop2 |
|----------------------------------------------|
1. | NE < 5百万 Connecticut 1322.291 |
2. | NE < 5百万 Vermont 1322.291 |
3. | NE < 5百万 Maine 1322.291 |
4. | NE < 5百万 Rhode Island 1322.291 |
5. | NE < 5百万 New Hampshire 1322.291 |
|----------------------------------------------|
6. | NE > 5百万 New York 10630.96 |
7. | NE > 5百万 New Jersey 10630.96 |
8. | NE > 5百万 Pennsylvania 10630.96 |
9. | NE > 5百万 Massachusetts 10630.96 |
|----------------------------------------------|
10. | N Cntrl < 5百万 Kansas 2736.153 |
11. | N Cntrl < 5百万 Wisconsin 2736.153 |
12. | N Cntrl < 5百万 S. Dakota 2736.153 |
13. | N Cntrl < 5百万 Minnesota 2736.153 |
14. | N Cntrl < 5百万 Missouri 2736.153 |
15. | N Cntrl < 5百万 N. Dakota 2736.153 |
16. | N Cntrl < 5百万 Iowa 2736.153 |
17. | N Cntrl < 5百万 Nebraska 2736.153 |
|----------------------------------------------|
18. | N Cntrl > 5百万 Illinois 9244.112 |
19. | N Cntrl > 5百万 Ohio 9244.112 |
20. | N Cntrl > 5百万 Michigan 9244.112 |
21. | N Cntrl > 5百万 Indiana 9244.112 |
+----------------------------------------------+

局部宏

  • 当do文件运行终止时,local将不复存在,但全局宏可以在程序的全过程存在(一个程序中可能包含多个do文件)。
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
. * local

. clear all

. local anx None Mild Moderate Severe

. foreach a of local anx{
2. local count `count' + 1
3. di as text "Anxiety level `count': `a'"
4. }
Anxiety level 4 + 1: None
Anxiety level 4 + 1 + 1: Mild
Anxiety level 4 + 1 + 1 + 1: Moderate
Anxiety level 4 + 1 + 1 + 1 + 1: Severe

. * 如果使用等号

. clear all

. local anx None Mild Moderate Severe

. foreach a of local anx{
2. local count = `count' + 1
3. di as text "Anxiety level `count': `a'"
4. }
Anxiety level 9: None
Anxiety level 10: Mild
Anxiety level 11: Moderate
Anxiety level 12: Severe

.
. local count = 0

. local anx None Mild Moderate Severe

. foreach a of local anx{
2. local count = `count' + 1
3. local newlist "`newlist' `count' `a'"
4. }

. di "`newlist'"
1 None 2 Mild 3 Moderate 4 Severe 1 None 2 Mild 3 Moderate 4 Severe 1
> None 2 Mild 3 Moderate 4 Severe

拓展宏函数和宏列表函数

1
2
3
4
5
6
7
8
9
10
11
12
* 拓展的宏函数
* 单词计数
local anx None Mild Moderate Severe
local wds: word count `anx'
di `wds'
* 列举
forval i = 1/`wds'{
local wd: word `i' of `anx'
di "`wd'"
}

help extended_fcn

标量:存储数值

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
. use fem2, clear

. scalar lb1 = 80

. scalar ub1 = 88

. scalar lb2 = 89

. scalar ub2 = 97

. scalar lb3 = 98

. scalar ub3 = 109

. forval i = 1/3{
2. di _n "IQ " lb`i' " - " ub`i'
3. tab anxiety if inrange(iq, lb`i', ub`i')
4. }

IQ 80 - 88

ANXIETY | Freq. Percent Cum.
------------+-----------------------------------
1 | 3 12.00 12.00
2 | 13 52.00 64.00
3 | 7 28.00 92.00
4 | 2 8.00 100.00
------------+-----------------------------------
Total | 25 100.00

IQ 89 - 97

ANXIETY | Freq. Percent Cum.
------------+-----------------------------------
1 | 5 7.35 7.35
2 | 38 55.88 63.24
3 | 24 35.29 98.53
4 | 1 1.47 100.00
------------+-----------------------------------
Total | 68 100.00

IQ 98 - 109

ANXIETY | Freq. Percent Cum.
------------+-----------------------------------
2 | 9 75.00 75.00
3 | 2 16.67 91.67
4 | 1 8.33 100.00
------------+-----------------------------------
Total | 12 100.00

矩阵:存储数值矩阵

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
. use fem2, clear

. reg weight c.age##c.age

Source | SS df MS Number of obs = 107
-------------+---------------------------------- F(2, 104) = 10.86
Model | 135.170028 2 67.5850139 Prob > F = 0.0001
Residual | 647.10605 104 6.22217356 R-squared = 0.1728
-------------+---------------------------------- Adj R-squared = 0.1569
Total | 782.276078 106 7.379963 Root MSE = 2.4944

------------------------------------------------------------------------------
weight | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
age | .1652777 1.0152 0.16 0.871 -1.847902 2.178458
|
c.age#c.age | .0009098 .0136163 0.07 0.947 -.0260919 .0279115
|
_cons | -5.918914 18.6551 -0.32 0.752 -42.91268 31.07485
------------------------------------------------------------------------------

. ereturn list

scalars:
e(N) = 107
e(df_m) = 2
e(df_r) = 104
e(F) = 10.8619621896282
e(r2) = .1727906958561397
e(rmse) = 2.494428502870775
e(mss) = 135.1700278037221
e(rss) = 647.1060498171503
e(r2_a) = .1568828246226039
e(ll) = -248.1093756352844
e(ll_0) = -258.2581933584128
e(rank) = 3

macros:
e(cmdline) : "regress weight c.age##c.age"
e(title) : "Linear regression"
e(marginsok) : "XB default"
e(vce) : "ols"
e(depvar) : "weight"
e(cmd) : "regress"
e(properties) : "b V"
e(predict) : "regres_p"
e(model) : "ols"
e(estat_cmd) : "regress_estat"

matrices:
e(b) : 1 x 3
e(V) : 3 x 3

functions:
e(sample)

. matrix b = e(b)

. matrix V = e(V)

. matrix list b

b[1,3]
c.age#
age c.age _cons
y1 .16527767 .00090982 -5.9189145

. matrix list V

symmetric V[3,3]
c.age#
age c.age _cons
age 1.0306315
c.age#c.age -.01380654 .0001854
_cons -18.91199 .25270496 348.01273

reg weight c.age##c.age
等价于
reg weight c.age c.age#c.age

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×