rcpp

Gibbs Sampling in R and C++

喜你入骨 提交于 2019-12-02 13:19:18
I have checked gibbs sampling in different programming languages; in R x <- rgamma(1,3,y*y+4) y <- rnorm(1,1/(x+1),1/sqrt(2*(x+1))) in c++ x = R::rgamma(3.0,1.0/(y*y+4)); y = R::rnorm(1.0/(x+1),1.0/sqrt(2*x+2)); If it uses R functions why it differs in c++ as rgamma takes no n=number of observation and it takes scale instead of rate as default input and rnorm has no n= number of observation as well. For Rcpp it is totaly different such as; y = ::Rf_rnorm(1.0/(x+1),1.0/sqrt(2*x+2)); What's your question? R::rgamma() is from Rcpp too. It conveniently wraps the C-level, non-namespaced ::Rf_rnorm(

Is there a limit on working with matrix in R with Rcpp?

岁酱吖の 提交于 2019-12-02 12:59:07
问题 I was trying to develop a program in R to estimate a Spearman correlation with Rcpp. I did it, but it only works with matrix with less of a range between 45 00 - 50 000 vectors. I don't know why, but it only works with that dimension. I suppose there's limit with that type of information, maybe if I work it like a data.frame? I would really appreciate if someone gives me insight. Here i post my code. Ive been trying to limit the max integer number that i call "denominador", which exceeds it.

MappedSparseMatrix in RcppEigen

て烟熏妆下的殇ゞ 提交于 2019-12-02 11:57:56
I want to use conjugate gradient algorithm implemented in RcppEigen package for solving large sparse matrices. Since I am new to Rcpp and C++, I just started with the dense matrices. // [[Rcpp::depends(RcppEigen)]] #include <Rcpp.h> #include <RcppEigen.h> #include <Eigen/IterativeLinearSolvers> using Eigen::SparseMatrix; using Eigen::MappedSparseMatrix; using Eigen::Map; using Eigen::MatrixXd; using Eigen::VectorXd; using Rcpp::as; using Eigen::ConjugateGradient; typedef Eigen::MappedSparseMatrix<double> MSpMat; // [[Rcpp::export]] VectorXd getEigenValues(SEXP As, SEXP bs) { const Map<MatrixXd

Is my using gperftools to profile a R script with RCpp correct?

爷,独闯天下 提交于 2019-12-02 10:53:04
问题 I am trying to profile a R script with the last (third) approach in http://pj.freefaculty.org/blog/?p=140. I wonder if what I do (described in the following) is correct? Firstly, in my.cpp , the link says to add #include <gperftools/profiler.h> , which doesn't work for me. I found that the header is located in a different dir, and what works for me is #include <google/profiler.h> . Then I put the following around some code to be profiled ProfilerStart("./myprofile.log") //the part of the code

Difference between RInside and Rcpp

烂漫一生 提交于 2019-12-02 10:35:38
I understand RInside allows C++ program to embed R code, while Rcpp enables R code to call C++ functions or library. Are there other differences and commonalities between RInside and Rcpp? Why RInside has a namespace called Rcpp? Do developers always need both RInside and Rcpp to call R code as a class in Cpp? Rcpp : The Rcpp package provides R functions and a C++ library facilitating the integration of R and C++. RInside : The RInside package provides a few classes for seamless embedding of R inside of C++ applications by relying on Rcpp. So it appears the Rcpp is lower level and facilities

converting loop from R to C++ using Rcpp

别等时光非礼了梦想. 提交于 2019-12-02 10:28:47
I want to improve the speed of some of my R code using Rcpp. However, my knowledge of C++ is very little. So, I checked the documentation provided with Rcpp, and other documents provided at Dirk Eddelbuttel’s site. After reading all the stuff, I tried to execute a simple loop that I wrote in R. unfortunately, I was unable to do it. Here is the R function: Inverse Wishart beta = matrix(rnorm(15),ncol=3) a = rnorm(3) InW = function(beta,a) { n = nrow(beta) p = ncol(beta) I = diag(rep(1,times = p)) H = matrix(0,nrow=p,ncol=p) for(i in 1:n){ subBi = beta[i,] H = H + tcrossprod(a - subBi) } H = H +

Rcpp causes segfault RcppArmadillo does not

故事扮演 提交于 2019-12-02 09:50:37
I'm currently trying to parallelize an existing hierarchical MCMC sampling scheme. The majority of my (by now sequential) source code is written in RcppArmadillo, so I'd like to stick with this framework for parallelization, too. Before starting with parallelizing my code I have read a couple of blog posts on Rcpp/Openmp. In the majority of these blog posts (e.g. Drew Schmidt, wrathematics ) the authors warn about the issue of thread safety, R/Rcpp data structures and Openmp. The bottom line of all posts I have read so far is, R and Rcpp are not thread safe, don't call them from within an omp

Update Rcpp::NumericMatrix passed by reference using RcppArmadillo submat()

夙愿已清 提交于 2019-12-02 09:18:55
Following on this question , I am trying to understand how to efficiently update a subset of a Rccp::NumericMatrix data type. I have the following scenario: Rcpp::NumericMatrix m of 5 x 5 that needs few rows and columns updated. It will be passed by reference to a function ( void return type) that will convert it to an arma::mat , and update the respective submat() . At this point I don't understand how to " apply " the changes that occurred inside the function to the m matrix that was passed to the function. The code looks like this: #include <iostream> #include <RcppArmadillo.h> // [[Rcpp:

Fatal error for <RInside.h>

老子叫甜甜 提交于 2019-12-02 08:16:42
I am running this example, through terminal. But got fatal error: RInside.h: No such file or directory error for the the line, #include<RInside.h> . Its a interface to R from c++. I have RInside package in R. my code: #include<iostream> #include<RInside.h> using namespace std; int main(){ int a=12; cout<<a<<endl; return 0; } Same error occurred for #include<Rcpp.h> header. #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector callFunction(NumericVector x, Function f) { NumericVector res = f(x); return res; } Package RInside version 0.2.14 Package Rcpp version 0.12.17 The

Parameterisation of R::dexp

不羁的心 提交于 2019-12-02 07:48:09
I have just spent a while trying to find a bug in my code which has turned out to be an unusual (at least to me) parameterisation for the R::dexp function. For example: cppFunction(" double my_dexp(double x, double lambda, double is_log) { return R::dexp(x, lambda, is_log); } ") > my_dexp(4.5, 2.5, FALSE) [1] 0.06611956 > dexp(4.5, 2.5, FALSE) [1] 3.251824e-05 Looking here I can see that they use the definition: double R::dexp(double x, double sl, int lg) but I haven't been able to find out what sl stands for. I'm not sure if this is documented anywhere - so hopefully this post stands as a