stata入门中文讲义(3)

2019-08-31 12:13

. global xlist price weight. reg mpg $xlist, noheader mpg Coef. Std. Err. t P>|t| [95% Conf. Interval] price -.0000935 .0001627 -0.57 0.567 -.000418 .0002309 weight -.0058175 .0006175 -9.42 0.000 -.0070489 -.0045862 _cons 39.43966 1.621563 24.32 0.000 36.20635 42.67296

全局宏可以适用于Stata软件的整个期间。比如修改example.do,调整宏xlist,都会起作用的。 Example.do sysuse auto,clear su mpg price weight reg mpg $xlist reg mpg `xlist’

局部宏用local 进行定义,用`’进行引用(其中左引号通常在~键上,与通常的单引号是不同的)。

. local xlist price weight. reg mpg `xlist', noheader mpg Coef. Std. Err. t P>|t| [95% Conf. Interval] price -.0000935 .0001627 -0.57 0.567 -.000418 .0002309 weight -.0058175 .0006175 -9.42 0.000 -.0070489 -.0045862 _cons 39.43966 1.621563 24.32 0.000 36.20635 42.67296

另一个例子

. local y mpg. local x \. reg `y' `x', noheader mpg Coef. Std. Err. t P>|t| [95% Conf. Interval] price -.0000935 .0001627 -0.57 0.567 -.000418 .0002309 weight -.0058175 .0006175 -9.42 0.000 -.0070489 -.0045862 _cons 39.43966 1.621563 24.32 0.000 36.20635 42.67296

1.7 循环语句

11

Stata的循环语句有三种:foreach、forvalues、while。foreach是根据列表中的每一项进行循环,forvalues是根据连续整数进行循环,while是根据用户的条件是否满足进行循环。 先看两处数据产生命令generate和replace,和随机数产生器runiform()。

. clear. set obs 100obs was 0, now 100. set seed 1234. generate x1=runiform(). generate x2=runiform(). generate x3=runiform(). generate x4=runiform(). generate sum=x1+x2+x3+x4

. summarize sum Variable Obs Mean Std. Dev. Min Max sum 100 1.909211 .5527524 .567311 3.092883

下面,我们分别用三种循环语句实现4个变量的求和。 首先用foreach

*foreach loop with a variable list quietly replace sum = 0

foreach var of varlist x1 x2 x3 x4 { }

summarize sum

. *foreach loop with a variable list. quietly replace sum = 0. foreach var of varlist x1 x2 x3 x4 { 2. quietly replace sum = sum +`var' 3. }. summarize sum Variable Obs Mean Std. Dev. Min Max sum 100 1.909211 .5527524 .5673109 3.092883quietly replace sum = sum +`var'

12

下面用forvales quietly replace sum = 0 forvalues i=1(1)4 { }

summarize sum

. quietly replace sum = 0. forvalues i=1(1)4 { 2. quietly replace sum = sum + x`i' 3. }. summarize sum Variable Obs Mean Std. Dev. Min Max sum 100 1.909211 .5527524 .5673109 3.092883quietly replace sum = sum + x`i'

利用while命令 quietly replace sum = 0 local i=1 while `i'<=4 { }

summarize sum

. quietly replace sum = 0. local i=1. while `i'<=4 { 2. quietly replace sum = sum + x`i' 3. local i = `i' + 1 4. } . summarize sum Variable Obs Mean Std. Dev. Min Max sum 100 1.909211 .5527524 .5673109 3.092883quietly replace sum = sum + x`i' local i = `i' + 1

13

1.8 有用的其他命令

用户手册中提出的每个人需要知道的最常用的42条命令 Getting online help [U] 4 Stata’s help and search facilities

help, hsearch, net search, search, findit

Keeping Stata up to date

ado, net, update [U] 28 Using the Internet to keep up to date adoupdate [R] adoupdate

Operating system interface

pwd, cd [D] cd

Using and saving data from disk

save, saveold [D] save use [D] use

append, merge [U] 22 Combining datasets compress [D] compress

Inputting data into Stata [U] 21 Inputting and importing data

import [D] import edit [D] edit

Basic data reporting

describe [D] describe codebook [D] codebook list [D] list browse [D] edit count [D] count inspect [D] inspect table [R] table

tabulate [R] tabulate oneway and [R] tabulate twoway

Data manipulation [U] 13 Functions and expressions

generate, replace [D] generate egen [D] egen

rename [D] rename, [D] rename group clear [D] clear

drop, keep [D] drop sort [D] sort

encode, decode [D] encode, destring/tostring recode

order [D] order

by [U] 11.5 by varlist: construct reshape [D] reshape

Keeping track of your work

log [U] 15 Saving and printing output—log files notes [D] notes

Convenience

display [R] display

[R] Base Reference Manual

[D] Data Management Reference Manual

14

[G] Graphics Reference Manual [M] Mata Reference Manual

[MV] Multivariate Statistics Reference Manual [P] Programming Reference Manual

[ST] Survival Analysis and Epidemiological Tables Reference Maunal [SVY] Survey Data Reference Manual [TS] Time-Series Reference Manual

[XT] Longitudinal/Panel-Data Reference Manual

1.8 用户写的程序

比如ivreg2,psmatch2, 可以运用findit ivreg2并安装使用

1.9 参考文献

Baum, Christopher F. An Introduction to Modern Econometrics Using Stata

Cameron, A. C. and P. K. Trivedi, 2009, Microeconometrics Using Stata, Stata Press, Texas.

1.10 练习

1. 运用数据auto.dta,获得价格price的简单统计信息,并利用r()的储存的信息计算价格的 变异系数(即标准差除以平均值)。

2. 利用数据auto.dta,做mpg对price, weight的回归,并利用e()中储存的信息计算R2。

3. 利用数据auto.dta,定义一个全局宏变量 varlist,代替变量mpg, price, weight,并获得varlist所代表变量的简单统计信息,利用局部宏重复上述过程。

4. 利用数据auto.dta,利用循环语句foreach产生一个新的变量total,等于headroom和length的加和,并用summarize命令检验均值是否正确。

参考答案: 1.

sysuse auto, clear

15


stata入门中文讲义(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:废旧轮胎回收利用创业方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: