Search Results: "Dirk Eddelbuettel"

29 April 2011

Dirk Eddelbuettel: Slides from Rcpp workshop / master class yesterday

Romain and I just posted our slides from yesterday's Rcpp workshop and class (preceding the now-ongoing R/Finance conference). You can access the slides via my presentation page, or directly from here as Part 1 (Introduction), Part 2 (Details), Part 3 (Advanced) and Part 4 (Applications). The page also contains a link to the examples as a tar.gz and zip file.

Update: One link corrected.

25 April 2011

Dirk Eddelbuettel: RInside 0.2.4

After several months, it was time for a new release 0.2.4 of RInside which is now on CRAN. RInside is a set of convenience classes which facilitate embedding of R inside of C++ applications and programs, using the classes and functions provided by the Rcpp R and C++ integration package. This new release contains a few small internal changes to the initialization code, as well as a number of slightly improved examples employing more of the simplifications which Rcpp offers. Also included is a brand new example embedding R inside of the Qt framework; an earlier blog post had already discussed this example. Two more examples were added to the standard/examples directory which now contains fourteen plus two tests. We also added a NEWS file, the entry for this release is below.
0.2.4   2011-04-24
   o    Minor code cleanups in initialization code 
   o    New example embedding R inside a Qt application, along with pro file
        for Qt's qmake providing a complete simple C++ GUI application
   o    New examples rinside_sample 10,11  based on questions on the
        r-help and r-devel mailing list
   o    Some improvements and simplifications throughout examples/standard 
        as well as examples/mpi/
   o    Added this NEWS files -- with entries below summarised from ChangeLog
        and the corresponding blog posts
And courtesy of CRANberries, here are the changes from the previous release.
RInside-0.2.3/RInside/inst/ChangeLog only RInside-0.2.3/RInside/libRInside.a only RInside-0.2.4/RInside/ChangeLog only RInside-0.2.4/RInside/DESCRIPTION 24 ++++-- RInside-0.2.4/RInside/R/RInsidePaths.R 4 - RInside-0.2.4/RInside/doxyfile 8 +- RInside-0.2.4/RInside/inst/NEWS only RInside-0.2.4/RInside/inst/THANKS 1 RInside-0.2.4/RInside/inst/examples/mpi/rinside_mpi_sample0.cpp 10 +- RInside-0.2.4/RInside/inst/examples/mpi/rinside_mpi_sample1.cpp 11 +-- RInside-0.2.4/RInside/inst/examples/mpi/rinside_mpi_sample2.cpp 10 +- RInside-0.2.4/RInside/inst/examples/mpi/rinside_mpi_sample3.cpp 9 -- RInside-0.2.4/RInside/inst/examples/qt only RInside-0.2.4/RInside/inst/examples/standard/rinside_sample1.cpp 30 ++++---- RInside-0.2.4/RInside/inst/examples/standard/rinside_sample10.cpp only RInside-0.2.4/RInside/inst/examples/standard/rinside_sample11.cpp only RInside-0.2.4/RInside/inst/examples/standard/rinside_sample2.cpp 15 +--- RInside-0.2.4/RInside/inst/examples/standard/rinside_sample3.cpp 6 - RInside-0.2.4/RInside/inst/examples/standard/rinside_sample5.cpp 10 +- RInside-0.2.4/RInside/inst/examples/standard/rinside_sample6.cpp 12 +-- RInside-0.2.4/RInside/inst/examples/standard/rinside_sample7.cpp 9 -- RInside-0.2.4/RInside/inst/examples/standard/rinside_sample9.cpp 2 RInside-0.2.4/RInside/inst/include/RInsideAutoloads.h 35 ++++++++-- RInside-0.2.4/RInside/inst/include/RInsideEnvVars.h 13 +-- RInside-0.2.4/RInside/src/Makevars 2 RInside-0.2.4/RInside/src/RInside.cpp 21 +++--- 26 files changed, 121 insertions(+), 111 deletions(-)
More information is on the RInside page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

24 April 2011

Dirk Eddelbuettel: RcppArmadillo 0.2.19

Last Monday, Conrad Sanderson released version 1.2.10 of his most excellent Armadillo templated C++ library for linear algebra; I followed up the same day with version 0.2.19 of our RcppArmadillo wrapper for R based on our Rcpp library. However, the chief gatekeeper at CRAN took a well-deserved vacation last week, and with that it arrived only today on CRAN. The short NEWS file extract follows below containing just Conrad's entry for 1.2.0. No further changes from our side (and the benchmark comparison between R, compiled R and Rcpp for computing a VAR(1) dataset which I blogged about yesterday is only in SVN and will be in the next release.)
0.2.19	2011-04-18
    o	Upgraded to Armadillo version 1.2.0 "Unscrupulous Carbon Emitter"
	  * Added ability to use Blas & Lapack libraries with capitalised
	    function names 
	  * Reduction of pedantic compiler warnings
And courtesy of CRANberries, here are the diffs to the previous release.
 RcppArmadillo-0.2.18/RcppArmadillo/inst/doc/RcppArmadillo-unitTests.tex                     only
 RcppArmadillo-0.2.19/RcppArmadillo/ChangeLog                                                   10 
 RcppArmadillo-0.2.19/RcppArmadillo/DESCRIPTION                                                 10 
 RcppArmadillo-0.2.19/RcppArmadillo/R/fastLm.R                                                   2 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/NEWS                                                   22 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/doc/Makefile                                            5 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/doc/RcppArmadillo-unitTests.pdf                     binary
 RcppArmadillo-0.2.19/RcppArmadillo/inst/doc/unitTests-results/RcppArmadillo-unitTests.html     16 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/doc/unitTests-results/RcppArmadillo-unitTests.txt      40 -
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/arma_ostream_meat.hpp           14 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/arma_ostream_proto.hpp           4 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/arma_version.hpp                 6 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/auxlib_meat.hpp                128 +--
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/blas_proto.hpp                  92 +-
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/compiler_setup.hpp              12 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/config.hpp                       3 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/constants.hpp                   20 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/diskio_meat.hpp                  2 
 RcppArmadillo-0.2.19/RcppArmadillo/inst/include/armadillo_bits/lapack_proto.hpp               344 ++++++----
 19 files changed, 460 insertions(+), 270 deletions(-)
More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

23 April 2011

Dirk Eddelbuettel: Another nice Rcpp example

