Stata编程及解释 下载本文

Stata编程及解释 调整变量格式:

format x1 .3f ——将x1的列宽固定为10,小数点后取三位 format x1 .3g ——将x1的列宽固定为10,有效数字取三位 format x1 .3e ——将x1的列宽固定为10,采用科学计数法

format x1 .3fc ——将x1的列宽固定为10,小数点后取三位,加入千分位分隔符

format x1 .3gc ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符

format x1 %-10.3gc ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符,加入―-‖表示左对齐 合并数据:

use \桌面\\2006.dta\ merge using \桌面\\1999.dta\

——将1999和2006的数据按照样本(observation)排列的自然顺序合并起来 use \桌面\\2006.dta\

merge id using \桌面\\1999.dta\

——将1999和2006的数据按照唯一的(unique)变量id来合并,在合并时对id进行排序(sort)

建议采用第一种方法。 对样本进行随机筛选: sample 50

在观测案例中随机选取50%的样本,其余删除 sample 50,count

在观测案例中随机选取50个样本,其余删除 查看与编辑数据:

browse x1 x2 if x3>3 (按所列变量与条件打开数据查看器) edit x1 x2 if x3>3 (按所列变量与条件打开数据编辑器) 数据合并(merge)与扩展(append)

merge表示样本量不变,但增加了一些新变量;append表示样本总量增加了,但变量数目不变。 one-to-one merge:

数据源自stata tutorial中的exampw1和exampw2 第一步:将exampw1按v001~v003这三个编码排序,并建立临时数据库tempw1 clear

use \ su ——summarize的简写 sort v001 v002 v003 save tempw1

第二步:对exampw2做同样的处理 clear

use \ su

sort v001 v002 v003

save tempw2

第三步:使用tempw1数据库,将其与tempw2合并: clear

use tempw1

merge v001 v002 v003 using tempw2 第四步:查看合并后的数据状况: ta _merge ——tabulate _merge的简写 su

第五步:清理临时数据库,并删除_merge,以免日后合并新变量时出错 erase tempw1.dta erase tempw2.dta drop _merge

数据扩展append:

数据源自stata tutorial中的fac19和newfac clear

use \ ta region

append using \ ta region

合并后样本量增加,但变量数不变

茎叶图:

stem x1,line(2) (做x1的茎叶图,每一个十分位的树茎都被拆分成两段来显示,前半段为0~4,后半段为5~9) stem x1,width(2) (做x1的茎叶图,每一个十分位的树茎都被拆分成五段来显示,每个小树茎的组距为2)

stem x1,round(100) (将x1除以100后再做x1的茎叶图) 直方图

采用auto数据库

histogram mpg, discrete frequency normal xlabel(1(1)5) (discrete表示变量不连续,frequency表示显示频数,normal加入正太分布曲线,xlabel设定x轴,1和5为极端值,(1)为单位) histogram price, fraction norm

(fraction表示y轴显示小数,除了frequency和fraction这两个选择之外,该命令可替换为―percent‖百分比,和―density‖密度;未加上discrete就表示将price当作连续变量来绘图)

histogram price, percent by(foreign)

(按照变量―foreign‖的分类,将不同类样本的―price‖绘制出来,两个图分左右排布)

histogram mpg, discrete by(foreign, col(1))

(按照变量―foreign‖的分类,将不同类样本的―mpg‖绘制出来,两个图分上下排布)

histogram mpg, discrete percent by(foreign, total) norm

(按照变量―foreign‖的分类,将不同类样本的―mpg‖绘制出来,同时绘出样本整

体的―总‖直方图) 二变量图:

graph twoway lfit price weight || scatter price weight

(作出price和weight的回归线图——―lfit‖,然后与price和weight的散点图相叠加)

twoway scatter price weight,mlabel(make)

(做price和weight的散点图,并在每个点上标注―make‖,即厂商的取值) twoway scatter price weight || lfit price weight,by(foreign) (按照变量foreign的分类,分别对不同类样本的price和weight做散点图和回归线图的叠加,两图呈左右分布)

twoway scatter price weight || lfit price weight,by(foreign,col(1)) (按照变量foreign的分类,分别对不同类样本的price和weight做散点图和回归线图的叠加,两图呈上下分布)

twoway scatter price weight [fweight= displacement],msymbol(oh)

(画出price和weight的散点图,―msybol(oh)‖表示每个点均为中空的圆圈,[fweight= displacement]表示每个点的大小与displacement的取值大小成比例) twoway connected y1 time,yaxis(1) || y2 time,yaxis(2)

(画出y1和y2这两个变量的时间点线图,并将它们叠加在一个图中,左边―yaxis(1)‖为y1的度量,右边―yaxis(2)‖为y2的) twoway line y1 time,yaxis(1) || y2 time,yaxis(2) (与上图基本相同,就是没有点,只显示曲线)

graph twoway scatter var1 var4 || scatter var2 var4 || scatter var3 var4 (做三个点图的叠加)

graph twoway line var1 var4 || line var2 var4 || line var3 var4 (做三个线图的叠加)

graph twoway connected var1 var4 || connected var2 var4 || connected var3 var4 (叠加三个点线相连图) 更多变量:

graph matrix a b c y

(画出一个散点图矩阵,显示各变量之间所有可能的两两相互散点图) graph matrix a b c d,half

(生成散点图矩阵,只显示下半部分的三角形区域) 用auto数据集:

graph matrix price mpg weight length,half by( foreign,total col(1) )

(根据foreign变量的不同类型绘制price等四个变量的散点图矩阵,要求绘出总图,并上下排列】=具) 其他图形:

graph box y,over(x) yline(.22)

(对应x的每一个取值构建y的箱型图,并在y轴的0.22处划一条水平线) graph bar (mean) y,over(x)

对应x的每一个取值,显示y的平均数的条形图。括号中的―mean‖也可换成median、sum、sd、p25、p75等 graph bar a1 a2,over(b) stack

(对应在b的每一个取值,显示a1和a2的条形图,a1和a2是叠放成一根条形

柱。若不写入―stack‖,则a1和a2显示为两个并排的条形柱) graph dot (median)y,over(x)

(画点图,沿着水平刻度,在x的每一个取值水平所对应的y的中位数上打点) qnorm x

(画出一幅分位-正态标绘图) rchart a1 a2 a2

(画出质量控制R图,显示a1到a3的取值范围)

简单统计量的计算: ameans x

(计算变量x的算术平均值、几何平均值和简单调和平均值,均显示样本量和置信区间)

mean var1 [pweight = var2]

(求取分组数据的平均值和标准误,var1为各组的赋值,var2为每组的频数) summarize y x1 x2,detail

(可以获得各个变量的百分比数、最大最小值、样本量、平均数、标准差、方差、峰度、偏度) ***注意***

stata中summarize所计算出来的峰度skewness和偏度kurtosis有问题,与ECELL和SPSS有较大差异,建议不采用stata的结果。 summarize var1 [aweight = var2], detail

(求取分组数据的统计量,var1为各组的赋值,var2为每组的频数) tabstat X1,stats(mean n q max min sd var cv)

(计算变量X1的算术平均值、样本量、四分位线、最大最小值、标准差、方差和变异系数) 概率分布的计算:

(1)贝努利概率分布测试: webuse quick

bitest quick==0.3,detail

(假设每次得到成功案例?1‘的概率等于0.3,计算在变量quick所显示的二项分布情况下,各种累计概率和单个概率是多少) bitesti 10,3,0.5,detail

(计算当每次成功的概率为0.5时,十次抽样中抽到三次成功案例的概率:低于或高于三次成功的累计概率和恰好三次成功概率) (2)泊松分布概率: display poisson(7,6) .44971106

(计算均值为7,成功案例小于等于6个的泊松概率) display poissonp(7,6) .14900278

(计算均值为7,成功案例恰好等于6个的泊松概率) display poissontail(7,6) .69929172

(计算均值为7,成功案例大于等于6个的泊松概率)

(3)超几何分布概率:

display hypergeometricp(10,3,4,2) .3

(计算在样本总量为10,成功案例为3的样本总体中,不重置地抽取4个样本,其中恰好有2个为成功案例的概率) display hypergeometric(10,3,4,2) .96666667

(计算在样本总量为10,成功案例为3的样本总体中,不重置地抽取4个样本,其中有小于或等于2个为成功案例的概率) 检验极端值的步骤:

常见命令:tabulate、stem、codebook、summarize、list、histogram、graph box、gragh matrix

step1.用codebook、summarize、histogram、graph boxs、graph matrix、stem看检验数据的总体情况: codebook y x1 x2

summarize y x1 x2,detail

