收藏 分享(赏)

滚动窗口回归(stata).pdf

上传人:weiwoduzun 文档编号:5646106 上传时间:2019-03-10 格式:PDF 页数:8 大小:160.85KB
下载 相关 举报
滚动窗口回归(stata).pdf_第1页
第1页 / 共8页
滚动窗口回归(stata).pdf_第2页
第2页 / 共8页
滚动窗口回归(stata).pdf_第3页
第3页 / 共8页
滚动窗口回归(stata).pdf_第4页
第4页 / 共8页
滚动窗口回归(stata).pdf_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、Title rolling Rolling-window and recursive estimationSyntax Menu Description OptionsRemarks and examples Stored results Acknowledgment ReferencesAlso seeSyntaxrolling exp list if in , options : commandoptions DescriptionMainwindow(#) number of consecutive data points in each samplerecursive use recu

2、rsive samplesrrecursive use reverse recursive samplesOptionsclear replace data in memory with resultssaving(filename, :) save results to filename; save statistics in double precision;save results to filename every # replicationsstepsize(#) number of periods to advance windowstart(time constant) peri

3、od at which rolling is to startend(time constant) period at which rolling is to endkeep(varname , start ) save varname along with results; optionally, use value atleft edge of windowReportingnodots suppress replication dotsnoisily display any output from commandtrace trace commands executionAdvanced

4、reject(exp) identify invalid resultswindow(#) is required.You must tsset your data before using rolling; see TS tsset.aweights are allowed in command if command accepts aweights; see U 11.1.6 weight.exp list contains (name: elist)elisteexpelist contains newvar = (exp)(exp)eexp is specnameeqnospecnam

5、especname is b12 rolling Rolling-window and recursive estimationbseseeqno is # #nameexp is a standard Stata expression; see U 13 Functions and expressions.Distinguish between , which are to be typed, and , which indicate optional arguments.MenuStatistics Time series Rolling-window and recursive esti

6、mationDescriptionrolling is a moving sampler that collects statistics from command after executing command onsubsets of the data in memory. Typing. rolling exp list, window(50) clear: commandexecutes command on sample windows of span 50. That is, rolling will first execute command byusing periods 15

7、0 of the dataset, and then using periods 251, 352, and so on. rolling can alsoperform recursive and reverse recursive analyses, in which the starting or ending period is held fixedand the window size mand defines the statistical command to be executed. Most Stata commands and user-writtenprograms ca

8、n be used with rolling, as long as they follow standard Stata syntax and allow the ifqualifier; see U 11 Language syntax. The by prefix cannot be part of command.exp list specifies the statistics to be collected from the execution of command. If no expressionsare given, exp list assumes a default of

9、 b if command stores results in e() and of all the scalars ifcommand stores results in r() and not in e(). Otherwise, not specifying an expression in exp listis an error.OptionsMain window(#) defines the window size used each time command is executed. The window size refers tocalendar periods, not t

10、he number of observations. If there are missing data (for example, becauseof weekends), the actual number of observations used by command may be less than window(#).window(#) is required.recursive specifies that a recursive analysis be done. The starting period is held fixed, the endingperiod advanc

11、es, and the window size grows.rrecursive specifies that a reverse recursive analysis be done. Here the ending period is held fixed,the starting period advances, and the window size shrinks.rolling Rolling-window and recursive estimation 3Options clear specifies that Stata replace the data in memory

12、with the collected statistics even though thecurrent data in memory have not been saved to disk.saving( filename , suboptions ) creates a Stata data file (.dta file) consisting of (for each statisticin exp list) a variable containing the window replicates.double specifies that the results for each r

13、eplication be saved as doubles, meaning 8-byte reals.By default, they are saved as floats, meaning 4-byte reals.every(#) specifies that results be written to disk every #th replication. every() should be specifiedin conjunction only with saving() when command takes a long time for each replication.

14、Thiswill allow recovery of partial results should your computer crash. See P postfile.stepsize(#) specifies the number of periods the window is to be advanced each time command isexecuted.start(time constant) specifies the date on which rolling is to start. start() may be specifiedas an integer or a

15、s a date literal.end(time constant) specifies the date on which rolling is to end. end() may be specified as aninteger or as a date literal.keep(varname , start ) specifies a variable to be posted along with the results. The value postedis the value that corresponds to the right edge of the window.

16、Specifying the start() optionrequests that the value corresponding to the left edge of the window be posted instead. This optionis often used to record calendar dates.Reporting nodots suppresses display of the replication dot for each window on which command is executed.By default, one dot character

17、 is printed for each window. A red x is printed if command returnswith an error or if any value in exp list is missing.noisily causes the output of command to be displayed for each window on which command isexecuted. This option implies the nodots option.trace causes a trace of the execution of comm

18、and to be displayed. This option implies the noisilyand nodots options.Advanced reject(exp) identifies an expression that indicates when results should be rejected. When exp istrue, the saved statistics are set to missing values.Remarks and examples rolling executes a command on each of a series of

19、windows of observations and stores theresults. rolling can perform what are commonly called rolling regressions, recursive regressions,and reverse recursive regressions. However, rolling is not limited to just linear regression analysis:any command that stores results in e() or r() can be used with

20、rolling.Suppose that you have data collected at 100 consecutive points in time, numbered 1100, and youwish to perform a rolling regression with a window size of 20 periods. Typing. rolling _b, window(20) clear: regress depvar indepvar4 rolling Rolling-window and recursive estimationcauses Stata to r

21、egress depvar on indepvar using periods 120, store the regression coefficients( b), run the regression using periods 221, and so on, finishing with a regression using periods81100 (the last 20 periods).The stepsize() option specifies how far ahead the window is moved each time. For example,if you sp

22、ecify step(2), then command is executed on periods 120, and then 322, 524, etc. Bydefault, rolling replaces the dataset in memory with the computed statistics unless the saving()option is specified, in which case the computed statistics are saved in the filename specified. If thedataset in memory ha

23、s been changed since it was last saved and you do not specify saving(), youmust use clear.rolling can also perform recursive and reverse recursive analyses. In a recursive analysis, thestarting date is held fixed, and the window size grows as the ending date is advanced. In a reverserecursive analys

24、is, the ending date is held fixed, and the window size shrinks as the starting date isadvanced.Example 1We have data on the daily returns to IBM stock (ibm), the S&P 500 (spx), and short-term interestrates (irx), and we want to create a series containing the beta of IBM by using the previous 200 tra

25、dingdays at each date. We will also record the standard errors, so that we can obtain 95% confidenceintervals for the betas. See, for example, Stock and Watson (2011, 118) for more information onestimating betas. We type. use http:/www.stata- Yahoo! Finance). tsset ttime variable: t, 1 to 494delta:

26、1 unit. generate ibmadj = ibm - irx(1 missing value generated). generate spxadj = spx - irx(1 missing value generated). rolling _b _se, window(200) saving(betas, replace) keep(date): regress ibmadj spxadj(running regress on estimation sample)(note: file betas.dta not found)Rolling replications (295)

27、1 2 3 4 550100150200250.file betas.dta savedOur dataset has both a time variable t that runs consecutively and a date variable date thatmeasures the calendar date and therefore has gaps at weekends and holidays. Had we used the datevariable as our time variable, rolling would have used windows consi

28、sting of 200 calendar daysinstead of 200 trading days, and each window would not have exactly 200 observations. We usedthe keep(date) option so that we could refer to the date variable when working with the resultsdataset.rolling Rolling-window and recursive estimation 5We can list a portion of the

29、dataset created by rolling to see what it contains:. use betas, clear(rolling: regress). sort date. list in 1/3, abbrev(10)start end date _b_spxadj _b_cons _se_spxadj _se_cons1. 1 200 16oct2003 1.043422 -.0181504 .0658531 .07482952. 2 201 17oct2003 1.039024 -.0126876 .0656893 .0746093. 3 202 20oct20

30、03 1.038371 -.0235616 .0654591 .0743851The variables start and end indicate the first and last observations used each time that rollingcalled regress, and the date variable contains the calendar date corresponding the period representedby end. The remaining variables are the estimated coefficients a

31、nd standard errors from the regression.In our example , b spxadj contains the estimated betas, and b cons contains the estimated alphas.The variables se spxadj and se cons have the corresponding standard errors.Finally, we compute the confidence intervals for the betas and examine how they have chan

32、gedover time:. generate lower = _b_spxadj - 1.96*_se_spxadj. generate upper = _b_spxadj + 1.96*_se_spxadj. twoway (line _b_spxadj date) (rline lower upper date) if date=td(1oct2003), ytitle(“Beta“).6.811.2Beta01oct2003 01jan2004 01apr2004 01jul2004 01oct2004 01jan2005date_bspxadj lower/upperAs 2004

33、progressed, IBMs stock returns were less influenced by returns in the broader market.Beginning in June of 2004, IBMs beta became significantly different from unity at the 95% confidencelevel, as indicated by the fact that the confidence interval does not contain one from then onward.In addition to r

34、olling-window analyses, rolling can also perform recursive ones. Suppose againthat you have data collected at 100 consecutive points in time, and now you type. rolling _b, window(20) recursive clear: regress depvar indepvar6 rolling Rolling-window and recursive estimationStata will first regress dep

35、var on indepvar by using observations 120, store the coefficients, runthe regression using observations 121, observations 122, and so on, finishing with a regressionusing all 100 observations. Unlike a rolling regression, in which case the number of observations isheld constant and the starting and

36、ending points are shifted, a recursive regression holds the startingpoint fixed and increases the number of observations. Recursive analyses are often used in forecastingsituations. As time goes by, more information becomes available that can be used in making forecasts.See Kmenta (1997, 423424).Exa

37、mple 2Using the same dataset, we type. use http:/www.stata- clear(Source: Yahoo! Finance). tsset ttime variable: t, 1 to 494delta: 1 unit. generate ibmadj = ibm - irx(1 missing value generated). generate spxadj = spx - irx(1 missing value generated). rolling _b _se, recursive window(200) clear: regr

38、ess ibmadj spxadj(output omitted ). list in 1/3, abbrev(10)start end _b_spxadj _b_cons _se_spxadj _se_cons1. 1 200 1.043422 -.0181504 .0658531 .07482952. 1 201 1.039024 -.0126876 .0656893 .0746093. 1 202 1.037687 -.016475 .0655896 .0743481Here the starting period remains fixed and the window grows l

39、arger.In a reverse recursive analysis, the ending date is held fixed, and the window size becomes smalleras the starting date is advanced. For example, with a dataset that has observations numbered 1100,typing. rolling _b, window(20) reverse recursive clear: regress depvar indepvarcreates a dataset

40、in which the first observation has the results based on periods 1100, the secondobservation has the results based on 2100, the third having 3100, and so on, up to the lastobservation having results based on periods 81100 (the last 20 observations).Example 3Using the data on stock returns, we want to

41、 build a model in which we predict todays IBM stockreturn on the basis of yesterdays returns on IBM and the S&P 500. That is, letting it and st denotethe returns to IBM and the S&P 500 on date t, we want to fit the regression modelit = 0 + 1it 1 + 2st 1 + twhere t is a regression error term, and the

42、n computedit+1 =c 0 +c 1it +c 2strolling Rolling-window and recursive estimation 7We will use recursive regression because we suspect that the more data we have to fit the regressionmodel, the better the model will predict returns. We will use at least 20 periods in fitting the regression. use http:

43、/www.stata- clear(Source: Yahoo! Finance). tsset ttime variable: t, 1 to 494delta: 1 unitOne alternative would be to use rolling with the recursive option to fit the regressions, collectthe coefficients, and then compute the predicted values afterward. However, we will instead write ashort program t

44、hat computes the forecasts automatically and then use rolling, recursive on thatprogram. The program must accept an if expression so that rolling can indicate to the programwhich observations are to be used. Our program isprogram myforecast, rclasssyntax ifregress ibm L.ibm L.spx if/ Find last time

45、period of estimation sample and/ make forecast for period just after thatsumm t if e(sample)local last = r(max)local fcast = _b_cons + _bL.ibm*ibmlast + /_bL.spx*spxlastreturn scalar forecast = fcast/ Next periods actual return/ Will return missing value for final periodreturn scalar actual = ibmlas

46、t+1endNow we call rolling:. rolling actual=r(actual) forecast=r(forecast), recursive window(20): myforecast(output omitted ). corr actual forecast(obs=474)actual forecastactual 1.0000forecast -0.0957 1.0000Our model does not work too wellthe correlation between actual returns and our forecasts isneg

47、ative.Stored resultsrolling sets no r- or e-class macros. The results from the command used with rolling, dependingon the last window of data used, are available after rolling has finished.AcknowledgmentWe thank Christopher F. Baum of the Department of Economics at Boston College and author ofthe St

48、ata Press books An Introduction to Modern Econometrics Using Stata and An Introduction toStata Programming for an earlier rolling regression command.8 rolling Rolling-window and recursive estimationReferencesKmenta, J. 1997. Elements of Econometrics. 2nd ed. Ann Arbor: University of Michigan Press.Stock, J. H., and M. W. Watson. 2011. Introduction to Econometrics. 3rd ed. Boston: AddisonWesley.Also seeD statsby Collect statistics for a command across a by listR stored results Stored results

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报