rcpp

Generating same random variable in Rcpp and R

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:37:29
问题 I am converting my sampling algorithm from R to Rcpp. Output of Rcpp and R are not matching there is some bug in the Rcpp code ( and the difference is not different because of randomization). I am trying to match internal variables of Rcpp with those from R code. However, this is problematic because of randomization due to sampler from distribution. Rcpp::rbinom(1, 1, 10) rbinom(1, 1, 10) How can I make the code give same output in R and Rcpp, I mean setting a common seed from R and Rcpp? 回答1

R crashes when calling a Rcpp function in a loop

孤者浪人 提交于 2019-12-13 04:33:42
问题 So I have this Rcpp function in a .cpp file. You'll see that it is calling other custom functions that I don't show for simplicity, but those don't show any problem whatsoever. // [[Rcpp::export]] int sim_probability(float present_wealth , int time_left, int n, float mu, float sigma, float r, float gamma, float gu, float gl){ int i; int count = 0; float final_wealth; NumericVector y(time_left); NumericVector rw(time_left); for(i=0;i<n;i++){ rw = random_walk(time_left, 0); y = Y(rw, mu, sigma,

for loop crashes in Rcpp

天大地大妈咪最大 提交于 2019-12-13 04:33:36
问题 I am trying to replicate the following code in Rcpp (original pandas source from the following link- https://engineering.upside.com/a-beginners-guide-to-optimizing-pandas-code-for-speed-c09ef2c6a4d6: library(data.table) library(microbenchmark) deg2rad <- function(deg) {(deg * pi) / (180)} haversine = function(lat1, lon1, lat2, lon2) { MILES = 3959 lat1 = deg2rad(lat1) lon1 = deg2rad(lon1) lat2 = deg2rad(lat2) lon2 = deg2rad(lon2) dlat = lat2 - lat1 dlon = lon2 - lon1 a = sin(dlat/2)^2 + cos

Using Rcpp within parallel code via snow to make a cluster

穿精又带淫゛_ 提交于 2019-12-13 04:15:53
问题 I've written a function in Rcpp and compiled it with inline . Now, I want to run it in parallel on different cores, but I'm getting a strange error. Here's a minimal example, where the function funCPP1 can be compiled and runs well by itself, but cannot be called by snow 's clusterCall function. The function runs well as a single process, but gives the following error when ran in parallel: Error in checkForRemoteErrors(lapply(cl, recvResult)) : 2 nodes produced errors; first error: NULL value

dqrng with Rcpp for drawing from a normal and a binomial distribution

我怕爱的太早我们不能终老 提交于 2019-12-13 03:49:34
问题 I'm trying to learn how to draw random numbers from a normal and a binomial distribution from within a Rcpp OpenMP loop. I wrote the following code using R::rnorm and R::rbinom which I understand to be a be a don't do that. #include <RcppArmadillo.h> #include <omp.h> #include <dqrng.h> // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::depends(dqrng)]] // [[Rcpp::plugins(openmp)]] using namespace Rcpp; // [[Rcpp::export]] arma::mat my_matrix3(const arma::mat& A, const arma::mat& B) { dqrng:

attempt to apply non-function when using roxygenise(), can't find source of error

痴心易碎 提交于 2019-12-13 03:24:25
问题 I'm trying to use add documentation to my arbitrary Rcpp package using roxygen2, and I keep getting the same error message > roxygen2::roxygenise("anRpackage") First time using roxygen2. Upgrading automatically... Error in .f(.x[[i]], ...) : attempt to apply non-function In addition: Warning message: roxygen2 requires Encoding: UTF-8 I do not have ".f(.x[[i]])" anywhere in my source code, and I don't recognize the form ".f". Is this some kind of hidden function? How should I debug this error?

Rcpp cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization

浪子不回头ぞ 提交于 2019-12-13 01:54:17
问题 I am trying to duplicate the R vectorised sum in Rcpp I first try the following trouble-free code: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double call(NumericVector x){ return sum(x); } Type call(Time) > call(Time) [1] 1919853 Then an environment version, also works well, #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double call(){ Environment env = Environment::global_env(); NumericVector Time = env["Time"]; return sum(Time); } Type call() > call() [1]

Converting Eigen::MatrixXd to arma::mat and make a copy on a new object

时光毁灭记忆、已成空白 提交于 2019-12-12 22:44:33
问题 I have a function within which I want to convert an Eigen::MatrixXd object to arma::mat . I am aware of this question but I can't seem to be able to correct the behavior that I'm getting. Calling matrixxd_to_armamat from R would cause no issues, the problem is when I have this conversion in another function in C. This is a little confusing and I would like to understand what's going on. #include <RcppArmadillo.h> #include <RcppEigen.h> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends

rcpp: removing NAs in a moving window calculation

笑着哭i 提交于 2019-12-12 21:36:27
问题 My idea is to calculate several statistics in a moving window (2 by 2). For example, the code below calculate the mean value in a moving window. It works well when the input data hasn't got NA values, however gives bad results (NAs are treated as the lowest int) when NAs are in the dataset. Can you guide me how it can be improved - for example by excluding NA in these calculations? #include <RcppArmadillo.h> using namespace Rcpp; // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] Rcpp:

Forcing a serial compilation on make for R package

你。 提交于 2019-12-12 19:12:14
问题 I submitted a package to CRAN (sundialr) around 6 months back which was recently archived as it fails with the parallel version of make . The exact error message from one of CRAN maintainers is as follows This have just failed to install for me with a parallel make: g++ -std=gnu++98 -std=gnu++98 -shared -L/data/blackswan/ripley/extras/lib64 -L/usrlocal/lib64 -o sundialr.so cvode.o RcppExports.o -L/data/blackswan/ripley/R/R-patched/lib -lRlapack -L/data/blackswan/ripley/R/R-patched/lib -lRblas