公卫百科  > 所属分类  >  Stata   
[0] 评论[0] 编辑

rangerun

rangerun runs a user-supplied Stata program for each observation in the sample. At each pass, the data in memory is cleared and replaced with observations that fall within the interval bounds specified for the current observation. The user's program is run and results are collected from new variables left in memory when the program terminates without error. rangerun provides the same functionality as rangestat except that, instead of using Mata functions, statistics are calculated by running a user-written Stata program that may contain any number of Stata statistical commands.

公卫论坛

安装: 公卫人

ssc install rangerun
ssc install rangestat 

公卫家园

公卫人

rangerun provides a general solution to any type of rolling window problem. To contrast the mechanics of rolling compared to rangerun, here's the last example from rolling's help file:

公卫百科

webuse lutkepohl2, clear
tsset qtr
rolling ratio=(r(mean)/r(p50)), window(10): summarize inc, detail
list in 1/10

clear all
program myprog
  if _N < 10 exit
  summarize inc, detail
  gen ratio = r(mean)/r(p50)
end
webuse lutkepohl2, clear
rangerun myprog, interval(qtr -9 0) use(inc)
list qtr inc ratio in 1/19, sep(0) 公卫人 
公卫人

With rolling, execution times increase exponentially as the data size increases. With rangerun (and rangestat), execution times increase linearly with the number of observations in the sample. If you double the number of observations, the run time for rangerun will be twice as long. For large problems, rangerun will be orders of magnitude faster than rolling.  公卫百科


公卫人

Everything that can be done with statsby can also be done with rangerun. Since statsby's execution times also increase exponentially as the data size increases, rangerun will be orders of magnitude faster for large data problems. See the help file for a fully spelled out example that shows how to specify the interval bounds so that rangerun only runs the user's program once per group.  公卫家园


公卫考场

Because rangerun manages all the data in Mata and runs the user's program from Mata, there is very little overhead. The commands in the user's program do not require in or if qualifiers since the data in memory is only populated with observations in range for the current observation. So even with the most efficient Stata loop, rangerun will be significantly faster (and more convenient). Here's an example that calculates a regression on a rolling window of 7 years (including the current observation) and stores the constant term. A minimum of 4 observations is required: 公卫论坛

clear all
webuse grunfeld

local nobs = _N
gen alpha = .
quietly forvalues i = 1/`nobs' {
  capture regress invest kstock if company == company[`i'] &inrange(year, year[`i']-6, year[`i'])
  if _rc == 0 & e(N) >= 4 replace alpha = _b[_cons] in `i'
}

program myprog
  if _N < 4 exit
  regress invest kstock
  gen alpha_rr = _b[_cons]
end
rangerun myprog, interval(year -6 0) by(company)

assert alpha == alpha_rr 
公卫家园

公卫人

附件列表


0

词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。

如果您认为本词条还有待完善,请 编辑

上一篇 reghdfe    下一篇 kmatch

标签

暂无标签

同义词

暂无同义词