1
#include <Rcpp.h>
2
using namespace Rcpp;
3

4
// [[Rcpp::export]]
5 1
NumericMatrix approx_relative(NumericVector Nu, 
6
                              NumericVector Nd, 
7
                              IntegerMatrix P,
8
                              bool iterative,
9
                              int max_iter) {
10 1
  int n=Nu.size();
11 1
  NumericMatrix rrp(n,n);
12 1
  for(int x=0;x<(n-1);++x){
13 1
    for(int y=x+1;y<n;++y){
14 1
      if(P(y,x)==1){
15 0
        rrp(y,x)=1;
16 0
        rrp(x,y)=0;
17
      }
18 1
      else if(P(x,y)==1){
19 1
        rrp(y,x)=0;
20 1
        rrp(x,y)=1;
21
      }
22
      else{
23 1
        rrp(y,x)=double(Nu[y]+1)*(Nd[x]+1)/double((Nd[y]+1)*(Nu[x]+1)+(Nu[y]+1)*(Nd[x]+1));
24 1
        rrp(x,y)=1-rrp(y,x);
25
      }
26
    }
27
  }
28 1
  if(iterative){
29 1
    for(int it=0; it<max_iter-1; ++it){
30 1
      for(int i=0; i<n; ++i){
31 1
        Nu[i]=sum(rrp(i,_));
32 1
        Nd[i]=sum(rrp(_,i));
33
      }
34 1
      for(int x=0;x<(n-1);++x){
35 1
        for(int y=x+1;y<n;++y){
36 1
          if(P(y,x)==1){
37 0
            rrp(y,x)=1;
38 0
            rrp(x,y)=0;
39
          }
40 1
          else if(P(x,y)==1){
41 1
            rrp(y,x)=0;
42 1
            rrp(x,y)=1;
43
          }
44
          else{
45 0
            rrp(y,x)=double(Nu[y]+1)*(Nd[x]+1)/double((Nd[y]+1)*(Nu[x]+1)+(Nu[y]+1)*(Nd[x]+1));
46 0
            rrp(x,y)=1-rrp(y,x);
47
          }
48
        }
49
      }
50
    }
51
  }
52 1
  return rrp;
53
}

Read our documentation on viewing source code .

Loading