histogram x1,norm(正态直方图) graph box x1(箱图)

graph matrix y x1 x2,half(画出各个变量的两两x-y图) stem x1(做x1的茎叶图)

可以看出数据分布状况,尤其是最大、最小值 step2.用tabulate、list细致寻找极端值

tabulate code if x1==极端值(作出x1等于极端值时code的频数分布表,code表示地区、年份等序列变量,这样便可找出那些地区的数值出现了错误)

list code if x1==极端值(直接列出x1等于极端值时code的值,当x1的错误过多时,不建议使用该命令)

list in -20/l(l表示last one,-20表示倒数第20个样本,该命令列出了从倒数第20个到倒数第一个样本的各变量值) step3.用replace命令替换极端值 replace x1=? if x1==极端值 去除极端值: keep if y<1000 drop if y>1000 对数据排序: sort x gsort +x

(对数据按x进行升序排列) gsort -x

(对数据按x进行降序排列) gsort -x, generate(id) mfirst

(对数据按x进行降序排列,缺失值排最前,生成反映位次的变量id) 对变量进行排序: order y x3 x1 x2

(将变量按照y、x3、x1、x2的顺序排列)

生成新变量:

gen logx1=log(x1)(得出x1的对数) gen x1`=exp(logx1)(将logx1反对数化)

gen r61_100=1 if rank>=61&rank<=100(若rank在61与100之间,则新变量r61_100的取值为1,其他为缺失值)

replace r61_100 if r61_100!=1(―!=‖表示不等于,若r61_100取值不为1,则将r61_100替换为0,就是将上式中的缺失值替换为0) gen abs(x)(取x的绝对值)

gen ceil(x)(取大于或等于x的最小整数) gen trunc(x)(取x的整数部分) gen round(x)(对x进行四舍五入)

gen round(x,y)(以y为单位,对x进行四舍五入) gen sqrt(x)(取x的平方根) gen mod(x,y)(取x/y的余数)

gen reldif(x,y)(取x与y的相对差异,即|x-y|/(|y|+1)) gen logit(x)(取ln[x/(1-x)])

gen x=autocode(x,n,xmin,xmax)(将x的值域,即xmax-xmin,分为等距的n份) gen x=cond(x1>x2,x1,x2)(若x1>x2成立,则取x1,若x1>x2不成立,则取x2) sort x

gen gx=group(n)(将经过排序的变量x分为尽量等规模的n个组) egen zx1=std(x1)(得出x1的标准值,就是用(x1-avgx1)/sdx1) egen zx1=std(x1),m(0) s(1)(得出x1的标准分,标准分的平均值为0,标准差为1) egen sdx1=sd(x1)(得出x1的标准差)

egen meanx1=mean(x1)(得出x1的平均值) egen maxx1=max(x1)(最大值) egen minx1=min(x1)(最小值) egen medx1=med(x1)(中数) egen modex1=mode(x1)(众数)

egen totalx1=total(x1)(得出x1的总数)

egen rowsd=sd(x1 x2 x3)(得出x1、x2和x3联合的标准差)

egen rowmean=mean(x1 x2 x3)(得出x1、x2和x3联合的平均值) egen rowmax=max(x1 x2 x3)(联合最大值) egen rowmin=min(x1 x2 x3)(联合最小值) egen rowmed=med(x1 x2 x3)(联合中数) egen rowmode=mode(x1 x2 x3) (联合众数) egen rowtotal=total(x1 x2 x3)(联合总数)

egen xrank=rank(x)(在不改变变量x各个值排序的情况下,获得反映x值大小排序的xrank)

数据计算器display命令:

display x[12](显示x的第十二个观察值)

display chi2(n,x)(自由度为n的累计卡方分布)

display chi2tail(n,x)(自由度为n的反向累计卡方分布,chi2tail(n,x)=1-chi2(n,x)) display invchi2(n,p)(卡方分布的逆运算,若chi2(n,x)=p,那么invchi2(n,p)=x) display invchi2tail(n,p)(chi2tail的逆运算)

display F(n1,n2,f)(分子、分母自由度分别为n1和n2的累计F分布)

display Ftail(n1,n2,f)(分子、分母自由度分别为n1和n2的反向累计F分布) display invF(n1,n2,P)(F分布的逆运算,若F(n1,n2,f)=p,那么invF(n1,n2,p)=f) display invFtail(n1,n2,p)(Ftail的逆运算) display tden(n,t)(自由度为n的t分布)

display ttail(n,t)(自由度为n的反向累计t分布) display invttail(n,p)(ttail的逆运算)

给数据库和变量做标记:

label data \(对现用的数据库做标记,\就是标记,可自行填写) label variable x \(对变量x做标记)

label values x label1(赋予变量x一组标签:label1)

label define label1 1 \(定义标签的具体内容:当x=1时,标记为a1,当x=2时,标记为a2) 频数表:

tabulate x1,sort

tab1 x1-x7,sort(做x1到x7的频数表,并按照频数以降序显示行) table c1,c(n x1 mean x1 sd x1)(在分类变量c1的不同水平上列出x1的样本量和平均值)

二维交互表: auto数据库:

table rep78 foreign, c(n mpg mean mpg sd mpg median mpg) center row col

(rep78,foreign均为分类变量,rep78为行变量,foreign为列变量,center表示结果显示在单元格中间,row表示计算行变量整体的统计量,col表示计算列变量整体的统计量) tabulate x1 x2,all

(做x1和x2的二维交互表,要求显示独立性检验chi2、似然比卡方独立性检验lrchi2、对定序变量适用的等级相关系数gamma和taub、以及对名义变量适用的V)

tabulate x1 x2,column chi2(做x1和x2的二维交互表,要求显示列百分比和行变量和列变量的独立性检验——零假设为变量之间独立无统计关系) tab2 x1-x7,all nofreq(对x1到x7这七个变量两两地做二维交互表,不显示频数:nofreq)

三维交互表:

by x3,sort:tabulate x1 x2,nofreq col chi2(同时进行x3的每一个取值内的x1和x2的二维交互表,不显示频数、显示列百分比和独立性检验) 四维交互表:

table x1 x2 x3,c(ferq mean x1 mean x2 mean x3) by(x4)

tabstat X1 X2,by(X3) stats(mean n q max min sd var cv) col(stats)

tabstat X1 X2,by(X3) stats(mean range q sd var cv p5 p95 median),[aw=X4](以X4为权重求X1、X2的均值,标准差、方差等)

ttest X1=1 count if X1==0

count if X1>=0 gen X2=1 if X1>=0

corr x1 x2 x3(做x1、x2、x3的相关系数表)

swilk x1 x2 x3(用Shapiro-Wilk W test对x1、x2、x3进行正太性分析) sktest x1 x2 x3(对x1、x2、x3进行正太性分析,可以求出峰度和偏度) ttest x1=x2(对x1、x2的均值是否相等进行T检验)

ttest x1,by(x2) unequal(按x2的分组方式对x1进行T检验,假设方差不齐性) sdtest x1=x2(方差齐性检验)

sdtest x1,by(x2)(按x2的分组方式对x1进行方差齐性检验)

聚类分析:

cluster kmeans y x1 x2 x3, k(3)

——依据y、x1、x2、x3,将样本分为n类,聚类的核为随机选取 cluster kmeans y x1 x2 x3, k(3) measure(L1) start(everykth)

—— \用于确定聚类的核,\表示将通过构造三组样本获得聚类核:构造方法为将样本id为1、1+3、1+3×2、 1+3×3……分为一组、将样本id为2、2+3、2+3×2、2+3×3……分为第二组,以此类推,将这三组的均值作为聚类的核;\用 于计算相似性和相异性的方法,\表示采用欧式距离的绝对值,也直接可采用欧式距离(L2)和欧式距离的平方(L2squared)。PS:这个方法 所得的结果与SPSS所得结果相同。

sort c1 c2(对c1和c2两个分类变量排序)

by c1 c2:reg y x1 x2 x3(在c1、c2的各个水平上分别进行回归)

bysort c1 c2:reg y x1 x2 x3 if c3=1(逗号前面相当于将上面两步骤合一,既排序又回归,逗号后面的―if c3=1‖表示只有在c3=1的情况下才进行回归) stepwise, pr(.2): reg y x1 x2 x3(使用Backward selection,去除P值大于0.2时变量) stepwise, pe(.2): reg y x1 x2 x3(使用forward selection,去除P值小于0.2时变量) stepwise, pr(.2) pe(.01):reg y x1 x2 x3(使用backward-stepwise selection,取P值在0.01和0.2之间的变量)

stepwise, pe(.2) forward: reg y x1 x2 x3(使用forward-stepwise selection) reg y x1 x2 x3 predict Yhat,xb predict u,resid

predict ustd,stdr(获得残差的标准误) predict std,stdp(获得y估计值的标准误) predict stdf,stdf(获得y预测值的标准误)

predict e,e(1,12)(获得y在1到12之间的估计值) predict p,pr(1,12)(获得y在1到12之间的概率) predict rstu,rstudent(获得student的t值) predict lerg,leverage(获得杠杆值) predict ckd,cooksd(获得cooksd) reg y x1 x2 x3 c1 c2 adjust x1 x2 x3,se(使得变量x1、x2和x3等于其均值,求y的预测值和标准误) adjust x1 x2 x3,stdf ci(使得变量x1、x2和x3等于其均值,求y的预测值,预

测标准误和置信区间)

adjust x1 x2,by(c1) se ci(控制变量x1、x2,亦即取它们的均值,在分类变量c1的不同水平上求y预测值,标准误和置信区间)

adjust x1 x2 x3,by(c1) stdf ci(控制变量x1、x2、x3,亦即取它们的均值,在分类变量c1的不同水平上求y预测值,预测标准误和置信区间)

adjust x1 x2,by(c1 c2) se ci(控制变量x1、x2,在分类变量c1、c2的不同水平上求y的预测值,标准误和置信区间)

adjust x1 x2 x3,by(c1 c2) stdf ci(控制变量x1、x2、x3,在分类变量c1、c2的不同水平上求y的预测值,预测标准误和置信区间)

adjust x1=a x2=b x3=c,se ci(当x1=a、x2=b、x3=c时,求y的预测值、标准误和置信区间)

adjust x1=a x2=b x3=c,by(c1) se ci(当x1=a、x2=b、x3=c时,在分类变量c1的不同水平上,求y的预测值、标准误和置信区间) adjust x1=a x2=b c1=1,by(c1) se ci(当x1=a、x2=b,并假设所有的样本均为c1=1,求在分类变量c1的不同水平上,因为变量x3的均值不同,而导致的y的不同的预测值……)

mvreg Y1 Y2 ……: X1 X2 X3……(多元回归) mvreg y1 y2 y3: x1 x3 x3(多元回归分析,y1 y2 y3为因变量,x1 x3 x3为自变量) 以下命令只有在进行了mvreg之后才能进行 test [y1](测试对y1的回归系数联合为0)

test [y1]: x1 x2(测试对y1的回归中x1、x2的系数为0)

test x1 x2 x3(测试在所有的回归中,x1、x2、x3的系数均为0) test [y1=y2](对y1的回归和对y2的回归系数相等)

test [y1=y2]: x1 x2 x3, mtest(对y1和y2的回归中,分别测试x1、x2、x3的系数是否相等,若没有mtest这个命令,则测试他们的联和统计)

test [y1=y2=y3](三个回归的系数是否相等,可加mtest以分别测试)

test [y1=y2=y3]: x1 x2 (测试三个回归中的x1、x2是否相等,可加mtest) est命令的用法:

(1)储存回归结果:

reg y x1 x2 x3(不限于reg,也可储存ivreg、mvreg、reg3) est store A

(2)重现回归结果: est replay A

(3)对回归结果进行进一步分析

est for A:sum(对A回归结果中的各个变量运行sum命令) 异方差问题: 获得稳健性标准误

reg y x1 x2 x3 if c1==1(当分类变量c1=1时,进行y和诸x的回归) reg y x1 x2 x3,robust(回归后显示各个自变量的异方差-稳健性标准误) estat vif(回归之后获得VIF) estat hettest,mtest(异方差检验) 异方差检验的套路: (1)Breusch-pagan法: reg y x1 x2 x3

predict u,resid gen usq=u^2 reg usq x1 x2 x3 求F值

display R/(1-R)*n2/n1(n1表示分子除数,n2表示分母除数) display Ftail(……) 求LM值

display R*n(n表示总样本量) display chi2tail(……) (2)white法: reg y x1 x2 x3 predict u,resid gen usq=u^2 predict y gen ysq=y^2 reg usq y ysq 求F值

display R/(1-R)*n2/n1(n1表示分子除数,n2表示分母除数) display Ftail(……) 求LM值

display R*n(n表示总样本量) display chi2tail(……) (3)必要补充

F值和LM值转换为P值的命令:

display Ftail(n1,n2,a)(利用F值求p值,n1表示分子除数,n2表示分母除数,a为F值)

display chi2tail(n3,b)(利用LM值求p值,n3表示自由度的损失量,一般等于n1,b为LM值)

异方差的纠正——WLS(weighted least square estimator) (1)基本思路:

reg y x1 x2 x3 [aw=x1](将x1作为异方差的来源,对方程进行修正) 上式相当于:

reg y/(x1^0.5) 1/(x1^0.5) x1/(x1^0.5) x2/(x1^0.5) x3/(x1^0.5),noconstant (2)纠正异方差的常用套路(构造h值) reg y x1 x2 x3 predict u,resid gen usq=u^2

gen logusq=log(usq) reg logusq x1 x2 x3 predict g gen h=exp(g)

reg y x1 x2 x3 [aw=1/h] 异方差hausman检验: reg y x1 x2 x3

est store A(将上述回归结果储存到A中) reg y x1 x2 x3 [aw=1/h] est store B hausman A B

当因变量为对数形式时(log(y))如何预测y reg logy x1 x2 x3 predict k

gen m=exp(k) reg y m,noconstant m的系数为i y的预测值=i×exp(k)

方差分析: 一元方差分析

anova y g1 / g1|g2 /(g*表示不同分类变量,计算g1和交互项/ g1|g2 /这两种分类的y值是否存在组内差异)

anova y d1 d2 d1*d2(d*表示虚拟变量,计算d1、d2和d1*d2的这三种分类的y值是否有组内差异)

anova y d1 d2 x1 d2*x1, continuous(x1)(x*表示连续的控制变量) 多元方差分析 webuse jaw

manova y1 y2 y3 = gender fracture gender*fracture(按性别、是否骨折及二者的交互项对y1、y2和y3进行方差分析)

manova y1 = gender fracture gender*fracture(相当于一元方差分析,以y1为因变量)

———————————— webuse nobetween gen mycons = 1

manova test1 test2 test3 = mycons, noconstant mat c = (1,0,-1 \\ 0,1,-1)

manovatest mycons, ytransform(c) 进行多元回归的方法: 多元回归分析:(与mvreg相同)

foreach vname in y1 y2 y3 { (确定y变量组vname)

reg `vname' x1 x2 x3 (将y变量组中的各个变量与诸x变量进行回归分析,注意vname的标点符号) }

