文档库 最新最全的文档下载
当前位置:文档库 › 工具变量stata命令

工具变量stata命令

工具变量stata命令
工具变量stata命令

Specification

One important thing in Econometrics is choosing proper model(s) in accordance with our data type, especially distribution. Then what we should concentrate on is that which regressors can be used or which type of predictor variables should be used.

Generally speaking, we can use linear, log, log-linear, quadratic, interaction terms etc. forms. Different forms have different explanations. And we should be careful. For example, one drawback to using a dependent variable in log form is that it is more difficult to predict the original variable y, not just e^log(yhat). quadratic functions can capture decreasing or increasing marginal effects because of x2. Another important thing is that we can use F statistics to test nested models and a-R2 to choose between nonnested models( but the latter can be only restricted in the same form for y, you can compute a new R2 for comparison of different y forms, no detail information here).

Omitted variables(OVs) can always happen because sometimes obtaining data is so tough. The key is that we must consider whether the OVs are correlated with existing variables,especially the variables of interest. For precise estimators or solving part endogeneity problems, we can take proper and practical measures including adding more control variables, using PV or IV, using Panel data, RD model and so on.

we have worried about omitting important factors from a model that might be correlated with the independent variables. It is also possible to control for too many variables in a regression analysis. It makes no sense to hold some factors fixed precisely because they should be allowed to change when a policy variable changes.So, different models serve different purposes, and wrong factors should not be included.

We can use Ramsey's RESET, linktest to judge whether nonlinear variables are omitted.

syntax:

estat ovtest [, rhs]

linktest

. estat ovtest

Ramsey RESET test using powers of the fitted values of price

Ho: model has no omitted variables

F(3, 67) = 15.31

Prob > F = 0.0000

. estat ovtest, rhs

Ramsey RESET test using powers of the independent variables

Ho: model has no omitted variables

F(6, 64) = 2.68

Prob > F = 0.0220

We can also use AIC, BIC and HQIC to decide the number of variables, BIC stricter than AIC. syntax:

estat ic

Some certain independent variables should not be included in a regression model, even though

they are correlated with the dependent variable. We also know that adding a new independent variable to a regression can exacerbate the multi-collinearity problem and reduce the error variance. However, there is one case that is clear: we should always include independent variables that affect y and are uncorrelated with all of the independent variables of interest. everyone can think the reasons for this add-on.

On the other hand, what will happen if a variable "has nothing to do with "explained variable? It can be proved that the OLS is still unbiased consistent but the variance can be larger.

In general, we should follow the guidance of economic theory and reality when choosing variables.

Proxy variable(PV)

The common example is that we want to hold unobserved ability fixed when measuring the return to education level. If educ is correlated with ability, then putting the ability in the error term causes estimator of educ to be biased and we have an endogeneity problems.

One possibility to solve, or at least mitigate, the omitted variables bias is to obtain a PV for the OV. We can use intelligence quotient as a proxy for ability, which does not require iq to be the same thing as ability, just correlated with it.

y = b0 + b1x1 + b2x2 + b3x3* + u

x3* = m0 + m3x3 + v3

The equations above mean that x3* is unobserved and x3 is the proxy variables. We can pretend these two are the same and then regress y x1 x2 x3, which is called plug-in solution. The assumptions to provide consistent estimators of b1 b2 are: 1) the error u is uncorrelated with x1 x2 x3*; 2) the error v3 is uncorrelated with x1 x2 x3. A new equation :

y = c0 + b1x1 + b2x2 +c3x3 + e

So, we can get unbiased estimators of b1 b2 c3. The important thing is that we get good estimates of the parameters b1 b2.

As some books said, there are also two formal requirements for a PV, similar to the assumptions mentioned. The first is that PV should be redundant(sometimes called ignorable) in the structure equation. The second requirement of a good PV is we require that the correlation between the omitted variable and each xi be zero once we partial out PV.

The question is that, In the wage-education example, if the ability has a positive partial correlation with educ, we could still be getting an upward bias in the return to education. We call the iq imperfect proxy. But the bias is smaller than if we ignored the problem of omitted ability entirely. When including PV induces substantial collinearty, it might be better use OLS without the PV. However, in making these decisions we must recognize that including PV reduces the error variance. Including a PV can actually reduce asymptotic variance as well as mitigate bias.

Sometimes, we can include the lagged dependent variable as the PV when we suspect that independent variables are correlated with an omitted variable, but we have no idea how to obtain a proxy for it.

Instrumental Variable(IV)

We have several options as for the OV bias or endogeneity problems:

(1) we can ignore the problem and suffer the consequences of biased and inconsistent estimators;

(2) we can try to find and use a suitable PV for the unobserved variable;

(3) we can assume that the omitted variable does not change over time and use the fixed effects or

first-differencing methods;

(4) we can use RD method;

(5) we can find a good IV to try solving the problems.

IV estimation is a method of leaving the unobserved variable in the error term, but rather than estimating the model by OLS, it uses an estimation method that recognizes the presence of the omitted variable.

On one hand, we can use a PV iq for the unobserved variable ability to estimate the educ, and then do the regression of log(wage) on educ, iq. On the other hand, we can also use a IV for educ.