While preparing my slides for the Rcpp workshop this Thursday, I had wondered about more nice examples motivating Rcpp. So I posed a quick question on the rcpp-devel list. And I received a few friendly answers. My favourite, so far, was a suggestion by Lance Bachmeier who sent me a short script which used both R and C++ (via Rcpp) to simulate a first-order vector autoregressive process (and he ensured me that it worked well enough on his graduate students). It is indeed a great example as it involves (simple) matrix multiplication in an iterative fashion. Which makes it a great example not only for Rcpp but also for our RcppArmadillo package (which wraps Conrad Sanderson's wonderful Armadillo C++ templated library for linear algebra and more). And at the same time, we can also add another look at the new and shiny R compiler I also blogged about recently. So Lance and I iterated over this a little more over email, and I now added this as a new (and initial) example file in the RcppArmadillo SVN repo. (As an aside: The newest version 0.2.19 of RcppArmadillo has been sitting in incoming at CRAN since earlier in the week while the archive maintainer takes a well-deserved vacation. It should hit the public archive within a few days, and is otherwise available too from my site.) So let's walk through the example:
R> ## parameter and error terms used throughout
R> a <- matrix(c(0.5,0.1,0.1,0.5),nrow=2)
R> e <- matrix(rnorm(10000),ncol=2)
R> ## Let's start with the R version
R> rSim <- function(coeff, errors)  
+   simdata <- matrix(0, nrow(errors), ncol(errors))
+   for (row in 2:nrow(errors))  
+     simdata[row,] = coeff %*% simdata[(row-1),] + errors[row,]
+    
+   return(simdata)
+  
R> rData <- rSim(a, e)                     # generated by R
This starts with a simple enough loop. After skipping the first row, each iteration multiplies the previous row with the parameters and adds error terms. We can then turn to the R compiler:
R> ## Now let's load the R compiler (requires R 2.13 or later)
R> suppressMessages(require(compiler))
R> compRsim <- cmpfun(rSim)
R> compRData <- compRsim(a,e)              # generated by R 'compiled'
R> stopifnot(all.equal(rData, compRData))  # checking results
Nice and easy: We load the compiler package, create a compiled function and use it. We check the results and surely enough find them to be identical. With that, time to turn to C++ using Armadillo via RcppArmadillo:
R> ## Now load 'inline' to compile C++ code on the fly
R> suppressMessages(require(inline))
R> code <- '
+   arma::mat coeff = Rcpp::as<arma::mat>(a);
+   arma::mat errors = Rcpp::as<arma::mat>(e);
+   int m = errors.n_rows; int n = errors.n_cols;
+   arma::mat simdata(m,n);
+   simdata.row(0) = arma::zeros<arma::mat>(1,n);
+   for (int row=1; row<m; row++)  
+     simdata.row(row) = simdata.row(row-1)*trans(coeff)+errors.row(row);
+    
+   return Rcpp::wrap(simdata);
+ '
R> ## create the compiled function
R> rcppSim <- cxxfunction(signature(a="numeric",e="numeric"),
+                        code,plugin="RcppArmadillo")
R> rcppData <- rcppSim(a,e)                # generated by C++ code
R> stopifnot(all.equal(rData, rcppData))   # checking results
Here we load the inline package to compile, link and load C++ snippets. We define a short C++ function in the code variable, declare a signature taking a and e as before and ask cxxfunction() to deploy the plugin for RcppArmadillo so that it and Rcpp are found during build. With that, we have a compiled function to generate data, and we once again check the result. The C++ code is pretty straightforward as well. We can instatiate Armadillo matrices directly from the R objects we pass down; we then run a similar loop building the result row by row. Now, with all the build-up, here is the final timing comparison, using the rbenchmark package:
R> ## now load the rbenchmark package and compare all three
R> suppressMessages(library(rbenchmark))
R> res <- benchmark(rcppSim(a,e),
+                  rSim(a,e),
+                  compRsim(a,e),
+                  columns=c("test", "replications", "elapsed",
+                            "relative", "user.self", "sys.self"),
+                  order="relative")
R> print(res)
            test replications elapsed relative user.self sys.self
1  rcppSim(a, e)          100   0.038   1.0000      0.04        0
3 compRsim(a, e)          100   2.011  52.9211      2.01        0
2     rSim(a, e)          100   4.148 109.1579      4.14        0
So in a real-world example involving looping and some algebra (which is of course already done by BLAS and LAPACK libraries), the new R compiler improves by more than a factor of two, cutting time from 4.14 seconds down to about 2 seconds. Yet, this still leaves the C++ solution, clocking in at a mere 38 milliseconds, ahead by a factor of over fifty relative to the new R compiler And compared to just R itself, the simple solution involving Rcpp and RcppArmadillo is almost 110 times faster. As I mentioned, I quite like this example ;-).

16 April 2011

Dirk Eddelbuettel: Dianne Reeves at the CSO

Another Friday night at the Chicago Symphony with some live jazz, and once again Dianne Reeves (wikipedia) who was in town with her quartet. They performed a fairly short set which seemed not quite as polished as some of her previous concerts we had seen. But she is still an amazing live performer, radiant and with amazing vocal chops---which made for another nice evening out.

13 April 2011

Dirk Eddelbuettel: Rcpp 0.9.4, and a paper in the Journal of Statistical Software

A brand new 0.9.4 release of Rcpp is now on CRAN and Debian. This version contains an improvement to loading and initialization of Rcpp modules, a bug fix for vectors of factors, another build issue fix as well as (per common practice with JSS) citation information for the article Rcpp: Seamless R and C++ Integration which is now Volume 40, Issue 8 in the Journal of Statistical Software (or JSS for short). The complete NEWS entry is below; more details are in the ChangeLog file in the package and on the Rcpp Changelog page.
0.9.4   2011-04-12
    o   New R function "loadRcppModules" to load Rcpp modules automatically
        from a package. This function must be called from the .onLoad function
        and works with the "RcppModules" field of the package's DESCRIPTION file
    o   The Modules example wrapped the STL std::vector received some editing
        to disambiguate some symbols the newer compilers did not like
    o   Coercing of vectors of factors is now done with an explicit callback
        to R's "as.character()" as Rf_coerceVector no longer plays along
    o   A CITATION file for the published JSS paper has been added, and 
        references were added to Rcpp-package.Rd and the different vignettes
