#include "RcppArmadillo.h" using namespace arma; // use the Armadillo library for matrix computations using namespace Rcpp; using namespace R; // [[Rcpp::depends(RcppArmadillo)]] //timing functions time_t itime; char buf[100]; void startMcmcTimer() { itime = time(NULL); Rcout << " MCMC Iteration (est time to end - min) \n"; } void infoMcmcTimer(int rep, int R) { time_t ctime = time(NULL); char buf[32]; double timetoend = difftime(ctime, itime) / 60.0 * (R - rep - 1) / (rep+1); sprintf(buf, " %d (%.1f)\n", rep+1, timetoend); Rcout << buf; } void endMcmcTimer() { time_t ctime = time(NULL); char buf[32]; sprintf(buf, " Total Time Elapsed: %.2f \n", difftime(ctime, itime) / 60.0); Rcout << buf; itime = 0; } //necessary functions for loop //RI likelihood // [[Rcpp::export]] List RI_lik_single_looped(List Data, vec beta, double lambda, vec mu, mat states, int nobs, uvec simp_idx, uvec comp_idx, mat optimal_action_weight2){ // Computes the joint likelihood of the RI choices of an individual int nstates = states.n_cols; double nalt = states.n_rows; // int nvar = beta.n_elem; // dim(optimal_action_weight)=c(nvar,nobs) vec lik(nobs); vec oldProbs = zeros(nalt); vec cProbs = oldProbs; mat oldCondProbs = zeros(nalt,nstates); mat cCondProbs = oldCondProbs; mat post = zeros(nalt,nstates); mat outprobs(nalt,nobs); for(int i=0;i(as(Data[i])["X"]); int y = as(as(Data[i])["y"]); int v = as(as(Data[i])["v"]); // GENERAL VERSION // complex element(s) of beta and complex column(s) of X (indicated by complex_idx) are separated from the rest // and then Omega is calculated accordingly mat X_simp(X.n_rows,simp_idx.n_elem); X_simp = X.cols(simp_idx); vec beta_simp(simp_idx.n_elem); beta_simp = beta.elem(simp_idx); // Calculate Omega (matrix of utility values at each state) | beta mat Xbeta = X.cols(simp_idx)*beta(simp_idx); mat Omega = zeros(nalt,nstates); for(int j=0;j::from(beta(comp_idx)) * states; //here beta(comp_idx) needs to converted to double for multiplication. change this when there are more than 1 simple attributes // Calculate P(v) | Omega, mu, lambda using BA algorithm // max_iter: maximal number of iterations // precision: threshold for the sum of squared differences int max_iter = 100000000; double precision = 10e-10; // Transformation of the utility index matrix //mat Z = exp(Omega/lambda); //numerically stable mat G = Omega/lambda; rowvec maxl = max(Omega/lambda,0); //vectorwise maximum mat Z = zeros(nalt,nstates); for(int j=0;jprecision && counter(as(Data[0])["X"]).n_cols; //int ncomplevels = 5;//rho.n_elem; int nstates = states.n_cols;//rho.size();//ncomplevels^(nalt-1); // number of all possible states //simp_idx = simp_idx - 1; //adopt cpp indexing //comp_idx = comp_idx - 1; //adopt cpp indexing //creating storage for results //cube betadraw(nind,nvar,R),hiervar(nvar,nvar,R); //mat likdraw(nind,R),hiermean(nvar,R); //vec oldlik(nind); mat betadraw(nvar,R); vec likdraw(R); // mu (prior over states) assuming a uniform prior vec mu(nstates); mu.fill(1.0/nstates); // initiliazing starting values for betadraws //mat betastar(nind, nvar); //for(int i=0;i(BAout["probs"]);//*0.99; // oldlik(i) = BAout["lik"]; // oldlpostbeta(i) = oldlik(i) + lndMvn(trans(betastar.row(i)), currmean,rooti); //} BAout = RI_lik_single_looped(Data, beta_start, lambda, mu, states, nobs,simp_idx, comp_idx, BA_start); //BA_start=as(BAout["probs"])*0.5+(1/nalt)*0.5;//*0.99; //BA_start=as(BAout["probs"]);//*0.99; oldlik = BAout["lik"]; oldlpostbeta= oldlik + lndMvn(beta_start, priomean,rooti); startMcmcTimer(); //main iteration loop for(int reps=0;reps(BAout["probs"])*0.5+(1/nalt)*0.5;//*0.99; } //i-break if ((reps+1)%100==0) infoMcmcTimer(reps, R); betadraw.col(reps) = betastar; likdraw(reps)=oldlik; } //R break endMcmcTimer(); return List::create( Named("betadraw") = betadraw, Named("lhdraw") = likdraw); }