As for the IV z , it should have two assumptions: (1)"Instrument exogeneity". z is uncorrelated with u, Cov(z,u)=0; (2)"Instrument relevance". z is correlated with x, Cov(z,x)!=0. Compared PV and IV, it is clear that iq is not a good instrumental variable for educ but a good proxy variable for ability. It is no accident that when z = x we obtain the OLS estimator . In other words, when x is exogenous, it can be used as its own IV, and the IV estimator is then identical to the OLS estimator.

Though IV is consistent when z and u are uncorrelated and z and x have any positive or negative correlation, IV estimates can have large standard errors, especially if z and x are only weakly correlated. Weak correlation between z and x can have even more serious consequences: the IV estimator can have a large asymptotic bias even if z and u are only moderately correlated.

As for the R2, When x and u are correlated, we cannot decompose the variance of y into β2 Var(x) +Var(u), and so the R2 has no natural interpretation. And R2 cannot be used in the usual way to compute F tests of joint restrictions. IV methods are intended to provide better estimates of the ceteris paribus effect of x on y when x and u are correlated; goodness-of-fit is not a factor. A high R-squared resulting from OLS is of little comfort if we cannot consistently estimateβ.

As for the multiple regression cases, considering the equation:

which we call structural equation. y1 is endogenous, as it is correlated with u1. The variables y2 and z1 are the explanatory variables, and u1 is the error. z1 is exogenous and y2is suspected of being correlated with u1. We use a IV-z2 。Then we have

which is called reduced form equation. We have E(v2)=0, cov(z1, v2)=0, cov(z2, v2)=0. and π2!=0.

Two-stage least squares(2SLS) estimation

With multiple instruments, the IV estimator is also called 2SLS estimator. The reason is simple.

Using the algebra of OLS, it can be shown that when we use y ?2 as the IV for y2, the IV estimates are identical to the OLS estimates. In other words, we can obtain the 2SLS estimator in two stages. The first stage is to obtain the fitted values y ? 2 . The second stage is the OLS regression. Because we use y ? 2 in place of y2, the 2SLS estimates can differ substantially from the OLS estimates. syntax:

ivregress estimator depvar [varlist1] (varlist2 = varlist_iv) [if] [in] [weight] [, options] example:

ivregress liml rent pcturban (hsngval = faminc i.region)

(limited information maximum likelihood estimation, which maybe better when weak IV exists) ivregress 2sls rent pcturban (hsngval = faminc i.region), small

And if you want to see the result of first stage, add "first".

ivregress 2sls rent pcturban (hsngval = faminc i.region), small first

Besides, ivreg2 is a very useful command, you can enter "ssc install ivreg2" and then "h ivreg2" for more information.

Testing for Endogeneity and Testing Overidentifying Restrictions

Testing for Endogeneity of a Single Explanatory Variable:

(i) Estimate the reduced form for y2 by regressing it on all exogenous variables (including those in the structural equation and the additional IVs). Obtain the residuals, v ? 2 .

(ii) Add v ? 2 to the structural equation (which includes y2) and test for significance of v ? 2 using an OLS regression. If the coefficient on v ? 2 is statistically different from zero, we conclude that y2 is indeed endogenous. We might want to use a heteroskedasticity-robust t test.

Hausman specification test for Endogeneity.

reg y x1 x2

estimates store ols

ivregress 2sls y x1 (x2 = z1 z2)

estimates store iv

hausman iv ols, constant sigmamore

However, one drawback about this is that Hausman test is not proper if heteroskedasticity exists (not BLUE).

Testing Over-identification Restrictions.

The procedure of comparing different IV estimates of the same parameter is an example of testing over-identifying restrictions. We have 3 situations including unidentified, just or exactly indentified, over-identified according to order condition. If all instruments are exogenous, the 2SLS residuals should be uncorrelated with the instruments, up to sampling error. But if there are k+1 parameters and k+1+ q instruments, the 2SLS residuals have a zero mean and are identically uncorrelated with k linear combinations of the instruments.

we can use the syntax: estat overid

Generalized Method of Moment(GMM) is a better way when u has heteroskedasticity or autocorrelation. It is necessary to use GMM on the over-identified condition.

syntax:

ivregress gmm y x1 (x2=z1)

