[Rcpp-devel] dmvnorm() of mvtnorm using RcppArmadillo

Shaami nzshaam at gmail.com
Thu Dec 19 19:50:45 CET 2019


Dear Sir

Here is my MWE.

I am still getting the follwoing error.

Error in dmvnormC(x, mean, sigma, 0) : Cannot convert object to a function:
[type=NULL; target=CLOSXP, SPECIALSXP, or BUILTINSXP].

Could you please guide what is going wrong?
y <- matrix(1:10, nrow=5)
mean <- c(4, 5)
sigma <- matrix(c(0.01, 0.009, 0.009, 1.2), nrow =2, byrow = TRUE)
dmvnormC(y, mean, sigma, 0) # Function is below.
#include <RcppArmadillo.h>
#include <mvtnormAPI.h>
using namespace Rcpp;
using namespace RcppArmadillo;
using namespace arma;

//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::depends(mvtnorm)]]
//[[Rcpp::export]]

arma::vec  dmvnormC(const arma::mat& x, const arma::vec& mean, const
arma::mat& sigma, int give_log)
{
  Environment pkg = Environment::namespace_env("mvtnorm");
  Function dmvnorm = pkg["mvtnorm"];
  int N = x.n_rows;
  arma::rowvec f(N);
  for(int k=0; k<N; k++)
    {
      f(k) = Rcpp::as<double>(dmvnorm(x.row(k), mean,  sigma, give_log));
    }
  return f;
}


Thank you


On Fri, Dec 20, 2019 at 12:19 AM Ralf Stubner <ralf.stubner at gmail.com>
wrote:

>
>
> On Thu, Dec 19, 2019 at 8:01 AM Shaami <nzshaam at gmail.com> wrote:
>
>>   Environment pkg = Environment::namespace_env("mvtnorm");
>>   Function dmvnorm = pkg["mvtnorm"];
>>
>
> There is no mvtnorm function in the mvtnorm package. You probably want to
> dmvnorm function instead.
>
> BTW, the mvtnorm package provides a C API for the dmvnorm function, c.f.
> https://stackoverflow.com/a/51294364/8416610
>
> cheerio
> ralf
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191220/21ca3ab4/attachment.html>


More information about the Rcpp-devel mailing list