Thanks to CRANberries, there is also a diff to the previous release 0.9.3:
Diff between Rcpp versions 0.9.3 dated 2011-04-05 and 0.9.4 dated 2011-04-12
 Rcpp-0.9.3/Rcpp/build                                              only
 Rcpp-0.9.3/Rcpp/inst/skeleton/yada.Rd                              only
 Rcpp-0.9.3/Rcpp/inst/unitTests/testRcppModule/R/Modules.R          only
 Rcpp-0.9.3/Rcpp/inst/unitTests/testRcppModule/man/yada.Rd          only
 Rcpp-0.9.4/Rcpp/ChangeLog                                             55 
 Rcpp-0.9.4/Rcpp/DESCRIPTION                                           10 
 Rcpp-0.9.4/Rcpp/NAMESPACE                                             29 
 Rcpp-0.9.4/Rcpp/R/Rcpp.package.skeleton.R                             11 
 Rcpp-0.9.4/Rcpp/R/loadRcppModules.R                                only
 Rcpp-0.9.4/Rcpp/inst/CITATION                                      only
 Rcpp-0.9.4/Rcpp/inst/NEWS                                             17 
 Rcpp-0.9.4/Rcpp/inst/doc/Makefile                                      2 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-FAQ.pdf                              binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw                       153 +-
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-extending.pdf                        binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-extending/Rcpp-extending.Rnw             2 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-introduction.Rnw                       734 ++++------
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-introduction.pdf                     binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-modules.pdf                          binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw                36 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-package.pdf                          binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-package/Rcpp-package.Rnw                 4 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-quickref.pdf                         binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-quickref/Rcpp-quickref.Rnw              34 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-sugar.pdf                            binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw                     2 
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp-unitTests.pdf                        binary
 Rcpp-0.9.4/Rcpp/inst/doc/Rcpp.bib                                     73 
 Rcpp-0.9.4/Rcpp/inst/doc/unitTests-results/Rcpp-unitTests.html        18 
 Rcpp-0.9.4/Rcpp/inst/doc/unitTests-results/Rcpp-unitTests.txt         40 
 Rcpp-0.9.4/Rcpp/inst/include/Rcpp/config.h                             2 
 Rcpp-0.9.4/Rcpp/inst/skeleton/zzz.R                                   11 
 Rcpp-0.9.4/Rcpp/inst/unitTests/runit.Module.client.package.R          27 
 Rcpp-0.9.4/Rcpp/inst/unitTests/runit.Vector.R                         18 
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/DESCRIPTION              2 
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/NAMESPACE                3 
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/R/zzz.R              only
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/src/rcpp_module.cpp      6 
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/src/stdVector.cpp       18 
 Rcpp-0.9.4/Rcpp/inst/unitTests/testRcppModule/tests/modules.R         21 
 Rcpp-0.9.4/Rcpp/man/Rcpp-package.Rd                                    6 
 Rcpp-0.9.4/Rcpp/man/loadRcppModules.Rd                             only
 Rcpp-0.9.4/Rcpp/src/r_cast.cpp                                         9 
 43 files changed, 647 insertions(+), 696 deletions(-)
As always, even fuller details are on the Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

Dirk Eddelbuettel: RProtoBuf 0.2.3

A maintenance release 0.2.3 of RProtoBuf is now on CRAN. RProtoBuf provides GNU R bindings for the Google Protobuf data encoding library used and released by Google. The NEWS file entry follows below:
0.2.3   2011-04-12
   o    Protect UINT64 and INT64 with '#ifdef RCPP_HAS_LONG_LONG' which
        itself is valid with either g++ <= 4.4, or newer versions if the 
        -std=c++0x is used
    o   The documentation Makefile now uses the $R_HOME environment variable
    o   The documentation Makefile no longer calls clean in the all target
And courtesy of CRANberries, here is the diff to the previous release 0.2.2:
Diff between RProtoBuf versions 0.2.2 dated 2011-01-12 and 0.2.3 dated 2011-04-13
 RProtoBuf-0.2.2/RProtoBuf/inst/doc/RProtoBuf-quickref.Rnw                      only
 RProtoBuf-0.2.3/RProtoBuf/ChangeLog                                               23 
 RProtoBuf-0.2.3/RProtoBuf/DESCRIPTION                                              8 
 RProtoBuf-0.2.3/RProtoBuf/R/with.R                                                12 
 RProtoBuf-0.2.3/RProtoBuf/cleanup                                                  3 
 RProtoBuf-0.2.3/RProtoBuf/inst/NEWS                                               10 
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/Makefile                                       42 
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/RProtoBuf-quickref.pdf                      binary
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/RProtoBuf-unitTests.pdf                     binary
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/RProtoBuf.pdf                                1267 +++++-----
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/unitTests-results/RProtoBuf-unitTests.html     20 
 RProtoBuf-0.2.3/RProtoBuf/inst/doc/unitTests-results/RProtoBuf-unitTests.txt      14 
 RProtoBuf-0.2.3/RProtoBuf/src/extractors.cpp                                      44 
 RProtoBuf-0.2.3/RProtoBuf/src/mutators.cpp                                        55 
 RProtoBuf-0.2.3/RProtoBuf/src/wrapper_FieldDescriptor.cpp                         24 
 15 files changed, 819 insertions(+), 703 deletions(-)
As always, there is more information at the RProtoBuf page which has a draft package vignette, a 'quick' overview vignette and a unit test summary vignette. Questions, comments etc should go to the rprotobuf mailing list off the RProtoBuf page at R-Forge.

12 April 2011

Dirk Eddelbuettel: The new R compiler package in R 2.13.0: Some first experiments

R 2.13.0 will be released tomorrow. It contains a new package by R Core member Luke Tierney: compiler. What a simple and straightforward name, and something that Luke has been working on for several years. The NEWS file says
    o Package compiler is now provided as a standard package.  See
      ?compiler::compile for information on how to use the compiler.
      This package implements a byte code compiler for R: by default
      the compiler is not used in this release.  See the 'R
      Installation and Administration Manual' for how to compile the
      base and recommended packages.
so the compiler is not yet used for R's own base and recommended packages. While working on my slides for the upcoming Rcpp workshop preceding R/Finance 2011, I thought of a nice test example to illustrate the compiler. Last summer and fall, Radford Neal had sent a very nice, long and detailed list of possible patches for R performance improvements to the development list. Some patches were integrated and some more discussion ensued. One strand was on the difference in parsing between normal parens and curly braces. In isolation, these seem too large, but (as I recall) this is due some things 'deep down' in the parser. However, some folks really harped on this topic. And it just doesn't die as a post from last week demonstrated once more. Last year, Christian Robert had whittled it down to a set of functions, and I made a somewhat sarcastic post argueing that I'd rather use Rcpp to get 80-fold speed increase than spend my time argueing over ten percent changes in code that could be made faster so easily. So let us now examine what the compiler package can do for us. The starting point is the same as last year: five variants of computing 1/(1+x) for a scalar x inside an explicit for loop. Real code would never do it this way as vectorisation comes to the rescue. But for (Christian's) argument's sake, it is useful to highlight differences in the parser. We once again use the nice rbenchmark package to run, time and summarise alternatives:
> ## cf http://dirk.eddelbuettel.com/blog/2010/09/07#straight_curly_or_compiled
> f <- function(n, x=1) for (i in 1:n) x=1/(1+x)
> g <- function(n, x=1) for (i in 1:n) x=(1/(1+x))
> h <- function(n, x=1) for (i in 1:n) x=(1+x)^(-1)
> j <- function(n, x=1) for (i in 1:n) x= 1/ 1+x 
> k <- function(n, x=1) for (i in 1:n) x=1/ 1+x 
> ## now load some tools
> library(rbenchmark)
> ## now run the benchmark
> N <- 1e6
> benchmark(f(N,1), g(N,1), h(N,1), j(N,1), k(N,1),
+           columns=c("test", "replications",
+           "elapsed", "relative"),
+           order="relative", replications=10)
     test replications elapsed relative
