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

ichengzi commented Aug 17, 2023

  1. palette line // 所有线类型
  2. palette symbol // 所有符号类型
  3. palette color red
  4. help symbolstyle
******* 线图
line unemp unempf unempm year 
     if country=="United States" 
// 利用 if 条件,只画美国失业率的时序图

summarize unemp unempf unempm
replace unemp=. if unemp==0
replace unempf=. if unempf==0
replace unempm=. if unempm==0 
// 将变量为0的值变成缺失值
summarize unemp unempf unempm
line unemp unempf unempm year ///
     if country=="United States"

twoway line unemp unempf unempm year ///
  if country=="United States", ///
  title("Unemployment rate in the US, 1980-2012") /// 
  legend(label(1 "Total") ///
         label(2 "Females") ///
         label(3 "Males")) ///
  lpattern(solid dash dot) ///
  ytitle("Percentage")
/*
命令依次为:指定画线形图、添加标题、
调整图例、改变线条类型、添加y轴标题
*/
	   	   
twoway connected unemp unempf unempm year 
  if country=="United States", ///
  title("Unemployment rate in the US, 1980-2012") ///
  legend(label(1 "Total") ///
         label(2 "Females") ///
         label(3 "Males")) ///
  msymbol(circle diamond square) ///
  ytitle("Percentage")
/*
命令依次为:指定点连线图、添加标题、调整图例、
改变点的标记符号(实心圆圈、实心菱形、实心正方形)、
添加y轴标题。
标记符号的各种代号可参考 help symbolstyle。 
*/	   
	   
twoway connected unemp year 
       if country=="United States" | ///
          country=="United Kingdom" | ///
          country=="Australia" | ///
          country=="Qatar", ///
       by(country, title("Unemployment")) ///
       msymbol(circle_hollow)
	   
twoway connected unemp year 
       if country=="United States" | ///
          country=="United Kingdom" | ///
          country=="Australia" | ///
          country=="Qatar", ///
       by(country) ///
       title("Unemployment") ///
       msymbol(circle_hollow)	   
/*
利用by(varname)同时做多个图。 
注意:by(varname)功能适用于 matrix 和 star 以外所有图形,
该选项使 graph 按照指定的分组变量分别绘制图形。
title("")放在by()里面,画出的图共用一个标题;
title("")放在by()外面,每个图一个标题
*/

twoway (connected unemp year 
          if country=="United States", ///
          msymbol(diamond_hollow)) ///
       (connected unemp year 
          if country=="United Kingdom", ///
          msymbol(triangle_hollow)) ///
       (connected unemp year 
          if country=="Australia", ///
          msymbol(square_hollow)) ///
       (connected unemp year ///
          if country=="Qatar", ///
       title("Unemployment") ///
       msymbol(circle_hollow) ///
       legend(label(1 "USA") label(2 "UK") ///
              label(3 "Australia") label(4 "Qatar")))
/*
将四个国家的失业率时序图放在同一张图里
*/	   

twoway connected gdppc year if gdppc>40000, ///
       by(country) msymbol(diamond)
/* 保留人均GDP高于40000美元的子样本,再按国家分组绘制时序图*/


bysort year: egen gdppc_mean=mean(gdppc)
bysort year: egen gdppc_median=median(gdppc)
// 利用bysort命令,计算出人均GDP每年的平均数和中位数
twoway connected gdppc gdppc_mean year ///
       if country=="United States" | ///
          country=="United Kingdom" | ///
          country=="Australia" | ///
          country=="Qatar", ///
       by(country, title("GDP pc (PPP, 2005=100)")) ///
       legend(label(1 "GDP-PC") ///
              label(2 "Mean GDP-PC")) ///
       msymbol(circle_hollow)
/*
将四个国家的人均GDP与平均水平进行比较
*/

help twoway line // 查看线图的帮助文件
help twoway connected // 查看点线图的帮助文件

palette symbolpalette // 图示标记符号及对应代号
palette linepalette // 图示线型及对应代号
palette color green // 图示颜色
help palette

@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