<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:12.0pt;color:black'>Hi, <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'>I'm running a MCMC estimation of a Bayesian model, and I'm running into a particular error during the MCMC in my likelihood function call. I’ve implemented my likelihood function in C++ using RCpp, and I get this “unimplemented type” error:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'>Error in loglik.func(): unimplemented type ‘double’ in ‘coerceToReal.’<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'>The nasty thing about this is that it is very hard to replicate, and only occurs after a particular iteration in my MCMC.</span><span style='font-size:12.0pt'> While my function stalls when this errors occurs, all the other times it returns the correct values. My specific question is: are there any particular functions in the Rcpp library that calls coerceToReal?” I’m trying to understand what would cause Rcpp to throw an error like this (eg. memory leaks, mismatching of types…etc). A Google search returns very few relevant results on this type of error. <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>I think I’ve isolated the problem area in my code to this function:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>//////////////////////////////////////////<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'>Rcpp::NumericVector pbivpois(arma::colvec x, arma::colvec y, arma::rowvec lambda, bool returnLog) {<br> // Unlike the pbivpois function, this assumes that x is a vector, and does not error check. <br> // Declare Internal Variables<br> int N = x.n_rows; <br> Rcpp::NumericVector logbp(N);<br> Rcpp::NumericVector result(N);<br> int x0 = 0;<br> int y0 = 0;<br> int xymin = 0;<br> Rcpp::NumericVector i(2);<br> arma::rowvec sums = arma::zeros<arma::rowvec>(3);<br> double lambdaratio = 0;<br> double maxsums = 0;<br> double logsummation =0;<br> <br> for(int k = 0; k < N; k++) {<br> x0 = x.row(k);<br> y0 = y.row(k);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'> //////////////////////////////////////////////////<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'> // I think these lines are causing the problem.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'><br> xymin = std::min(x0, y0);<br> lambdaratio = lambda.col(2)/(lambda.col(0)*lambda.col(1));<br> i = Rcpp::seq(0,xymin); <br> sums = -Rcpp::lgamma(x0 - i + 1)-Rcpp::lgamma(i+1) - Rcpp::lgamma(y0-i+1) + i*std::log(lambdaratio);<br> maxsums = sums.max();<br> sums = sums-maxsums; <br> logsummation = std::log(arma::accu(arma::exp(sums))) + maxsums; <br> logbp[k] = -arma::accu(lambda) + x0*std::log(lambda.col(0)) + y0*std::log(lambda.col(1)) + logsummation;<o:p></o:p></span></p><p class=MsoNormal style='text-indent:.5in'><span style='font-size:12.0pt;color:black'><o:p> </o:p></span></p><p class=MsoNormal style='text-indent:.5in'><span style='font-size:12.0pt;color:black'>////////////////////////////////////<br> }<br><br> if( returnLog == TRUE ) {<br> result = logbp;<br> } else {<br> result = Rcpp::exp(logbp);<br> }<br> <br> return(result); <br>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;color:black'>////////////////////////////////////////////////<br><br></span><span style='font-size:12.0pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Thanks for your help!<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Best,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>Clarence<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>