[英]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.我正在用 Ox 模拟一些数据(语法类似于 C、C++ 和 Java),我被困在我的分配部分。 Let's say, I have this function simulating my data, g_mY:
假设,我有这个 function 模拟我的数据 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.我想要做的是通过使用上面定义的切点(cut ...)和潜在变量(Yt)来创建 g_mY,并计算 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.当我打印 g_mY 时,我只有零。 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).它显示了随机选择变量的几种方法:使用循环 (#1)、使用 function
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.