标题:新手上路,请高手帮忙加个注释
只看楼主
icmba
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-7-13
结帖率:0
已结贴  问题点数:20 回复次数:1 
新手上路,请高手帮忙加个注释
需要用到一个R计算 要调用C语言的程序,关于数据筛选的,对C不熟悉,请高手帮忙给点注释,十分感谢!
开始是一些变量的注释。C程序在注释之后。
# read data and make call to run for screening rules
# nhh = number of respondents (households)
# nset = number of choice tasks
# nsize = number of alternatives per choice task
# nxvar = number of attribute levels, dim(beta)
# natvar = number of attributes
# ntheta = total number of grid points needed for discrete attributes
# the file "camchoice.txt" contains the dependent variable (y=1 or 0)
# the file "camdesign.txt" contains the design matrix (dummy variable coding)
# the file "camatt3.dat" contains the design matrix (levels coding)
# the file "tindex.txt" contains the theta index for each attribute


#include <R.h>
#include <Rmath.h>
#include <math.h>

double rtrun(double mu, double sigma,double trunpt, int above)
{
/*    function to draw truncated normal
        above=1 means from above b=trunpt, a=-inf
        above=0 means from below a=trunpt, b= +inf   
*/
    double FA,FB,rnd,result ;
    if (above) {
        FA=0.0; FB=pnorm(((trunpt-mu)/(sigma)),0.0,1.0,1,0);
            }
    else {
        FB=1.0; FA=pnorm(((trunpt-mu)/(sigma)),0.0,1.0,1,0);
        }
   
    GetRNGstate();
    rnd=unif_rand();
    result = mu + sigma*qnorm((rnd*(FB-FA)+FA),0.0,1.0,1,0);
    PutRNGstate();
    return result;
}

void choiceset(int *natvar_i, int *nsize_i , int *nset_i, double *xatt, double *gamma, int *cset)
{
   int natvar = *natvar_i; int nsize = *nsize_i; int nset = *nset_i;
   int i,j,k;
   int cst;

   for(i=0;i<nset;i++) {
      for(j=0;j<nsize;j++) {
         cst = 1;
         k=0;
         while((k<natvar) && cst) {
            if((*(xatt + i*(natvar*nsize) + j*natvar + k)) > gamma[k]) k++;
            else cst = 0;
         }
         if(j == (nsize-1)) cst=1;
         *(cset + i*nsize + j) = cst;
      }
   }
}

void testgam(int *natvar_i, int *nsize_i , int *nset_i, double *xatt, int *y, double *z,double *gamma, int *test)
{
   int natvar = *natvar_i; int nsize = *nsize_i; int nset = *nset_i;
   int i,j,k;  
   double zmax;
   int cset;
   double zz;
   int yy;

   *test = 1;
   i=0;
   while((i<nset) && (*test)) {
      k=0;
      while((*(y + k + i*nsize)) != 1) k++;
      zmax = *(z + k + i*nsize);
      j=0;
      while((j<nsize) && (*test)) {
         cset = 1;
         k=0;
         while((k<natvar) && cset) {
            if((*(xatt + i*(natvar*nsize) + j*natvar + k)) > gamma[k]) k++;
            else cset=0;
         }
         yy = *(y+j+i*nsize);
         if(!cset && yy) *test = 0;
         zz = *(z+j+i*nsize);
         if(cset && (zz>zmax)) *test = 0;
         if(j==(nsize-1)) *test = 1;
         j++;
      }
      i++;
   }
}

void getbounds(int n,double *z, int *c, int *y, double *zc, double *zy)
{
   *zc = -1000;
    int k;
    for(k=0;k<n;k++) {
       if(y[k]) *zy = z[k];
        else {
           if(c[k] && (z[k] > (*zc))) *zc = z[k];
        }
    }
}

void drawz(int *nsize_i, int *nset_i, double *v, int *y, int *c, double *z)
{
    int nsize = *nsize_i; int nset = *nset_i;

    int j,k;
   double *vt;
    double *zt;
    int *ct;
    int *yt;
    double zc,zy,zold;

    GetRNGstate();

   for(j=0;j<nset;j++) {

      vt = v+j*nsize;
      zt = z+j*nsize;
       ct = c+j*nsize;
       yt = y+j*nsize;

      getbounds(nsize,zt,ct,yt,&zc,&zy);

       for(k=0;k<nsize;k++) {
           if(ct[k]) {
               if(yt[k]) {
                   zt[k] = rtrun(vt[k],1.0,zc,0);
                    zy = zt[k];
                }
                else {
                    zold = zt[k];
                   zt[k] = rtrun(vt[k],1.0,zy,1);
                    if(zt[k]>zc) zc = zt[k];
                    else {
                       if(zold == zc) getbounds(nsize,zt,ct,yt,&zc,&zy);
                    }
                }
            }
            else {
               zt[k] = vt[k] + norm_rand();
            }
        }
    }
    PutRNGstate();
}





搜索更多相关主题的帖子: 新手上路 needed number points C语言 
2011-07-13 23:15
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
得分:20 
..............
2011-07-13 23:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-345444-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.073919 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved