-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRcppFunc.cpp
More file actions
39 lines (37 loc) · 1.01 KB
/
RcppFunc.cpp
File metadata and controls
39 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix cmat(NumericVector id, NumericVector time, IntegerVector result, double phi1, double phi0, double negpred) {
NumericVector utime = unique(time);
utime.sort();
int J = utime.size(), nsub = unique(id).size(), nobs = id.size(), i, j;
NumericVector rid(nobs);
NumericMatrix Cm(nsub, J + 1), Dm(nsub, J + 1);
utime.push_back(utime[J-1] + 1);
//initiate with 1 for Cm
std::fill(Cm.begin(), Cm.end(), 1);
//Calculate Cm
j = 0;
for (i = 1; i < nobs; i++) {
if (id[i] != id[i-1]) j++;
rid[i] = j;
}
for (j = 0; j <= J; j++) {
for (i = 0; i < nobs; i++) {
if (result[i]==0) {
if (time[i] >= utime[j]) {
Cm(rid[i], j) *= 1 - phi1;
} else {
Cm(rid[i], j) *= phi0;
}
} else {
if (time[i] >= utime[j]) {
Cm(rid[i], j) *= phi1;
} else {
Cm(rid[i], j) *= 1 - phi0;
}
}
}
}
return Cm;
}