-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBVSG_binary.cpp
More file actions
722 lines (508 loc) · 17.9 KB
/
BVSG_binary.cpp
File metadata and controls
722 lines (508 loc) · 17.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include<RcppArmadillo.h>
#include<cstdio>
#include<cstdlib>
#include<iostream>
//Sampling Scheme from Inverse Gaussian Distribution
//[[Rcpp::export]]
Rcpp::NumericVector inversegauss(int k,Rcpp::NumericVector mu_ig_v,Rcpp::NumericVector lambda_ig_v){
//variable declaration
Rcpp::NumericVector x_ig_v(k);
Rcpp::NumericVector UniRand = Rcpp::runif(k); //sampling a vector from U(0,1)
Rcpp::NumericVector NormRand = Rcpp::rnorm(k); //sampling a vector from N(0,1)
for(int i =0; i<k ; i++){
//variable declaration
double mu_ig = mu_ig_v(i);
double lambda_ig = lambda_ig_v(i);
double y_ig;
double x_ig;
// returns 0, when we sample from inverse gauss(0.0,lambda)
if(mu_ig!=0.0) {
y_ig = pow(NormRand[i],2) ;
x_ig = mu_ig + (pow(mu_ig,2)*y_ig)/(2*lambda_ig) - (mu_ig/(2*lambda_ig))*sqrt(4*mu_ig*lambda_ig*y_ig+ pow(mu_ig,2)*pow(y_ig,2));
if (UniRand[i] >= mu_ig/(mu_ig+x_ig)) {
x_ig_v[i] = (pow(mu_ig,2))/x_ig;
} else {
x_ig_v[i] = x_ig;
}
} else {
x_ig_v[i] = 0.0;
}
}
return x_ig_v;
}
//Sampling Beta from Multivariate Normal
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
Rcpp::NumericVector sampleBetainC(double sigma2, Rcpp::NumericMatrix Lambda,Rcpp::NumericMatrix XtX,Rcpp::NumericVector XtYminusYbar){
int n=XtX.nrow(), p=XtX.ncol();
arma::mat xtx(XtX.begin(),n,p,false) ;
arma::mat lambda(Lambda.begin(),p,p,false) ;
arma::vec xtyminusybar(XtYminusYbar.begin(),XtYminusYbar.size(),false);
arma::mat cov_beta = arma::inv(xtx + lambda);
arma::vec mean_beta = cov_beta*xtyminusybar;
/*
Rcpp::Environment mvtnorm("package:mvtnorm");
Rcpp::Function rmvnorm =mvtnorm["rmvnorm"];
*/
Rcpp::NumericVector mean = Rcpp::as<Rcpp::NumericVector>(Rcpp::wrap(mean_beta));
Rcpp::NumericMatrix cov = Rcpp::as<Rcpp::NumericMatrix>(Rcpp::wrap(cov_beta));
/*
Rcpp::String Boolean = "svd";
Rcpp::NumericVector beta= rmvnorm(1,Rcpp::Named("mean",mean),Rcpp::Named("sigma",cov),Rcpp::Named("method",Boolean));
Rcpp::Environment myEnv("package::base");
Rcpp::Function mychol = myEnv["chol"];
Rcpp::NumericMatrix chol_cov(p,p);
chol_cov = mychol(cov);
*/
arma::vec beta(p);
arma::vec normrand = Rcpp::as<arma::vec>(Rcpp::rnorm(p)) ;
beta = mean_beta + sqrt(sigma2)*arma::chol(cov_beta)*normrand;
return Rcpp::as<Rcpp::NumericVector>(Rcpp::wrap(beta));
}
//Sampling from C_matrix
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
Rcpp::NumericMatrix sampleC_matrixinC(Rcpp::NumericVector beta,double sigma2,double r,double a,double b){
//variable declaration
int p=beta.size();
arma::colvec one(p,arma::fill::ones);
arma::colvec Beta(p);
Beta = Rcpp::as<arma::colvec>(Rcpp::wrap(beta));
arma::mat Beta_Plus(p,p);
Beta_Plus = Beta*one.t() + one*Beta.t();
arma::mat Beta_minus(p,p);
Beta_minus = Beta*one.t() - one*Beta.t();
arma::mat Pmat(p,p);
arma::mat onemat(p,p);
onemat.fill(1.0);
Pmat = r*b*(arma::abs(Beta_Plus) - arma::abs(Beta_minus))*(1/(2*sqrt(sigma2)));
Pmat = onemat + arma::exp(Pmat);
Pmat = arma::pow(Pmat,-1.0);
arma::mat unifmat(p,p) ;
unifmat = arma::randu<arma::mat>(p,p);
arma::mat diffmat = unifmat - Pmat;
arma::mat Cmat = arma::sign(diffmat);
Cmat = arma::symmatu(Cmat)-arma::diagmat(Cmat)+ arma::diagmat(one);
Rcpp::NumericMatrix Cmatrix = Rcpp::as<Rcpp::NumericMatrix>(Rcpp::wrap(Cmat));
return Cmatrix ;
}
// norm_rs(a, b)
// generates a sample from a N(0,1) RV restricted to be in the interval
// (a,b) via rejection sampling.
// ======================================================================
// [[Rcpp::export]]
double norm_rs(double a, double b)
{
double x;
x = Rf_rnorm(0.0, 1.0);
while( (x < a) || (x > b) ) x = norm_rand();
return x;
}
// half_norm_rs(a, b)
// generates a sample from a N(0,1) RV restricted to the interval
// (a,b) (with a > 0) using half normal rejection sampling.
// ======================================================================
// [[Rcpp::export]]
double half_norm_rs(double a, double b)
{
double x;
x = fabs(norm_rand());
while( (x<a) || (x>b) ) x = fabs(norm_rand());
return x;
}
// unif_rs(a, b)
// generates a sample from a N(0,1) RV restricted to the interval
// (a,b) using uniform rejection sampling.
// ======================================================================
// [[Rcpp::export]]
double unif_rs(double a, double b)
{
double xstar, logphixstar, x, logu;
// Find the argmax (b is always >= 0)
// This works because we want to sample from N(0,1)
if(a <= 0.0) xstar = 0.0;
else xstar = a;
logphixstar = R::dnorm(xstar, 0.0, 1.0, 1.0);
x = R::runif(a, b);
logu = log(R::runif(0.0, 1.0));
while( logu > (R::dnorm(x, 0.0, 1.0,1.0) - logphixstar))
{
x = R::runif(a, b);
logu = log(R::runif(0.0, 1.0));
}
return x;
}
// exp_rs(a, b)
// generates a sample from a N(0,1) RV restricted to the interval
// (a,b) using exponential rejection sampling.
// ======================================================================
// [[Rcpp::export]]
double exp_rs(double a, double b)
{
double z, u, rate;
// Rprintf("in exp_rs");
rate = 1/a;
//1/a
// Generate a proposal on (0, b-a)
z = R::rexp(rate);
while(z > (b-a)) z = R::rexp(rate);
u = R::runif(0.0, 1.0);
while( log(u) > (-0.5*z*z))
{
z = R::rexp(rate);
while(z > (b-a)) z = R::rexp(rate);
u = R::runif(0.0,1.0);
}
return(z+a);
}
// rnorm_trunc( mu, sigma, lower, upper)
//
// generates one random normal RVs with mean 'mu' and standard
// deviation 'sigma', truncated to the interval (lower,upper), where
// lower can be -Inf and upper can be Inf.
//======================================================================
// [[Rcpp::export]]
double rnorm_trunc (double mu, double sigma, double lower, double upper)
{
int change;
double a, b;
double logt1 = log(0.150), logt2 = log(2.18), t3 = 0.725;
double z, tmp, lograt;
change = 0;
a = (lower - mu)/sigma;
b = (upper - mu)/sigma;
// First scenario
if( (a == R_NegInf) || (b == R_PosInf))
{
if(a == R_NegInf)
{
change = 1;
a = -b;
b = R_PosInf;
}
// The two possibilities for this scenario
if(a <= 0.45) z = norm_rs(a, b);
else z = exp_rs(a, b);
if(change) z = -z;
}
// Second scenario
else if((a * b) <= 0.0)
{
// The two possibilities for this scenario
if((R::dnorm(a, 0.0, 1.0,1.0) <= logt1) || (R::dnorm(b, 0.0, 1.0, 1.0) <= logt1))
{
z = norm_rs(a, b);
}
else z = unif_rs(a,b);
}
// Third scenario
else
{
if(b < 0)
{
tmp = b; b = -a; a = -tmp; change = 1;
}
lograt = R::dnorm(a, 0.0, 1.0, 1.0) - R::dnorm(b, 0.0, 1.0, 1.0);
if(lograt <= logt2) z = unif_rs(a,b);
else if((lograt > logt1) && (a < t3)) z = half_norm_rs(a,b);
else z = exp_rs(a,b);
if(change) z = -z;
}
double output;
output = sigma*z + mu;
return (output);
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
Rcpp::NumericVector sampleTruncY(Rcpp::NumericMatrix X, Rcpp::NumericVector Y0, Rcpp::NumericVector Beta, double sigma2 ){
// variable declarations;
arma::mat x ;
arma::vec beta ;
arma::mat mu ;
int p =Y0.size();
double rand;
double mean ;
Rcpp::NumericVector Z(p);
Rcpp::NumericVector v(p) ;
x = Rcpp::as<arma::mat>(X) ;
beta = Rcpp::as<arma::vec>(Beta);
mu = x*beta ;
v =Rcpp::as<Rcpp::NumericVector>(Rcpp::wrap(mu)) ;
for ( int i =0 ; i < p ; i++) {
mean = v[i] ;
if ( Y0(i) == 1 )
{
while ( rand <= 0 || rand > 100) {
rand = Rcpp::as<double>(Rcpp::rnorm(1))*sqrt(sigma2) + mean ;
}
Z[i] = rand ;
rand = 0;
}
else if(Y0(i) == 0)
{
while ( rand >= 0 || rand <- 100) {
rand = Rcpp::as<double>(Rcpp::rnorm(1))*sqrt(sigma2) + mean ;
}
Z[i] = rand ;
rand = 0;
}
}
return Z ;
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
Rcpp::NumericMatrix sampleLambdainC(Rcpp::NumericVector beta,Rcpp::NumericMatrix C_matrix,double sigma2,double r,double a,double b){
//variable declaration
int p = beta.size();
int d = p*(p-1)/2;
int count ;
Rcpp::NumericMatrix Lambda_off(p,p);
Rcpp::NumericMatrix betam(p,p);
Rcpp::NumericVector b_mu_ig(d);
Rcpp::NumericVector b_lambda_ig(d);
Rcpp::NumericVector b_ig(d);
Rcpp::NumericVector a_mu_ig(p);
Rcpp::NumericVector a_lambda_ig(p);
Rcpp::NumericVector a_ig(p);
arma::mat Lambda(p,p);
arma::colvec one(p,arma::fill::ones);
arma::mat Cmatrix(p,p);
Cmatrix=Rcpp::as<arma::mat>(C_matrix);
for (int i =0;i <p;i++){
for (int j =0;j<p;j++){
betam(i,j) = fabs(beta[i] + C_matrix(i,j)*beta[j]);
}
}
count = -1;
for (int i=0;i<p;i++){
for (int j = 0;j<p;j++){
if ( i > j)
{
count = count + 1;
b_mu_ig[count] = (b*sqrt(sigma2))/(fabs(betam(i,j)*sqrt(r))); //betam is already nonnegative
b_lambda_ig[count]= pow(b,2);
}
if(i==j){
a_mu_ig[i] = a*sqrt(sigma2)/(sqrt(r)*fabs(beta[i]));
a_lambda_ig[i] =pow(a,2);
}
}
}
b_ig = inversegauss(d,b_mu_ig,b_lambda_ig);
a_ig = inversegauss(p,a_mu_ig,a_lambda_ig);
count = -1;
for (int i=0;i<p;i++){
for (int j = 0;j<p;j++){
if ( i > j)
{
count = count + 1;
Lambda_off(i,j) =b_ig(count);
}
}
}
Lambda = Rcpp::as<arma::mat>(Lambda_off);
Lambda = Lambda + Lambda.t();
Lambda = Lambda%Cmatrix ;
Lambda.diag() = arma::sum(arma::abs(Lambda),1) + one + Rcpp::as<arma::colvec>(a_ig);
Lambda = r*Lambda;
Lambda_off = Rcpp::as<Rcpp::NumericMatrix>(Rcpp::wrap(Lambda));
return Lambda_off;
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
double samplesigma2inC(Rcpp::NumericMatrix Lambda,Rcpp::NumericMatrix XtX,Rcpp::NumericVector XtYminusYbar,Rcpp::NumericVector Y){
int n = Y.size();
int p = Lambda.ncol();
double shape = n/2;
arma::vec rate(1);
arma::vec y(n) ;
y = Rcpp::as<arma::vec>(Y);
arma::colvec xtyminusybar(p);
xtyminusybar = Rcpp::as<arma::colvec>(XtYminusYbar);
arma::mat xtx (p,p);
xtx = Rcpp::as<arma::mat>(XtX);
arma::mat lambda(p,p);
lambda = Rcpp::as<arma::mat>(Lambda);
lambda = xtx+lambda;
lambda = arma::inv(lambda);
rate = arma::sum(arma::square(y))/2 - (xtyminusybar.t())*lambda*xtyminusybar/2;
double rate1;
rate1 = Rcpp::as<double>(Rcpp::wrap(rate));
return 1/(R::rgamma(shape,1/rate1));
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
double samplehyperainC(Rcpp::NumericVector Beta,double sigma2,double r,double ahyper){
//variable declaration
int p =Beta.size() ;
double a_shape = 1.0;
double a_rate =0;// = r*arma::accu(arma::abs(beta))/sqrt(sigma2)/2;
for(int i=0;i<p;i++){
a_rate = a_rate + fabs(Beta[i]);
}
a_rate = a_rate*r/sqrt(sigma2)/2 +ahyper;
return R::rgamma(a_shape,1/a_rate); //Rgamma takes shape and scale//
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
double samplehyperbinC(Rcpp::NumericVector Beta,Rcpp::NumericMatrix CMatrix,double sigma2,double r,double bhyper){
int p =Beta.size();
arma::colvec beta(p);
beta = Rcpp::as<arma::vec>(Beta);
arma::mat C_matrix(p,p);
C_matrix = Rcpp::as<arma::mat>(CMatrix);
arma::colvec col_one(p);
col_one.fill(1.0);
C_matrix.diag() = (-1)*col_one;
double b_shape = 1.0;
arma::mat t(p,p);
arma::mat prod(p,p);
prod =beta*(col_one.t());
// t = arma::abs(prod+C_matrix%prod.t());
for(int i=0;i<p;i++){
for(int j=0;j<p;j++){
t(i,j) = fabs(prod(i,j) + C_matrix(i,j)*prod(j,i));
}
}
double z = arma::accu(t);
double b_rate = r*z/4/sqrt(sigma2) + bhyper;
return R::rgamma(b_shape,1/b_rate); //Rgamma takes shape and scale//
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
double samplehyperrinC(Rcpp::NumericVector Beta,double sigma2,Rcpp::NumericMatrix C_Matrix,double a,double b, double rhyper){
double b_r = 0.01;
int p = Beta.size();
double r_shape = p/2.0 + rhyper;
arma::colvec beta(p);
beta = Rcpp::as<arma::colvec>(Beta);
arma::mat C_matrix(p,p);
C_matrix = Rcpp::as<arma::mat>(C_Matrix);
arma::colvec col_one(p,arma::fill::ones);
C_matrix.diag() = (-1)*col_one;
arma::mat prod(p,p);
arma::mat t(p,p);
prod =beta*(col_one.t());
for(int i=0;i<p;i++){
for(int j=0;j<p;j++){
t(i,j) = fabs(prod(i,j) + C_matrix(i,j)*prod(j,i));
}
}
arma::colvec beta_mult = arma::square(beta);
double r_rate = arma::accu(beta_mult)/sigma2/2 + a*arma::accu(arma::abs(beta))/sqrt(sigma2)/2 + b*(arma::accu(t))/4/sqrt(sigma2);
r_rate = r_rate+b_r/2;
//double r_rate = b*(arma::accu(t))/(4*sqrt(sigma2));
double r = R::rgamma(r_shape,1/r_rate);// rgamma takes shape and scale//
return r;
}
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
Rcpp::List BVSG(Rcpp::NumericMatrix X,Rcpp::NumericVector Y,Rcpp::NumericVector Y0, double a_init, double b_init, double r_init, Rcpp::NumericVector beta_init, Rcpp::NumericMatrix Lambda_init, double sigma2_init, double ahyper, double bhyper, double rhyper, int Nburn, int Niter, int Nthin ){
//std::cout << "The error line is after this " << std::endl;
double a = a_init;
double b = b_init;
double r = r_init;
int n = Y.size();
int p = X.ncol();
int N = (Niter)/Nthin;
int ind;
Rcpp::NumericVector ahist(N);
Rcpp::NumericVector bhist(N);
Rcpp::NumericVector rhist(N);
Rcpp::NumericVector sigma2hist(N);
Rcpp::NumericVector sigma2m(N) ;
arma::mat Betahist(N,p);
Rcpp::NumericVector beta(p);
beta = beta_init;
Rcpp::NumericMatrix Lambda(p,p);
Lambda = Lambda_init;
Rcpp::NumericMatrix C_matrix(p,p);
double sigma2 = sigma2_init;
arma::mat x(n,p);
x = Rcpp::as<arma::mat>(X);
arma::colvec y(n) ;
y = Rcpp::as<arma::colvec>(Y);
arma::mat xtx(p,p);
xtx = (x.t())*x;
arma::colvec xtzminuszbar(p);
Rcpp::NumericMatrix XtX(p,p);
XtX = Rcpp::as<Rcpp::NumericMatrix>(Rcpp::wrap(xtx));
Rcpp::NumericVector XtZminusZbar(p);
Rcpp::NumericVector Z(p) ;
arma::mat covmat(p,p) ;
arma::vec Betahat(p);
arma::mat Lambdahat(p,p);
Z = Y;
double mean_sigma2 ;
for(int i=0;i<Nburn;i++){
xtzminuszbar = (x.t())*(Rcpp::as<arma::colvec>(Z));
XtZminusZbar = Rcpp::as<Rcpp::NumericVector>(Rcpp::wrap(xtzminuszbar));
C_matrix = sampleC_matrixinC(beta,sigma2,r,a,b);
sigma2 = samplesigma2inC(Lambda,XtX,XtZminusZbar,Z);
beta = sampleBetainC(sigma2,Lambda,XtX,XtZminusZbar);
Lambda = sampleLambdainC(beta,C_matrix,sigma2,r,a,b);
a = samplehyperainC(beta,sigma2,r,ahyper) ;
b = samplehyperbinC(beta,C_matrix,sigma2,r,bhyper) ;
r = samplehyperrinC(beta,sigma2,C_matrix,a,b,rhyper);
sigma2m[ i ] = sigma2 ;
double L = i - 100 + 1;
double U = i ;
if ( i < 100 )
{
Z = sampleTruncY(X,Y0,beta,sigma2) ;
} else
{
mean_sigma2 = 0;
for ( int j = L; j <= U ;++ j) {
mean_sigma2 = mean_sigma2 + sigma2m[j] ;
}
mean_sigma2 = mean_sigma2/100;
Z = sampleTruncY(X,Y0,beta,mean_sigma2) ;
}
if( (1+i) % Nthin == 0)
{
ind = i/Nthin;
ahist[ind] = a;
bhist[ind] = b;
rhist[ind] = r;
sigma2hist[ind] = sigma2;
Betahist.row(ind) = Rcpp::as<arma::rowvec>(Rcpp::wrap(beta));
}
}
for(int i=0;i<Niter-Nburn;i++){
xtzminuszbar = (x.t())*(Rcpp::as<arma::colvec>(Z));
XtZminusZbar = Rcpp::as<Rcpp::NumericVector>(Rcpp::wrap(xtzminuszbar));
C_matrix = sampleC_matrixinC(beta,sigma2,r,a,b);
sigma2 = samplesigma2inC(Lambda,XtX,XtZminusZbar,Z);
beta = sampleBetainC(sigma2,Lambda,XtX,XtZminusZbar);
Lambda = sampleLambdainC(beta,C_matrix,sigma2,r,a,b);
a = samplehyperainC(beta,sigma2,r,ahyper) ;
b = samplehyperbinC(beta,C_matrix,sigma2,r,bhyper) ;
r = samplehyperrinC(beta,sigma2,C_matrix,a,b,rhyper);
sigma2m[ i + Nburn ] = sigma2 ;
double L = i + Nburn - 100 + 1;
double U = i + Nburn ;
mean_sigma2 = 0;
for ( int j = L; j <= U ;++ j) {
mean_sigma2 = mean_sigma2 + sigma2m[j] ;
}
mean_sigma2 = mean_sigma2/100;
Z = sampleTruncY(X,Y0,beta,mean_sigma2) ;
if ((Nburn+i)%Nthin == 0)
{
ind = (Nburn+i)/Nthin;
ahist[ind] = a;
bhist[ind] = b;
rhist[ind] = r;
sigma2hist[ind] = sigma2;
Betahist.row(ind) = Rcpp::as<arma::rowvec>(Rcpp::wrap(beta));
sigma2hist[ind] = sigma2;
Lambdahat = Lambdahat + Rcpp::as<arma::mat>(Rcpp::wrap(Lambda));
Betahat = Betahat + Rcpp::as<arma::vec>(Rcpp::wrap(beta));
}
}
int total_iter = (Niter-Nburn)/Nthin;
Lambdahat = Lambdahat/total_iter;
Betahat = Betahat/total_iter;
return Rcpp::List::create(Rcpp::Named("Betahat")=Betahat,Rcpp::Named("Lambdahat")=Lambdahat,Rcpp::Named("ahist")=ahist,Rcpp::Named("bhist")=bhist,Rcpp::Named("rhist")=rhist,Rcpp::Named("sigma2hist")=sigma2hist,Rcpp::Named("Betahist")=Betahist);
}