计量上机整理version2

更新时间:2023-10-05 13:32:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

*CH-1 基础

*导入后缀非.dta的数据集 insheet using class.csv, clear

*将变量命名为xxxx

rename (v1-v13) (xhxmpylbxyxsnjzybjxqxkcgbzjxbhxnd xq2)

*为变量添加标签

label variable xh \学号\

*将所有缺失值转化为数据999; mvencode _all, mv(999)

*删去变量。 dropxs

drop if xs==999

*导入Excel文件 import excel \路径\

*计算增长率

genpop_gr=(pop-pop[_n-1])/pop[_n-1]

*CH-2、3 简单线性回归&多元 regress //回归

predict e , resid //生成残差序列e predict y1, xb //生成y的预测值序列y1 predict yhat /*同predict y1, xb*/ rstandard //标准化残差 stdr //残差的标准误

sum educ, detail //detail是指更加详细的数据信息 count if female==1 //计算变量的数目

sum educ if female==1 //计算女性的教育程度

reg wage educ if married==1 //reg命令和样本选择参数一起使用 reglwageeduc,noconstant //过原点的回归

_b[variable] //表示变量前的系数

scalar price_0=_b[bdrms]+140*_b[sqrft] //定义数字变量

corr // 计算相关系数 test //检验

*CH-4 检验

test x1 //检验x1对应的系数的显著性

test x1 x2 x3 //检验x1 x2 x3对应系数的联合显著性 test x1=x2 //检验x1 x2对应系数 test x1=2 //检验 test _b[x1]=2 //检验

lincom //系数线性组合的点估计、标准误、检验与推断 lincom x1+ x2

lincom x1+ x2 //t检验β1+β2=0 lincom x1+ x2 - 1 //t检验β1+β2=1 testnl //非线性假设检验

// 备择假设为beta1!=0 * 给定alpha=0.05,双侧检验,右尾端分得概率2.5%,该临界值等于t分布的第97.5分位数点 * 该t分布的自由度为n-k,即173-4=169 * 由于t分布是对称,我们只需考察右尾端,左尾端是等价的 scalar crit1 = invttail(e(df_r),0.025) //显著性水平等于0.05,双侧检验的临界值 scalar t1 = _b[expendA]/_se[expendA] display \ display \ =\

* 方法二:计算p值 // 备择假设为beta1!=0,如何计算p值呢?p-value = P(|T|>|t|)= 2P(T>|t|) * 需要先计算t值 scalar p1 = 2*ttail(e(df_r), abs(t1)) scalar list t1 p1

*CH-6 深入专题

reg price nox crime rooms diststratio, beta //报告标准化参数(beta参数)

*各种点效应的计算 *(转折点。。。)

display -1*_b[rooms]/(2*_b[rooms2]) // display turnaround value of rooms

display 100*(_b[rooms]+2*_b[rooms2]*5) // display change in price if rooms increases from 5 to 6

display 100*(_b[rooms]+2*_b[rooms2]*6) // display change in price if rooms increases from 6 to 7

*(在均值处。。。) sumlnox

scalar mean = r(mean) // keep the mean value of lnox

ereturn list //列出回归后的相应指标,如自由度,sse,sst,ssr等

*CH-7 虚拟变量与Chow检验

*1. Basics of Dummy Independent Variables

gen male = (!female) // generate a dummy indicating male *模型的两种表示方法,当心虚拟变量陷阱

reg wage female educexper tenure // with constant reg wage female educexper tenure male, nocon // without constant display exp(_b[female]*1)-1 //男性和女性平均工资水平的精确差异

*- 2. Multiple Categories 多类别模型

*生产多类别虚拟变量,注意变量系数的意义

*- 3. Interaction Involving Dummies 包括虚拟变量的交叉项 *注意点效应和转折点即可

*- 4. Chow Test 邹检验

* Method I

quireglwageeducexperexpersq tenure tenursq fem*

test female = femed = femex = femeq = femte = femtq = 0

* Method II (邹检验基本形式) * Step 1:

quireglwageeducexperexpersq tenure tenursq if female==0 scalar rss1 = e(rss)

quireglwageeducexperexpersq tenure tenursq if female==1 scalar rss2 = e(rss) * Step 2:

quireglwageeducexperexpersq tenure tenursq scalar k = e(df_m) scalar N = e(N) scalarrss = e(rss) * Step 3:

scalar d1 = k + 1

scalar d2 = N - 2*(k + 1)

scalar F = ((rss - (rss1 + rss2))/d1) / ((rss1 + rss2)/d2) // F statistics scalar P = 1 - F(d1,d2,F) // p-value display \display \ = \

*如果允许前多少项变动的话,要注意ssr的自由度!!

*- 5. Effects of Education Rankings on Wage

use wage1.dta, clear

gen educ6_8 = (educ>=6 &educ<9) gen educ9_11 = (educ>=9 &educ<12) gen educ12 = (educ>=12)

geneduc_rank = 1 + educ6_8*2 + educ9_11*3 + educ12*4 // generate ranking variable for education

reglwage female educ_rankexperexpersq tenure tenursq // one-unit increase in education rankings has a constant effect on lwage

reglwage female educ6_8 educ9_11 educ12 experexpersq tenure tenursq

displayexp(_b[educ12]*1)-1 // display the average percentage difference in lwage between high school graduates and those without primary school diploma

* CH-8 异方差

ssc install bpagan, replace // install command bpagan ssc install whitetst, replace // install command whitetst

reg narr86 qemp86 inc86 black hispan, robust//稳健回归,然后进行各种检验

*Heteroskedasticity-Robust LM Statistic 异方差稳健的LM统计量 quireg narr86 pcnv ptime86 qemp86 inc86 black hispan predict u1, resid

quiregavgsenpcnv ptime86 qemp86 inc86 black hispan predict r1, resid

quiregavgsensqpcnv ptime86 qemp86 inc86 black hispan predict r2, resid gen ur1 = u1*r1 gen ur2 = u1*r2 geni = 1

quiregi ur1 ur2, nocons

scalar lm1 = e(N)-e(rss) scalarlpl = chi2tail(2,lm1)

display \display \ = \

*异方差检验

*The Breusch-Pagan Test for Heteroskedasticity //BP检验 * Method I

quireg narr86 avgsenavgsensqpcnv ptime86 qemp86 inc86 black hispan estathettestavgsenavgsensqpcnv ptime86 qemp86 inc86 black hispan

* Method II

quireg narr86 avgsenavgsensqpcnv ptime86 qemp86 inc86 black hispan bpaganavgsenavgsensqpcnv ptime86 qemp86 inc86 black hispan

*The White Test for Heteroskedasticity //怀特检验

use crime1.dta, clear

genavgsensq = avgsen*avgsen

quireg narr86 avgsenavgsensqpcnv ptime86 qemp86 inc86 black hispan predictyhat, xb predict u3, resid

* Method I (Original Form) estatimtest, white

* Method II (Original Form) whitetst // the same as above

* Method III (Alternate Form) gen yhat2 = yhat^2 gen u3s = u3^2 quireg u3s yhat yhat2 testyhat yhat2

*WLS和FGLS (使用时修改hi权重) * Method I (WLS)

reg cigs lincomelcigpriceduc age agesqrestaurn [aw = 1/hi]

* Method II (FGLS)

quireg cigs lincomelcigpriceduc age agesqrestaurn predictub, resid

genlubar = log(ub*ub)

quireglubarlincomelcigpriceduc age agesqrestaurn predictcigsh, xb

gencigse = exp(cigsh)

reg cigs lincomelcigpriceduc age agesqrestaurn [aw = 1/hi]

*CH10-12 时间序列

*设置数据格式(时间序列) tsset t

本文来源:https://www.bwwdw.com/article/jwrd.html

Top