上式等价于:

mvreg y1 y2 y3 = x1 x2 x3

reg3命令:

(1)简单用法:

reg3 (y1 = x1 x2 x3) (y2 = x1 x3 x4) (y3 = x1 x2 x5) 测试y1 coefs = 0

test [y1]

测试不同回归中相同变量的系数: test [y1=y2=y3], common

test ([y1=y2]) ([y1=y3]), common constant(constant表示包含截距项) (2)用reg3进行2SLS

reg3 (y1 = y2 x1 x2) (y2 = y1 x4),2sls (2)用reg3进行OLS

reg3 (y1 = y2 x1 x2) (y2 = y1 x4),ols 对两个回归结果进行hausman检验: reg3 (y1=x1 x2 x3)(y2=y1 x4),2sls est store twosls

reg3 (y1=x1 x2 x3)(y2=y1 x4),ols est store ols

hausman twosls ols,equations(1:1)(对两次回归中的方程1,即―y1=x1 x2 x3‖进行hausman检验)

hausman twosls ols,equations(2:2)(对两次回归中的方程2,即―y2=y1 x4‖进行hausman检验)

hausman twosls ols,alleqs(对所有方程一起进行检验) 检验忽略变量(模型的RESET): reg y x1 x2 x3 estat ovtest

滞后变量的制取 对变量y滞后一期: gen y_l1=y[_n-1] 滞后两期:

gen y_l2=y[_n-2] 以此类推。

制取样本序号: gen id=_n

获得样本总量: gen id=_N

时间序列回归:

回归元严格外生时AR(1)序列相关的检验 reg y x1 x2 predict u,resid gen u_1=u[_n-1] reg u u_1,noconstant

回归之后,u_1的序数如果不异于零,则该序列不相关 用Durbin-Watson Statistics检验序列相关:

tsset year @(对时间序列回归中代表时间的变量进行定义)@ reg y x1 x2

dwstat @(求出时间序列回归的DW值)@ durbina @(对该回归是否具有序列相关进行检验,H0为无序列相关,可根据chi2

值求出P值)@

durbina,small @(small可以根据F值求出P值,以代替chi2值)@ durbina,force @(让检验能在robust、neway之后进行)@ durbina,small lag(1/10) @(lag可以求出更高阶滞后的序列相关,如本例中可求出1到10阶的序列相关)@

durbina,robust lag(1/10) @(robust可进行异方差—稳健性回归,避免未知形式的异方差)@

bgodfrey @(利用Breusch-Godfrey test求出高阶序列相关)@ bgodfrey,small lag(1/10) 数据调查:survey data

源数据:dataset文件夹中的svydata 步骤:

1、定义survey data

svyset psuid [pweight=finalwgt], strata(stratid)

——定义primary sampling unit为psuid。可能是测试的编号,1or2 ——定义pweight为finalwgt

——定义stratum identifer为stratid。可能是测试中被试的编号,1to31 2、生成male

gen male= (sex==1) if !missing(sex)

——当sex不缺失且等于1时,male=sex

3、生成行变量为highbp,列变量为sizplace的表格

svy, subpop(male): tabulate highbp sizplace, col obs pearson lr null wald ——subpop规定了以male为数据调查的范围

——tabulate highbp sizplace表示绘制行变量为highbp,列变量为sizplace的表格 ——col表示每一列的加总为100%,row表示每一行的加总为100%,cell表示横纵所有单元格的加总为100%

——obs表示列出每个单元格的样本量,se表示列出每个单元格的标准误,ci表示列出每个单元格的置信区间

——pearson表示求取pearson's chi-squired,皮尔逊的卡方检定 ——lr表示求取likelihood ratio

——null表示求取null-based statistics

——wald表示求取adjusted wald,llwald表示求取adjusted log-linear Wald,noadjust表示求取unadjusted Wald statistics 4、svy:mean x1 x2 x3

——对x1、x2、x3求取mean、se和ci

5、简单的tabulate twoway(不用svyset就可执行) tab2 y x,col chi2 exact lr

——col、cell、row等均可换用,chi2指的是Pearson's chi-squared、exact指的是fisher exact test、lr指的是likelihood-ratio chi-squared 6、svy的其他用法: svy:reg y x

建立人工数据集:

创建一个包含从独立标准正态分布中抽取的2000个观察案例和三个随机Z1、Z2、Z3,并分别定义他们的平均值和标准差。

matrix m=(0,2,3) ——定义三个变量的平均值 matrix sd=(1,.5,2) ——定义三个变量的标准差

drawnorm z1 z2 z3,n(2000) means(m) sds(sd) ——创建样本量为2000,均值和标准差符合上面定义的数据集

补充:除了定义均值和标准差之外,还可定义相关矩阵和协方差矩阵等。 logit回归

logit y x1 x2 x3

——y必须为二分变量

glogit outcomedata populationdata x1 x2 x3

——outcomedata为目标样本总量,populationdata为观测样本总量,outcomedata/populationdata的值便是一个概率,相当于logit命令中的y 面板数据(Panel Data) 1、基本套路: xtreg y x1 x2,re est store re

xtreg y x1 x2,fe est store fe hausman re fe

——如果hausman检验的结果为显著,则采用固定效应(fe)模型,不显著,则选取随机效应(re)模型 2、随机效应的检验: xtreg y x1 x2,re xttest0 xttest1

——xttest1是xttest0的扩展,若这xttest0的结果为显著,则采用随机效应(re)模型

xttest1的假设是没有随机效应和/或没有序列相关,它的七个结果分别表示: 1) LM Test for random effects, assuming no serial correlation (假设没有序列相关情况下对随机效应进行LM检验)

2) Adjusted LM test for random effects, which works even under serial correlation

(假设有序列相关的情况下对随机LM检验) 3) One sided version of the LM test for random effects

(假设没有序列相关的情况下对随机效应进行单边检验) 4) One sided version of the adjusted LM test for random effects (假设有序列相关的情况下对随机效应进行单边检验)

5) LM test for first-order serial correlation, assuming no random effects (假设没有随机效应的情况下对一阶序列相关进行检验)

6) Adjusted test for first-order serial correlation, which works even under random effects

(假设有随机效应的情况下对一阶序列相关进行检验) 7) LM Joint test for random effects and serial correlation (随机效应和序列相关的联合检验)

3、固定效应模型,可采用广义最小二乘法(gls)进行估算,也可采用固定效应

方程(fe): xtserial y x1 x2 xtgls y x1 x2 xttest2 xttest3

——xtserial用于检验固定效应模型中的一阶序列自相关,可通用于xtgls和fe之前

——xttest2用于检验不同厂商的相似性,若显著则各厂家的截面相似,可通用于xtgls和fe之后

——xttest3用于检验固定效应模型中的异方差问题,若显著则有异方差,可通用于xtgls和fe之后

Stata: 输出regression table到word和excel

1. 安装estout。最简单的方式是在stata的指令输入: ssc install estout, replace

EST安装的指导网址是:http://repec.org/bocode/e/estout/installation.html 2.跑你的regression

3.写下这行指令esttab using test.rtf,然后就会出现个漂亮的表格给你(WORD文档)。只要再小幅修改,就可以直接用了。这个档案会存在my document\\stata下。如果你用打开的是一个stata do file,结果会保存到do文件所在文件夹中。如果要得到excel文件,就把后缀改为.xls或者.csv就可以了

4.跑多个其实也不难,只要每跑完一个regression,你把它取个名字存起来:est store m1。m1是你要改的,第一个model所以我叫m1,第二个的话指令就变成est store m2,依次类推。

5.运行指令:esttab m1 m2 ... using test.rtf就行了。

异方差的检验:

Breusch-Pagan test in STATA:

其基本命令是:estat hettest var1 var2 var3

其中,var1 var2 var3 分别为你认为导致异方差性的几个自变量。是你自己设定的一个

滞后项数量。

同样,如果输出的P-Value 显著小于0.05,则拒绝原假设,即不存在异方差性。

White检验:

其基本命令是在完成基本的OLS 回归之后,输入

imtest, white

如果输出的P-Value 显著小于0.05,则拒绝原假设,即不存在异方差性

处理异方差性问题的方法:

方法一:WLS

WLS是GLS(一般最小二乘法)的一种,也可以说在异方差情形下的GLS就是WLS。在WLS下,我们设定扰动项的条件方差是某个解释变量子集的函数。之所以被称为加权最小二乘法,是因为这个估计最小化的是残差的加权平方和,而上述函数的倒数恰为其权重。

在stata中实现WLS的方法如下:

reg (被解释变量) (解释变量1) (解释变量2)…… [aweight=变量名]

其中,aweight后面的变量就是权重,是我们设定的函数。

一种经常的设定是假设扰动项的条件方差是所有解释变量的某个线性组合的指数函数。在stata中也可以方便地实现:

首先做标准的OLS回归,并得到残差项;

reg (被解释变量) (解释变量1) (解释变量2)…… predict r, resid

生成新变量logusq,并用它对所有解释变量做回归,得到这个回归的拟合值,再对这个拟合值求指数函数;

gen logusq=ln(r^2)

reg logusq (解释变量1) (解释变量2)…… predict g, xb gen h=exp(g)

最后以h作为权重做WLS回归;

reg (被解释变量) (解释变量1) (解释变量2)…… [aweight=h]

如果我们确切地知道扰动项的协方差矩阵的形式,那么GLS估计是最小方差线性无偏估计,是所有线性估计中最好的。显然它比OLS更有效率。虽然GLS有很多好处,但有一个致命弱点:就是一般而言我们不知道扰动项的协方差矩阵,

因而无法保证结果的有效性。

方法二:HC SE

There are 3 kinds of HC SE

(1)Huber-White Robust Standard Errors HC1, 其基本命令是:

reg var1 var2 var3, robust

White(1980)证明了这种方法得到的标准误是渐进可用(asymptotically valid)的。这种方法的优点是简单,而且需要的信息少,在各种情况下都通用。缺点是损失了一些效率。这种方法在我们日常的实证研究中是最经常使用。

(2)MacKinnon-White SE HC2,其基本命令是:

reg var1 var2 var3, hc2

(3)Long-Ervin SE HC3,其基本命令是:

reg var1 var2 var3, hc3

约束条件检验:

如果需要检验两个变量,比如x 与y,之间系

数之间的关系,以检验两者系数相等为例,我们可以直接输入命令:

test x=y

再如检验两者系数之和等于1,我们可以直接输入命令:

test x+y=1

如果输出结果对应的P-Value 小于0.05,则说明原假设显著不成立,即拒绝原假设。

