我正在为 N(0,1) 分布编写 Metropolis-Hastings 算法:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector metropolis(int R, double b, double x0){
NumericVector y(R);
y(1) = x0;
for(int i=1; i<R; ++i){
y(i) = y(i-1);
double xs = y(i)+runif(1, -b,b)[0];
double a = dnorm(xs, 0, 1)[0]/dnorm(y(i), 0, 1)[0];
NumericVector A(2);
A(0) = 1;
A(1) = a;
double random = runif(1)[0];
if(random <= min(A)){
y[i] = xs;
}
}
return y;
}
Run Code Online (Sandbox Code Playgroud)
但每次我尝试编译该函数时,都会出现此错误:
第 12 行:没有匹配的函数来调用“dnorm4”
我尝试使用 dnorm 编写一个简单的函数,例如
NumericVector den(NumericVector y, double a, double …Run Code Online (Sandbox Code Playgroud)