保存实验结果

时间:2018-11-06 12:15:51

标签: stata

我有以下动物清单:

bird, wolf, lion, zebra, horse

我想创建一个小型实验,例如说100试验,看看这些试验如何 每次随机抽奖都会更改。我已经看过Stata中的simulate命令,但这仅报告摘要统计信息。

如何保存每个试验的结果?

1 个答案:

答案 0 :(得分:1)

以下内容将为您提供所需的内容:

clear 

tempfile animals
save `animals', emptyok

input str5 animal
"bird"
"wolf"
"lion"  
"zebra"
"horse"  
end

forvalues i = 1 / 7 {
    generate double random = runiform()
    sort random
    drop random

    generate result = _n
    append using `animals'
    save `animals', replace

    keep in 1 / 5
    drop result
}

use `animals', clear

egen _j = seq(), block(5)
egen _i = seq(), from(1) to(5)

reshape wide animal, i(_i) j(_j)

order result
drop _i

下面您可以找到7试用的结果:

list
     +------------------------------------------------------------------------------+
     | result   animal1   animal2   animal3   animal4   animal5   animal6   animal7 |
     |------------------------------------------------------------------------------|
  1. |      1      bird      bird     zebra     zebra      lion      bird     zebra |
  2. |      2      wolf     horse     horse      wolf      bird     zebra      bird |
  3. |      3     horse      wolf      lion     horse     horse     horse      wolf |
  4. |      4     zebra      lion      wolf      bird      wolf      wolf     horse |
  5. |      5      lion     zebra      bird      lion     zebra      lion      lion |
     +------------------------------------------------------------------------------+