A short presentation of Rcpp* packages is available at: https://jeanneclement.github.io/Rcpp-training/pres_Rcpp.pdf.

This tutorial is available at the following webpage: https://jeanneclement.github.io/Rcpp-training/.

All the source code is available on GitHub: https://github.com/JeanneClement/Rcpp-training/.

1 Packages to install

  • Rcpp
  • RcppGSL require GSL installed (~$ sudo apt-get install libgsl23 libgsl-dev with Debian/Ubuntu)
  • RcppArmadillo
  • datasets
  • rdist
  • rbenchmark
  • rmarkdown
  • knitr
  • kableExtra
  • magrittr

3 RcppArmadillo example

Function to center and scale the columns of a numeric matrix:

3.2 R code

## [1] -3.469447e-17 -1.137979e-16  2.268151e-17  5.551115e-18  2.428613e-17
##             [,1]         [,2]        [,3]       [,4]         [,5]
## [1,]  1.00000000  0.632458512  0.05379625 -0.5393759  0.163506516
## [2,]  0.63245851  1.000000000 -0.05137871 -0.4164714  0.006563847
## [3,]  0.05379625 -0.051378708  1.00000000 -0.4441156  0.762666664
## [4,] -0.53937594 -0.416471383 -0.44411557  1.0000000 -0.480016645
## [5,]  0.16350652  0.006563847  0.76266666 -0.4800166  1.000000000

4 Distance computation

4.1 Mathematical definition

The Euclidean distance between two points whose coordinates are \(A=(x_A, y_A)\) and \(B=(x_B,y_B)\) is given by \[\sqrt{(x_B-x_A)^2 + (y_B-y_A)^2}.\]

4.4 Data simulation

Simulation of 500 points dispersed all over the space

Coordinates of 5 points

x y
58.5 55.5
15.2 22.0
5.8 11.6
65.9 83.0
88.9 95.1

4.6 Results

Distance matrix for the 5 points

0.0 54.7 68.5 28.5 49.9
54.7 0.0 14.0 79.3 103.8
68.5 14.0 0.0 93.3 117.7
28.5 79.3 93.3 0.0 26.0
49.9 103.8 117.7 26.0 0.0

5 Simple linear regression

5.1 Mathematical definition

We have \(n\) observations of a response variable \(Y=(y_i)_{i=1,\ldots,n}\) and \(p\) explanatory variables \((X_1,\ldots,X_p)\),
such as \(X_1=(x_{11},\ldots,x_{i1},\ldots,x_{n1})'\).

We want to estimate coefficients of the linear regression \(\beta=(\beta_0,\beta_1,\ldots,\beta_p)'\) such as : \[ y_i = \beta_0 + \beta_1x_{i1}+\ldots+\beta_px_{ip}+\epsilon_i,\] where \(\epsilon_i \sim \mathcal{N}(0,\sigma^2) \ iid\).

Then \(y_i \sim \mathcal{N}(\beta_0 + \beta_1x_{i1}+\ldots+\beta_px_{ip}, \ \sigma^2)\) \(iid\) for \(i=1,\ldots,n\).

This gives in matrix writing :

\[Y = X\beta + \epsilon \] where \(X=(\mathbb{1}_n,X_1,\ldots,X_p)\) and \(\epsilon=(\epsilon_i)_{i=1,\ldots,n}\).

According to the Ordinary Least Squares (OLS) method, \(\beta\) is estimated by : \[\widehat{\beta}=\left(X'X\right)^{-1}X'y.\]

We define the residuals \[\widehat{\epsilon} = Y - X\widehat{\beta}=Y-\widehat{Y}.\]

Then residual variance is given by \[\widehat{\sigma^2}=\dfrac{\sum\limits_{i=1}^n \widehat{\epsilon_i}^2}{n-p-1}.\]

Finally the variance-covariance matrix of coefficients is estimated by \[V_{\widehat{\beta}}=\widehat{\sigma^2}\left(X'X\right)^{-1}.\]

5.4 Linear regression on trees data set

This data set provides measurements of the girth, height and volume of timber in 31 felled black cherry trees.

Volume will be considered as the variable to be explained and girths and heights as the explanatory variables. To have linear relationship between this variables, we will consider their log.

6 Log-likelihood computation

6.1 Mathematical definition

The likelihood function expresses the plausibilities of different parameter values for a given sample of data. The maximum of this function, if it exists, correspond to the combination of model parameter values that maximize the probability of drawing the sample actually obtained.

The likelihood corresponding to the previous simple linear model is given by : \[\begin{aligned} L(\beta,\sigma^2) &= \prod \limits_{i=1}^n p(y_i \ | \ \beta, \sigma^2) \\ &=\prod \limits_{i=1}^n \frac{1}{\sigma \sqrt{2\pi}}\exp{\left(-\frac{1}{2\sigma^2}(y_i-\beta_0-x_{i1}\beta_1-\ldots-x_{ip}\beta_p)^2 \right)} \end{aligned}\]

Then the log-likelihood is : \[\begin{aligned} l(\beta,\sigma^2) &= \log{\left(L(\beta,\sigma^2)\right)} \\ &= \sum \limits_{i=1}^n \ \log \left(\frac{1}{\sigma \sqrt{2\pi}}\right) -\frac{1}{2\sigma^2}(y_i-\beta_0-x_{i1}\beta_1-\ldots-x_{ip}\beta_p)^2\\ &= -n\log(\sigma)-n\frac{\log(2\pi)}{2} -\frac{1}{2\sigma^2} \sum \limits_{i=1}^n (y_i-\beta_0-x_{i1}\beta_1-\ldots-x_{ip}\beta_p)^2 \end{aligned}\]

6.2 Exercise

  1. Implement a function in C++ using RcppArmadillo to compute the log-likelihood corresponding to the previous simple linear model taking as argument the explicative variables \(X\), the response variable \(Y\), the estimated regression coefficients \(\widehat{\beta}\) and the computed residual variance \(\widehat{\sigma^2}\).

  2. Use trees data set to fit three distincts linear models with the log of the Volume as response variable and different choices of explanatory variables.
  3. Compute the log-likelihood of each model.

  4. Deduce from the values obtained which model best reflects the data.

6.6 Comparison of compilation times

We compare the speed of C++ and R functions used to compute de log-likelihood corresponding to model 3.

test elapsed relative
arma_logL 0.009 1.8
R_logL 0.005 1.0