[Rcpp-commits] r2908 - in pkg/RcppArmadillo: . inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 21 00:07:10 CET 2011


Author: edd
Date: 2011-02-21 00:07:09 +0100 (Mon, 21 Feb 2011)
New Revision: 2908

Modified:
   pkg/RcppArmadillo/ChangeLog
   pkg/RcppArmadillo/inst/NEWS
   pkg/RcppArmadillo/src/fastLm.cpp
Log:
simpler fastLm


Modified: pkg/RcppArmadillo/ChangeLog
===================================================================
--- pkg/RcppArmadillo/ChangeLog	2011-02-20 16:23:57 UTC (rev 2907)
+++ pkg/RcppArmadillo/ChangeLog	2011-02-20 23:07:09 UTC (rev 2908)
@@ -1,3 +1,8 @@
+2011-02-20  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/fastLm.cpp (fastLm): Further simplified and shortened by using
+	Rcpp::as<arma::mat>() on the input data
+
 2011-02-18  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.2.13

Modified: pkg/RcppArmadillo/inst/NEWS
===================================================================
--- pkg/RcppArmadillo/inst/NEWS	2011-02-20 16:23:57 UTC (rev 2907)
+++ pkg/RcppArmadillo/inst/NEWS	2011-02-20 23:07:09 UTC (rev 2908)
@@ -1,3 +1,8 @@
+0.2.14  2011-ab-cd
+
+    o   fastLm code simplified further by instantiating the Armadillo matrix
+        and vector directly from the SEXP coming from R
+
 0.2.13  2011-02-18
 
     o   Upgraded to Armadillo Version 1.1.4   “Manta Lodge”

Modified: pkg/RcppArmadillo/src/fastLm.cpp
===================================================================
--- pkg/RcppArmadillo/src/fastLm.cpp	2011-02-20 16:23:57 UTC (rev 2907)
+++ pkg/RcppArmadillo/src/fastLm.cpp	2011-02-20 23:07:09 UTC (rev 2908)
@@ -24,23 +24,20 @@
 extern "C" SEXP fastLm(SEXP ys, SEXP Xs) {
 
     try {
-	Rcpp::NumericVector yr(ys);			// creates Rcpp vector from SEXP
-	Rcpp::NumericMatrix Xr(Xs);			// creates Rcpp matrix from SEXP
-	int n = Xr.nrow(), k = Xr.ncol();
+	arma::colvec y = Rcpp::as<arma::colvec>(ys);	// direct from SEXP to arma::mat
+	arma::mat X    = Rcpp::as<arma::mat>(Xs);
+	int df = X.n_rows - X.n_cols;
 
-	arma::mat X(Xr.begin(), n, k, false);   	// reuses memory and avoids extra copy
-	arma::colvec y(yr.begin(), yr.size(), false);
-
 	arma::colvec coef = arma::solve(X, y);      	// fit model y ~ X
-	arma::colvec res = y - X*coef;			// residuals
+	arma::colvec res  = y - X*coef;			// residuals
 
-	double s2 = std::inner_product(res.begin(), res.end(), res.begin(), double())/(n - k);
+	double s2 = std::inner_product(res.begin(), res.end(), res.begin(), double())/df;
 							// std.errors of coefficients
 	arma::colvec std_err = arma::sqrt(s2 * arma::diagvec( arma::pinv(arma::trans(X)*X) ));	
 
 	return Rcpp::List::create(Rcpp::Named("coefficients") = coef,
 				  Rcpp::Named("stderr")       = std_err,
-				  Rcpp::Named("df")           = n - k
+				  Rcpp::Named("df")           = df
 				  );
 
     } catch( std::exception &ex ) {



More information about the Rcpp-commits mailing list