5 k(N, 1)           10   9.764  1.00000
1 f(N, 1)           10   9.998  1.02397
4 j(N, 1)           10  11.019  1.12853
2 g(N, 1)           10  11.822  1.21077
3 h(N, 1)           10  14.560  1.49119
This replicates Christian's result. We find that function k() is the fastest using curlies, and that explicit exponentiation in function h() is the slowest with a relative penalty of 49%, or an absolute difference of almost five seconds between the 9.7 for the winner and 14.6 for the worst variant. On the other hand, function f(), the normal way of writing things, does pretty well. So what happens when we throw the compiler into the mix? Let's first create compiled variants using the new cmpfun() function and then try again:
> ## R 2.13.0 brings this toy
> library(compiler)
> lf <- cmpfun(f)
> lg <- cmpfun(g)
> lh <- cmpfun(h)
> lj <- cmpfun(j)
> lk <- cmpfun(k)
> # now run the benchmark
> N <- 1e6
> benchmark(f(N,1), g(N,1), h(N,1), j(N,1), k(N,1),
+           lf(N,1), lg(N,1), lh(N,1), lj(N,1), lk(N,1),
+           columns=c("test", "replications",
+           "elapsed", "relative"),
+           order="relative", replications=10)
       test replications elapsed relative
9  lj(N, 1)           10   2.971  1.00000
10 lk(N, 1)           10   2.980  1.00303
6  lf(N, 1)           10   2.998  1.00909
7  lg(N, 1)           10   3.007  1.01212
8  lh(N, 1)           10   4.024  1.35443
1   f(N, 1)           10   9.479  3.19051
5   k(N, 1)           10   9.526  3.20633
4   j(N, 1)           10  10.775  3.62673
2   g(N, 1)           10  11.299  3.80310
3   h(N, 1)           10  14.049  4.72871
Now things have gotten interesting and substantially faster, for very little cost. Usage is straightforward: take your function and compile it, and reap more than a threefold speed gain. Not bad at all. Also of note, the difference between the different expressions essentially vanishes. The explicit exponentiation is still the loser, but there may be an additional explicit function call involved. So we do see the new compiler as a potentially very useful addition. I am sure more folks will jump on this and run more tests to find clearer corner cases. To finish, we have to of course once more go back to Rcpp for some <emph>real</emph> gains, and some comparison between compiled and byte code compiled code.
> ## now with Rcpp and C++
> library(inline)
> ## and define our version in C++
> src <- 'int n = as<int>(ns);
+         double x = as<double>(xs);
+         for (int i=0; i<n; i++) x=1/(1+x);
+         return wrap(x); '
> l <- cxxfunction(signature(ns="integer",
+                            xs="numeric"),
+                  body=src, plugin="Rcpp")
> ## now run the benchmark again
> benchmark(f(N,1), g(N,1), h(N,1), j(N,1), k(N,1),
+           l(N,1),
+           lf(N,1), lg(N,1), lh(N,1), lj(N,1), lk(N,1),
+           columns=c("test", "replications",
+           "elapsed", "relative"),
+           order="relative", replications=10)
       test replications elapsed relative
6   l(N, 1)           10   0.120   1.0000
11 lk(N, 1)           10   2.961  24.6750
7  lf(N, 1)           10   3.128  26.0667
8  lg(N, 1)           10   3.140  26.1667
10 lj(N, 1)           10   3.161  26.3417
9  lh(N, 1)           10   4.212  35.1000
5   k(N, 1)           10   9.500  79.1667
1   f(N, 1)           10   9.621  80.1750
4   j(N, 1)           10  10.868  90.5667
2   g(N, 1)           10  11.409  95.0750
3   h(N, 1)           10  14.077 117.3083
Rcpp still shoots the lights out by a factor of 80 (or even almost 120 to the worst manual implementation) relative to interpreted code. Relative to the compiled byte code, the speed difference is about 25-fold. Now, these are of course entirely unrealistic code examples that are in no way, shape or form representative of real R work. Effective speed gains will be smaller for both the (pretty exciting new) compiler package and also for our C++ integration package Rcpp. Before I close, two more public service announcements. First, if you use Ubuntu see this post by Michael on r-sig-debian announcing his implementation of a suggestion of mine: we now have R alpha/beta/rc builds via his Launchpad PPA. Last Friday, I had the current R-rc snapshot of R 2.13.0 on my Ubuntu box only about six hours after I (as Debian maintainer for R) uploaded the underlying new R-rc package build to Debian unstable. This will be nice for testing of upcoming releases. Second, as I mentioned, the Rcpp workshop on April 28 preceding R/Finance 2011 on April 29 and 30 still has a few slots available, as has the conference itself.

7 April 2011

Dirk Eddelbuettel: Review of 'R in a Nutshell' by J Adler now in JSS

Volume 40 of of the (electronic) Journal of Statistical Software just came out. It contains my short review of Josef Adler's already pretty popular R in a Nutshell from O'Reilly. You can my review as a pdf via this local preprint, or by visiting the JSS page of the review and its official copy.

Dirk Eddelbuettel: RcppClassic 0.9.1

Another pure maintenance release, this time of RcppClassic, the package supporting the deprecated older classic Rcpp API defined in the earlier 2005 to 2006 releases, is now on CRAN It contains no new code, but smoothes one or two edges in the build process and noticed by the newest versions of R CMD check, just like yesterday's RcppGSL release. Courtesy of CRANberries, here are the changes to the previous release.
Diff between RcppClassic versions 0.9.0 dated 2010-12-20 and 0.9.1 dated 2011-04-07
 RcppClassic-0.9.0/RcppClassic/build                                                  only
 RcppClassic-0.9.0/RcppClassic/inst/lib                                               only
 RcppClassic-0.9.1/RcppClassic/ChangeLog                                                 17 ++++++
 RcppClassic-0.9.1/RcppClassic/DESCRIPTION                                                8 +--
 RcppClassic-0.9.1/RcppClassic/inst/doc/Makefile                                         25 ++++++----
 RcppClassic-0.9.1/RcppClassic/inst/doc/RcppClassic-unitTests.pdf                     binary
 RcppClassic-0.9.1/RcppClassic/inst/doc/RcppClassic.pdf                               binary
 RcppClassic-0.9.1/RcppClassic/inst/doc/unitTests-results/RcppClassic-unitTests.html  only
 RcppClassic-0.9.1/RcppClassic/inst/doc/unitTests-results/RcppClassic-unitTests.txt   only
 RcppClassic-0.9.1/RcppClassic/inst/unitTests/runit.RcppDate.R                           24 ++++-----
 10 files changed, 49 insertions(+), 25 deletions(-)
Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

6 April 2011

Dirk Eddelbuettel: Rcpp workshop / master class on April 28 in Chicago