序列相关性问题的检验与处理

序列相关性问题的检验:

首先,要保证所用的数据必须为时间序列数据。如果原数据不是时间序列数据, 则需要进行必要的处理,最常用的方法就是: gen n=_n tsset n

这两个命令的意思是,首先要生成一个时间序列的标志变量n(或者t 也可以); 然后通过tsset 命令将这个数据集定义为依据时间序列标志变量n定义的时间序

列数据。

最直观的检验方式是通过观察残差分布,其基本步骤是在跑完回归之后,直接输 入

Predict error, stdp

这样就得到了残差值;然后输入命令: plot error n

会得到一个error 随n 变化的一个散点图。

D-W检验——对一阶自相关问题的检验:

D-W检验是对一阶自相关问题的常用检验方法,但是如果实际问题中存在高阶 序列相关性问题,则不能用这个检验方法。 D-W 检验的命令如下: 首先,输入回归命令,

reg Variable1 Variable2 Variable3…VariableM

输出一个简单的OLS估计结果。然后,再输入命令:

dwstat

这时会输出一个DW 统计量。通过与临界值之间的比较,可以得出结论。也可 以执行如下命令

estat durbinalt

直接进行Durbin检验。

Breusch-GodfreyTest in STATA——检验高阶序列相关性:

在得到一个基本回归结果和error 之后,我们假设这样一个关系: et = α0 + α1 et-1 + α2 et-2 …+ αk et-p + β1 x1t + β2 x2t … +βk xkt +εt BG 检验的原假设是:H0 : α1 = α2 = … αp =0。 其基本命令是: bgodfrey , lags(p)

其中p 是你自己设定的一个滞后项数量。如果输出的p-value 显著小于0.05,则 可以拒绝原假设,这就意味着模型存在p 阶序列相关性;如果输出的p-value 显 著大于0.05 甚至很大,则可以接受原假设,即不存在p 阶序列相关性。

处理序列相关性问题的方法——GLS: 常用的几种GLS 方法:

(1) Cochrane-Orcutt estimator 和Prais-Winsten estimator 其基本命令是

prais var1 var2 var3, corc

(2) Newey-West standard errors 其基本命令是

newey var1 var2 var3, lag(3)

其中,lag(3)意思是对三阶序列相关性问题进行处理;如果需要对p 阶序列相

关性问题进行处理,则为lag(p)

t因变量,g,f,c是自变量,_26存放了弟26个观测值,为需要预测的值

reg t g f c if _n!=26

点预测

predict taxpredict if _n==26

均值的区间预测

predictnl py=predict(xb),ci(lb ub) l(95)

因变量的区间预测

adjust g=117251.9 f=24649.95 c=99.9,stdf ci level(95)

Hausman检验是检验内生性的最常用的方法。它是通过比较一致估计量与有效估计量的Wald统计量。 命令格式为:

.hausman name-constistent [name-efficent] [,options]

其中,name-cosistent指一致估计的结果, name-efficent 指有效估计的结果。注意,一致、有效估计量的先后顺序不能改变。

Option选项:

constant 计算检验统计量将常数也包括在内,默认值为排除常数 allegs 利用所有方程进行检验,默认只对第一个方程进行检验 skipeqs(eqlist) eqlist只能以方程名称而不能以方程序号表示 equation(matchlist) 比较设定的方程。 force 即使假设条件不满足仍进行检验

