1、STATA实用学习笔记docSTATA实用学习笔记北京科技大学STATA应用学习摘录内存为空时才可以导入数据集,否则会出现(you must start with an empty dataset)(1)清空内存中的所有变量:.drop _all(2)导入语句后加入“clear”命令。一、保存文件1、save “C:Documents and SettingsAdministrator桌面ST9007datasetFees1.dta”2、save “C:Documents and SettingsAdministrator桌面ST9007datasetFees1.dta”, replace二、
2、打开及退出已存文件use1、.Use 文件路径及文件名, clear2、. Drop _all/.exit三、记录命令和输出结果(log)1、开始建立记录文件:log using J:phdoutput.log, replace2、暂停记录文件:log off3、重新打开记录文件:log on4、关闭记录文件:log close十一、创建和保存程序文件:(doedit, do)1、打开程序编辑窗口:doedit2、写入命令3、保存文件,.do.4、运行命令:.do 程序文件路径及文件名十二、多个数据集合并为一个数据集(变量和结构相同)纵向合并appendinsheet using J:phdF
3、ees1.csv, clearsave J:phdFees1.dta, replaceinsheet using J:phdFees2.csv, clearappend using J:phdFees1.dtasave J:phdFees1.dta, replace十三、横向合并,在原数据集基础上加上另外的变量merge1、insheet using J:phdFees1.csv, clearsort companyid yearend save J:phdFees1.dta, replacedescribeinsheet using J:phdFees6.csv, clearsort com
4、panyid yearend merge companyid yearend using J:phdFees1.dtasave J:phdFees1.dta, replacedescribe 2、_merge=1 obs. From master data _merge=2 obs. From using data _merge=3 obs. From both master and using data十四、帮助文件:help 1、. Help describe十五、描述性统计量 1、summarize incorporationyear 单个summarize incorporationy
5、ear-big6 连续多个summarize _all or simply summarize 所有 2、更详细的统计量 summarize incorporationyear, detail 3、centilecentile auditfees, centile(0(10)100) centile auditfees, centile(0(5)100) 4、tabulate不同类型变量的频数和比例tabulate companytype tabulate companytype big6, column 按列计算百分比tabulate companytype big6, row 按行计算百分
6、比tab companytype big6 if companytype=3, row col 同时按行列和条件计算百分比 5、 计算满足条件观测的个数 count if big6=1count if big6=0 | big6=1 6、按离散变量排序,对连续变量计算描述性统计量:(1)by companytype, sort: summarize auditfees, detail(2)sort companytype By companytype:summarize auditees 十六、转换变量1、按公司类型将公开发行股票公司赋值为1,其他为0gen listed=0 replace
7、listed=1 if companytype=2 replace listed=1 if companytype=3 replace listed=1 if companytype=5replace listed=. if companytype=.十七、产生新变量gen Generate newvar=表达式十八、数据类型1、数值型Storage typeBytesMinMaxbyte1-127+100int2-32,767+32,740long4-2,147,483,6472,147,483,620float4-1.70141173319*1038 1.70141173319*1036
8、double8-8.9884656743*103078.9884656743*103082、字符型Storage typeBytesMax length (characters)str111str222str8080803、新建变量的过程中定义数据类型gen str3 gender= malelist gender in 1/104、变量所占字节过长drop gendergen str30 gender= malebrowsedescribe gendercompress gender5、日期数据类型:%d dates, which is a count of the number of da
9、ys elapsed since January 1, 1960。(1)date( 日期变量 )gen fye=date(yearend, MDY) MDY应根据前面日期的排列顺序而定,结果显示的是距离1960年1月1日的天数list yearend fye in 1/10(2)日期格式化%d(显示fye变量为日期形式,但数值并未真正变动):format fye %d list yearend fye in 1/10sum fye(3)利用日期天数求对应的年、月、日gen year=year(fye) gen month=month(fye) gen day=day(fye) list yea
10、rend fye year month day in 1/10(4)将三个分别表示年、月、日的变量合并为一个日期变量drop fyegen fye=mdy(month, day, year)format fye %dlist yearend fye in 1/10(5) 将一个数值型的时间数据(20080131)转变为ST可识别的时间数据gen year=int(date/10000)gen month=int(date-year*10000)/100)gen day=date-year*10000-month*100list date year month day in 1/10gen ed
11、ate=mdy(month, day, year)format edate %dlist edate date in 1/10十九、存贮统计量的内部变量R( )sum auditfeesgen meanadjaf= auditfees-r(mean) list meanadjaf in 1/10SUM命令后常见的几种R()值r(N)Number of casesr(sd)Standard deviationr(sum_w)Sum of weightsr(min)Minimumr(mean)Arithmetic meanr(max)Maximumr(var)Variancer(sum)Sum o
12、f variable显示这些变量值的命令sum auditfees, detailreturn list二十、recode命令(PPT61)1、产生有多个值的变量的哑变量recoderecode year (min/1999 = 0) (2000/max = 1), gen (yeardum)min/1999表示小于等于1999的值全部赋值为02000/max表示大于等于2000的值全部赋为1。2、对一个连续变量按一定值分为不同间隔的组recodegen assets_categ=recode(totalassets, 100, 500, 1000, 5000, 20000, 100000,
13、1000000)。分组的值为每组的上限,包含该值。sort assets_categby assets_categ: sum totalassets assets_categ 3、对一个连续变量按一定值分为相同间隔的组autocodeautocode(variable name, # of intervals, min value, max value)for example: gen assets_categ=autocode(totalassets, 10, 0, 10000)4、对一个连续变量按每组样本数相同进行分组:xtilextile assets_categ=totalassets
14、, nquantiles(10)每组样本不一定完全相同二十一、一次性计算同一变量不同组别的均值:egen命令按公司类型先排序,再计算每一类型公司审计费用的均值并赋值给新变量:by companytype, sort: egen meanaf2=mean(auditfees)count()mean()median()sum()二十二、_n和_N命令1、显示每个观测的序号并显示总观测数sort companyid fyecapture drop xgen x=_ncapture drop ygen y=_Nlist companyid fye x y in 1/302、分组显示每个组中变量的序号和
15、每组总的样本数capture drop x ysort companyid fyeby companyid: gen x=_nby companyid: gen y=_Nlist companyid fye x y in 1/303、创建新变量等于每个分组中变量的第一个值或最后一个值sort companyid fyeby companyid: gen auditfees_first=auditfees1by companyid: gen auditfees_last=auditfees_N list companyid fye auditfees auditfees_first auditf
16、ees_last in 1/304、创建新变量等于滞后一期或滞后两期的值sort companyid fye by companyid: gen auditfees_lag1= auditfees_n-1by companyid: gen auditfees_lag2= auditfees_n-2list companyid fye auditfees auditfees_lag1 auditfees_lag2 in 1/30 二十三、转变数据集结构:reshape 不同数据库的数据集结构不同:长型是指同一公司不同年度数据在不同的行。宽型数据是指同一数据不同年度数据在现一行。二者间的转换可通过
17、reshape命令来实现。需要注意的是,在转换过程中对数据集是有要求的,一个公司只能有一个年度数据,否则会出错。 1、长型转换为宽型:reshape wide yearend incorporationyear companytype sales auditfees nonauditfees currentassets currentliabilities totalassets big6 fye, i(companyid) j(year) 2、宽型转换为长型:reshape long yearend incorporationyear companytype sales auditfees
18、nonauditfees currentassets currentliabilities totalassets big6 fye, i(companyid) j(year) 3、第二次转换时命令可简化:reshape widereshape long二十四、计算CAR的例子:已知股票日回报率,市场回报率,事件日,计算窗口期为三天的CAR。 1、定义三天的窗口期:sort ticker edategen window=0 if eventdate.(事件日为0)replace window=-1 if window_n+1=0 & ticker=ticker_n+1replace windo
19、w=1 if window_n-1=0 & ticker=ticker_n-1 2、计算AR和CARgen ar=ret-vwretdgen car=ar+ar_n-1+ar_n+1 if window=0 & ticker=ticker_n+1 & ticker=ticker_n-1 3、检验list ticker edate ret vwretd ar car window if window.二十五、means 的T检验: 1、检验总体上big6的审计收费有无显著不同use J:phdFees.dta, cleargen lnaf=ln(auditfees)by big6, sort:
20、sum lnaftest lnaf, by (big6)2、分年度比较big6的审计收费有无显著不同,加入by year命令。gen fye=date(yearend, MDY)format fye %dgen year=year(fye)sort yearby year: ttest lnaf, by(big6) 3、均值等于特定值得的T检验: sum lnafttest lnaf=2.1二十六、meadian的显著性检验:1、获取中位数的命令:by big6, sort: sum lnaf, detailby big6, sort: centile lnaf 2、中位数检验:median
21、lnaf, by(big6)ranksum lnaf, by(big6) 二十七、列联表检验: 1、创建列联表的命令:tabulate companytype big6, row第一个变量是表的最左侧一列的项目,第二个变量是表的第一行的项目。 2、两变量之间的相关性检验:chi2tabulate companytype big6, chi2 row 3、相关矩阵:pwcorr lnaf big6 year listed 4、列出相关矩阵并进行符号检验 pwcorr lnaf big6 year listed, sig 5、在矩阵中列出观测数pwcorr lnaf big6 listed if
22、year=2000, sig obs二十八、创建一个不包含缺失值的数据集 1、无缺失值的变量值为1,至少有一个的为0gen samp=1 if lnaf. & big6. & year. & listed=0 & lnaf=0 & lnaf=0 & lnaf=5, width(0.25) normal2、散点图(scatter) scatter lnaf lnta第一个变量是纵轴,第二个变量是横轴。twoway (scatter lnaf lnta, msize(tiny) (lfit lnaf lnta)在散点图上加入最适合的一条直线。三十、缩尾处理winsor . winsor rev,
23、gen(wrev) p(0.01)0.01代表去掉的百分数。 Winsor rev, gen(wrev) h(5),5代表去掉的个数第二章 线性回归内容简介:2.1 The basic idea underlying linear regression2.2 Single variable OLS2.3 Correctly interpreting the coefficients2.4 Examining the residuals2.5 Multiple regression2.6 Heteroskedasticity2.7 Correlated errors 2.8 Multicolli
24、nearity2.9 Outlying observations2.10 Median regression2.11 “Looping”2.1 The basic idea underlying linear regression 1残差 F为真实值,为预测值,为残差。OLS回归就是使残差最小。2. 基本一元回归regress y x3回归结果的保存回归结果的系数保存在_bvarname内存变量中,常数项的系数保存在 (_cons)内存变量中。4、预测值及残差predict yhatpredict yres, residyres即为真实值得与预测值之差。5、残差与X的散点图twoway (sc
25、atter y_res x) (lfit y_res x)6、衡量估计系数准确程度:标准误差。用样本的标准偏差与系数之间的关系来衡量即T值(用系数除以标准差),同时P值是根据T值的分布计算出来的,表示系数落入标准对应上下限的可能性。前提是残差符合以下假设:同方差:Homoscedasticity (i.e., the residuals have a constant variance)独立不相关:Non-correlation (i.e., the residuals are not correlated with each other)正态分布:Normality (i.e., the r
26、esiduals are normally distributed) 7、回归结果包含的一些内容的意思各变差的自由度:For the ESS, df = k-1 where k = number of regression coefficients (df = 2 1)For the RSS, df = n k where n = number of observations (= 11 - 2)For the TSS, df = n-1 ( = 11 1)MS:变差除以自由度:The last column (MS) reports the ESS, RSS and TSS divided
27、by their respective degrees of freedom R平方:The R-squared = ESS / TSS 调整的R平方:Adj R-squared = 1-(1-R2)(n-1)/(n-k) ,消除了加入相关度不高解释变量后R平方增加的不足。Root MSE = square root of RSS/n-k:模型的平均解释能力The F-statistic = (ESS/k-1)/(RSS/n-k):模型的总解释能力2.3 Correctly interpreting the coefficients1、假如想检验big6的审计费用在公开发行和非公开发行公司之间
28、的区别时,可用交互变量。Big6*listed.2、变量回归系数的解释(1)对连续变量系数的解释:估计系数的经济意义是指X对Y的影响,可以有不同的方法来衡量:一种是用X从25%变动到75%时Y的变动量。或X变动一个标准差时Y的变动。reg auditfees totalassets sum totalassets if auditfees., detail gen fees_low=_b_cons+_btotalassets*r(p25)gen fees_high=_b_cons+_btotalassets*r(p75)sum fees_low fees_high (2)对非连续变量的解释一般使用0和1,而不是百分比。reg lnaf big6gen fees_nb6=exp(_b_cons)gen fees_b6=exp(_b_cons+_bbig6)sum fees_nb6 fees_b62.4 Examining the residuals1、报告结果时,不仅用R平方来衡量显著性,而且需要报告其他统计结果:is there significant heteroscedasticity? is the
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1