【问题标题】:Reshaping and merging simulations in Stata在 Stata 中重塑和合并模拟
【发布时间】:2015-12-04 12:11:38
【问题描述】:

我有一个数据集,其中包含 1000 个模拟。每个模拟的输出都保存为一行数据。变量有alphabetasimulationid

这是一个示例数据集:

simulationid    beta          alpha
1               0.025840106   20.59671241
2               0.019850549   18.72183088
3               0.022440886   21.02298228
4               0.018124857   20.38965861
5               0.024134726   22.08678021
6               0.023619479   20.67689981
7               0.016907209   17.69609466
8               0.020036455   24.6443037
9               0.017203175   24.32682682
10              0.020273349   19.1513272

我想估计一个新值 - 我们称之为 new - 这取决于 alphabeta 以及我们将称之为 riskprice 的其他两个变量的不同级别。 risk 的值范围从 0 到 100,price 从 0 到 500,步长为 5。

我想要实现的是一个数据集,其中包含的值表示(在模拟中)new 对于riskprice 的组合大于 0 的概率。

我可以使用下面的代码来实现这一点。但是,reshape 过程花费的时间比我想要的要多。在我看来,它可以更快地完成。

所以,我的问题是:

i) 有没有一种有效的方法可以从没有多个reshape 的单行数据生成多个数据集,或者

ii) 我是否以完全错误的方式处理这个问题?

set maxvar 15000

/* Input sample data */
input     simulationid  beta          alpha
1               0.025840106   20.59671241
2               0.019850549   18.72183088
3               0.022440886   21.02298228
4               0.018124857   20.38965861
5               0.024134726   22.08678021
6               0.023619479   20.67689981
7               0.016907209   17.69609466
8               0.020036455   24.6443037
9               0.017203175   24.32682682
10              0.020273349   19.1513272
end


forvalues risk = 0(1)100 {
forvalues price = 0(5)500 {
    gen new_r`risk'_p`price' = `price' * (`risk'/200)* beta - alpha
        gen probnew_r`risk'_p`price' = 0
        replace probnew_r`risk'_p`price' = 1 if new_r`risk'_p`price' > 0
        sum probnew_r`risk'_p`price', mean
        gen mnew_r`risk'_p`price' = r(mean)
    drop new_r`risk'_p`price' probnew_r`risk'_p`price'
}
}
drop if simulationid > 1
save simresults.dta, replace

forvalues risk = 0(1)100 {
    clear
    use simresults.dta
    reshape long mnew_r`risk'_p, i(simulationid) j(price)
    keep simulation price mnew_r`risk'_p
    rename mnew_r`risk'_p risk`risk'
    save risk`risk'.dta, replace
}

clear
use risk0.dta
forvalues risk = 1(1)100 {
    merge m:m price using risk`risk'.dta, nogen
    save merged.dta, replace
}

【问题讨论】:

  • 谢谢,@NickCox - 问题已相应编辑。
  • 代码中用到的所有数据集都是代码生成的。如果示例数据为input,则代码将运行

标签: merge simulation stata reshape


【解决方案1】:

这是您的问题的开始。

据我所知,您不需要多个数据集。

各种reshapes 和merges 只是重新排列第一个generated 的内容,这可以在一个数据集中完成。

第一个实例中的代码仅用于一对值alphabeta。要模拟 1000 个这样的,您将需要 1000 倍以上的观察,即大约 1000 万,这通常不是问题,并循环遍历 alpha 和 beta。但循环可以是默认的。我们会解决的。

此代码已运行并且是合法的。它仅限于一对alphabeta

clear 
input     simulationid  beta          alpha
1               0.025840106   20.59671241
2               0.019850549   18.72183088
3               0.022440886   21.02298228
4               0.018124857   20.38965861
5               0.024134726   22.08678021
6               0.023619479   20.67689981
7               0.016907209   17.69609466
8               0.020036455   24.6443037
9               0.017203175   24.32682682
10              0.020273349   19.1513272
end

local N = 101 * 101 
set obs `N' 

egen risk = seq(), block(101) 
replace risk = risk - 1 
egen price = seq(), from(0) to(100)
replace price = 5 * price 

gen result = (price * (risk/200)* beta[1] - alpha[1]) > 0 
bysort price risk: gen mean = sum(result) 
by price risk: replace mean = mean[_N]/_N 

现在假设您首先读取了 1000 个值,下面是如何获取整个值的草图。此代码未经测试。也就是说,您的数据集以 1000 个观察值开始;然后你把它放大到1000万左右,然后得到你的结果。棘手的部分是使用下标表达式来确保每个结果块都是针对不同的alpha, beta 对。这不是强制性的;您可以在循环中执行此操作,但是您需要在循环外使用 generate 并在循环内使用 replace

local N = 101 * 101 * 1000 
set obs `N' 

egen risk = seq(), block(101) 
replace risk = risk - 1 
egen price = seq(), from(0) to(100)
replace price = 5 * price 
egen sim = seq(), block(10201) 

gen result = (price * (risk/200)* beta[ceil(_n/10201)] - alpha[ceil(_n/10201)]) > 0 
bysort sim price risk: gen mean = sum(result) 
by sim price risk: replace mean = mean[_N]/_N 

使用的其他设备:egen 以块设置;在不重复调用summarize 的情况下获得平均值;直接使用真或假表达式。

注意:我没有试图理解您在做什么,但在我看来,价格风险模拟条件定义了单个值,因此计算平均值看起来是多余的。但也许那是在代码中,因为您希望在代码工作后向代码中添加更多细节。

NB2:这似乎是一个纯粹的确定性计算。不确定您是否需要此代码。

【讨论】:

  • 谢谢,@尼克。抱歉没有尽快回复。这并没有达到我的期望,但它可能包含必要的组件。我会试着弄清楚并在适当的时候编辑这个问题/答案。
  • 如果你有一个问题,我会问一个不同的问题ab initio。这个线程已经太乱了,不能承受太多的复杂性。
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多