Rcpp Version 1.0.9
lmArmadillo.R
Go to the documentation of this file.
1 #
2 # lm() via Armadillo -- improving on the previous GSL solution
3 #
4 # Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
5 #
6 # This file is part of Rcpp.
7 #
8 # Rcpp is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # Rcpp is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20 
21 suppressMessages(require(Rcpp))
22 
23 ## NOTE: This is the old way to compile Rcpp code inline.
24 ## The code here has left as a historical artifact and tribute to the old way.
25 ## Please use the code under the "new" inline compilation section.
26 
27 suppressMessages(require(inline))
28 
29 lmArmadillo_old <- function() {
30  src <- '
31 
32  Rcpp::NumericVector yr(Ysexp);
33  Rcpp::NumericVector Xr(Xsexp);
34  std::vector<int> dims = Xr.attr("dim") ;
35  int n = dims[0], k = dims[1];
36 
37  arma::mat X(Xr.begin(), n, k, false); // use advanced armadillo constructors
38  arma::colvec y(yr.begin(), yr.size());
39 
40  arma::colvec coef = solve(X, y); // fit model y ~ X
41 
42  arma::colvec resid = y - X*coef; // to compute std. error of the coefficients
43  double sig2 = arma::as_scalar(trans(resid)*resid)/(n-k); // requires Armadillo 0.8.2 or later
44  arma::mat covmat = sig2 * arma::inv(arma::trans(X)*X);
45 
46  Rcpp::NumericVector coefr(k), stderrestr(k);
47  for (int i=0; i<k; i++) { // this is easier in RcppArmadillo with proper wrappers
48  coefr[i] = coef[i];
49  stderrestr[i] = sqrt(covmat(i,i));
50  }
51 
52  return Rcpp::List::create( Rcpp::Named( "coefficients", coefr),
53  Rcpp::Named( "stderr", stderrestr));
54  '
55 
56  ## turn into a function that R can call
57  fun_old <- cxxfunction(signature(Ysexp="numeric", Xsexp="numeric"),
58  src,
59  includes="#include <armadillo>",
60  plugin="RcppArmadillo")
61 }
62 
63 
64 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
65 ## written. Please use the code next as a template for your own project.
66 
67 lmArmadillo <- function() {
68 
69  sourceCpp(code='
70  #include <RcppArmadillo.h>
71  // [[Rcpp::depends(RcppArmadillo)]]
72 
73  // [[Rcpp::export]]
74  Rcpp::List fun(const arma::vec & y, const arma::mat & X){
75 
76  int n = X.n_rows, k = X.n_cols;
77 
78  arma::vec coef = solve(X, y); // fit model y ~ X
79 
80  arma::vec resid = y - X*coef; // to compute std. error of the coefficients
81  double sig2 = arma::as_scalar(trans(resid)*resid)/(n-k); // requires Armadillo 0.8.2 or later
82  arma::mat covmat = sig2 * arma::inv(arma::trans(X)*X);
83 
84  return Rcpp::List::create( Rcpp::Named( "coefficients") = coef,
85  Rcpp::Named( "stderr") = sqrt(arma::diagvec(covmat)));
86  }')
87 
88  fun
89 }