1

我正在用 Ox(类似于 C、C++ 和 Java 的语法)模拟一些数据,但我被困在我的分配部分。假设,我有这个函数来模拟我的数据 g_mY:

decl g_mX, g_mY; 

simuldata(const ct) //  ct : number of observations 
{                                                      

    decl mx = ranbinomial(ct, 1, 1, 0.40)~ 100*ranu(ct, 1); 
    decl veps = rann(ct, 1); 
    decl vp = < .0485434;-.006764 ; -.0187657; -1.106632 ; .3647326 ; 1.11204 >; 
    g_mX = mx[][0:1] ; // regressors: Gender, Age.
    
    decl cut1 = vp[2], cut2 = vp[3], cut3 = vp[4], cut4 = vp[5] ;
    decl Yt = g_mX*vp[:1] + veps ; // latent variable

我想要做的是通过使用上面定义的切点(cut ...)和潜在变量(Yt)来创建 g_mY,并计算 g_mY 的替代值。更像这样:

g_mY = new matrix[rows(g_mX)][1] ;  // dependent variable
        for(decl i = 0; i < rows(g_mX); ++i)
          {
            if(Yt[i] < cut1)
                {
                 g_mY[i] = < a number between 1 and 100, but != to a multiple of 5 >
                }

            else if(Yt[i]> cut1 .&& Yt[i]<= cut2)
                {
                g_mY[i] = 5   || g_mY[i] = 15  || g_mY[i] = 35  || g_mY[i] = 45  || g_mY[i] = 55 ||
                g_mY[i] = 65  || g_mY[i] = 85  || g_mY[i] = 95 ; 

                // one of these multiples of 5 that are not multiples of 10
                }

            else if(Yt[i]> cut2 .&& Yt[i] <= cut3)
                {
                 g_mY[i] = 10  || g_mY[i] = 20  || g_mY[i] = 30   || g_mY[i] = 40  ||
                 g_mY[i] = 60  || g_mY[i] = 70  || g_mY[i] = 80   || g_mY[i] = 90 ; 

                // one of these multiples of 10
                }

            else if(Yt[i] > cut3 .&& Yt[i] <= cut4)
                {
                g_mY[i] = 25 || g_mY[i] = 75 ; //either 25 or 75
                }

            else if(Yt[i] > cut4)
                {
                g_mY[i] = 50 || g_mY[i] = 100; //either 50 or 100
                }
           }
return 1
}

当我打印 g_mY 时,我只有零。我怎样才能成功地做到这一点?

非常感谢。

4

1 回答 1

2

如果我正确理解了您的问题,则以下代码应该可以回答。它展示了几种随机选择变量的方法:使用循环 (#1)、使用函数ranindex(#2) 或使用三元运算符 (#3)。

#include <oxstd.oxh>
#include <oxprob.h>

decl g_mX, g_mY;

simuldata(const ct) { //  ct : number of observations
    decl mx = ranbinomial(ct, 1, 1, 0.40)~ 100 * ranu(ct, 1);
    decl veps = rann(ct, 1);
    decl vp = < .0485434; -.006764 ; -.0187657; -1.106632 ; .3647326 ; 1.11204 >;
    g_mX = mx[][0:1] ; // regressors: Gender, Age.
    decl cut1 = vp[2], cut2 = vp[3], cut3 = vp[4], cut4 = vp[5] ;
    decl Yt = g_mX * vp[:1] + veps ; // latent variable
    g_mY = new matrix[rows(g_mX)][1] ;  // dependent variable
    for (decl i = 0; i < rows(g_mX); ++i) {
        if (Yt[i] < cut1) {
            decl temp ;
            do { //#1
                temp = 1 + ranindex(1, 100);
            } while (imod(temp, 5) == 0); // a number between 1 and 100, but != to a multiple of 5
            g_mY[i] = temp ;
        } else if (Yt[i] > cut1 .&& Yt[i] <= cut2) {
            decl ar = < 5; 15; 35; 45; 55; 65; 85; 95 >;
            g_mY[i] = ar[ranindex(1, rows(ar))] ;//#2
        } else if (Yt[i] > cut2 .&& Yt[i] <= cut3) {
            decl ar = range(10,90,10)'; // == < 10; 20; 30; 40; 60; 70; 80; 90 >;
            g_mY[i] =  ar[ranindex(1, rows(ar))] ;//#2
        } else if (Yt[i] > cut3 .&& Yt[i] <= cut4) {
            g_mY[i] = ranu(1, 1) > 0.5 ? 25 : 75  ;//#3
        } else if (Yt[i] > cut4) {
            g_mY[i] = ranu(1, 1) > 0.5 ? 50 : 100  ;//#3
        }
    }
    return 1;
}

main() {
    simuldata(100);
    println(g_mY);
}
于 2021-06-03T14:57:50.200 回答