如何在Stata中存储循环的回归结果?

如何在Stata中存储循环的回归结果?,stata,Stata,我建立了一个模型,基本上实现了以下功能: run regressions on single time period organise stocks into quantiles based on coefficient from linear regression statsby to calculate portfolio returns for stocks based on quantile (averaging all quantile x returns) store quantil

我建立了一个模型,基本上实现了以下功能:

run regressions on single time period
organise stocks into quantiles based on coefficient from linear regression
statsby to calculate portfolio returns for stocks based on quantile (averaging all quantile x returns)
store quantile 1 portolio and quantile 10 return for the last period 
这对变量只是时间框架中的最终条目。然而,我打算将单一时间段延长到一个大的时间段,实质上:

for i in timeperiod {
    organise stocks into quantiles based on coefficient from linear regression
    statsby to calculate portfolio returns for stocks based on quantile (averaging all quantile x returns)
    store quantile 1 portolio and quantile 10 return for the last period 
}
我关注的数据是每个时间段最后一天的投资组合1和10回报(使用前3年的数据构建)。这应该会产生一个时间序列(我的总数据60-3年来建立第一个结果,所以57年)的回报,然后我可以相互回归

regress portfolio 1 against portfolio 10
我来自一个R的背景,在那里把一个变量存储在一个向量中是非常简单的,但我不太确定如何在Stata中实现这一点

最后,我想要一个2xn矩阵(一个单独的数据集)的数字,每对都是一次滚动回归的结果。很抱歉,描述得很模糊,但这比解释我的模型是关于什么要好。任何指针(即使是正确的手动输入)都将不胜感激。多谢各位


编辑:我想要存储的实际数据只是一个变量。我添加了回归,使它变得混乱。我已经更改了代码,使其更能表示我想要的内容。

听起来像是一个或的例子,这取决于您到底想做什么。这些是前缀命令,可以作为回归模型的前缀<代码>滚动或
statsby
将为您处理结果的循环和存储


如果您想要最大程度的控制,您可以使用或自己执行循环,并使用将结果存储在单独的文件中。事实上,如果您查看
rolling
statsby
(使用)内部,您将看到这些命令在内部就是这样做的。

听起来像是一个或的例子,具体取决于您想要做什么。这些是前缀命令,可以作为回归模型的前缀<代码>滚动或
statsby
将为您处理结果的循环和存储


如果您想要最大程度的控制,您可以使用或自己执行循环,并使用将结果存储在单独的文件中。事实上,如果您查看
rolling
statsby
(使用)内部,您将看到这就是这些命令在内部执行的操作。

R
不同,Stata在内存中只使用一个称为(ta da!)数据集的主要矩形对象进行操作。(当然,它还有许多其他内容,但这些内容很少能像使用
use
将数据集带入内存那样容易处理)。因为您的最终目标是运行回归,所以您要么需要创建一个额外的数据集,要么笨拙地将数据添加到现有的数据集中。考虑到您的问题已经足够定制,您似乎需要定制解决方案

解决方案1:使用
post
(请参阅)创建单独的数据集

解决方案2:将内容保留在当前数据集中。如果上面的代码看起来很笨拙,您可以创建一个奇怪的半人马座数据集,其中包含原始股票和摘要

use my_data, clear

gen int collapsed_time = .
gen double collapsed_return_q1 = .
gen double collapsed_return_q10 = .
* 1. set up placeholders for your results

levelsof time_period, local( allyears )
* 2. This will create a local macro allyears 
*    that contains all the values of time_period

local T : word count `allyears'
* 3. I now use the local macro allyears as is
*    and count how many distinct values there are of time_period variable

forvalues n=1/`T' {
   * 4. my cycle now only runs for the numbers from 1 to `T'

   local t : word `n' of `allyears'
   * 5. I pull the `n'-th value of time_period

   ** computations as in the previous solution

   replace collapsed_time_period = `t' in `n'
   replace collapsed_return_q1 = (compute) in `n'
   replace collapsed_return_q10 = (compute) in `n'
   * 6. I am filling the pre-arranged variables with the relevant values
}

tsset collapsed_time_period, year
* 7. this will likely complain about missing values, so you may have to fix it
newey collapsed_return_q10 collapsed_return_q1, lag(3)
* 8. just in case the business cycles have about 3 years of effect

exit
* 9. you always end your do-files with exit

我避免使用
statsby
,因为它会覆盖内存中的数据集。请记住,与
R
不同,Stata一次只能记住一个数据集,因此我倾向于避免过多的I/O操作,因为如果数据集为50+MB,它们可能是整个过程中最慢的部分。

R
不同,Stata在内存中只使用一个称为(ta da!)的主要矩形对象进行操作数据集。(当然,它还有许多其他内容,但这些内容很少能像使用
use
将数据集带入内存那样容易处理)。因为您的最终目标是运行回归,所以您要么需要创建一个额外的数据集,要么笨拙地将数据添加到现有的数据集中。考虑到您的问题已经足够定制,您似乎需要定制解决方案

解决方案1:使用
post
(请参阅)创建单独的数据集

解决方案2:将内容保留在当前数据集中。如果上面的代码看起来很笨拙,您可以创建一个奇怪的半人马座数据集,其中包含原始股票和摘要

use my_data, clear

gen int collapsed_time = .
gen double collapsed_return_q1 = .
gen double collapsed_return_q10 = .
* 1. set up placeholders for your results

levelsof time_period, local( allyears )
* 2. This will create a local macro allyears 
*    that contains all the values of time_period

local T : word count `allyears'
* 3. I now use the local macro allyears as is
*    and count how many distinct values there are of time_period variable

forvalues n=1/`T' {
   * 4. my cycle now only runs for the numbers from 1 to `T'

   local t : word `n' of `allyears'
   * 5. I pull the `n'-th value of time_period

   ** computations as in the previous solution

   replace collapsed_time_period = `t' in `n'
   replace collapsed_return_q1 = (compute) in `n'
   replace collapsed_return_q10 = (compute) in `n'
   * 6. I am filling the pre-arranged variables with the relevant values
}

tsset collapsed_time_period, year
* 7. this will likely complain about missing values, so you may have to fix it
newey collapsed_return_q10 collapsed_return_q1, lag(3)
* 8. just in case the business cycles have about 3 years of effect

exit
* 9. you always end your do-files with exit

我避免使用
statsby
,因为它会覆盖内存中的数据集。请记住,与
R
不同,Stata一次只能记住一个数据集,因此,我倾向于避免过多的I/O操作,因为如果数据集为50+MB,它们可能是整个过程中最慢的部分。

我认为您正在寻找
estout
命令来存储回归结果。

我认为您正在寻找
estout
命令来存储回归结果回归。

请提供更具体的代码。从
网站开始使用nlswork,清除
;给出你感兴趣的每一年
的回归类型,或者至少用
回归
伪造一些东西;并指出每次回归都要存储什么。我已经扩展了响应,希望现在应该更清楚一些。实际的代码很长,但这应该提供一个很好的概述。我应该说清楚,我想存储的不是回归系数,而是回归产生的变量。用Stata术语来说,这不是一个
2xn
矩阵。您更想要一个数据集。(就像在
R
中,你可能不会说你想要一个矩阵,而是想要一个数据帧。)请给我们更具体的代码。从
网站开始使用nlswork,清除
;给出你感兴趣的每一年
的回归类型,或者至少用
回归
伪造一些东西;并指出每次回归都要存储什么。我已经扩展了响应,希望现在应该更清楚一些。实际的代码很长,但这应该提供一个很好的概述。我本应该做的