2# lm() via Armadillo -- improving on the previous GSL solution
 
    4# Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
 
    6# This file is part of Rcpp.
 
    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.
 
   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.
 
   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/>.
 
   21suppressMessages(require(Rcpp))
 
   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.
 
   27suppressMessages(require(inline))
 
   29lmArmadillo_old <- function() {
 
   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];
 
   37    arma::mat X(Xr.begin(), n, k, false);       // use advanced armadillo constructors
 
   38    arma::colvec y(yr.begin(), yr.size());
 
   40    arma::colvec coef = solve(X, y);            // fit model y ~ X
 
   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);
 
   46    Rcpp::NumericVector coefr(k), stderrestr(k);
 
   47    for (int i=0; i<k; i++) {                   // this is easier in RcppArmadillo with proper wrappers
 
   49        stderrestr[i] = sqrt(covmat(i,i));
 
   52    return Rcpp::List::create( Rcpp::Named( "coefficients", coefr),
 
   53                               Rcpp::Named( "stderr", stderrestr));
 
   56    ## turn into a function that R can call
 
   57    fun_old <- cxxfunction(signature(Ysexp="numeric", Xsexp="numeric"),
 
   59                       includes="#include <armadillo>",
 
   60                       plugin="RcppArmadillo")
 
   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.
 
   67lmArmadillo <- function() {
 
   70    #include <RcppArmadillo.h>
 
   71    // [[Rcpp::depends(RcppArmadillo)]]
 
   74    Rcpp::List fun(const arma::vec & y, const arma::mat & X){
 
   76        int n = X.n_rows, k = X.n_cols;
 
   78        arma::vec coef = solve(X, y);           // fit model y ~ X
 
   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);
 
   84        return Rcpp::List::create( Rcpp::Named( "coefficients") = coef,
 
   85                                   Rcpp::Named( "stderr") = sqrt(arma::diagvec(covmat)));