2

我正在尝试在 proc IML 中使用 call randgen 来创建 10 个随机数,这些随机数遵循具有某些参数的 Weibull 分布。这是我正在使用的代码(显然会有不止一个循环,但我现在只是在测试):

do i = 1 to 1;
    Call randgen(Rands[i,1:Ntimes], 'Weibull', alpha[i], beta[i]);
    print (rands[1,1:Ntimes]);
    print (alpha[i]) (beta[i]);
end;

对于此示例,Ntimes = 10,alpha[i] = 4.5985111,beta[i] = 131.79508。我的问题是 10 个迭代/随机数中的每一个都返回为 1。我在 R 中使用了具有相同参数的 rweibull 函数并得到了有意义的结果,所以我认为它与 SAS 或我的代码有关,而不是参数的问题。我是否正确使用了 Randgen 调用?有谁知道为什么结果会这样出来?

4

2 回答 2

0

This works:

proc iml;
 alpha=j(10);
 beta=j(10);
 alpha[1]=4.59;
 beta[1] = 131.8;
 Ntimes=10;
 rands = j(1,10);
 print (rands);
 do i = 1 to 1;
    Call randgen(Rands, 'WEIB', alpha[1],beta[1]);
    print (rands);
 end;
quit;

I don't think you can use Rands[1:Ntimes] that way. I think you would want to assign it to a temporary matrix and then assign that matrix's results to a larger matrix.

IE:

allRands=j(10,10);
do i = 1 to 10;
    Call randgen(Rands, 'WEIB', alpha[1],beta[1]);
    print (rands);
    allRands[i,1:10]=Rands;
end;
print(allRands);
于 2014-08-26T15:15:48.813 回答
0

实际上,除非您使用的是旧版本的 SAS/IML,否则您不需要任何循环。从 SAS/IML 12.3 开始,RANDGEN 子例程接受参数向量。在您的情况下,为 alpha 和 beta 参数定义一个向量。假设有“Nparam”参数。然后分配一个 N x Nparam 矩阵来保存结果。通过一次调用 RANDGEN,您可以填充矩阵,以便第 i_th 列是来自 Weibull(alpha[i], beta[i]) 的大小为 N 的样本,如以下示例所示:

 proc iml;
 Nparam = 8; N = 1000;

 alpha= 1:Nparam;  /* assign parameter values */
 beta = 10 + (Nparam:1);
 rands = j(N,Nparam);
 call randgen(rands, 'WEIB', alpha,beta);  /* SAS/IML 12.1 */

 /* DONE. The i_th column is a sample from Weibul(alpha[i], beta[i]) 
    TEST IT: Compute the mean of each sample: */
 mean = mean(rands); std = std(rands);
 print (alpha//beta//mean//std)[r={"alpha" "beta" "mean" "std"}];
 /* TEST IT: Plot the distribution of each sample (SAS/IML 12.3) */  
 title "First param"; call histogram(rands[,1]);
 title "Last param";  call histogram(rands[,Nparam]);
于 2014-08-27T12:33:09.643 回答