简体   繁体   中英

How to assign alternative values to a variable relative to another variable in Ox (a bit similar to C++)

I was simulating some data with Ox (syntax similar to C, C++ and Java) and I was stuck in my assignation part. Let's say, I have this function simulating my data, 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

What I want to do is to create g_mY by using the cutpoints (cut...) and latent variable (Yt) defined above, and to compute alternative values to g_mY. More like this:

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
}

When I print g_mY, I only have zeros. How can I achieve this successfully?

Many thanks.

If I understood correctly your question, the following code should answer it. It shows several ways to randomly pick a variable: with a loop (#1), with the function ranindex (#2) or with the ternary operator (#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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM