*-呈现相关系数矩阵
logout, save("$Out/Table2_corr") excel replace: ///
pwcorr_a $y $x
*-回归分析:基本版本
reg $y $x //OLS 回归, basic model
est store m1 //存储回归结果
reg $y $x i.race //i.race表示种族虚拟变量, help fvvarlist
est store m2
reg $y $x i.race i.industry
est store m3
reg $y $x i.race i.occupation
est store m4
*-列表呈现回归结果 see [A4_Regress, 4.7 回归结果呈现]
local s "using $Out/Table3.csv" //指定存储结果的 Excel 文档名称
local m "m1 m2 m3 m4"
esttab `m' `s', replace nogap compress ///
ar2 scalar(N F rss) b(%6.3f) ///
star(* 0.1 ** 0.05 *** 0.01) ///
noomit nobase ///
indicate("行业效应=*.industry" "职业效应 =*.occupation")
命令:
. lianxh 码农 重现代码 可重现
目的: 将同一个 project 相关的文档汇总到一个文档下,便于管理
用途: 多个 dofile/data 的管理,如论文,课题或课程资料
新建一个 Project:
打开一个 Project:
Source: stata.com
Stack Exchange Superuser 高级用户经常出没的地方
Economics Job market Rumors 很多博士生出没
Stata 命令:
. lianxh 数据处理
help use // 导入 Stata 格式数据
help sysuse // Stata 自带数据
sysuse dir, all // 数据列表
help webuse // 电子手册数据
help dta_manuals // 数据列表
help import // 导入 Excel, csv, txt 格式的数据
import excel data.xls, firstrow clear
import excel "D:/data.xlsx", cellrange(A1:A10) firstrow clear
copy "https://www.stata.com/examples/auto.csv" "auto.csv" // CSV
import delimited auto, rowrange(3:6)
help bcuse // Wooldridge 书中配套数据
bcuse wagepan, clear
help language
help operators
help function
help expression
help gen
help egen
sysuse "auto.dta", clear
gen wei2len = weight/length
gen lnPrice = ln(price)
gen bad = (rep78>=4 & rep78!=.)
bysort foreign: egen sd_price = sd(price)
regress mpg price weight##weight i.foreign i.foreign#weight
因子变量 reg wage hours i.race i.industry i.industry#c.year
时序变量 reg D.wage D.hours D.L.(z1 z2 z3)
符号 | 含义 | 实例 |
---|---|---|
i. |
标示为类别变量 | reg y x i.id i.year → |
c. |
标示为连续变量 | |
o. |
略去某个类别或变量 | |
# |
交乘项 | reg y D x i.D#c.x → reg y x c.x#i.year → |
## |
两个变量及其交乘项 | reg y x##z → |
clear
input group x
1 30
1 50
2 40
2 60
2 80
3 70
end
. list group i.group i.group#c.x, clean
1. 2. 3. 1.group# 2.group# 3.group#
group group group group c.x c.x c.x
1. 1 1 0 0 30 0 0
2. 1 1 0 0 50 0 0
3. 2 0 1 0 0 40 0
4. 2 0 1 0 0 60 0
5. 2 0 1 0 0 80 0
6. 3 0 0 1 0 0 70
. reg x i.group, noheader
--------------------------------------------------
x | Coefficient Std. err. t P>|t|
-------+------------------------------------------
group |
2 | 20.000 16.667 1.20 0.316
3 | 30.000 22.361 1.34 0.272
|
_cons | 40.000 12.910 3.10 0.053
--------------------------------------------------
. regfit
x = 40.00 + 0.00*1b.group + 20.00*2.group + 30.00*3.group
(12.91) (0.00) (16.67) (22.36)
N = 6, R2 = 0.43, adj-R2 = 0.05
基准组运算符 | 含义 |
---|---|
ib#.x |
使用*作为基准组,*为变量中其中一类的值 |
ib(#*).x |
使用变量值中的第*位排序的值所对应的类别作为基准组 |
ib(first).x |
使用变量的最小值所对应的类别作为基准组 (该项为 Stata 默认选项) |
ib (last).x |
使用变量的最大值所对应的类别作为基准组 |
ib( freq).x |
使用变量值的频数最大的类别作为基准组 |
ibn.x |
不设基准组 |
模型设定:
. sysuse "nlsw88.dta", clear
*-传统方法
. gen black=1
. replace black=0 if race!=2
. gen black_x_hours = black*hours
. reg wage black hours black_x_hours
*-因子变量法
. reg wage 2.race c.hours 2.race#c.hours
. reg wage 2.race##c.hours //与上一行等价
------------------------------------------------------------------------------
wage | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
race |
Black | -1.619 1.266 -1.28 0.201 -4.101 0.864
hours | 0.089 0.012 7.24 0.000 0.065 0.113
race#c.hours |
Black | 0.007 0.033 0.22 0.827 -0.057 0.071
_cons | 4.810 0.474 10.14 0.000 3.880 5.741
------------------------------------------------------------------------------
模型设定:
. sysuse "nlsw88.dta", clear
. reg wage 2.race##c.hours##c.hours
-------------------------------------------------------------------
wage | Coeff SE t P>|t|
---------------------+---------------------------------------------
race |
Black | -3.461 2.274 -1.52 0.128
hours | 0.133 0.044 3.03 0.002
|
race#c.hours |
Black | 0.123 0.127 0.97 0.331
|
c.hours#c.hours | -0.001 0.001 -1.05 0.296
|
race#c.hours#c.hours |
Black | -0.002 0.002 -0.95 0.344
|
_cons | 4.165 0.778 5.35 0.000
-------------------------------------------------------------------
帮助:help varlist
;help tsvarlist
reg y L.y L(0/2).x D.L.z
. use "https://www.stata-press.com/data/r17/gxmpl1", clear
. format gnp cpi %5.1f
. list year L(1/3).(gnp cpi) D.cpi, clean
L. L2. L3. L. L2. L3. D.
year gnp gnp gnp cpi cpi cpi cpi
---------------------------------------------------------------------
1. 1989 . . . . . . .
2. 1990 5837.9 . . 124.0 . . 6.7
3. 1991 6026.3 5837.9 . 130.7 124.0 . 5.5
4. 1992 6367.4 6026.3 5837.9 136.2 130.7 124.0 4.1
5. 1993 6689.3 6367.4 6026.3 140.3 136.2 130.7 4.2
6. 1994 7098.4 6689.3 6367.4 144.5 140.3 136.2 3.7
7. 1995 7433.4 7098.4 6689.3 148.2 144.5 140.3 4.2
8. 1996 7851.9 7433.4 7098.4 152.4 148.2 144.5 4.5
. use "https://www.stata-press.com/data/r17/invest2", clear
. keep if time<=5&company<=2
. format invest market %4.1f
. list company time invest L(1/2).invest market D1.market
+--------------------------------------------------------------+
| L. L2. D.|
| company time invest invest invest market market |
|--------------------------------------------------------------|
1. | 1 1 317.6 . . 3078.5 . |
2. | 1 2 391.8 317.6 . 4661.7 1583.2 |
3. | 1 3 410.6 391.8 317.6 5387.1 725.4 |
4. | 1 4 257.7 410.6 391.8 2792.2 -2594.9 |
5. | 1 5 330.8 257.7 410.6 4313.2 1521.0 |
|--------------------------------------------------------------|
6. | 2 1 40.3 . . 417.5 . |
7. | 2 2 72.8 40.3 . 837.8 420.3 |
8. | 2 3 66.3 72.8 40.3 883.9 46.1 |
9. | 2 4 51.6 66.3 72.8 437.9 -446.0 |
1. | 2 5 52.4 51.6 66.3 679.7 241.8 |
+--------------------------------------------------------------+
long
+------------+ wide
| i j var | +----------------+
|------------| | i var1 var2 |
| 1 1 4.1 | reshape |----------------|
| 1 2 4.5 | <---------> | 1 4.1 4.5 |
| 2 1 3.3 | | 2 3.3 3.0 |
| 2 2 3.0 | +----------------+
+------------+
// long --> wide:
j 旧变量名称
/
reshape wide year, i(i) j(j)
// wide --> long:
reshape long stub, i(i) j(j)
\
j 新变量名称
. reshape long inc ue, i(id) j(year) //Note: [1] sex 不发生变化,无需转换
// [2] j() 选项中填写新的变量名称
Stata 命令:
. lianxh 数据处理 面板
Stata 绘图:PDF 手册
Book: Mitchell, M. A visual guide to stata graphics (4e). Stata Press, 2022. -Link-
. net get vgsg4
Denny, K., 2021, Basic Stata graphics for social science students, -PDF-
An Introduction to Stata Graphics, -Link-
. lianxh 模板 选项
.lianxh 可视
.lianxh 直方 柱状 密度
.lianxh 散点
Stata 绘图最难也是最灵活的部分就是选项的设定。
若将一幅图形的各个要素拆解开,了解对应的选项设定和帮助文件,积累一段时间,便可以熟练地绘制图形了。选项大致可以分成如下几类:
help scheme
help axis_options
help title_options
help addline_options
sysuse "sp500", clear
replace volume = volume/1000
keep in 1/57
*----------------------------------------Begin
#delimit ;
twoway //help twoway
(rspike hi low date, lw(*1.3))
(line close date,
lpattern(solid) lwidth(*1.2) lcolor(blue))
,
yscale(range(1100 1400)) //help axis_options
ylabel(1100(100)1400, grid) //help axis_options
ymtick(##5) //help axis_options
xlabel(, angle(30)) //help axis_options
ytitle("股价", place(top)) //help title_options
xtitle("交易日") //help title_options
legend(order(1 "High-Low" 2 "Close")
ring(0) position(2) row(2)) //help legend_options
subtitle("S&P 500", margin(b+2.5)) //help title_options
note("数据来源: 雅虎财经!") //help title_options
scheme(s2mono); //help scheme
#delimit cr
graph export "sp500_rspike_01.png", replace
*----------------------------------------Over
sysuse "sp500", clear
replace volume = volume/1000
keep in 1/57
*----------------------------------------Begin
#delimit ;
twoway
(rspike hi low date)
(line close date)
(bar volume date, barw(.25) yaxis(2))
,
yscale(axis(1) r(900 1400))
yscale(axis(2) r( 9 45))
ylabel(, axis(2) grid)
ytitle("股价: 最高, 最低, 收盘",place(top))
ytitle("交易量 (百万股)", axis(2)
bexpand just(left))
xtitle(" ")
legend(off)
subtitle("S&P 500", margin(b+2.5))
note("数据来源: 雅虎财经!") ;
#delimit cr
*----------------------------------------Over
graph export "sp500_rspike_02.png", replace
.lianxh 复现 重现 可重复
help cmd
help xtreg
help winsor2
search keywords
search dynamic panel data
lianxh keywords
-click-
lianxh DID 倍分法
songbl 合成控制
Global style
封面图片
底部链接
顶部文字
幻灯片标题
作者信息