关注获取最新资讯及各种国贸资源
基础命令
在stata中,*表示注释,不会作为命令执行。在stata操作中,建议大家常用注释。
*第一步,建立log文件
log using "C:\Users\Administrator\Desktop\20181121.log",replace
log using "C:\Users\Administrator\Desktop\20181121.log",append
log using "C:\Users\Administrator\Desktop\20181121.log"
*其中,replace表示替换/覆盖原有的,append表示在原有的后面继续写
*导入excel数据,可以直接导入,也可以输入命令导入
import excel "H:\大四上\唐丹计量助教\ch8_cps.xls"
*清除内存中原有的所有数据
clear
import excel "H:\大四上\唐丹计量助教\ch8_cps.xls",sheet("ch8_cps")
clear
import excel "H:\大四上\唐丹计量助教\ch8_cps.xls",sheet("ch8_cps") firstrow
*删除内存中原有的变量,var表示要删除的变量的名称,drop命令一旦保存不可撤销
drop ahe
*更改变量名称,stata is case sensitive
rename ahe AHE
rename female Female
rename age Age
rename yrseduc Bachelor
*将变量排序,方便操作
order AHE Age Female Bachelor
*数据描述性统计 sum是summarize的缩写
sum AHE Age Female Bachelor
*详细的数据描述性统计 de是detail的缩写
sum AHE Age Female Bachelor,de
*散点图 scatter表示散点图
scatter AHE Age
*散点图+线性拟合线 lfit-linear fit
twoway (scatter AHE Age) (lfit AHE Age)
*在stata内存中存储该图
graph save Graph1.gph
*散点图+非线性拟合线 qfit-quadratic fit
twoway (scatter AHE Age) (qfit AHE Age)
*在stata内存中存储该图
graph save Graph2.gph
*将刚刚的两幅图合在一起
graph combine Graph1.gph Graph2.gph
*将该图片保存到电脑上
graph save Graph "C:\Users\Administrator\Desktop\Graph.gph"
*生成变量的相关系数矩阵 corr是correlate的缩写
corr AHE Age Female Bachelor
*定义文件存储路径
cd "C:\Users\Administrator\Desktop"
以Ch8实证练习1为例
*1.多元线性回归及解释 reg是regress的缩写
reg AHE Age Female Bachelor,r
*存储该回归结果为R1
est store R1
*2.对数线性模型
*生成对数变量 gen是generate的缩写,ln()是stata内置的数学函数
gen ln_AHE=ln(AHE)
reg ln_AHE Age Female Bachelor,r
est store R2
*3.双对数模型
gen ln_Age=ln(Age)
reg ln_AHE ln_Age Female Bachelor,r
est store R3
*4.多项式模型
*生成高次项 ^
gen Age_square=Age^2
reg ln_AHE Age Age_square Female Bachelor,r
est store R4
*5-7.比较回归结果
*安装外部命令
ssc install outreg2
*导出回归结果,注意是中括号
outreg2 [R1 R2 R3 R4] using Regression_results,word
*打开桌面上的Regression_results文件,或使用下面的方法进行简单比较
estimates table R1 R2 R3 R4
*8.绘图
*9.交互效应
*生成交互项
gen Female_Bachelor=Female*Bachelor
reg ln_AHE Age Age_square Female Bachelor Female_Bachelor,r
est store R5
*10.交互效应
gen Female_Age=Female*Age
reg ln_AHE Age Age_square Female Bachelor Female_Age,r
est store R6
*11.生成一个二值变量,再生成一个交互项
gen status=1 if Bachelor==16
replace status=0 if Bachelor==12
gen Age_status=Age*status
reg ln_AHE Age Age_square Female Bachelor Age_status if Bachelor==12|Bachelor==16,r
est store R7
相关阅读
指南 | 计量报告写作数据库推荐
—责编:解恩泽—
—推送:施竞澄—