ivregress gmm y x1 (x2=z1),vce(hac nwest[#])

Main Sources:

Introductory Econometrics: A Modern Approach(Jeffry M. Wooldridge)

高级计量经济学及stata应用(陈强)

For further information, you can also use the book Econometrics analysis of cross section and panel data(Jeffry M. Wooldridge), which is very useful.

STATA最常用命令大全

stata save命令 FileSave As 例1. 表1.为某一降压药临床试验数据,试从键盘输入Stata,并保存为Stata格式文件。 STATA数据库的维护 排序 SORT 变量名1 变量名2 …… 变量更名 rename 原变量名新变量名 STATA数据库的维护 删除变量或记录 drop x1 x2 /* 删除变量x1和x2 drop x1-x5 /* 删除数据库中介于x1和x5间的所有变量(包括x1和x5) drop if x<0 /* 删去x1<0的所有记录 drop in 10/12 /* 删去第10~12个记录 drop if x==. /* 删去x为缺失值的所有记录 drop if x==.|y==. /* 删去x或y之一为缺失值的所有记录 drop if x==.&y==. /* 删去x和y同时为缺失值的所有记录 drop _all /* 删掉数据库中所有变量和数据 STATA的变量赋值 用generate产生新变量 generate 新变量=表达式 generate bh=_n /* 将数据库的内部编号赋给变量bh。 generate group=int((_n-1)/5)+1 /* 按当前数据库的顺序,依次产生5个1,5个2,5个3……。直到数据库结束。 generate block=mod(_n,6) /* 按当前数据库的顺序,依次产生1,2,3,4,5,0。generate y=log(x) if x>0 /* 产生新变量y,其值为所有x>0的对数值log(x),当x<=0时,用缺失值代替。 egen产生新变量 set obs 12 egen a=seq() /*产生1到N的自然数 egen b=seq(),b(3) /*产生一个序列,每个元素重复#次 egen c=seq(),to(4) /*产生多个序列,每个序列从1到# egen d=seq(),f(4)t(6) /*产生多个序列,每个序列从#1到#2 encode 字符变量名,gen(新数值变量名) 作用:将字符型变量转化为数值变量。 STATA数据库的维护 保留变量或记录 keep in 10/20 /* 保留第10~20个记录,其余记录删除 keep x1-x5 /* 保留数据库中介于x1和x5间的所有变量(包括x1和x5),其余变量删除keep if x>0 /* 保留x>0的所有记录,其余记录删除

Stata命令整理教学内容

Stata 命令语句格式: [by varlist:] command [varlist] [=exp] [if exp] [in range] [weight] [, options] 1、[by varlist:] *如果需要分别知道国产车和进口车的价格和重量,可以采用分类操作来求得, sort foreign //按国产车和进口车排序 . by foreign: sum price weight *更简略的方式是把两个命令用一个组合命令来写。 . by foreign, sort: sum price weight 如果不想从小到大排序,而是从大到小排序,其命令为gsort。 . sort - price //按价格从高到低排序 . sort foreign -price /*先把国产车都排在前,进口车排在后面,然后在国产车内再按价格从大小到排序,在进口车内部,也按从大到小排序*/ 2、[=exp]赋值运算 . gen nprice=price+10 //生成新变量nprice,其值为price+10 /*上面的命令generate(略写为gen) 生成一个新的变量,新变量的变量名为 nprice,新的价格在原价格的基础上均增加了10 元。 . replace nprice=nprice-10 /*命令replace 则直接改变原变量的赋值,nprice 调减后与price 变量取值相等*/ 3、[if exp]条件表达式 . list make price if foreign==0 *只查看价格超过1 万元的进口车(同时满足两个条件),则 . list make price if foreign==1 & price>10000 *查看价格超过1 万元或者进口车(两个条件任满足一个) . list make price if foreign==1 | price>10000 4、[in range]范围筛选 sum price in 1/5 注意“1/5”中,斜杠不是除号,而是从1 到 5 的意思,即1,2,3,4,5。 如果要计算前10 台车中的国产车的平均价格,则可将范围和条件筛选联合使用。 . sum price in 1/10 if foreign==0 5、[weight] 加权 sum score [weight=num] 其中,num为每个成绩所对应的人数 6、[, options]其他可选项 例如,我们不仅要计算平均成绩,还想知道成绩的中值,方差,偏度和峰度等*/ . sum score, detail . sum score, d //d 为detail 的略写,两个命令完全等价 . list price, nohead //不要表头 Stata 数据类型转换 1、字符型转化成数值型 destring, replace //全部转换为数值型,replace 表示将原来的变量(值)更新 destring date, replace ignore(“ ”) 将字符型数据转换为数值型数据:去掉字符间的空格destring price percent, gen(price2 percent2) ignore(“$ ,%”) 与date 变量类似,变量price 前面有美元符号,变量percent 后有百分号,换为数值型时需要忽略这些非数值型字符 2、数值型转化为字符型

(完整)stata命令总结,推荐文档

stata11 常用命令 注:JB统计量对应的p大于0.05 ,则表明非正态,这点跟sktest 和 swilk 检验刚好相反;dta 为数据文件;gph 为图文件;do 为程序文件;注 意stata 要区别大小写;不得用作用户变量名: _all _n _N _skip _b _coef _cons _pi _pred _rc _weight double float long int in if using with 命令:读入数据一种方式 input x y 14 2 5.5 3 6.2 47.7 58.5 end su/summarise/sum x 或su/summarise/sum x,d 对分组的描述: sort group by group:su x %%%%% tabstat economy,stats(max)%返回变量economy的最大值 %%stats括号里可以是:mean,count(非缺失观测值个数),sum(总 和),max,min,range , %% sd ,var ,cv(变易系数=标准差/ 均值),skewness,kurtosis , median,p1(1 %分位 %% 数,类似地有p10, p25, p50, p75, p95, p99),iqr(interquantile range = p75 –p25) _all %描述全部 _N 数据库中观察值的总个数。 _n 当前观察值的位置。 _pi 圆周率π 的数值。 list gen/generate % 产生数列egen wagemax=max(wage) clear use by(分组变量)

stata命令大全(全)

*********面板数据计量分析与软件实现********* 说明:以下do文件相当一部分内容来自于中山大学连玉君STATA教程,感谢他的贡献。本人做了一定的修改与筛选。 *----------面板数据模型 * 1.静态面板模型:FE 和RE * 2.模型选择:FE vs POLS, RE vs POLS, FE vs RE (pols混合最小二乘估计) * 3.异方差、序列相关和截面相关检验 * 4.动态面板模型(DID-GMM,SYS-GMM) * 5.面板随机前沿模型 * 6.面板协整分析(FMOLS,DOLS) *** 说明:1-5均用STATA软件实现, 6用GAUSS软件实现。 * 生产效率分析(尤其指TFP):数据包络分析(DEA)与随机前沿分析(SFA) *** 说明:DEA由DEAP2.1软件实现,SFA由Frontier4.1实现,尤其后者,侧重于比较C-D与Translog生产函数,一步法与两步法的区别。常应用于地区经济差异、FDI溢出效应(Spillovers Effect)、工业行业效率状况等。 * 空间计量分析:SLM模型与SEM模型 *说明:STATA与Matlab结合使用。常应用于空间溢出效应(R&D)、财政分权、地方政府公共行为等。 * --------------------------------- * --------一、常用的数据处理与作图----------- * --------------------------------- * 指定面板格式 xtset id year (id为截面名称,year为时间名称) xtdes /*数据特征*/ xtsum logy h /*数据统计特征*/ sum logy h /*数据统计特征*/ *添加标签或更改变量名 label var h "人力资本" rename h hum *排序 sort id year /*是以STATA面板数据格式出现*/ sort year id /*是以DEA格式出现*/ *删除个别年份或省份 drop if year<1992 drop if id==2 /*注意用==*/ *如何得到连续year或id编号(当完成上述操作时,year或id就不连续,为形成panel格式,需要用egen命令) egen year_new=group(year) xtset id year_new **保留变量或保留观测值 keep inv /*删除变量*/ **或 keep if year==2000 **排序 sort id year /*是以STATA面板数据格式出现 sort year id /*是以DEA格式出现 **长数据和宽数据的转换 *长>>>宽数据 reshape wide logy,i(id) j(year)

[推荐] stata基本操作汇总常用命令

[推荐] Stata基本操作汇总——常用命令 help和search都是查找帮助文件的命令,它们之间的 区别在于help用于查找精确的命令名,而search是模糊查找。 如果你知道某个命令的名字,并且想知道它的具体使用方法,只须在stata的命令行窗口中输入help空格加上这个名字。回车后结果屏幕上就会显示出这个命令的帮助文件的全部 内容。如果你想知道在stata下做某个估计或某种计算,而 不知道具体该如何实现,就需要用search命令了。使用的 方法和help类似,只须把准确的命令名改成某个关键词。回车后结果窗口会给出所有和这个关键词相关的帮助文件名 和链接列表。在列表中寻找最相关的内容,点击后在弹出的查看窗口中会给出相关的帮助文件。耐心寻找,反复实验,通常可以较快地找到你需要的内容.下面该正式处理数据了。我的处理数据经验是最好能用stata的do文件编辑器记下你做过的工作。因为很少有一项实证研究能够一次完成,所以,当你下次继续工作时。能够重复前面的工作是非常重要的。有时因为一些细小的不同,你会发现无法复制原先的结果了。这时如果有记录下以往工作的do文件将把你从地狱带到天堂。因为你不必一遍又一遍地试图重现做过的工作。在stata 窗口上部的工具栏中有个孤立的小按钮,把鼠标放上去会出

现“bring do-file editor to front”,点击它就会出现do文件编 辑器。 为了使do文件能够顺利工作,一般需要编辑do文件的“头”和“尾”。这里给出我使用的“头”和“尾”。capture clear (清空内存中的数据)capture log close (关闭所有 打开的日志文件)set more off (关闭more选项。如果打开该选项,那么结果分屏输出,即一次只输出一屏结果。你按空格键后再输出下一屏,直到全部输完。如果关闭则中间不停,一次全部输出。)set matsize 4000 (设置矩阵的最大阶数。我用的是不是太大了?)cd D: (进入数据所在的盘符和文件夹。和dos的命令行很相似。)log using (文件名).log,replace (打开日志文件,并更新。日志文件将记录下所有文件运行后给出的结果,如果你修改了文件内容,replace选项可以将其更新为最近运行的结果。)use (文件名),clear (打开数据文件。)(文件内容)log close (关闭日志文件。)exit,clear (退出并清空内存中的数据。) 实证工作中往往接触的是原始数据。这些数据没有经过整理,有一些错漏和不统一的地方。比如,对某个变量的缺失观察值,有时会用点,有时会用-9,-99等来表示。回归时如果 使用这些观察,往往得出非常错误的结果。还有,在不同的数据文件中,相同变量有时使用的变量名不同,会给合并数

常用到的stata命令

常用到的sta命令 闲话不说了。help和search都是查找帮助文件的命令,它们之间的区别在于help用于查找精确的命令名,而search是模糊查找。如果你知道某个命令的名字,并且想知道它的具体使用方法,只须在sta的命令行窗口中输入help空格加上这个名字。回车后结果屏幕上就会显示出这个命令的帮助文件的全部内容。如果你想知道在sta下做某个估计或某种计算,而不知道具体该如何实现,就需要用search命令了。使用的方法和help类似,只须把准确的命令名改成某个关键词。回车后结果窗口会给出所有和这个关键词相关的帮助文件名和链接列表。在列表中寻找最相关的内容,点击后在弹出的查看窗口中会给出相关的帮助文件。耐心寻找,反复实验,通常可以较快地找到你需要的内容。 下面该正式处理数据了。我的处理数据经验是最好能用sta的do文件编辑器记下你做过的工作。因为很少有一项实证研究能够一次完成,所以,当你下次继续工作时。能够重复前面的工作是非常重要的。有时因为一些细小的不同,你会发现无法复制原先的结果了。这时如果有记录下以往工作的do文件将把你从地狱带到天堂。因为你不必一遍又一遍地试图重现做过的工作。在sta窗口上部的工具栏中有个孤立的小按钮,把鼠标放上去会出现“bring do-file editor to front”,点击它就会出现do文件编辑器。 为了使do文件能够顺利工作,一般需要编辑do文件的“头”和“尾”。这里给出我使用的“头”和“尾”。 /*(标签。简单记下文件的使命。)*/ capture clear(清空内存中的数据) capture log close(关闭所有打开的日志文件) set mem 128m(设置用于sta使用的内存容量) set more off(关闭more选项。如果打开该选项,那么结果分屏输出,即一次只输出一屏结果。你按空格键后再输出下一屏,直到全部输完。如果关闭则中间不停,一次全部输出。) set matsize4000(设置矩阵的最大阶数。我用的是不是太大了?)

Stata常用15条命令

【命令1】:导入数据 一般做实证分析使用的是excel中的数据,其后缀名为.xls,需要将其修改为.csv insheet using name.csv, clear 【命令2】:删除重复变量 sort var1 var2 duplicatesdrop var1 var2, force 【命令3】:合并数据 use data1, clear merge m:m var1 var2 using data2 drop if _merge==2 drop if _merge==1 drop _merge 【命令4】:描述性统计分析 tabstat var1var2, stat(n min mean median p25 p75 max sd), if groupvar==0 or 1 输出到word中: logout, save(name) word replace: tabstat var, stat(n min mean p50 max sd) col(stat)f(%9.2g) 【命令5】:结果输出 安装 ssc install estout, replace 单个回归 reg y x esttab using name.rtf, compress nogap r2 ar2 star(* 0.1 ** 0.05 *** 0.01) 多个回归一起 reg y x1 est store m1 reg y x2 est store m2 esttab m1 m2 using name.rtf, compress nogap r2 ar2 star(* 0.1 ** 0.05 *** 0.01)

(完整)stata命令总结,推荐文档

stata11常用命令 注:JB统计量对应的p大于0.05,则表明非正态,这点跟sktest和swilk 检验刚好相反; dta为数据文件; gph为图文件; do为程序文件; 注意stata要区别大小写; 不得用作用户变量名: _all _n _N _skip _b _coef _cons _pi _pred _rc _weight double float long int in if using with 命令: 读入数据一种方式 input x y 1 4 2 5.5 3 6.2 4 7.7 5 8.5 end su/summarise/sum x 或 su/summarise/sum x,d 对分组的描述: sort group by group:su x %%%%% tabstat economy,stats(max) %返回变量economy的最大值 %%stats括号里可以是:mean,count(非缺失观测值个数),sum(总和),max,min,range, %% sd,var,cv(变易系数=标准差/均值),skewness,kurtosis,median,p1(1%分位 %% 数,类似地有p10, p25, p50, p75, p95, p99),iqr(interquantile range = p75 – p25) _all %描述全部 _N 数据库中观察值的总个数。 _n 当前观察值的位置。 _pi 圆周率π的数值。 list gen/generate %产生数列 egen wagemax=max(wage) clear use by(分组变量)

常用到的stata命令

安装estat: ssc install estout,replace\ 2010-10-14 11:38:15来自: 杨囡囡(all a woman lack is a wife) (转自人大论坛) 调整变量格式: format x1 %10.3f ——将x1的列宽固定为10,小数点后取三位 format x1 %10.3g ——将x1的列宽固定为10,有效数字取三位 format x1 %10.3e ——将x1的列宽固定为10,采用科学计数法 format x1 %10.3fc ——将x1的列宽固定为10,小数点后取三位,加入千分位分隔符 format x1 %10.3gc ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符 format x1 %-10.3gc ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符,加入“-”表示左对齐 合并数据: use "C:\Documents and Settings\xks\桌面\2006.dta", clear merge using "C:\Documents and Settings\xks\桌面\1999.dta" ——将1999和2006的数据按照样本(observation)排列的自然顺序合并起来 use "C:\Documents and Settings\xks\桌面\2006.dta", clear merge id using "C:\Documents and Settings\xks\桌面\1999.dta" ,unique sort ——将1999和2006的数据按照唯一的(unique)变量id来合并,在合并时对id进行排序(sort) 建议采用第一种方法。 对样本进行随机筛选: sample 50 在观测案例中随机选取50%的样本,其余删除 sample 50,count 在观测案例中随机选取50个样本,其余删除 查看与编辑数据:

stata常用命令

面板数据估计 首先对面板数据进行声明: 前面是截面单元,后面是时间标识: 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

stata常用命令

stata常用命令 stata save命令 FileSave As 例1. 表1.为某一降压药临床试验数据,试从键盘输入Stata,并保存为Stata格式文件。STATA数据库的维护 排序 SORT 变量名1 变量名2 …… 变量更名 rename 原变量名新变量名 STATA数据库的维护 删除变量或记录 drop x1 x2 /* 删除变量x1和x2 drop x1-x5 /* 删除数据库中介于x1和x5间的所有变量(包括x1和x5) drop if x<0 /* 删去x1<0的所有记录 drop in 10/12 /* 删去第10~12个记录 drop if x==. /* 删去x为缺失值的所有记录 drop if x==.|y==. /* 删去x或y之一为缺失值的所有记录 drop if x==.&y==. /* 删去x和y同时为缺失值的所有记录 drop _all /* 删掉数据库中所有变量和数据 STATA的变量赋值 用generate产生新变量 generate 新变量=表达式 generate bh=_n /* 将数据库的内部编号赋给变量bh。 generate group=int((_n-1)/5)+1 /* 按当前数据库的顺序,依次产生5个1,5个2,5个3……。直到数据库结束。 generate block=mod(_n,6) /* 按当前数据库的顺序,依次产生1,2,3,4,5,0。generate y=log(x) if x>0 /* 产生新变量y,其值为所有x>0的对数值log(x),当x<=0时,用缺失值代替。 egen产生新变量 set obs 12 egen a=seq() /*产生1到N的自然数 egen b=seq(),b(3) /*产生一个序列,每个元素重复#次 egen c=seq(),to(4) /*产生多个序列,每个序列从1到# egen d=seq(),f(4)t(6) /*产生多个序列,每个序列从#1到#2

常用stata命令-好用

我常用到的stata命令 最重要的两个命令莫过于help和search了。即使是经常使用stata的人也很难,也没必要记住常用命令的每一个细节,更不用说那些不常用到的了。所以,在遇到困难又没有免费专家咨询时,使用stata自带的帮助文件就是最佳选择。stata的帮助文件十分详尽,面面俱到,这既是好处也是麻烦。当你看到长长的帮助文件时,是不是对迅速找到相关信息感到没有信心? 闲话不说了。help和search都是查找帮助文件的命令,它们之间的区别在于help用于查找精确的命令名,而search是模糊查找。如果你知道某个命令的名字,并且想知道它的具体使用方法,只须在stata的命令行窗口中输入help空格加上这个名字。回车后结果屏幕上就会显示出这个命令的帮助文件的全部内容。如果你想知道在stata下做某个估计或某种计算,而不知道具体该如何实现,就需要用search命令了。使用的方法和help类似,只须把准确的命令名改成某个关键词。回车后结果窗口会给出所有和这个关键词相关的帮助文件名和链接列表。在列表中寻找最相关的内容,点击后在弹出的查看窗口中会给出相关的帮助文件。耐心寻找,反复实验,通常可以较快地找到你需要的内容。 下面该正式处理数据了。我的处理数据经验是最好能用stata的do文件编辑器记下你做过的工作。因为很少有一项实证研究能够一次完成,所以,当你下次继续工作时。能够重复前面的工作是非常重要的。有时因为一些细小的不同,你会发现无法复制原先的结果了。这时如果有记录下以往工作的do文件将把你从地狱带到天堂。因为你不必一遍又一遍地试图重现做过的工作。在stata窗口上部的工具栏中有个孤立的小按钮,把鼠标放上去会出现“bring do-file editor to front”,点击它就会出现do文件编辑器。 为了使do文件能够顺利工作,一般需要编辑do文件的“头”和“尾”。这里给出我使用的“头”和“尾”。 /*(标签。简单记下文件的使命。)*/ capture clear (清空内存中的数据) capture log close (关闭所有打开的日志文件) set mem 128m (设置用于stata使用的内存容量) set more off (关闭more选项。如果打开该选项,那么结果分屏输出,即一次只输出一屏结果。你按空格键后再输出下一屏,直到全部输完。如果关闭则中间不停,一次全部输出。)set matsize 4000 (设置矩阵的最大阶数。我用的是不是太大了?) cd D: (进入数据所在的盘符和文件夹。和dos的命令行很相似。) log using (文件名).log,replace (打开日志文件,并更新。日志文件将记录下所有文件运行后给出的结果,如果你修改了文件内容,replace选项可以将其更新为最近运行的结果。) use (文件名),clear (打开数据文件。) (文件内容)

stata常用命令

调整变量格式: format x1 % ——将x1的列宽固定为10,小数点后取三位 format x1 % ——将x1的列宽固定为10,有效数字取三位 format x1 % ——将x1的列宽固定为10,采用科学计数法 format x1 % ——将x1的列宽固定为10,小数点后取三位,加入千分位分隔符 format x1 % ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符 format x1 % ——将x1的列宽固定为10,有效数字取三位,加入千分位分隔符,加入“-”表示左对齐合并数据: use "C:\Documents and Settings\xks\桌面\", clear merge using "C:\Documents and Settings\xks\桌面\" ——将1999和2006的数据按照样本(observation)排列的自然顺序合并起来 use "C:\Documents and Settings\xks\桌面\", clear merge id using "C:\Documents and Settings\xks\桌面\" ,unique sort ——将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 "t:\statatut\" su ——summarize的简写 sort v001 v002 v003 save tempw1 第二步:对exampw2做同样的处理 clear use "t:\statatut\" su sort v001 v002 v003 save tempw2 第三步:使用tempw1数据库,将其与tempw2合并: clear use tempw1 merge v001 v002 v003 using tempw2 第四步:查看合并后的数据状况:

Stata统计分析命令

Stata统计分析常用命令汇总 一、winsorize极端值处理 范围:一般在1%和99%分位做极端值处理,对于小于1%的数用1%的值赋值,对于大于99%的数用99%的值赋值。 1、Stata中的单变量极端值处理: stata 11.0,在命令窗口输入“findit winsor”后,系统弹出一个窗口,安装winsor模块 安装好模块之后,就可以调用winsor命令,命令格式:winsor var1, gen(new var) p(0.01) 或者在命令窗口中输入:ssc install winsor安装winsor命令。winsor命令不能进行批量处理。 2、批量进行winsorize极端值处理: 打开链接:.edu/judson.caskey/data.html,找到winsorizeJ,点击右键,另存为到stata中的ado/plus/目录下即可。命令格式:winsorizeJ var1var2var3,suffix(w)即可,这样会生成三个新变量,var1w var2w var3w,而且默认的是上下1%winsorize。如果要修改分位点,则写成如下格式:winsorizeJ var 1 var2 var3,suffix(w) cuts(5 95)。 3、Excel中的极端值处理:(略) winsor2 命令使用说明 简介:winsor2 winsorize or trim (if trim option is specified) the variables in varlist at particular percentiles specified by option cuts(# #). In defult, new variables will be generated with a suffix "_w" or "_tr", which can be changed by specifying suffix() option. The replace option replaces the variables with their winsorized or trimmed ones. 相比于winsor命令的改进: (1) 可以批量处理多个变量; (2) 不仅可以winsor,也可以trimming; (3) 附加了by() 选项,可以分组winsor 或trimming; (4) 增加了replace 选项,可以不必生成新变量,直接替换原变量。 范例: *- winsor at (p1 p99), get new variable "wage_w" . sysuse nlsw88, clear . winsor2 wage *- left-trimming at 2th percentile . winsor2 wage, cuts(2 100) trim *- winsor variables by (industry south), overwrite the old variables . winsor2 wage hours, replace by(industry south) 使用方法: 1. 请将winsor 2.ado 和winsor2.sthlp 放置于stata12\ado\base\w 文件夹下; 2. 输入help winsor2 可以查看帮助文件; 二、描述性统计 1、summarize 命令格式:su、sum或者summarize [varlist] [if] [in] [weight] [,options] 如果summarize或sum后不加任何变量,则默认对数据中的所有变量进行描述统计options 选项:detail 表示产生更加详细的统计变量

常用到的STATA命令

年0606月月0101日,日,日,200820082008年第年第年第44期,总第期,总第6 6期编者按:Stata 的数据管理能力非常powerful ,而我们EpiMan 论坛上有很多优秀的帖子,我们会不定期对其进行整理,以期使得大家可以更好地掌握Stata 软件,提高工作和学习的效率和乐趣。毕竟,Stata 是一款巨有特色的统计软件包,推荐使用。 我常用到的Stata命令 by 无为而治的西瓜与西瓜老死不相往来 Special Issue Fundamental of Data Manipulation Using Stata https://www.wendangku.net/doc/bb3821262.html,

EpiMan Cluster特刊:我常用到的Stata命令 https://www.wendangku.net/doc/bb3821262.html, Special Issue 我常用到的Stata命令 文章作者:无为而治的西瓜与西瓜老死不相往来 本期主编:epiman 本期审校:epiman 本期排版:epiman 2008年06月01日 2008年第4期 总第6期 邮箱:epiman@https://www.wendangku.net/doc/bb3821262.html, 网址:https://www.wendangku.net/doc/bb3821262.html, 目录 第一讲,do文件的穿靴戴帽 第二讲,原始数据→分析数据 第三讲,批量生成虚拟变量 第四讲,改变数据结构 第五讲,复杂数据处理技巧--迂回 第六讲,本期声明和好汉招募

第一讲,help和search的区别以及do文件的穿靴戴帽第一讲,help和search的区别以及do文件的穿靴戴帽 作者:无为而治的西瓜与西瓜老死不相往来@https://www.wendangku.net/doc/bb3821262.html, 最重要的两个命令莫过于help和search了。即使是经常使用stata的人也很难,也没必要记住常用命令的每一个细节,更不用说那些不常用到的了。所以,在遇到困难又没有免费专家咨询时,使用stata自带的帮助文件就是最佳选择。stata的帮助文件十分详尽,面面俱到,这既是好处也是麻烦。当你看到长长的帮助文件时,是不是对迅速找到相关信息感到没有信心?闲话不说了。help和search都是查找帮助文件的命令,它们之间的区别在于help用于查找精确的命令名,而search是模糊查找。如果你知道某个命令的名字,并且想知道它的具体使用方法,只须在stata的命令行窗口中输入help空格加上这个名字。回车后结果屏幕上就会显示出这个命令的帮助文件的全部内容。如果你想知道在stata下做某个估计或某种计算,而不知道具体该如何实现,就需要用search命令了。使用的方法和help类似,只须把准确的命令名改成某个关键词。回车后结果窗口会给出所有和这个关键词相关的帮助文件名和链接列表。在列表中寻找最相关的内容,点击后在弹出的查看窗口中会给出相关的帮助文件。耐心寻找,反复实验,通常可以较快地找到你需要的内容。 下面该正式处理数据了。我的处理数据经验是最好能用stata的do文件编辑器记下你做过的工作。因为很少有一项实证研究能够一次完成,所以,当你下次继续工作时。能够重复前面的工作是非常重要的。有时因为一些细小的不同,你会发现无法复制原先的结果了。这时如果有记录下以往工作的do文件将把你从地狱带到天堂。因为你不必一遍又一遍地试图重现做过的工作。在stata窗口上部的工具栏中有个孤立的小按钮,把鼠标放上去会出现“bring do-file editor to front”,点击它就会出现do文件编辑器。 为了使do文件能够顺利工作,一般需要编辑do文件的“头”和“尾”。这里给出我使用的“头”和“尾”。 capture clear(清空内存中的数据) capture log close(关闭所有打开的日志文件) set mem128m(设置用于stata使用的内存容量) set more off(关闭more选项。如果打开该选项,那么结果分屏输出,即一次只输出一屏结果。你按空格键后再输出下一屏,直到全部输完。如果关闭则中间不停,一次全部输出。)set matsiz e4000(设置矩阵的最大阶数。我用的是不是太大了?) cd D:(进入数据所在的盘符和文件夹。和dos的命令行很相似。) log using(文件名).log,replace(打开日志文件,并更新。日志文件将记录下所有文件运行后给出的结果,如果你修改了文件内容,replace选项可以将其更新为最近运行的结果。)use(文件名),clear(打开数据文件。) (文件内容) log close(关闭日志文件。) exit,clear(退出并清空内存中的数据。) 这个do文件的“头尾”并非我的发明,而是从沈明高老师那里学到的。版权归沈明高老师。

stata常用命令

stata 常用命令 (2012-07-29 17:22:25) 转载▼ 分类:stata 标签: 杂谈 save命令 FileSave As 例1. 表1.为某一降压药临床试验数据,试从键盘输入Stata,并保存为Stata格式文件。STATA数据库的维护 排序 SORT 变量名1 变量名2 …… 变量更名 rename 原变量名新变量名 STATA数据库的维护 删除变量或记录 drop x1 x2 /* 删除变量x1和x2 drop x1-x5 /* 删除数据库中介于x1和x5间的所有变量(包括x1和x5) drop if x<0 /* 删去x1<0的所有记录 drop in 10/12 /* 删去第10~12个记录 drop if x==. /* 删去x为缺失值的所有记录 drop if x==.|y==. /* 删去x或y之一为缺失值的所有记录 drop if x==.&y==. /* 删去x和y同时为缺失值的所有记录 drop _all /* 删掉数据库中所有变量和数据 STATA的变量赋值 用generate产生新变量 generate 新变量=表达式 generate bh=_n /* 将数据库的内部编号赋给变量bh。 generate group=int((_n-1)/5)+1 /* 按当前数据库的顺序,依次产生5个1,5个2,5个 3……。直到数据库结束。 generate block=mod(_n,6) /* 按当前数据库的顺序,依次产生1,2,3,4,5,0。generate y=log(x) if x>0 /* 产生新变量y,其值为所有x>0的对数值log(x),当x<=0时,用缺失值代替。 egen产生新变量 set obs 12 egen a=seq() /*产生1到N的自然数 egen b=seq(),b(3) /*产生一个序列,每个元素重复#次 egen c=seq(),to(4) /*产生多个序列,每个序列从1到# egen d=seq(),f(4)t(6) /*产生多个序列,每个序列从#1到#2 encode 字符变量名,gen(新数值变量名) 作用:将字符型变量转化为数值变量。

STATA简单命令

STATA 的简单命令 Stata 中最重要的命令莫过于help 和search 了。 help 用于查找精确的命令,而search 是模糊查找。例如:help regress 又如:我们记不清regress命令的全名,只记得regress 的前半部分reg,那么可以输入search reg 用户获得信息最有效的另一个途径是使用Statalist 在线论坛,该论坛提供Stata 用户交流的一个良好的平台。要加入Statalist,我们可以给以下地址发个邮件: majordomo@https://www.wendangku.net/doc/bb3821262.html, 邮件的内容为:subscribe Statalist 变量的命名: 1. 变量名可达32 个字符。 2. 字符组成部分为A~Z 、a~z、0~9与下划线“ _”,这些字符以外的其他符号不可以出现在 变量名中。 3. 变量名不能以数字开头。 4. 变量名区分大小写。 5. 倘若遵循以上原则依然无法正常命名变量,那么这个变量可能与Stata 自身保留的供系 统使用的变量重复了。 创建数据文件的方法: 1. 手动输入。 2. 从excel 等文件中复制粘贴到stata 数据表中。 3. 运用stata 软件导入。 查看数据的概貌: summarize x codebook x 如果上面两个命令后面不加内容,那么显示的结果是所有变量的概貌。 对数据进行排序的命令:sort 标准 1 标准 2 标准3 生成数据的命令:gen 1. 如果要得到一阶差分,可以用以下命令:gen Difference_invest(新变量的名称是任意 的)=d.invest ( d.是运算符号,不得改变;invest 是变量名称) 2. 要想产生一个新的变量Lag_invest ,也就是invest 的一阶滞后,那么我们可以采用如下 命令:gen Lag_invest = l.invest 3. 生成对数的命令:gen Ln_invest=ln(invest) 作散点图的命令:scatter 1. scatter x1 x2:scatter 后的第一个变量是纵轴的变量,第二个变量是横轴的变量。 2. scatter x1 x2, connect(1) :以直线的方式连接相邻的两个点。

相关文档