Skip to content

Instantly share code, notes, and snippets.

@ichengzi
Last active August 22, 2023 06:33
Show Gist options
  • Save ichengzi/0eea2af7bbd227f0bd481b8ae8167e79 to your computer and use it in GitHub Desktop.
Save ichengzi/0eea2af7bbd227f0bd481b8ae8167e79 to your computer and use it in GitHub Desktop.
stata分析

一、简单操作

下载外部命令:ssc install outreg2 logout pwcorr_a

二、描述性统计

outreg2 using xxx.doc, replace sum(log) title(Decriptive statistics)
outreg2 using xxx.doc, replace sum(log) keep(**v** **v** **v**) title(Decriptive statistics)
outreg2 using xxx.doc, replace sum(log) keep(**v** **v** **v**) eqkeep(N mean min max) title(Decriptive statistics)
bysort x:outreg2 using xxx.doc, replace sum(log) title(Decriptive statistics)

三、相关性分析

logout, save (xxx)word replace: pwcorr 
logout, save(xxx) word replace: pwcorr_a 

四、多元回归

outreg2 using xxx.doc,replace tstat bdec(3) tdec(2) ctitle(y)
outreg2 using xxx.doc,replace tstat bdec(3) tdec(2) e(r2_a,F) addstat(F test,e(p))
outreg2 using xxx.doc,append tstat bdec(3) tdec(2) e(r2_a,F) addstat(F test,e(p))
固定效应xtreg y x1 x2 x3,fe r
outreg2 using xxx.doc,replace tstat bdec(3) tdec(2) ctitle(y) e(r2_a,F) addstat(F test,e(p)) addtext(Company FE, YES )
xtreg y x1 x2 x3 i.year,fe r
outreg2 using xxx.doc,replace tstat bdec(3) tdec(2) ctitle(y) e(r2_a,F) addstat(F test,e(p)) addtext(Company FE, YES,Year FE, YES)

基础命令Describe 
List 
Sum(obs统计数量mean平均值std.dev标准差, min最小值, max最大值)

pwcorr_a price weight mpg displ, star1(0.01) star5(0.05) star10(0.1) 显著性解释变量是原因,被解释变量是结果。

@ichengzi
Copy link
Author

summarize -> su
su unemp if country == "China"

@ichengzi
Copy link
Author

*******  条形图
graph hbar (mean) gdppc 
// 绘制横向条形图,平均值mean选项是默认值
graph hbar (mean) gdppc, ///
      over(country, sort(1) descending)
graph hbar (mean) gdppc, ///
      over(country, sort(1) ///
      descending label(labsize(*0.5)))

graph hbar (mean) gdppc (median) gdppc ///
      if gdppc>40000, ///
      over(country, sort(1) descending ///
           label(labsize(*1))) ///
      legend(label(1 "GDPpc (mean)") ///
             label(2 "GDPpc (median)"))
/*
over() 设定分组变量,这里表示按国家分组
sort(1) 选项表示根据第一个变量,
        即 gdppc 的柱体高度进行升序排列
descending 表示降序排列
labsize(*0.5) 表示标签字体大小缩放 0.5 倍
*/

help graph bar // 查看条形图的帮助文件

@ichengzi
Copy link
Author

******* 面板数据时间趋势图
*xtset country year 
// 会报错,'country' 为字符串变量*/
encode country, gen(country1) 
xtset country1 year // 声明数据是面板数据

xtline gdppc 
xtline gdppc if gdppc>39000, overlay 
// overlay 将所有国家放在同一图中

help xtline // 查看面板数据时间趋势图的帮助文件

@ichengzi
Copy link
Author

******* 饼图
graph pie export if     ///
   (country=="Brazil" | ///
    country=="Russia" | ///
    country=="India"  | ///
    country=="China") & year == 2010, ///
    over(country) noclockwise 
// noclockwise 逆时针排序

graph pie export if     /// 
   (country=="Brazil" | ///
    country=="Russia" | ///
    country=="India"  | ///
    country=="China") & year == 2010, ///
    over(country) sort descending 			

graph pie export if     /// 
   (country=="Brazil" | ///
    country=="Russia" | ///
    country=="India"  | ///
    country=="China") & year == 2010,    ///
    over(country) sort descending        ///
    plabel(_all percent,format("%5.2f")) ///
    pie(1,explode)
					  					 
/*
sort descending 降序排列; sort 升序排列
plabel(_all percent,format("%7.2f")) 
为所有饼块按 %7.2f 格式显示百分比
pie(1,explode)  突出/分离第1块饼块
*/

help graph pie // 查看饼图的帮助文件

@ichengzi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment