使用 Rcpp Sugar 将均值和标准差传递给 dnorm() [英] Passing mean and standard deviation into dnorm() using Rcpp Sugar

查看:44
本文介绍了使用 Rcpp Sugar 将均值和标准差传递给 dnorm()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 R 代码转换为 Rcpp 代码,并且需要计算给定均值向量和标准差向量的观察向量的似然性.如果我假设均值为 0 且标准差为 1,我可以编写此函数(运行此函数需要加载 'inline' 和 'Rcpp' 包),

I am converting some R code to Rcpp code and need to calculate the likelihood for a vector of observations given a vector of means and vector of standard deviations. If I assume the means are 0 and the standard deviations 1, I can write this function (running this requires the 'inline' and 'Rcpp' packages to be loaded),

dtest1 = cxxfunction(signature( x = "numeric"),
                      'Rcpp::NumericVector xx(x);
                       return::wrap(dnorm(xx, 0.0, 1.0));', 
                       plugin='Rcpp')

结果如预期.

> dtest1(1:3) 
[1] 0.241970725 0.053990967 0.004431848

但是,如果我尝试创建一个函数

However, if I try to make a function

dtest2 = cxxfunction(signature( x = "numeric", y="numeric", z="numeric" ),
                  'Rcpp::NumericVector xx(x);
                   Rcpp::NumericVector yy(y);
                   Rcpp::NumericVector zz(z);
                   return::wrap(dnorm(xx, yy, zz));',
                   plugin='Rcpp')

这将允许我传入不同的均值和标准差会导致错误,如下所示.有没有办法制作我想要制作的功能,或者我确实需要手动编程正常密度?

which would allow me to pass in different means and standard deviations results in an error, shown below. Is there a way to make the function I am trying to make, or I do need to program the normal density manually?

错误

Error in compileCode(f, code, language = language, verbose = verbose) :   
    Compilation ERROR, function(s)/method(s) not created! file31c82bff9d7c.cpp: In function ‘SEXPREC* file31c82bff9d7c(SEXP, SEXP, SEXP)’:
file31c82bff9d7c.cpp:33:53: error: no matching function for call to 
     ‘dnorm4(Rcpp::NumericVector&, Rcpp::NumericVector&, Rcpp::NumericVector&)’
file31c82bff9d7c.cpp:33:53: note: candidates are:
/home/chris/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/stats/norm.h:106:1: 
     note: template<int RTYPE, bool NA, class T> Rcpp::stats::D0<RTYPE, NA, T> Rcpp::dnorm4(const Rcpp::VectorBase<RTYPE, NA, VECTOR>&, bool)
/home/chris/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/stats/norm.h:107:1: 
     note: template<int RTYPE, bool NA, class T> Rcpp::stats::D1<RTYPE, NA, T> Rcpp::dnorm4(const Rcpp::VectorBase<RTYPE, NA, VECTOR>&, double, bool)
/home/chris/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/stats/norm.h:108:1: 
     note: template<int RTYPE, bool NA, class T> Rcpp::stats::D2<RTYPE, NA, T> Rcpp::dnorm4(const Rcpp::VectorBase<RTYPE, NA,
In addition: Warning message:
running command '/usr/lib/R/bin/R CMD SHLIB file31c82bff9d7c.cpp 2> file31c82bff9d7c.cpp.err.txt' had status 1

推荐答案

dnorm 仅根据第一个参数进行矢量化.

The sugar dnorm is only vectorized in terms of the first argument.

为了简化(稍微复杂一些,但我们还不需要在这里关注这个),调用

To simplify (it is slightly more involved, but we don't need to concern ourselves with this yet here), the call

dnorm(xx, 0.0, 1.0)

使用重载

NumericVector dnorm( NumericVector, double, double )

第二次调用尝试使用类似

And the second call tries to use something like

NumericVector dnorm( NumericVector, NumericVector, NumericVector )

未实现.我们可以实施它,它必须在我们的优先级列表中达到足够高的位置.

which is not implemented. We could implement it, it would have to go high enough in our priority list.

与此同时,编写一个小包装器很容易,例如(这不处理参数长度等......):

In the meantime, it is easy enough to write a small wrapper, like (this does not handle argument lengths, etc ...) :

NumericVector my_dnorm( NumericVector x, NumericVector means, NumericVector sds){
    int n = x.size() ;
    NumericVector res(n) ;
    for( int i=0; i<n; i++) res[i] = R::dnorm( x[i], means[i], sds[i] ) ;
    return res ;
}

这篇关于使用 Rcpp Sugar 将均值和标准差传递给 dnorm()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