How to use these vignettes
chapter-0.Rmd
Notation
In the textbook, the notation is of the form:
where is the dependent variable, are the independent variables, are the coefficients, and is the error term.
In these notes I use the following notation:
Code structure
For the code, I use two functions, one with the suffix _
that receives the data in C++ data types and another without the suffix that receives the data in R data types. The function with the suffix _
is the one that does the actual work, and the function without the suffix is the one that converts the data from R to C++ data types and vice versa.
[C++ data type] my_function_([C++ data type] a, [C++ data type] b) {
... C++ code ...
return result;
}
[[cpp11::register]] [R data type] my_function_r_([R data type] a,
[R data type] b) {
[C++ data type] A = as_[C++ data type](a);
[C++ data type] B = as_[C++ data type](b);
[C++ data type] result = my_function_(A, B);
return as_[R data type](result);
}
This allows to reuse functions in the C++ code more easily and also avoids overhead coping data between R and C++ multiple times.
The functions must be exposed and documented so that these can be used by the end-user:
#' Add Title
#' @param a Description
#' @param b Description
#' @return matrix, vector, etc containing the result
#' @export
my_function <- function(a, b) {
my_function_r_(a, b)
}
Please look at the code in the ‘R’ and ‘src’ folders in the package repository.
The code uses a header used across all the scripts in order to avoid importing the libraries multiple times and it allows us to use the ols_
function across all the scripts. The header is:
#include <cpp11.hpp>
#include <cpp11armadillo.hpp>
#include <unordered_map>
using namespace arma;
using namespace cpp11;
double> ols_(const Mat<double>& Y, const Mat<double>& X); Mat<