df(#) 默认值为一致估计与有效估计的协方差矩阵的差的估计 sigmamore 协方差矩阵采用有效估计量的协方差矩阵 sigmaless 协方差矩阵采用一致估计量的协方差矩阵 tconsistent(string) 一致估计量的标题 tefficient(string) 有效估计量的标题

工具变量估计 命令格式:

.ivregress esitimator depvar [varlist1] [varlist2=varlist_iv] [if] [in] [weight][,options] 其中,estimator包括2sls,gmm,liml三种。varlist1为模型中的外生变量,varlist2为模型中的内生变量,varlist_iv为模型中的工具变量。

Nonconstant 不包括常数项 Hascons 用户自己设定常数项 CMM 选项:

wmatrix(wmtype) robust,cluster clustvar,hac kernel, unadjusted center 权数矩阵采用中心矩 igmm 采用迭代GMM估计

eps(#) 参数收敛标准。默认值为eps(le-6)

weps(#) 权数矩阵的收敛标准。默认值为w eps(le-6)

Vce(vcetype) unajusted,robust,cluster clustvar,bootstrap,jackknife,hac kernel level(#)置信区间

First 输出第一阶段的估计结果 Small 小样本下的自由度调整

.estat firststage [,all forcenonrobust]

该命令给出第一阶段的估计结果以及各种统计量,包括排除外生变量的相关性检验。All选项给出所有的拟合优度统计量。如果模型存在多个内生变量,则

stata给出R2、偏R2、调整的R2 、F统计量;如果模型存在多个内生变量,则stata给出Shea偏R2和调整的偏R2。

forcenonrobust给出最小特征值统计量及其临界值,即使采用稳健估计(这一检验的假设条件是误差项为独立正态分布)。

estat overid[,lag(#) forceweights forcenonrobust]

该命令给出了过度识别约束检验。如果使用2sls估计估计,则Stata给Sargan‘s(1958)和Basman‘s(1960)卡方统计量,这也是Wooldridge‘(1995)稳健得分检验。如果采用liml估计方法,则stata给出Anderson and Rubin‘s(1950) 卡方统计量以及Basmann F统计量;如果采用GMM估计,则stata给出hansen‘s(1982)J统计量。Lags(#)用于计算得分检验的HAC(异方差自相关一致)统计量的过程中进行去噪时设定滞后阶数。如果设定lag(0),则表示不进行去噪处理。默认选择为lag(1)。这一选择仅使用于2sls估计方法和设定vce(hac)选项情况。

Forceweight 表示即使采用aweights,pweights或iweights也进行检验。Stata仅对于fweights的情况进行检验,其他权数所得到临界值可能不准确。

Forcenonrobust 指在2sls或LIML估计中即使采用稳健标准差也进行Sargan and Basmann检验(这一检验的假设的假设条件是误差项为独立正态分布)。

例子:

log(wage)=a+b*educ+c*exper+d*expersq+u

怀疑模型教育(educ)具有内生性问题,利用父母接受教育的年数(fatheduc,motheduc)作educ的工具变量估计上述模型。 (1)利用2SLS估计模型

.ivregress 2sls lwage exper expersq (educ=fatheduc motheduc),first

第一阶段回归结果为:

educhat=9.1+0.19fatheduc+0.16motheduc+0.05exper (21.34) (5.62) (4.39) (1.12) - 0.001expersq (-0.84)

第二阶段的估计结果为:

lwagehat=0.05+0.06educ+0.04exper-0.001expersq (0.12) (1.95) (5.29) (-2.24)

(2)检验educ的内生性

.quietly ivreg iwage exper expersq {educ=fatheduc motheduc} .est store IV_reg

.quietly regress lwage exper expersq educ .est store LS_reg

.hausman IV_reg LS_reg

可以得到hausman估计量=2.7,P值=0.44。接受原假设,即educ是外生的。

(3)进行过度识别的约束检验 .estat overid

可得Sargan统计量=0.38,P值=0.54接受原假设。

面板数据估计

首先对面板数据进行声明:

前面是截面单元,后面是时间标识: tsset company year tsset industry year

产生新的变量:gen newvar=human*lnrd 产生滞后变量Gen fiscal(2)=L2.fiscal 产生差分变量Gen fiscal(D)=D.fiscal

描述性统计:

xtdes :对Panel Data截面个数、时间跨度的整体描述

Xtsum:分组内、组间和样本整体计算各个变量的基本统计量 xttab 采用列表的方式显示某个变量的分布

Stata中用于估计面板模型的主要命令:xtreg

xtreg depvar [varlist] [if exp] , model_type [level(#) ]

Model type 模型

be Between-effects estimator fe Fixed-effects estimator

re GLS Random-effects estimator

pa GEE population-averaged estimator

mle Maximum-likelihood Random-effects estimator

主要估计方法:

xtreg: Fixed-, between- and random-effects, and population-averaged linear models xtregar:Fixed- and random-effects linear models with an AR(1) disturbance xtpcse :OLS or Prais-Winsten models with panel-corrected standard errors xtrchh :Hildreth-Houck random coefficients models

xtivreg :Instrumental variables and two-stage least squares for panel-data models

xtabond:Arellano-Bond linear, dynamic panel data estimator xttobit :Random-effects tobit models

xtlogit : Fixed-effects, random-effects, population-averaged logit models xtprobit :Random-effects and population-averaged probit models

xtfrontier :Stochastic frontier models for panel-data xtrc gdp invest culture edu sci health social admin,beta

xtreg命令的应用:

声明面板数据类型:tsset sheng t

描述性统计:xtsum gdp invest sci admin 1.固定效应模型估计:

xtreg gdp invest culture sci health admin techno,fe

固定效应模型中个体效应和随机干扰项的方差估计值(分别为sigma u 和sigma e),二者之间的相关关系(rho)

最后一行给出了检验固定效应是否显著的F 统计量和相应的P 值

2.随机效应模型估计:

xtreg gdp invest culture sci health admin techno,re 检验随机效应模型是否优于混合OLS 模型: 在进行随机效应回归之后,使用xttest0

检验得到的P 值为0.0000,表明随机效应模型优于混合OLS 模型 3. 最大似然估计Ml:

xtreg gdp invest culture sci health admin techno,mle

Hausman检验

Hausman检验究竟选择固定效应模型还是随机效应模型: 第一步:估计固定效应模型,存储结果

xtreg gdp invest culture sci health admin techno,fe est store fe

第二步:估计随机效应模型,存储结果

xtreg gdp invest culture sci health admin techno,re est store re

第三步:进行hausman检验 hausman fe

Hausman检验量为: H=(b-B)′[Var(b)-Var(B)]-1(b-B)~x2(k)

Hausman统计量服从自由度为k的χ2分布。当H大于一定显著水平的临界值时,我们就认为模型中存在固定效应,从而选用固定效应模型,否则选用随机效应模型

如果hausman检验值为负,说明的模型设定有问题,导致Hausman 检验的基本假设得不到满足,遗漏变量的问题,或者某些变量是非平稳等等 可以改用hausman检验的其他形式: hausman fe, sigmaless

对于固定效应模型的异方差检验和序列相关检验: Xtserial gdp invest culture sci health admin techno 异方差检验:

xtreg gdp invest culture sci health admin techno,fe

xttest3 (Modified Wald statistic for groupwise heteroskedasticity in fixed effect model)

随机效应模型的序列相关检验:

xtreg gdp invest culture sci health admin techno,re Xttest1

Xttest1用于检验随机效应(单尾和双尾) 、一阶序列相关以及两者的联合显著 检验结果表明存在随机效应和序列相关,而且对随机效应和序列相关的联合检验也非常显著

可以使用广义线性模型xtgls对异方差和序列相关进行修正:

xtgls gdp invest culture sci health admin techno, panels(hetero),修正异方差

xtgls gdp invest culture sci health admin techno, panels(correlated),修正依横截面而变化的异方差

xtgls gdp invest culture sci health admin techno, panels(hetero) corr(ar1),修正异方差和一阶序列相关ar(1)

Chapter 1. Start STATA

1. Run STATA

Double click the ?STATA 10.0‘ icon in the Window.

2. Define Directory . cd

Show the current directory.

. cd c:\\mydata

Change the directory to the one where you will save the data and output files.

3. Create Output File (LOG File) . capture log close

Close log files, if any, currently open.

. log using c:\\stata10\\mylog, text

Create a new log file on the directory c:\\stata10 under the file name ?mylog‘ in ?text‘ format (mylog.txt). A log file saves all results (including STATA commands you have used) for review. You can copy and paste the results directly from the STATA window or open it using either Notepad or MSWord.

. log using c:\\stata10\\mylog, text replace

Replace the old log file, ?mylog‘, saved already with the updated one under the same

log file name.

. log close

Close the current log file. Any commands and results are saved in the log file, ?mylog‘, before closing.

. log off

Temporarily stop the log file.

. log on

Restart the log file.

4. Create Command File (DO File) Open do-file editor

Type in commands that you want to execute sysuse auto, clear

log using c:\\stata10\\mylog1, text reg mpg length weight foreign sort foreign

by foreign: reg mpg length weight foreign log close

Save the file with an extension ―do.‖ For example, c:\\stata10\\mydo.do

. do c:\\stata10\\mydo.do (or run c:\\stata10\\mydo.do)

Execute the do file, ?mydo‘. Run is different in that it does not display the execution on the screen

Use ―do‖ instead of ―run‖ when generating a log file Chapter 2. Data Input

There are three ways to enter data into STATA.

1. Load a data set . use smoke.dta, clear

Load the dataset ?smoke.dta‘ (which should be saved in a STATA-format) in the memory and clear other data in use from the memory.

2. Data in an Excel format

Copy the selected cells in Excel, open the STATA editor, place the cursor on the cell that will be the upper left corner, and paste.

3. Type the data in . input educ cigpric age

You can input data with variables named ―educ‖ ―cigpric‖ ―age‖. Type in the

following values one by one for each observation. 16 60.506 46 16 57.883 40 12 57.664 58 13.5 57.883 30 10 58.32 17 6 59.34 86 12 57.883 35

Type in ―end‖ to finish inputting

4. Data management . clear

Drop any data set in memory . save c:\\mydata\\smoke

Save the data in a file named ―smoke‖ in directory ―c:\\mydata‖ You can save the file in any other directory of your choice . edit

Allow one to input data or alter the data currently loaded using the STATA editor. Type in the following data for each observation 16 60.506 46 16 57.883 40 12 57.664 58 13.5 57.883 30 10 58.32 17 6 59.34 86 12 57.883 35

Click ―preserve‖ to end

l rename educ schooling change the name of the variable from ―educ‖ to ―schooling‖ l save c:\\stata10\\auto1, replace replace the old data ―smoke‖ with a new (modified) one under the

same data file name

l make a ―raw‖ file using any word processor

Type in the following data (comma- or tab-separated) and save it as a text file named c:\\stata10\\auto2.raw

make, mpg, weight, price AMC Concord, 1, 22, 2930 AMC Pacer, 2, 17, 3350 AMC Spirit, 3, 22, 2640 Buick Century, 4, 20, 3250 Buick Electra, 5, 15, 4080

l clear

l insheet using c:\\stata10\\auto2.raw Loads the data in STATA format l save c:\\stata10\\auto2

l make a ―raw‖ file using any word processor

Type in the following data (space-separated) using notepad or wordpad and save it as a text file named c:\\stata10\\auto3.raw

STATA could not read the double quotation mark in MS Word. But, it can read the double (or single) quotation mark in other basic programs, such as wordpad or notepad.

―AMC Concord‖ 1 22 2930 ―AMC Pacer‖ 2 17 3350 ―AMC Spirit‖ 3 22 2640 ―Buick Century‖ 4 20 3250 ―Buick Electra‖ 5 15 4080

l clear

l infile str14 make id mpg weight using c:\\stata10\\auto3.raw Loads the data in STATA format l save c:\\stata10\\auto3

l make a ―raw‖ file using any word processor

Type in the following data and save it as a text file named c:\\stata10\\auto4.raw AMC Concord 2229304099 AMC Pacer 1733504749 AMC Spirit 2226403799 Buick Century 42032504816 Buick Electra 51540807827

l clear

l infix 1: str18 make 1-18 2: mpg 1-2 weight 3-6 price 7-11 using c:\\stata10\\auto4.raw Loads the data in STATA format l save c:\\stata10\\auto4

l outsheet using c:\\stata10\\auto4

Writes data in a spreadsheet format and saves it in a file named c:\\stata10\\auto4.out l exit

Exist from STATA

5. Other Useful Commands

LARGE FILES

l set memory 512000

Sets the amount of memory STATA will use to be 512000 kilobytes l set memory 512000, permanently

Specifies that in addition to making the change right now, Stata will remember the new limit and use it in the future when l set matsize 800

Sets the maximum number of variables that can be included in any of Stata's estimation commands

l set matsize 800, permanently

Specifies that, in addition to making the change right now, Stata will remember the new limit and use it in the future when l query memory

Displays memory settings

MERGE FILES l sysuse auto

l keep make price mpg l sort make

Must sort before merging l save c:\\stata10\\auto_1 l browse l sysuse auto l drop price mpg l sort make

Must sort before merging l save c:\\stata10\\auto_2

l merge make using c:\\stata10\\auto_1 l tab _merge l browse l

l use c:\\stata10\\auto_1[u1] , clear l drop if make==‖Audi Fox‖ l sort make l save, replace l browse

l use c:\\stata10\\auto_2

l drop if make==‖BMW 320i‖ l drop if make==‖Buick Opel‖ l sort make l save, replace

l merge make using c:\\stata10\\auto_1

If _merge==1, the original file has the observation, but the merging file does not If _merge==2, the merging file has the observation, but the original file does not If _merge==3, both files have the observation l tab _merge browse

RESHAPE A FILE

l sysuse bplong

l reshape wide bp, i(patient) j(when)

Reshapes the file to be wide. That is, time-variable is displayed in rows

Chapter 3. Variable Description & Management

l clear

l use c:\\stata10\\auto

Loads a data named auto.dta previously stored at c:\\stata10 Can also type in ―use c:\\stata10\\auto, clear‖ instead

l describe (or enter F3)

Shows what is in data c:\\stata\\auto.dta

Can see the number of variables & observations

Can also see the name & description of each variable

l list in 1

Allows us to see the actual data of observation 1

l list in 1/6

Allows us to see the actual data from observation 1 to 6

l summarize rep78 (or sum rep78)

Shows more about variable ―rep78‖ across all observations by summarizing it

Shows number of observations, mean, standard deviation, minimum value, and maximum value

Rep78: Repair record in 1978

l summarize rep78, detail

Shows 1, 5, 10, 25, 50, 75, 90, 99 percentiles Gives largest and smallest four figures

Gives number of observation, mean, standard deviation, variance, skewness, and kurtosis. Kurtosis measures the degree of peakedness of a distribution or the degree of fat tails of a distribution

l gen mpgsq=mpg^2

Generates a new variable named ―mpgsq‖ Mpg: mileage per gallon

l rename mpgsq mpgs

Changes the variable‘s name from ―mpgsq‖ to ―mpgs‖

l drop mpgs

Drop the variable name ―mpgs‖ from the data set l egen mmpg=mean(mpg)

Generates a new variable using a special function

Generates a variable named ―mmpg‖ which takes the mean value of variable ―mpg‖ Examples of formula: max, min, mean, median, etc.

l drop mmpg

l sort price

Sorts the observations in ascending order of ―price‖

l gsort -price

Sorts the observations in descending order of ―price‖

l drop if price>=5000

Drop observation if the value of variable ―price‖ is equal or greater than 5,000

l save c:\\stata10\\auto5 l use c:\\stata10\\auto l drop if price<5000 l save c:\\stata10\\auto6

l append using c:\\stata10\\auto5

Stacks data ―auto6‖ on top to data ―auto5‖

Chapter 4. Tabulation

l sysuse dir

List the names of data sets shipped with STATA l sysuse auto, clear

Loads auto.dta that is shipped with STATA

Clear option clears any file already in the memory

FREQUENCY TABLE

l tabulate rep78 (or tab rep78)

Shows frequency, percentage, cumulative percentage of each categorical value

l tabulate rep78, plot (or tab rep78, plot) Shows frequency of each categorical value The ―plot‖ option plots frequency l list if rep78==1

List all the observations in the data if variable named ―rep78‖ takes a value of 1 l list if rep78==5

List all the observations in the data if variable named ―rep78‖ takes a value of 5

CROSS TABULATION l tabulate rep78 foreign

Generates a frequency table of both variables ―rep78‖ and ―foreign‖ l tabulate rep78 foreign, column

Generates a frequency table of both variables ―rep78‖ and ―foreign‖ with column percent, i.e. the percent each ―rep78‖ category is of the overall l tabulate rep78 foreign, row

Generates a frequency table of both variables ―rep78‖ and ―foreign‖ with row percent, i.e. the percent each ―foreign‖ category is of the overall l tabulate rep78 foreign, cell

Generates a frequency table of both variables ―rep78‖ and ―foreign‖ with cell percent, i.e. the percent each ―rep78‖ and ―foreign‖ category is of the overall l tabulate rep78 foreign, column nofreq

Generates a frequency table of both variables ―rep78‖ and ―foreign‖ with column percent, but suppress actual count l tabulate foreign, summarize (rep78)

Compares the mean, standard deviation, and frequency of ―rep78‖ for each category of ―foreign‖

l tabulate rep78 foreign, summarize (weight)

Compares the mean, standard deviation, and frequency of ―weight‖ for each category of ―rep78‖ and ―foreign‖ Chapter 5. Graphics

SCATTER PLOTS l sysuse auto, clear l scatter price weight

Draws a scatter plot between variable ―weight‖ and ―price‖ l correlate weight price (or corr weight price)

Computes a correlation coefficient between variable ―weight‖ and ―price‖ l sort foreign

Sort observations by the value of ―foreign‖ l by foreign: sum weight price

Summarize ―weight‖ and ―price‖ by each value of ―foreign‖ l generate weightd=weight if ~foreign

Generates a new variable named ―weightd‖ which takes a value of ―weight‖ if the observation is a domestic car and a missing value if the observation is a foreign car

l generate weightf=weight if foreign

Generates a new variable named ―weightf‖ which takes a value of ―weight‖ if the observation is a foreign car and a missing value if the observation is a domestic car l scatter weightd weightf price

Draws a scatter plot between ―weight‖ and ―price‖ for each value of ―foreign‖ in the same plot

l scatter weight price, by(foreign )

Draws a scatter plot between ―weight‖ and ―price‖ for each value of ―foreign‖ in multiple plots l scatter mpg displ

Draws a scatter plot between ―mpg‖ and ―displ‖ Displ: displacement

l scatter mpg displ, saving(c:\\stata10\\scatter) Saves the scatter plot in a file named scatter.gph l graph use c:\\stata10\\scatter

Read the saved scatter plot from the file named scatter.gph l scatter mpg displ, msize(large)

Enlarges the marker font size. Also try out ―vtiny,‖ ―tiny,‖ ―vsmall,‖ ―small,‖ ―medsmall,‖ ―medium,‖ ―medlarge,‖ ―large,‖ ―vlarge,‖ ―huge,‖ ―vhuge,‖ ―ehuge‖ l scatter mpg displ, msymbol(triangle)

Changes the marker type to be a triangle. Also try out ―point,‖ ―none,‖ ―circle,‖ ―square,‖ ―diamond,‖ ―plus,‖ ―x,‖ ―smcircle,‖ ―smsquare,‖ ―smtriangle,‖ ―smdiamond,‖ ―smplus,‖ ―smx,‖ ―circle_hollow,‖ ―square_hollow,‖ ―triangle_hollow,‖ ―diamond_hollow,‖…

l scatter mpg displ, mcolor(green)

Changes the marker color to be a green. Also try out ―black,‖ ―none,‖ ―white,‖ ―yellow,‖ ―gold,‖ ―green,‖ ―lime,‖ ―mint,‖ …

l scatter mpg displ, msize(small) msymbol(circle_hollow) mcolor(red)

Produces a scatter plot with a small marker in red taking a shape of a hollow circle l scatter mpg displ, title(MPG vs. Engine Displacement) subtitle(1978 Automobile Data)

Inserts titles in the graph

l scatter mpg displ, scheme(s1mono)

Changes into black and white and puts a border around the graph l scatter mpg displ, scheme(economist) Changes the graph into an Economist style l scatter mpg displ, yline(25) Overlays a horizontal line at 25 l scatter mpg displ, mlabel(foreign)

Uses variable ―foreign‖ as a plotting symbol l graph matrix displ weight gear_ratio Draws a scatter plot matrix

HISTOGRAMS

l histogram mpg, bin(15)

Draws a histogram using 15 bins l histogram mpg, bin(15) normal

Draws a histogram using 15 bins with an overlaid normal distribution curve l histogram mpg, bin(15) normal by(foreign)

Draws two histograms: one for foreigners and the other for domestics

BOX-AND-WHISKER PLOTS l graph box price, by(foreign ) Draws box-and-whisker plots

Line in the middle of the box is 50th percentile (or median) of the data Box extends from the 25th percentile (X25) to the 75th percentile (X75) This range = interquartile range = IQR

Upper whisker is the largest data point less than or equal to X75 + 1.5IQR Lower whisker is the smallest data point greater than or equal to X25 – 1.5IQR

BAR CHARTS

l sysuse citytemp, clear

Loads STATA sample data set named City Temperature Data l graph bar (mean) tempjuly tempjan, over(region)

Draws a bar chart of temperature in July and January by region

LINE CHARTS

l sysuse uslifeexp, clear

Loads STATA sample data set named U.S. Life Expectancy Data l line le year

Draws a line chart le: life expectancy

PIE CHARTS l clear

l input sales marketing research development 1. 12 14 2 8 2. end

l graph pie sales marketing research development

HOW TO COPY AND PASTE GRAPHS

While the graph window is open, go to menu EDIT and click COPY GRAPH Go to a your Word file and PASTE

Chapter 6. Hypotheses Testing

DISTRIBUTIONS l sysuse auto, clear

l gen pvalue=norm(1[u2] )

Returns the left-tail p-value when the critical value is 1 under the standard normal distribution curve. That is, P[-¥ < Z < 1] l list pvalue in 1

l gen zvalue=invnorm(0.75)

Returns the left-tail z-value when the p-value is 0.75 under the standard normal distribution curve. That is, if norm(z) = 0.75 then invnorm(0.75) = z. l list zvalue in 1

l gen pvalue2=ttail(30, 2)

Returns the right-tail p-value when the critical value is 2 under the t-distribution curve with 30 degrees of freedom. This is, P[T > 2] if degrees of freedom is 30 l list pvalue2 in 1

l gen tvalue=invttail(30, 0.05)

Returns the right-tail t-value when the p-value is 0.05 under the t-distribution curve with 30 degrees of freedom. That is, if ttail(30, t) = 0.05, then invttail(30, 0.05) = t. l list tvalue in 1

l gen pvalue3=Ftail(30, 40, 2)

Returns the right-tail p-value when the critical value is 2 under the F-distribution curve with 30 numerator and 40 denominator degrees of freedom. l list pvalue3 in 1

l gen fvalue=invFtail(30, 40, 0.05)

Returns the right-tail F-value when the p-value is 0.05 under the F-distribution curve with 30 numerator and 40 denominator degrees of freedom.

ONE SAMPLE T-TEST l sysuse auto, clear l ttest mpg=20 One-sample t-test l ttest price=length Paired t-test

l ttest price=length, unpair

Unpaired two sample t-test with equal variance l ttest price=length, unpair unequal

Unpaired two sample t-test with unequal variance (by Satterwaite‘s degrees of freedom)

TWO SAMPLE T-TEST l sort foreign

l ttest mpg, by(foreign)

Unpaired two population t-test with equal variance l ttest mpg, by(foreign) unequal

Unpaired two population t-test with unequal variances l sysuse citytemp, clear Loads city temperature data l ttest tempjan=tempjuly

Paired-sample t-test with equal variances l ttest tempjan=tempjuly, unpaired

Unpaired-sample t-test with equal variances

l ttest tempjan=tempjuly, unpaired unequal Unpaired-sample t-test with unequal variances l sysuse census, clear

Loads 1980 census data by state l sort region

l ttest medage if region==1 | region==4, by(region) Unpaired two population t-test with equal variance l sysuse auto, clear

l xtile quart=price, nq(4) Group price into quartiles l tab quart l sort quart

l by quart: sum price

l ttest weight if quart==1 | quart==4, by(quart)

ANOVA TEST l edit

Type in the following data for each observation 1 117.5 1 113.8 1 104.4 2 48.9 2 50.4 2 58.9 3 70.4 3 86.9 4 87.7 4 67.3

Click preserve to end

l rename var1 treatment l rename var2 weight l anova weight treatment

Produce analysis-of-variance table

Chapter 7. Regression

SIMPLE LINEAR MODEL OLS l sysuse auto, clear l reg weight length

Run regress with a constant l reg weight length, nocons Run regress without a constant

FITTED LINE IN SCATTER PLOT l reg mpg displ

Regress ―mpg‖ on ―displ‖ and a constant l predict pmpg

Obtain predicted values from the regression

l twoway (scatter mpg displacement) (line pmpg displacement) Overlay a fitted line over the scatter plot

MULTIPLE LINEAR MODEL OLS l reg price weight mpg foreign

CONTROL

l reg price foreign

This will show no relationship between ?foreign‘ and ?price‘

But what if foreign cars are lighter and that is pulling down the price? What would be the relationship between ?price‘ and ?foreign‘ among the cars with similar car weight?

l pwcorr foreign weight l sum weight, detail l gen dum_w=1

l replace dum_w=2 if weight>=2240 l replace dum_w=3 if weight>=3190 l replace dum_w=4 if weight>=3600 l sort dum_w

l by dum_w: reg price foreign

What are the coefficients on foreign?

Last regression is not estimated because our ?foreign‘ variable is constant. Remember our variance formula. l reg price foreign weight

What happens to the coefficient on foreign? l reg price foreign

This will show no relationship between ?foreign‘ and ?price‘

But what if foreign cars are shorter and that is pulling down the price? What would be the relationship between ?price‘ and ?foreign‘ among the cars with similar length? l pwcorr price foreign length

l sum length, detail l gen dum_l=1

l replace dum_l=2 if length>=170 l replace dum_l=3 if length>=193 l replace dum_l=4 if length>=204 l sort dum_l

l by dum_l: reg price foreign

What are the coefficients on foreign?

Last regression is not estimated because our ?foreign‘ variable is constant. Remember our variance formula. l reg price foreign length

What happens to the coefficient on foreign?

COLLINEARITY

l gen domestic=~foreign

Generate a variable named domestic l reg price weight mpg foreign domestic Case of perfect collinearity

What happens to the variable domestic? l reg price displ

l reg price displ weight

Compare the t-values and the F-values l corr displ weight

Case of multicollinearity l reg price displ weight l vif

Displays VIF and 1/VIF for each right-hand-side variable

VIP = 1/(1-R2) from a regression where the variable of concern is on the left-hand-side and all other independent variables are on the right-hand-side (if above 0.5, evidence of multicollinearity)

CORRELATION

l corr displ weight rep78

Uses observations that exist for all the variables l pwcorr displ weight rep78 l pwcorr displ weight rep78, obs l corr displ weight rep78, covariance l pwcorr displ weight rep78, sig l pwcorr displ weight rep78, sig obs l pwcorr displ weight rep78, star(0.05)

RIGHT HAND SIDE DUMMY VARIABLES l sysuse auto, clear

l xi: reg mpg weight i.rep78

Regression with multiple dummy variables l xi: reg mpg i.foreign|weight

Regression with a ―foreign‖ dummy variable that interacts with a continuous variable l xi: reg mpg i.foreign*weight

Regression with a ―foreign‖ intercept dummy and a ―foreign‖ dummy variable that interacts with a continuous variable l xi: reg mpg i.foreign*i.rep78

Regression with dummy variables interacting with each other

Chapter 8. More on Regressions

F-TESTS

l reg mpg foreign weight length l test length=0

F-test if coefficient on length is zero Compare p-value from t-test l test weight=length F-test if weight=length

l test (foreign/100)-length=weight F-test if (foreign/100)-length=weight l test foreign=0

F-test if coefficient on foreign is zero l test foreign=0

l test length=0, accumulate Joint hypotheses testing

F-test if coefficients on ―foreign‖ and ―length‖ are zero l test foreign=0

l test length=0, accumulate l test weight=0, accumulate

F-test if coefficients on ―foreign,‖ ―length,‖ and ―weight‖ are zero Compare it with goodness of fit test l test foreign weight length Same as above l test

Shows the last test again

REGRESSION DIAGNOSTICS l reg mpg foreign weight length l avplot weight

Draws added-variable plots (also known as partial regression line) Partial regression line: fitted line between two residuals

One residual is estimated by regressing mpg on foreign and length

The other residual is estimated by regressing weight on foreign and length l rvfplot, yline(0)

Residual-versus-fitted plot

If assumptions are correct, there should be no pattern in the graph l reg price displ

l reg price displ, beta Standardized coefficients

l reg price weight mpg foreign l predict estu if e(sample), rstudent Generates studentized residuals l list estu if abs(estu)>1.96

Shows studentized residuals if its absolute value is greater than 1.96 l reg price weight mpg foreign if abs(estu)<1.96 l reg price weight mpg foreign l dfbeta

Generates DFBETA

l list DFweight DFmpg DFforeign if abs(DFweight)>2/sqrt(74) | abs(DFmpg)>2/sqrt(74) | abs(DFforeign)>2/sqrt(74)

Shows DFBETA if its absolute value is greater than 2/sqrt(N) N=number of observation

l reg price weight mpg foreign if abs(DFweight)<2/sqrt(74) & abs(DFmpg)<2/sqrt(74) & abs(DFforeign)<2/sqrt(74) l reg price weight mpg foreign

HETEROSCEDASTICITY l reg mpg weight l rvfplot, yline(0)

l reg mpg weight, robust

Use White‘s robust standard errors in the presence of heteroscedasticity

SERIAL-CORRELATION l sysuse uslifeexp, clear

Load sample STATA file named US Life Expectancy l tsset year

Set variable ―year‖ as the time variable l reg le_male le_female l dwstat

Computes Durbin-Watson statistics l rvfplot, yline(0)

Residual-versus-fitted plot l prais le_male le_female, corc Cochraine-Orcutt

[u1]Does not function; ―no; data in memory would be lost‖ ― [u2]Unknown function norm()‖ Cannot progress.