如何将替代值分配给相对于 Ox 中的另一个变量的变量(有点类似于 C++) [英] How to assign alternative values to a variable relative to another variable in Ox (a bit similar to C++)

查看:51
本文介绍了如何将替代值分配给相对于 Ox 中的另一个变量的变量(有点类似于 C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Ox(类似于 C、C++ 和 Java 的语法)模拟一些数据,但我被困在我的赋值部分.比方说,我有这个函数来模拟我的数据,g_mY:

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

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

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
}

当我打印 g_mY 时,我只有零.我怎样才能成功实现这一目标?

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

非常感谢.

推荐答案

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

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);
}

这篇关于如何将替代值分配给相对于 Ox 中的另一个变量的变量(有点类似于 C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