I realized I never announced this on the blog, so without further ado.... Rcpp Workshop in Chicago on April 28, 2011 This year's R/Finance conference will be preceded by a full-day masterclass on Rcpp and related topics which will be held on Thursday, April 28, 2011, on the University of Illinois at Chicago campus. Join Dirk Eddelbuettel and Romain Francois for six hours of detailed and hands-on instructions and discussions around Rcpp, inline, RInside, RcppArmadillo and other packages---in an intimate small-group setting. The full-day format allows to combine a morning introductory session with a more advanced afternoon session while leaving room for sufficient breaks. There will be about six hours of instructions, a one-hour lunch break and two half-hour coffee breaks. Morning session: "A hands-on introduction to R and C++" The morning session will provide a practical introduction to the Rcpp package (and other related packages). The focus will be on simple and straightforward applications of Rcpp in order to extend R and/or to significantly accelerate the execution of simple functions. The tutorial will cover the inline package which permits embedding of self-contained C, C++ or Fortran code in R scripts. We will also discuss RInside to embed R code in C++ applications, as well as standard Rcpp extension packages such as RcppArmadillo for linear algebra and RcppGSL. Afternoon session: "Advanced R and C++ topics" This afternoon tutorial will provide a hands-on introduction to more advanced Rcpp features. It will cover topics such as writing packages that use Rcpp, how 'Rcpp modules' and the new R ReferenceClasses interact, and how 'Rcpp sugar' lets us write C++ code that is often as expressive as R code. Another possible topic, time permitting, may be writing glue code to extend Rcpp to other C++ projects. We also hope to leave some time to discuss problems brought by the class participants. Prerequisites Knowledge of R as well as general programming knowledge; C or C++ knowledge is helpful but not required. Users should bring a laptop set up so that R packages can be built. That means on Windows, Rtools needs to be present and working, and on OS X the Xcode package should be installed. Registration Registration is available via the R/Finance conference at

http://www.RinFinance.com/register/
or directly at RegOnline
http://www.regonline.com/930153
The cost is USD 500 for the whole day, and space will be limited. Questions Please contact us directly at RomainAndDirk@r-enthusiasts.com.

Dirk Eddelbuettel: RcppGSL 0.1.1

A pure maintenance release of RcppGSL---our interface package between R and the GNU GSL using our Rcpp package for seamless R and C++ integration---is now on CRAN It contains no new code, but smoothes one or two edges in the build process and noticed by the newest versions of R CMD check. It also adds vignette from the unit tests as some of our other packages do. The short NEWS file extract follows below.
0.1.1   2011-04-05
    o   Unit tests produce a summary vignette as for some of the other packages
    o   The documentation Makefile now uses the $R_HOME environment variable
    o   The documentation Makefile no longer calls clean in the all target
And courtesy of CRANberries, here are the changes to the previous release.
Diff between RcppGSL versions 0.1.0 dated 2010-12-01 and 0.1.1 dated 2011-04-06
 ChangeLog                          22 ++++++++++++++++++++
 DESCRIPTION                         8 +++----
 inst/NEWS                           8 +++++++
 inst/doc/Makefile                  28 ++++++++++++++++++++------
 inst/doc/RcppGSL-unitTests.Rnw  only
 inst/doc/RcppGSL-unitTests.pdf  only
 inst/doc/RcppGSL-unitTests.tex  only
 inst/doc/RcppGSL.pdf            binary
 inst/doc/RcppGSL/RcppGSL.Rnw       43 ++++++++++++++++++-----------------------
 inst/doc/unitTests              only
 src/Makevars.in                     2 -
 11 files changed, 75 insertions(+), 36 deletions(-)
More information is on the RcppGSL page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

5 April 2011

Dirk Eddelbuettel: Rcpp 0.9.3

A brand new and shiny 0.9.3 release of Rcpp is now on CRAN and Debian. This version contains an actual bug fix for Rcpp modules and a few build improvements, including for both clang/llvm and g++-4.6 (which, being in Debian, is already used for conformance checks on CRAN's incoming directory) and g++-4.5. We also updated the main introductory vignette Rcpp-introduction to the content of what should be a forthcoming paper in the Journal of Statistical Software. The complete NEWS entry is below; more details are in the ChangeLog file in the package and on the Rcpp Changelog page.
0.9.3   2011-04-05
    o   Fixed a bug in which modules code was not behaving when compiled 
        twice as can easily happen with inline'ed version
    o   Exceptions code includes exception_defines.h only when g++ is 4.5 or
        younger as the file no longer exists with g++-4.6
    
    o   The documentation Makefile now uses the $R_HOME environment variable
    o   The documentation Makefile no longer calls clean in the all target
    o   C++ conformance issue found by clang/llvm addressed by re-ordering
        declarations in grow.h as unqualified names must be declared before
        they are used, even when used within templates
    o   The 'long long' typedef now depends on C++0x being enabled as this
        was not a feature in C++98; this suppresses a new g++-4.5 warning
    o   The Rcpp-introduction vignette was updated to the forthcoming JSS paper
Thanks to CRANberries, there is also a diff to the previous release 0.9.2:
Diff between Rcpp versions 0.9.2 dated 2011-02-24 and 0.9.3 dated 2011-04-05
 ChangeLog                                                      60 
 DESCRIPTION                                                     8 
 R/00_classes.R                                                  5 
 R/01_show.R                                                    44 
 R/Module.R                                                      1 
 cleanup                                                         2 
 inst/NEWS                                                      23 
 inst/doc/Makefile                                              74 
 inst/doc/Rcpp-FAQ.pdf                                       binary
 inst/doc/Rcpp-FAQ/Rcpp-FAQ.Rnw                                  2 
 inst/doc/Rcpp-extending.pdf                                 binary
 inst/doc/Rcpp-introduction.Rnw                               1355 +++++++------
 inst/doc/Rcpp-introduction.pdf                              binary
 inst/doc/Rcpp-modules.pdf                                   binary
 inst/doc/Rcpp-package.pdf                                   binary
 inst/doc/Rcpp-quickref.pdf                                  binary
 inst/doc/Rcpp-sugar.pdf                                     binary
 inst/doc/Rcpp-unitTests.pdf                                 binary
 inst/doc/Rcpp.bib                                              99 
 inst/doc/jss.bst                                            only
 inst/doc/unitTests-results/Rcpp-unitTests.html                 18 
 inst/doc/unitTests-results/Rcpp-unitTests.txt                  46 
 inst/include/Rcpp/Module.h                                     40 
 inst/include/Rcpp/Vector.h                                      6 
 inst/include/Rcpp/config.h                                      2 
 inst/include/Rcpp/grow.h                                       91 
 inst/include/Rcpp/module/Module_generated_ctor_signature.h     18 
 inst/include/Rcpp/vector/RangeIndexer.h                         5 
 inst/include/RcppCommon.h                                       6 
 inst/unitTests/runit.Module.R                                  19 
 inst/unitTests/testRcppModule/src/stdVector.cpp                 7 
 src/exceptions.cpp                                              2 
 32 files changed, 1167 insertions(+), 766 deletions(-)
As always, even fuller details are on the Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

4 April 2011

Dirk Eddelbuettel: RQuantLib 0.3.7

A build-fix release RQuantLib 0.3.7 is now on CRAN and in Debian. RQuantLib combines (some of) the quantitative analytics of QuantLib with the R statistical computing environment and language. Thanks to the help by Brian Ripley (who compiled QuantLib for 64 bit Windows), Josh Ulrich (who did the same for 32 bit Windows, and arranged the Windows builds) and Uwe Ligges (who runs win-builder for R) we once again have Windows binaries as well as the usual source distribution (and Debian binaries). The only other change was minor fix to the documentation files. We had found that the pdf reference manual build would break for Uwe and Kurt (using A4 paper settings) but not myself (using letter). Uwe finally tracked that down: we had some arguments to \url with over seventy characters, and that broke typesetting. I commented those out (as the entries were in doxygen-generated QuantLib page which have volatile names anyway) and fully automated builds now resume as usual. Thanks again to Uwe for that too. No other changes were made. Thanks to CRANberries, there is also a diff to the previous release 0.3.6. Full changelog details, examples and more details about this package are at my RQuantLib page.

Dirk Eddelbuettel: RcppArmadillo 0.2.18

Conrad Sanderson made a bug-fix release (1.1.92) by for his wonderful Armadillo templated C++ library for linear algebra appeared yesterday and as usual a new release 0.2.18 of RcppArmadillo, our Rcpp-based integration into R is now on CRAN mirrors. The short NEWS file extract follows below containing just Conrad's entry for 1.1.92. No further changes from our side.
0.2.18  2011-04-03
    o   Upgraded to Armadillo Version 1.1.92  "Jurassic Barbecue"
          * Bugfix in cor()
          * Automatic installation now requires CMake >= 2.6
And courtesy of CRANberries, here are the diffs to the previous release.
 ChangeLog                                          8 ++
 DESCRIPTION                                       10 +--
 inst/NEWS                                          7 ++
 inst/doc/Makefile                                  2 
 inst/doc/RcppArmadillo-unitTests.tex           only
 inst/include/armadillo_bits/Cube_meat.hpp         12 ---
 inst/include/armadillo_bits/Cube_proto.hpp         4 -
 inst/include/armadillo_bits/Mat_meat.hpp           2 
 inst/include/armadillo_bits/Mat_proto.hpp          4 -
 inst/include/armadillo_bits/arma_version.hpp       4 -
 inst/include/armadillo_bits/glue_cor_meat.hpp     83 ++++++++++++--------------
 11 files changed, 73 insertions(+), 63 deletions(-)
More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

3 April 2011

Dirk Eddelbuettel: Race That's Good For Life 2011

Today was the 30th annual Race That's Good for Life 5k race in Oak Park. I had run this in 2003, 2004, 2005 and 2006 but not afterwards as the spring marathons got in the way. As I am now on a more relaxed (and seemingly non-marathon ;-) schedule, I was able to participate again. Really nice to leave your house fifty minutes before race start and cycle over... And kudos to the Oak Park Runner's Club who once again put a nice race together. Given the lack of (more intense) training, I am also a lot slower: 21:50.7 was the chip time, or a 7:02 pace. We will see if I ever get faster again. On race day I am usually motivated; it remains to be seen if I get over to the track in the early morning to push myself through some speedwork.

26 March 2011

Dirk Eddelbuettel: Branford Marsalis and Terence Blanchard

Wonderful jazz concert at CSO yesterday in classic double-header plus intermission format. First up was Branford Marsalis (ss,ts) with his quartet featuring Joey Calderazzo (p), Eric Revis (b) and Justin Faulkner (dr) and they simply blew the roof off the place. Best set I heard in a while: dynamic, joyful, playful, modern, whathaveyou. Really really nice. The second set was the Terence Blanchard (tp) quintet featuring Brice Winston (ts), Fabian Almazan (p), Joe Sanders (b) and Kendrick Scott (dr) who also played a nice set, but as far as I am concerned did not gel as a much as the spectacular first set. Great evening.

25 March 2011

Dirk Eddelbuettel: R inside Qt: A simple RInside application

The RInside package makes it pretty simple and straightforward to embed R, the wonderful statistical programming environment and language, inside of a C++ application. This uses both the robust embedding API provided by R itself, and the higher-level abstractions from our Rcpp package. A number of examples are shown on this blog both here and here as well as on the RInside page; and the source package actually contains well over a dozen complete examples which cover anything from simple examples to parallel use via MPI for parallel computing. Beginning users sometimes ask about how to use RInside inside larger projects. And as I had meant to experiment with embedding inside of the powerful Qt framework anyway, I started to dabble a little. A first result is now in the SVN sources of RInside. My starting point was the classic tkdensity demo that comes with R itself. It is a good point of departure as Tcl/Tk makes it very portable---in fact it should run on every platform that runs R---and quite expressive. And having followed some of the GUI experiments around R over the years, I have also seen various re-implementations using different GUI frameworks. And so I am adding mine to this body of work: Example of embedding R via RInside into a Qt C++ application: density estimation for a mixture The problem I addressed first was actual buildability. For the RInside examples, Romain and I provide a Makefile that just works by making calls to R itself to learn about flags for R, Rcpp and RInside such that all required headers and libraries are found. That is actually relatively straightforward (and documented in our vignettes) but a little intimidating at first---which is why a ready-made Makefile is a good thing. Qt of course uses qmake and the .pro files to encode / resolve dependencies. So task one was to map what our Makefile does into its variables. Turns out that wasn't all that hard:
## -*- mode: Makefile; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
##
## Qt usage example for RInside, inspired by the standard 'density
## sliders' example for other GUI toolkits
##
## Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
TEMPLATE =              app
HEADERS =               qtdensity.h 
SOURCES =               qtdensity.cpp main.cpp
QT +=                   svg
## comment this out if you need a different version of R, 
## and set set R_HOME accordingly as an environment variable
R_HOME =                $$system(R RHOME)
## include headers and libraries for R 
RCPPFLAGS =             $$system($$R_HOME/bin/R CMD config --cppflags)
RLDFLAGS =              $$system($$R_HOME/bin/R CMD config --ldflags)
RBLAS =                 $$system($$R_HOME/bin/R CMD config BLAS_LIBS)
RLAPACK =               $$system($$R_HOME/bin/R CMD config LAPACK_LIBS)
## if you need to set an rpath to R itself, also uncomment
#RRPATH =               -Wl,-rpath,$$R_HOME/lib
## include headers and libraries for Rcpp interface classes
RCPPINCL =              $$system($$R_HOME/bin/Rscript -e \'Rcpp:::CxxFlags\(\)\')
RCPPLIBS =              $$system($$R_HOME/bin/Rscript -e \'Rcpp:::LdFlags\(\)\')
## for some reason when building with Qt we get this each time
## so we turn unused parameter warnings off
RCPPWARNING =           -Wno-unused-parameter 
## include headers and libraries for RInside embedding classes
RINSIDEINCL =           $$system($$R_HOME/bin/Rscript -e \'RInside:::CxxFlags\(\)\')
RINSIDELIBS =           $$system($$R_HOME/bin/Rscript -e \'RInside:::LdFlags\(\)\')
## compiler etc settings used in default make rules
QMAKE_CXXFLAGS +=       $$RCPPWARNING $$RCPPFLAGS $$RCPPINCL $$RINSIDEINCL
QMAKE_LFLAGS +=         $$RLDFLAGS $$RBLAS $$RLAPACK $$RCPPLIBS $$RINSIDELIBS
## addition clean targets
QMAKE_CLEAN +=          qtdensity Makefile
The double dollar signs and escaping of parentheses are a little tedious, but hey it works and expands the compiler and linker flags such that everything <emjust works="Works">. The code itself is pretty straightforward too. We instantiate the RInside object as well as the main Qt application object. We then instantiate a new object of class QtDensity that will launch the main widget; it is given a reference to the RInside object.
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Qt usage example for RInside, inspired by the standard 'density
// sliders' example for other GUI toolkits
//
// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
#include <QApplication>
#include "qtdensity.h"
int main(int argc, char *argv[])
 
    RInside R(argc, argv);  		// create an embedded R instance
    QApplication app(argc, argv);
    QtDensity qtdensity(R);
    return app.exec();
 
The definition of the main object is pretty simple: a few private variables, and a few functions to interact with the GUI and get values from the radio buttons, slider or input field---as well as functions to update the chart or re-draw the random variables.
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Qt usage example for RInside, inspired by the standard 'density
// sliders' example for other GUI toolkits
//
// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
#ifndef QTDENSITY_H
#define QTDENSITY_H
#include <RInside.h>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QLabel>
#include <QTemporaryFile>
#include <QSvgWidget>
class QtDensity : public QMainWindow
 
    Q_OBJECT
public:
    QtDensity(RInside & R);
private slots:
    void getBandwidth(int bw);
    void getKernel(int kernel);
    void getRandomDataCmd(QString txt);
    void runRandomDataCmd(void);
private:
    void setupDisplay(void);    // standard GUI boilderplate of arranging things
    void plot(void);            // run a density plot in R and update the
    void filterFile(void);      // modify the richer SVG produced by R
    QSvgWidget *m_svg;          // the SVG device
    RInside & m_R;              // reference to the R instance passed to constructor
    QString m_tempfile;         // name of file used by R for plots
    QString m_svgfile;          // another temp file, this time from Qt
    int m_bw, m_kernel;         // parameters used to estimate the density
    QString m_cmd;              // random draw command string
 ;
#endif
Lastly, no big magic in the code either (apart from the standard magic provided by RInside). A bit of standard GUI layouting, and then some functions to pick values from the inputs as well as to compute / update the output. One issue is worth mentioning. The screenshot and code show the second version of this little application. I built a first one using a standard portable network graphics (png) file. That was fine, but not crisp as png is a pixel format so I went back and experimented with scalable vector graphics (svg) instead. One can create svg output with R in a number of ways, one of which is the cairoDevice package by Michael Lawrence (who also wrote RGtk2 and good chunks of Ggobi). Now, it turns out that Qt displays the so-called SVG tiny standard whereas R creates a fuller SVG format. Some discussion with Michael reveals that one can modify the svg file suitably (which is what the function filterFile below does) and it all works. Well: almost. There is a bug (and Michael thinks it is the SVG rendering) in which the density estimate does not get clipped to the plotting region.
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Qt usage example for RInside, inspired by the standard 'density
// sliders' example for other GUI toolkits -- this time with SVG
//
// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
#include <QtGui>
#include "qtdensity.h"
QtDensity::QtDensity(RInside & R) : m_R(R)
 
    m_bw = 100;                 // initial bandwidth, will be scaled by 100 so 1.0
    m_kernel = 0;               // initial kernel: gaussian
    m_cmd = "c(rnorm(100,0,1), rnorm(50,5,1))"; // simple mixture
    m_R["bw"] = m_bw;           // pass bandwidth to R, and have R compute a temp.file name
    m_tempfile = QString::fromStdString(Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()")));
    m_svgfile = QString::fromStdString(Rcpp::as<std::string>(m_R.parseEval("sfile <- tempfile()")));
    m_R.parseEvalQ("library(cairoDevice)");
    setupDisplay();
 
void QtDensity::setupDisplay(void)   
    QWidget *window = new QWidget;
    window->setWindowTitle("Qt and RInside demo: density estimation");
    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(5, 200);
    slider->setRange(5, 200);
    QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
    spinBox->setValue(m_bw);
    QObject::connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(getBandwidth(int)));
    QLabel *cmdLabel = new QLabel("R command for random data creation");
    QLineEdit *cmdEntry = new QLineEdit(m_cmd);
    QObject::connect(cmdEntry,  SIGNAL(textEdited(QString)), this, SLOT(getRandomDataCmd(QString)));
    QObject::connect(cmdEntry,  SIGNAL(editingFinished()), this, SLOT(runRandomDataCmd()));
    QGroupBox *kernelRadioBox = new QGroupBox("Density Estimation kernel");
    QRadioButton *radio1 = new QRadioButton("&Gaussian");
    QRadioButton *radio2 = new QRadioButton("&Epanechnikov");
    QRadioButton *radio3 = new QRadioButton("&Rectangular");
    QRadioButton *radio4 = new QRadioButton("&Triangular");
    QRadioButton *radio5 = new QRadioButton("&Cosine");
    radio1->setChecked(true);
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(radio1);
    vbox->addWidget(radio2);
    vbox->addWidget(radio3);
    vbox->addWidget(radio4);
    vbox->addWidget(radio5);
    kernelRadioBox->setMinimumSize(260,140);
    kernelRadioBox->setMaximumSize(260,140);
    kernelRadioBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    kernelRadioBox->setLayout(vbox);
    QButtonGroup *kernelGroup = new QButtonGroup;
    kernelGroup->addButton(radio1, 0);
    kernelGroup->addButton(radio2, 1);
    kernelGroup->addButton(radio3, 2);
    kernelGroup->addButton(radio4, 3);
    kernelGroup->addButton(radio5, 4);
    QObject::connect(kernelGroup, SIGNAL(buttonClicked(int)), this, SLOT(getKernel(int)));
    m_svg = new QSvgWidget();
    runRandomDataCmd();         // also calls plot()
    QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
    QHBoxLayout *spinners = new QHBoxLayout;
    spinners->addWidget(spinBox);
    spinners->addWidget(slider);
    QVBoxLayout *topright = new QVBoxLayout;
    topright->addLayout(spinners);
    topright->addWidget(cmdLabel);
    topright->addWidget(cmdEntry);
    estimationBox->setMinimumSize(360,140);
    estimationBox->setMaximumSize(360,140);
    estimationBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    estimationBox->setLayout(topright);
    QHBoxLayout *upperlayout = new QHBoxLayout;
    upperlayout->addWidget(kernelRadioBox);
    upperlayout->addWidget(estimationBox);
    QHBoxLayout *svglayout = new QHBoxLayout;
    svglayout->addWidget(m_svg);
    QVBoxLayout *outer = new QVBoxLayout;
    outer->addLayout(upperlayout);
    outer->addLayout(svglayout);
    window->setLayout(outer);
    window->show();
 
void QtDensity::plot(void)  
    const char *kernelstrings[] =   "gaussian", "epanechnikov", "rectangular", "triangular", "cosine"  ;
    m_R["bw"] = m_bw;
    m_R["kernel"] = kernelstrings[m_kernel]; // that passes the string to R
    std::string cmd1 = "Cairo(width=6,height=6,pointsize=10,surface='svg',filename=tfile); "
                       "plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
    std::string cmd2 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
    std::string cmd = cmd1 + kernelstrings[m_kernel] + cmd2; // stick the selected kernel in the middle
    m_R.parseEvalQ(cmd);
    filterFile();               // we need to simplify the svg file for display by Qt 
    m_svg->load(m_svgfile);
 
void QtDensity::getBandwidth(int bw)  
    if (bw != m_bw)  
        m_bw = bw;
        plot();
     
 
void QtDensity::getKernel(int kernel)  
    if (kernel != m_kernel)  
        m_kernel = kernel;
        plot();
     
 
void QtDensity::getRandomDataCmd(QString txt)  
    m_cmd = txt;
 
void QtDensity::runRandomDataCmd(void)  
    std::string cmd = "y <- " + m_cmd.toStdString();
    m_R.parseEvalQ(cmd);
    plot();                     // after each random draw, update plot with estimate
 
void QtDensity::filterFile()  
    // cairoDevice creates richer SVG than Qt can display
    // but per Michaele Lawrence, a simple trick is to s/symbol/g/ which we do here
    QFile infile(m_tempfile);
    infile.open(QFile::ReadOnly);
    QFile outfile(m_svgfile);
    outfile.open(QFile::WriteOnly   QFile::Truncate);
    
    QTextStream in(&infile);
    QTextStream out(&outfile);
    QRegExp rx1("<symbol"); 
    QRegExp rx2("</symbol");    
    while (!in.atEnd())  
        QString line = in.readLine();
        line.replace(rx1, "<g"); // so '<symbol' becomes '<g ...'
        line.replace(rx2, "</g");// and '</symbol becomes '</g'
        out << line << "\n";
     
    infile.close();
    outfile.close();
 
What the little application does is actually somewhat neat for the few lines. One key features is that the generated data can be specified directly by an R expression which allows for mixtures (as shown, and as is the default). With that it easy to see how many points are needed in the second hump to make the estimate multi-modal, and how much of a distance between both centers is needed and so on. Obviously, the effect of the chosen kernel and bandwidth can also be visualized. And with the chart the being a support vector graphics display, we can resize and scale at will and it still looks crisp. The code (for both the simpler png variant and the svg version shown here) is in the SVN repository for RInside and will be in the next release. Special thanks to Michael Lawrence for patiently working through some svg woes with me over a few emails.

Update: Some typos fixed.

Update 2: Two URLs corrected.

23 March 2011

Dirk Eddelbuettel: RcppArmadillo 0.2.17

Another release (1.1.90) by Conrad Sanderson for his wonderful Armadillo templated C++ library for linear algebra appeared yesterday. Consequently, a new release 0.2.17 of RcppArmadillo, our Rcpp-based integration into R is now on CRAN mirrors. The short NEWS file extract follows below containing just Conrad's entry for 1.1.90. No further changes from our side.
0.2.17  2011-03-22
    o   Upgraded to Armadillo Version 1.1.90  "Inside Job"
         * Added .min() & .max(), which can provide the extremum's location
         * More robust mean(), var(), stddev()
And courtesy of CRANberries, here are the diffs to the previous release.
 ChangeLog                                                    6 
 DESCRIPTION                                                  8 
 inst/NEWS                                                    7 
 inst/doc/RcppArmadillo-unitTests.pdf                     binary
 inst/doc/unitTests-results/RcppArmadillo-unitTests.html      6 
 inst/doc/unitTests-results/RcppArmadillo-unitTests.txt      36 +-
 inst/include/armadillo_bits/Cube_meat.hpp                  112 ++++++++-
 inst/include/armadillo_bits/Cube_proto.hpp                  14 -
 inst/include/armadillo_bits/Mat_meat.hpp                    98 +++++++
 inst/include/armadillo_bits/Mat_proto.hpp                   10 
 inst/include/armadillo_bits/arma_version.hpp                 4 
 inst/include/armadillo_bits/fn_prod.hpp                     12 
 inst/include/armadillo_bits/fn_stddev.hpp                   18 -
 inst/include/armadillo_bits/fn_var.hpp                      18 -
 inst/include/armadillo_bits/glue_mixed_meat.hpp             22 -
 inst/include/armadillo_bits/op_max_meat.hpp                199 ++++++++++------
 inst/include/armadillo_bits/op_max_proto.hpp                21 +
 inst/include/armadillo_bits/op_mean_meat.hpp               154 +++++++++++-
 inst/include/armadillo_bits/op_mean_proto.hpp               22 +
 inst/include/armadillo_bits/op_min_meat.hpp                199 ++++++++++------
 inst/include/armadillo_bits/op_min_proto.hpp                19 +
 inst/include/armadillo_bits/op_stddev_meat.hpp              44 ++-
 inst/include/armadillo_bits/op_stddev_proto.hpp              9 
 inst/include/armadillo_bits/op_var_meat.hpp                198 +++++++++++----
 inst/include/armadillo_bits/op_var_proto.hpp                14 -
 25 files changed, 925 insertions(+), 325 deletions(-)
More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

22 March 2011

Dirk Eddelbuettel: 2011 March Madness Half Marathon in Cary

On Sunday the annual March Madness Half Marathon in Cary took place, which (at least informally) opens the running season in greater Chicago. Following my participation in 2005. 2006, 2007, 2008, 2009). and 2010, I kept that streak going. Weather was pretty bad: A forecast of rain materialized with a shower at the start, another at about the halfway mark and a more solid downpour at the end, while temperature where in the lower 40s. And as I train less than I used to, I was also few just a few second slower than last year at 1:39:55 (hand-stopped) but managed to sneak in just under 100 minutes (and having taken six seconds to the starting lime, the official non-chip time has me just over that mark). All-in-all another nice half-marathon and I was appropriately sore yesterday.

Next.

Previous.