是否可以从基数 R中获得相同sample
的整数?Rcpp
sample
我尝试过使用Rcpp::sample
,Rcpp::RcppArmadillo::sample
但它们没有返回相同的值——下面的示例代码。此外,帖子https://gallery.rcpp.org/articles/using-the-Rcpp-based-sample-implementation/的快速示例部分从基础 R 返回相同的样本,但是,我无法重现这些结果(我最后附上此代码)。Rcpp
可以这样做/请问我做错了什么?
我的尝试:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::export]]
Rcpp::IntegerVector mysamp1( int n) {
Rcpp::IntegerVector v = Rcpp::sample(n, n);
return v;
}
// [[Rcpp::export]]
Rcpp::IntegerVector mysamp2(int n) {
Rcpp::IntegerVector i = Rcpp::seq(1,n);
Rcpp::IntegerVector v = wrap(Rcpp::RcppArmadillo::sample(i,n,false));
return v;
}
// set seed https://stackoverflow.com/questions/43221681/changing-rs-seed-from-rcpp-to-guarantee-reproducibility
// [[Rcpp::export]]
void set_seed(double seed) {
Rcpp::Environment base_env("package:base");
Rcpp::Function set_seed_r = base_env["set.seed"];
set_seed_r(std::floor(std::fabs(seed)));
}
// [[Rcpp::export]]
Rcpp::IntegerVector mysamp3( int n, int seed) {
set_seed(seed);
Rcpp::IntegerVector v = Rcpp::sample(n, n);
return v;
}
/***R
set.seed(1)
sample(10)
# [1] 9 4 7 1 2 5 3 10 6 8
set.seed(1)
mysamp1(10)
# [1] 3 4 5 7 2 8 9 6 10 1
set.seed(1)
mysamp2(10)
# [1] 3 4 5 7 2 8 9 6 10 1
mysamp3(10, 1)
# [1] 3 4 5 7 2 8 9 6 10 1
*/
来自使用 RcppArmadillo-based Implementation of R's sample()库帖子的代码,该代码返回FALSE
我的系统:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
using namespace Rcpp ;
// [[Rcpp::export]]
CharacterVector csample_char( CharacterVector x,
int size,
bool replace,
NumericVector prob = NumericVector::create()) {
CharacterVector ret = RcppArmadillo::sample(x, size, replace, prob) ;
return ret ;
}
/*** R
N <- 10
set.seed(7)
sample.r <- sample(letters, N, replace=T)
set.seed(7)
sample.c <- csample_char(letters, N, replace=T)
print(identical(sample.r, sample.c))
# [1] FALSE
*/