-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnmixgof_manual.cpp
More file actions
197 lines (184 loc) · 5.09 KB
/
nmixgof_manual.cpp
File metadata and controls
197 lines (184 loc) · 5.09 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// This code originally published by Knape et al. 2018 (DOI: 10.1111/2041-210X.13062)
// Republished with permission from the authors
#include <Rcpp.h>
using namespace Rcpp;
// These functions compute the CDF of the sum of a sequence of
// independent binomial trials with the same index N.
// [[Rcpp::export]]
NumericVector pbinsumRow(NumericVector y, double N, NumericVector p) {
NumericVector res(3);
NumericVector ySum(1);
ySum(0) = -1;
// Handle NAs by setting p to 0 and y to 1 => dbinom(y,N,p) = 0.
for (int i=0; i<y.length(); i++) {
if(NumericVector::is_na(y[i]) || NumericVector::is_na(p[i])) {
y[i] = 1;
p[i] = 0.0;
} else {
if (ySum[0] == -1) {
ySum[0] = y[i];
} else {
ySum[0] += y[i];
}
}
}
if (ySum[0] == -1) {
res[0] = res[1] = res[2] = NA_REAL;
return res;
}
if (!Rcpp::traits::is_finite<REALSXP>(ySum(0))) {
res[0] = ySum[0];
res[1] = res[2] = R_NaN;
return res;
}
NumericMatrix pMat(ySum[0] + 1, p.length());
NumericVector pT(ySum[0] + 1);
NumericVector pF(ySum[0] + 1);
for (int col=0; col<pMat.ncol(); col++) {
for (int k = 0; k <= ySum[0]; k++) {
pMat(k, col) = Rf_dbinom(k, N, p[col], false);
}
}
for (int k = 0; k <= ySum[0]; k++) {
pT[k] = pMat(k, 0);
pF[k] = pT[k];
}
for (int col = 1; col<pMat.ncol(); col++) {
for (int j = 0; j <= ySum[0]; j++) {
pF[j] = 0;
for (int i = 0; i<=j; i++) {
pF[j] += pT[i]*pMat(j-i, col);
}
}
for (int k = 0; k <= ySum[0]; k++) {
pT[k] = pF[k];
}
}
for (int k = 0; k < ySum[0]; k++)
res[1] += pF[k];
res[2] = res[1] + pF[ySum[0]];
res[0] = ySum[0];
return res;
}
// [[Rcpp::export]]
NumericMatrix pbinsum(NumericMatrix y, NumericVector N, NumericMatrix p) {
NumericMatrix cumProb(y.nrow(),3);
if(y.ncol() != p.ncol() || y.nrow() != p.nrow()) {
stop("Dimensions of y do not match those of p.");
}
if(y.nrow() != N.length()) {
stop("Length of N does not match the number of rows of y or p.");
}
/*
for (int i = 0; i < y.nrow(); i++) {
for (int j = 0; j < y.ncol(); j++) {
if (NumericVector::is_na(y(i,j)) || NumericVector::is_na(p(i,j))) {
y(i,j) = NA_REAL;
p(i,j) = NA_REAL;
}
}
}
*/
for (int i = 0; i < y.nrow(); i++) {
cumProb.row(i) = pbinsumRow(y.row(i), N[i], p.row(i));
}
return cumProb;
}
#include <Rcpp.h>
using namespace Rcpp;
// These functions compute the CDF of the sum of a sequence of
// independent beta-binomial trials with the same index N.
// [[Rcpp::export]]
NumericVector pbbinsumRow(NumericVector y, double N, NumericVector p, NumericVector theta) {
NumericVector res(3);
NumericVector ySum(1);
double vif = 1/theta[0];
ySum(0) = -1;
// Handle NAs by setting p to 0 and y to 1 => dbinom(y,N,p) = 0.
for (int i=0; i<y.length(); i++) {
if(NumericVector::is_na(y[i]) || NumericVector::is_na(p[i])) {
y[i] = 1;
p[i] = -1.0;
} else {
if (ySum[0] == -1) {
ySum[0] = y[i];
} else {
ySum[0] += y[i];
}
}
}
if (ySum[0] == -1) {
res[0] = res[1] = res[2] = NA_REAL;
return res;
}
if (!Rcpp::traits::is_finite<REALSXP>(ySum(0))) {
res[0] = ySum[0];
res[1] = res[2] = R_NaN;
return res;
}
NumericMatrix pMat(ySum[0] + 1, p.length());
NumericVector pT(ySum[0] + 1);
NumericVector pF(ySum[0] + 1);
for (int col=0; col<pMat.ncol(); col++) {
for (int k = 0; k <= ySum[0]; k++) {
if (p[col] < 0) {
if (k==0)
pMat(k, col) = 1.0;
else
pMat(k, col) = 0.0;
} else {
pMat(k, col) = exp(Rf_lchoose(N, k) + Rf_lbeta(k+p[col]*vif, N-k+(1-p[col])*vif) - Rf_lbeta(p[col]*vif, (1-p[col])*vif));
if (NumericVector::is_na(pMat(k,col))) { // Rough trick to get around numerical underflow
pMat(k, col) = 0.0;
}
}
}
}
for (int k = 0; k <= ySum[0]; k++) {
pT[k] = pMat(k, 0);
pF[k] = pT[k];
}
for (int col = 1; col<pMat.ncol(); col++) {
for (int j = 0; j <= ySum[0]; j++) {
pF[j] = 0;
for (int i = 0; i<=j; i++) {
pF[j] += pT[i]*pMat(j-i, col);
}
}
for (int k = 0; k <= ySum[0]; k++) {
pT[k] = pF[k];
}
}
for (int k = 0; k < ySum[0]; k++)
res[1] += pF[k];
res[2] = res[1] + pF[ySum[0]];
res[0] = ySum[0];
return res;
}
// [[Rcpp::export]]
NumericMatrix pbbinsum(NumericMatrix y, NumericVector N, NumericMatrix p, NumericVector theta) {
NumericMatrix cumProb(y.nrow(),3);
if(y.ncol() != p.ncol() || y.nrow() != p.nrow()) {
stop("Dimensions of y do not match those of p.");
}
if(y.nrow() != N.length()) {
stop("Length of N does not match the number of rows of y or p.");
}
if(theta.length() != 1) {
stop("delta should have length 1.");
}
/*
for (int i = 0; i < y.nrow(); i++) {
for (int j = 0; j < y.ncol(); j++) {
if (NumericVector::is_na(y(i,j)) || NumericVector::is_na(p(i,j))) {
y(i,j) = NA_REAL;
p(i,j) = NA_REAL;
}
}
}
*/
for (int i = 0; i < y.nrow(); i++) {
cumProb.row(i) = pbbinsumRow(y.row(i), N[i], p.row(i), theta);
}
return cumProb;
}