paohuman paohuman
关注数: 1 粉丝数: 21 发帖数: 555 关注贴吧数: 10
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 "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个样本,其余删除 查看与编辑数据: 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\exampw1.dta" su ——summarize的简写 sort v001 v002 v003 save tempw1 第二步:对exampw2做同样的处理 clear use "t:\statatut\exampw2.dta" 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 "t:\statatut\fac19.dta" ta region append using "t:\statatut\newfac" 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的取值范围)
stata_panel_stepbystep 1 Setting up Stata We are going to allocate 10 megabites to the dataset. You do not want to allocate to much memory to the dataset because the more memory you allocate to the dataset, the less memory will be available to perform the commands. You could reduce the speed of Stata or even kill it. set mem 10m we can also decide to have the “more” separation line on the screen or not when the software displays results: set more on set more off Setting up a panel Now, we have to instruct Stata that we have a panel dataset. We do it with the command tsset, or iis and tis iis idcode tis year or tsset idcode year In the previous command, idcode is the variable that identifies individuals in our dataset. Year is the variable that identifies time periods. This is always the rule. The commands refering to panel data in Stata almost always start with the prefix xt. You can check for these commands by calling the help file for xt. help xt © Thierry Warin, 2006-2007 2 You should describe and summarize the dataset as usually before you perform estimations. Stata has specific commands for describing and summarizing panel datasets. xtdes xtsum xtdes permits you to observe the pattern of the data, like the number of individuals with different patterns of observations across time periods. In our case, we have an unbalanced panel because not all individuals have observations to all years. The xtsum command gives you general descriptive statistics of the variables in the dataset, considering the overall, the between and the within variations. Overall refers to the whole dataset. Between refers to the variation of the means to each individual (across time periods). Within refers to the variation of the deviation from the respective mean to each individual. You may be interested in applying the panel data tabulate command to a variable. For instance, to the variable south, in order to obtain a one-way table. xttab south As in the previous commands, Stata will report the tabulation for the overall variation, the within and the between variation. How to generate variables Generating variables gen age2=age^2 gen ttl_exp2=ttl_exp^2 gen tenure2=tenure^2 © Thierry Warin, 2006-2007 3 Now, let's compute the average wage for each individual (across time periods). bysort idcode: egen meanw=mean(ln_wage) In this case, we did not apply the sort command previously and then the by prefix command. We could have done it, but with this only command, you can always abreviate the implementation of the by prefix command. The command egen is an extension of the gen command to generate new variables. The general rule to apply egen is when you want to generate a new variable that is created using a function inside Stata. In our case, we used the function mean. You can apply the command list to list the first 10 observations of the new variable mwage. list meanw in 1/10 And then apply the xtsum command to summarize the new variable. xtsum meanw You may want to obtain the average of the logarithm of wages to each year in the panel. bysort year: egen meanw1=mean(ln_wage) And then you can apply the xttab command. xttab meanw1 Generating dates Let’s generate dates: Gen varname2 = date(varname1, “dmy”) © Thierry Warin, 2006-2007 4 And format: Format varname2 %d How to generate dummies Generating general dummies Let's generate the dummy variable black, which is not in our dataset. gen black=1 if race==2 replace black=0 if black==. Suppose you want to generate a new variable called tenure1 that is equal to the variable tenure lagged one period. Than you would use a time series operator (l). First, you would need to sort the dataset according to idcode and year, and then generate the new variable with the "by" prefix on the variable idcode. sort idcode year by idcode: gen tenure1=l.tenure If you were interested in generating a new variable tenure3 equal to one difference of the variable tenure, you would use the time series d operator. by idcode: gen tenure3=d.tenure If you would like to generate a new variable tenure4 equal to two lags of the variable tenure, you would type: by idcode: gen tenure4=l2.tenure The same principle would apply to the operator d. Let's just save our data file with the changes that we made to it. © Thierry Warin, 2006-2007 5 save, replace Another way would be to use the xi command. It takes the items (string of letters, for instance) of a designated variable (category, for instance) and create a dummy variable for each item. You need to change the base anyway: char _dta[omit] “prevalent” xi: i.category tabulate category Generating time dummies In order to do this, let's first generate our time dummies. We use the "tabulate" command with the option "gen" in order to generate time dummies for each year of our dataset. We will name the time dummies as "y", • and we will get a first time dummy called "y1" which takes the value 1 if year=1980, 0 otherwise, • a second time dummy "y2" which assumes the value 1 if year=1982, 0 otherwise, and similarly for the remaining years. You could give any other name to your time dummies. tab year, g(y) © Thierry Warin, 2006-2007 6 Running OLS regressions Let's now turn to estimation commands for panel data. The first type of regression that you may run is a pooled OLS regression, which is simply an OLS regression applied to the whole dataset. This regression is not considering that you have different individuals across time periods, and so, it is not considering for the panel nature of the dataset. reg ln_wage grade age ttl_exp tenure black not_smsa south In the previous command, you do not need to type age1 or age2. You just need to type age. When you do this, you are instructing Stata to include all the variables starting with the expression age to be included in the regression. Suppose you want to observe the internal results saved in Stata associated with the last estimation. This is valid for any regression that you perform. In order to observe them, you would type: ereturn list If you want to control for some categories: xi: reg dependent ind1 ind2 i.category1 i.category2 i.time Let's perform a regression where only the variation of the means across individuals is considered. This is the between regression. xtreg ln_wage grade age ttl_exp tenure black not_smsa south, be © Thierry Warin, 2006-2007 7 Running Panel regressions In empirical work in panel data, you are always concerned in choosing between two alternative regressions. This choice is between fixed effects (or within, or least squares dummy variables - LSDV) estimation and random effects (or feasible generalized least squares - FGLS) estimation. In panel data, in the two-way model, the error term can be the result of the sum of three components: 1. The two-way model assumes the error term as having a specific individual term effect, 2. a specific time effect 3. and an additional idiosyncratic term. In the one-way model, the error term can be the result of the sum of one
【转】跟美国教授沟通的18种礼仪 1. E-mail is forever. Once you send it off, you can't get it back. Once your professor has it, he or she owns it and can save it or, in the worst case, forward it onto colleagues for a good laugh—at your expense。 邮件是永久性的。一旦你把它发出去了,就不可能再收回来了。只要教授收到了邮件,他就主宰了这封邮件的命运,或者保存,或者是把邮件转发给同事博取一笑(这是最糟的情况)——后果自负哦。 2. E-mail goe s where it's told. Check—and double check—to see that the right address appears in the "To" line. Just because your mom and your professor are both named "Lynn" is no reason to send all your love to Professor Lynn。 邮件要发到你被告知的邮箱里。检查,再检查,看“发送到”那一栏的地址是不是正确。仅仅因为你的妈妈和你的教授的名字都是"Lynn",也没有理由把你所有的爱都寄送到了Lynn教授那里吧。 3. Professors might not be using the cruddy university e-mail system. So send it to the address they actually use, not the one on the university directory. (Check the syllabus or assignment sheet for clues。) 教授可能不使用大学的乱七八糟的邮箱系统。所以把邮件发送到他们真正在用的邮箱地址,而不是大学通讯录上的邮箱地址。(检查一下他们的个人摘要或者任务分配表来寻找一些线索。) 4. Professors might not open mail sent from [email protected]. They prefer to open mail sent from more reputable addresses, like [email protected]。 教授可能不会打开一封发自[email protected]的邮件。他们更喜欢打开那些来自比较有名的邮箱地址,学校邮箱最好,[email protected]。 5. Subject lines are for subjects. Put a brief explanation of the nature of the e-mail (like "question about paper") in the subject line. Never include demands such as "urgent request—immediate response needed." That's the surest way to get your request trashed。 主题栏要紧扣主题。在主题栏要对这封邮件有一个简要的解释。千万不要包含一些要求,比如说“紧急请求--请立即回复”,可以很肯定的说,你的请求会像垃圾一样处理掉。 6. Salutations matter. The safest way to start is with "Dear Professor So and So" (using their last name). That way you won't be getting into the issue of whether the prof has a Ph.D. or not, and you won't seem sexist when you address your female-professor as "Ms." or, worse yet, "Mrs. This and That." 称呼很重要。最安全的开头方式是用“亲爱的×××教授”。这样的话你就不会再考虑这个教授是不是个博士或其他,并且当你称呼你的女教授“Ms”或者“Mrs。”也不会显得像是个性别歧视者。 7. Clear and concise is best. Your prof might get 25 or 30 E-mails a day, so, it's best if you ask your questions in as focused and succinct a way as possible. (Hint: it's often good to number your questions). And, if your question is very elaborate or multifaceted, it's best to go to an in-person office hour. You'll get better service that way。 清楚简洁是最好的。你的教授每天会收到25-30封邮件,所以如果你的问题很简洁明了那是最好的(一般把问题一条条列出来比较好)。如果你的问题表述的很详尽或者多层面的,最好在教授的办公时间面谈,这样教授就能够更有效地帮你解决问题。 8. Always acknowledge. If your professor deigns to answer—or send you the handout or reference that you asked for—be sure to tell him or her that you got it. That way he or she will think kindly of you next time they see you in class。 必须要告知教授已收到邮件。如果你的教授屈尊给你回答或者发送你需要的资料,一定要告诉他你已经收到了。这样,下次在课堂上看到你的时候,他们就会觉得你是个不错的学生。 9. THIS IS NOT A SHOUTING MATCH. Don't write in all uppercase letters, which is an E-mail convention for anger or other strong emotions. No one likes yelling。 这不是大声嚷嚷的争吵。邮件不要都用大写字母来写,因为这是一种在邮件中表达愤怒或者其他强烈的感情的方式。没有人喜欢喊叫的。 10. No one really likes emoticons and smileys. Trust us on this one. 没有人真的喜欢表情符号和微笑符。真的。 11. This is not Facebook. Don't write the professor in the way you'd write on your friend's wall。 这不是Facebook。你写给教授的邮件不要像你在朋友留言板上的留言一样。 重要提示:千万不要和你的教授开玩笑,这个时候,无论多可笑都不可以。 12. This is not IM-ing. So pls dun wrte yor profeSR lIk urtxtN. uz abbrz @ yor own rsk. coRec me f Im wrng。 这不是写即时消息..。。所以不要像写短消息一样用单词的缩写形式,你绝对是在冒风险。(如果我说的不对请改正) 13. This is not CollegeHumor. Resist the temptation to talk about the "bad ass" paper you need help with, your "loser" TA who didn't teach you what you needed to know, or the "crappy" grade you just got on the midterm。 这不是校园幽默。不要随意拿以下的事情当谈资:你的论文很难搞,你那个“很衰”助教都不教些你想知道的东西,或者是你期中考[微博]试考了个很烂的分数。 14. This is not RateMyProfessors.com. The professor doesn't want your comments about his or her performance in the class. Save those for the end-of-semester evaluations, where you'll be able to spout off. Anonymously。 这不是在评价你的教授。没有教授想听你评价他们的课堂表现,所以把你的意见留到期末评价中,你可以滔滔不绝地表达出你对老师的看法,而且是匿名的。 15. Spelling mistakes make you look like a doofus. So always use the spel check, and proofread yyour e-mail, two 拼写错误会使你看起来像个笨蛋。所以每次都要使用拼写检查,并校对你的邮件两遍。 16. Signoffs and signatures count. Always end by thanking the professor for his or her time, and closing with "Best wishes" or "Regards" (or some other relatively formal, but friendly, closing). And always sign with your (entire) real name, not some wacky nickname like Ry-Ry or Biff。 最后的签名很重要。每封邮件最后都要感谢教授抽出时间来帮助你,并以“Best Wishes、Regards”来结尾。(或者其他相关的正式语,但是要用友好的语气。)然后签上你的全名,而不是什么昵称,就像Ry-Ry 或者 Biff。 17. Your prof doesn't want to hear your philosophy of life. Skip the cute quotes or statements of your religious or political views at the bottom of your E-mail. You never know what offends。 你的教授不想听你讲你的人生哲学。在你的邮件最后不要说什么你引用的宗教或者政治观点。你永远不知道这些会不会冒犯你的教授。 18. Don't lay it on too thick. It's one thing to be polite and friendly in your e-mail; it's another thing to wind up with a brown nose。 不要太过殷勤。在邮件中表现出礼貌和友好很重要,但是要适度,不要搞得像是在阿谀奉承。
【转】我常用到的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 (打开数据文件。) (文件内容) log close (关闭日志文件。) exit,clear (退出并清空内存中的数据。) 这个do文件的“头尾”并非我的发明,而是从沈明高老师那里学到的。版权归沈明高老师。 实证工作中往往接触的是原始数据。这些数据没有经过整理,有一些错漏和不统一的地方。比如,对某个变量的缺失观察值,有时会用点,有时会用-9,-99等来表示。回归时如果使用这些观察,往往得出非常错误的结果。还有,在不同的数据文件中,相同变量有时使用的变量名不同,会给合并数据造成麻烦。因此,拿到原始数据后,往往需要根据需要重新生成新的数据库,并且只使用这个新库处理数据。这部分工作不难,但是非常基础。因为如果在这里你不够小心,后面的事情往往会白做。 假设你清楚地知道所需的变量,现在要做的是检查数据、生成必要的数据并形成数据库供将来使用。检查数据的重要命令包括codebook,su,ta,des和list。其中,codebook提供的信息最全面,缺点是不能使用if条件限制范围,所以,有时还要用别的帮帮忙。su空格加变量名报告相应变量的非缺失的观察个数,均值,标准差,最小值和最大值。ta空格后面加一个(或两个)变量名是报告某个变量(或两个变量二维)的取值(不含缺失值)的频数,比率和按大小排列的累积比率。des后面可以加任意个变量名,只要数据中有。它报告变量的存储的类型,显示的格式和标签。标签中一般记录这个变量的定义和单位。list报告变量的观察值,可以用if或in来限制范围。所有这些命令都可以后面不加任何变量名,报告的结果是正在使用的数据库中的所有变量的相应信息。说起来苍白无力,打开stata亲自实验一下吧。 顺带说点儿题外话。除了codebook之外,上述统计类的命令都属于r族命令(又称一般命令)。执行后都可以使用return list报告储存在r()中的统计结果。最典型的r族命令当属summarize。它会把样本量、均值、标准差、方差、最小值、最大值、总和等统计信息储存起来。你在执行su之后,只需敲入return list就可以得到所有这些信息。其实,和一般命令的return命令类似,估计命令(又称e族命令)也有ereturn命令,具有报告,储存信息的功能。在更复杂的编程中,比如对回归分解,计算一些程序中无法直接计算的统计量,这些功能更是必不可少。 检查数据时,先用codebook看一下它的值域和单位。如果有-9,-99这样的取值,查一下问卷中对缺失值的记录方法。确定它们是缺失值后,改为用点记录。命令是replace (变量名)=. if (变量名)==-9。再看一下用点记录的缺失值有多少,作为选用变量的一个依据。 得到可用的数据后,我会给没有标签的变量加上注解。或者统一标签;或者统一变量的命名规则。更改变量名的命令是ren (原变量名)空格(新变量名)。定义标签的命令是label var (变量名)空格”(标签内容)”。整齐划一的变量名有助于记忆,简明的标签有助于明确变量的单位等信息。 如果你需要使用通过原始变量派生出的新变量,那么就需要了解gen,egen和replace这三个命令。gen和replace常常在一起使用。它们的基本语法是gen (或replace)空格(变量名)=(表达式)。二者的不同之处在于gen是生成新变量,replace是重新定义旧变量。 虚拟变量是我们常常需要用到的一类派生变量。如果你需要生成的虚拟变量个数不多,可以有两种方法生成。一种是简明方法:gen空格(变量名)=((限制条件))[这外面的小括弧是命令需要的,里面的小括弧不是命令需要的,只是说明“限制条件”并非命令]。如果某个观察满足限制条件,那么它的这个虚拟变量取值为1,否则为0。另一种要麻烦一点。就是 gen (变量名)=1 if (取值为一限制条件) replace(相同的变量名)=0 if (取值为零的限制条件) 两个方法貌似一样,但有一个小小的区别。如果限制条件中使用的变量都没有任何缺失值,那么两种方法的结果一样。如果有缺失值,第一种方法会把是缺失值的观察的虚拟变量都定义为0。而第二种方法可以将虚拟变量的取值分为三种,一是等于1,二是等于0,三是等于缺失值。这样就避免了把本来信息不明的观察错误地纳入到回归中去。下次再讲如何方便地生成成百上千个虚拟变量。 大量的虚拟变量往往是根据某个已知变量的取值生成的。比如,在某个回归中希望控制每个观察所在的社区,即希望控制标记社区的虚拟变量。社区数目可能有成百上千个,如果用上次的所说的方法生成就需要重复成百上千次,这也太笨了。大量生成虚拟变量的命令如下; ta (变量名), gen((变量名)) 第一个括号里的变量名是已知的变量,在上面的例子中是社区编码。后一个括号里的变量名是新生成的虚拟变量的共同前缀,后面跟数字表示不同的虚拟变量。如果我在这里填入d,那么,上述命令就会新生成d1,d2,等等,直到所有社区都有一个虚拟变量。 在回归中控制社区变量,只需简单地放入这些变量即可。一个麻烦是虚拟变量太多,怎么简单地加入呢?一个办法是用省略符号,d*表示所有d字母开头的变量,另一法是用破折号,d1-d150表示第一个到第150个社区虚拟变量(假设共有150个社区)。 还有一种方法可以在回归中直接控制虚拟变量,而无需真的去生成这些虚拟变量。使用命令areg可以做到,它的语法是 areg (被解释变量) (解释变量), absorb(变量名) absorb选项后面的变量名和前面讲的命令中第一个变量名相同。在上面的例子中即为社区编码。回归的结果和在reg中直接加入相应的虚拟变量相同。 生成变量的最后一招是egen。egen和gen都用于生成新变量,但egen的特点是它更强大的函数功能。gen可以支持一些函数,egen支持额外的函数。如果用gen搞不定,就得用egen想办法了。不过我比较懒,到现在为止只用用取平均、加和这些简单的函数。 有的时候数据情况复杂一些,往往生成所需变量不是非常直接,就需要多几个过程。曾经碰到原始数据中记录日期有些怪异的格式。比如,1991年10月23日被记录为19911023。我想使用它年份和月份,并生成虚拟变量。下面是我的做法: gen yr=int(date) gen mo=int((data-yr*10000)/100) ta yr, gen( yd) ta mo, gen( md) 假设你已经生成了所有需要的变量,现在最重要的就是保存好你的工作。使用的命令是save空格(文件名),replace。和前面介绍的一样,replace选项将更新你对数据库的修改,所以一定要小心使用。最好另存一个新的数据库,如果把原始库改了又变不回去,就叫天不应叫地不灵了。
【转】学习经济学需要熟悉哪些编程语言? 计量经济学也有很多小的门类,请对号入座。 有很多软件,Stata, matlab, R, SAS是相对来说用的比较多的。 如果是做应用计量(特别是横截面数据、面板数据),Stata是不二之选,因为不管是管理数据还是跑回归,实在太太太方便了。现在主流期刊的应用微观计量文章里面能用到的模型stata几乎都有,而且其中的绝大多数都是用stata做的。而且最大的优点是,简单! 如果做应用的时间序列,Eviews似乎是一个不错的选择。但是我一般不做这方面,也不是很有发言权。 如果做理论计量,stata eviews是没有现成的包的,而且即便Stata可以编程,可编程能力也是很差的,而且不稳健。所以懂R和Matlab就非常顺手。当然也可以用Python,最近Sargent就写了本用Python做计量的书。还有一个Julia,是这三种语言的混合,但是速度快很多,缺点是太过于小众。 如果对速度要求高,特别是金融计量很多对速度有要求的,可以考虑C、Fortran等语言。C和Fortran肯定是最快的。还有一个叫做OX的,速度快,但是也很小众。但是这些语言的缺点是学习难度比较高,开发时间比较慢。Julia据说速度堪比C,而且语法特别像Matlab、Python(意味着容易学习),但是处于刚起步阶段,用的人太少了。 如果是金融计量领域,强烈建议学会SAS。SAS是最权威,速度也很快,当然最大的问题是昂贵,而且可编程能力不是多么好。但是金融里面数据量都非常非常非常大,一般的软件都瘫的时候,SAS就派上用场了。 像我自己,做应用的时候都是用stata整理数据,能用stata的坚决不用其他软件。但是因为有的时候做一些理论计量的工作,所以matlab也是必不可少的。我也在学习Julia,因为matlab的速度实在太慢。Python我一般不用来做科学计算,用的人不多,而且速度慢,一般是用来抓数据的。 最后还是补充一点吧,为什么我推荐matlab而不是其他的软件,也是有道理的。很多模型,比如空间计量模型(spatial econometrics)、贝叶斯估计、以及宏观计量里面的DSGE model、SVAR等模型,在stata、Eviews里面都是没有什么现成的东西可以用的,但是matlab提供了丰富的包,比如Dynare就是基于Matlab的,还有LeSage的空间计量软件包等等,也是基于matlab的。所以几乎你想用的模型matlab里面都能找到codes然后直接用。就算没有,你自己在matlab里面写,也不是什么难事。 最后想起一句话来,关于这些软件的选择(stata除外,因为stata在应用计量领域的地位是几乎不可替代的)可以用两句话来概括:如果你自己的时间比计算机的时间宝贵的话,学习matlab、R、甚至Python、Julia是最合适的,如果计算机的时间比你的时间宝贵的话,学习C、Fortran是必须的。当然除非你的工作非常特殊(比如一些大型的结构模型的估计),一般来说,还是自己的时间更宝贵一点。 综上,做应用和做理论是不可能用一种软件解决的,建议根据自己的方向进行挑选。我觉着stata、matlab是比较推荐的,一个方便,一个灵活,都很强大,而且学习难度都不大,用的人都很多,交流起来相当方便。 PS:各个软件的速度比较, 可以看Julia的主页。
[转载]经济学、金融学博士书目 统计学基础部分 1、《统计学》《探索性数据分析》 David Freedman等,中国统计 (统计思想讲得好) 2、Mind on statistics 机械工业 (只需高中数学水平) 3、Mathematical Statistics and Data Analysis 机械工业(这本书理念很好,讲了很多新东西) 4、Business Statistics a decision making approach 中国统计 (实用) 5、Understanding Statistics in the behavioral science 中国统计 回归部分 1、《应用线性回归》 中国统计 (蓝皮书系列,有一定的深度,非常精彩) 2、Regression Analysis by example,(吸引人,推导少) 3、《Logistics回归模型——方法与应用》 王济川 郭志刚 高教 (不多的国内经典统计教材) 多元 1、《应用多元分析》 王学民 上海财大(国内很好的多元统计教材) 2、Analyzing Multivariate Data,Lattin等 机械工业(直观,对数学要求不高) 3、Applied Multivariate Statistical Analysis,Johnson & Wichem,中国统计(评价很高) 《应用回归分析和其他多元方法》Kleinbaum 《多元数据分析》Lattin 时间序列 1、《商务和经济预测中的时间序列模型》 弗朗西斯著(侧重应用,经典) 2、Forecasting and Time Series an applied approach,Bowerman & Connell(主讲Box-Jenkins(ARIMA)方法,附上了SAS和Minitab程序) 3、《时间序列分析:预测与控制》 Box,Jenkins 中国统计 《预测与时间序列》Bowerman 抽样 1、《抽样技术》 科克伦著(该领域权威,经典的书。不好懂——就算看得懂每个公式,未必能懂它的意思) 2、Sampling: Design and Analysis,Lohr,中国统计(讲了很多很新的方法,不好懂) 软件及其他 1、《SAS软件与应用统计分析》 王吉利 张尧庭 主编 (好书) 2、《SAS V8基础教程》 汪嘉冈编 中国统计(主要讲编程,没怎么讲统计) 3、《SPSS11统计分析教程(基础篇)(高级篇)》 张文彤 北京希望出版社 4、《金融市场的统计分析》 张尧庭著 广西师大(言简意赅) ◎经济和金融数学 ◎计量经济学,时间序列分析(回归分析(用于套期保值分析),多元分析(主成份分析和因子分析(用于风险管理))) John Y. Campbell, Andrew W. Lo, A. Craig MacKinlay, and Andrew Y. Lo ,The Econometrics of Financial Markets(金融经济学简明教材,不涉及宏观金融(宏观和货币经济学),不好读,需要一定经济学和金融学基础,水平没有Duffie和Cochrane的高) ★John H. Cochrane,Asset Pricing(易读,写法现代,需要必要金融经济学基础,读后可以看懂该领域论文,想学金融数学还是读Duffie的吧) ☆Russell Davidson,Econometric Theory and Methods (讲得最清晰的中级书,比格林的好读得多,虽然没林文夫的经典) ★Darrell Duffie,Dynamic Asset Pricing Theory(连续时间动态规划,虽然易读还是最好有泛函分析、测度论、随机微积分和向量空间优化知识基础,没有Hull的好读) ★Golderberg,A Course in Econometrics ☆William H. Greene ,Econometric Analysis(中级,应用计量经济学经典,难读,重点不突出,适合做参考书) ☆Gujarati,计量经济学(初级经典,易读但有点老旧) ☆林文夫Fumio Hayashi,Econometrics(中级,理论计量经济学经典,头两章重要,要一定数学基础和良师导读,比格林书易读) Helmut Lütkepohl,Markus Krātzig,Applied Time Series Econometrics,《应用时间序列计量经济学》 Ian Jacques,Mathematics for Economics and Business,《商务与经济数学》 B. Jerkins,Time Series Analysis:Forecasting & Control ☆Peter Kennedy, A Guide to Econometrics(绝佳初级教材,通俗易懂,不次于伍德里奇的《现代方法》)皮特,《计量经济学指南》 ☆平狄克《计量经济模型与经济预测》Econometric Models and Economic Forecasts 平狄克《不确定性下的投资》 Roger Myerson, Curt Hinrichs, Probability Models for Economic Decision,《经济决策的概率模型》 ★J. H. Stock, M. W. Watson, Introduction to Econometrics A. H. Studenmund,Introductory Econometrics with Applications,《应用计量经济学》(基础性) T. J. Watsham, K. Parramore《金融数量方法》 ★Jeffrey Wooldridge,Introductory Econometrics: A Modern Approach (初级,不侧重数学推理,可自学,适合经济类专业,不适合统计专业,Kennedy的书不次于它,古扎拉底的书比它深一些) ☆Wooldridge 伍德里奇,Econometric Analysis of Cross Section and Panel Data 《横截面与面板数据的计量经济学分析》(微观计量理论的经典,Green和Hayashi两本书的补充,需要初级或中级基础,易读) 邵宇《微观金融学及其数学基础》清华社 ○时间序列建模、时间序列分析及其算法研究 McKenzie,Research Design Issues in Time-Series Modeling of Financial Market Volatility Watsham,Parramore,Quantitative Methods in Finance ○数理金融学Econometrics of Finance Abramowitz,Stegun,Handbook of Mathematical Functions Briys,Options,Futures and Exotic Derivatives ★Brockwell, P. and Davis, Time series : theory and methods ☆《金融计量经济学导论》克里斯·布鲁克斯(Chris Brooks) ★Campbell, J.Y., A.W. Lo and A.C. MacKinlay, The econometrics of financial markets(消费的资本资产定价模型) Cox,Huang,Option Pricing and Application,Frontiers of Financial Theory Dempster,Pliska,Mathematics of Derivative Securities ☆Walter Enders, Applied Econometric Time Series(时间序列分析绝佳入门书,比汉密尔顿的经典易读得多) ★Gourieroux, G., ARCH models and financial applications ★James Douglas Hamilton, Time series analysis《时间序列分析》汉密尔顿(时间序列经典,侧重理论技术,不适合初学,需要一定基础,统计和经济都可用) ★Hamilton, J. and B. Raj, (Eds), Advances in markov switching models Karatzas,Lectures on the Mathematics of Finance ★Lardic S., V. Mignon, Econométrie des séries temporelles macroéconomiques et financières. Economica. ★《连续时间金融》罗伯特·莫顿(Robert Merton)Continuous time finance ★Mills, T.C., The econometric modelling of financial time series ★Muselia,Rutkowski,Martingale Models in Financial Modeling(连续时间、期权定价) ★Pliska,Introduction to Mathematical Finance:Discrete Time Models(离散时间模型高级教材) 数理金融学引论——离散时间模型 ★Reinsel, G., Elements of multivariate time series analysis 《金融数学》Stampfli ☆Ross,An Introduction to Mathematical Finance:Options and other Topics, Ross S. M., 《数理金融初步》罗斯(Sheldon M.Ross)(投资组合) Schachermayer,Introduction to the Mathematics of Financial Markets ★Tsay, R.S., Analysis of financial time series《金融时间序列分析》蔡瑞胸(Ruey S.Tsay)(美) 软件: 1、EViews 2、SAS
1 下一页