<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hello,<div><br></div><div>I had a problem this afternoon with an arma::mat constructor and the mat::insert_col method. Since I got the example from Dirk, I thought I would point out the problem I had and a solution. See</div><div><br></div><div><a href="http://gallery.rcpp.org/articles/fast-linear-model-with-armadillo/">http://gallery.rcpp.org/articles/fast-linear-model-with-armadillo/</a></div><div><br></div><div>for background. Before I get to the example to reproduce the problem and the fix, let me say I'm posting from home as I can't post code from my work email; otherwise I'm fired.</div><div><br></div><div>I do believe Rcpp::as should be used to marshall from NumericMatrix to arma::mat, not the arma::mat constructor that accesses raw memory, as Dirk uses. I read the Armadillo documentation this afternoon and do believe the problem with .insert_col is due to the handling of memory in the constructor</div><div><br></div><div><span style="font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; text-align: left; background-color: rgb(255, 255, 255); ">mat(aux_mem*, n_rows, n_cols, copy_aux_mem = true, strict = true) </span></div><div><span style="font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; text-align: left; background-color: rgb(255, 255, 255); "><br></span></div><div><span style="font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; text-align: left; background-color: rgb(255, 255, 255); ">see</span></div><div><span style="font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; text-align: left; background-color: rgb(255, 255, 255); "><br></span></div><div><a href="http://arma.sourceforge.net/docs.html">http://arma.sourceforge.net/docs.html</a></div><div><br></div><div>Note that I've run similar examples on my Windows 7 box at work and on my OS X laptop (Mountain Lion) at home to confirm this is not os-dependent.</div><div><br></div><div>Thanks,</div><div>Dale Smith</div><div><a href="mailto:dtsmith@mindspring.com">dtsmith@mindspring.com</a></div><div><br></div><div><div>mat <- cbind(rnorm(50), rnorm(50), rnorm(50))</div><div>y <- rnorm(50)</div><div><br></div><div>cppFunction('</div><div>List fastLm(NumericVector yr, NumericMatrix Xr) {</div><div>  int n = Xr.nrow(), k = Xr.ncol();</div><div>   </div><div>  arma::mat X(Xr.begin(), n, k, false); </div><div>  arma::colvec y(yr.begin(), yr.size(), false);</div><div>   </div><div>  arma::colvec coef = arma::solve(X, y);</div><div>  arma::colvec resid = y - X*coef;</div><div><br></div><div>  arma::colvec ones = arma::ones<arma::colvec>(X.n_rows);</div><div>  X.insert_cols(1, ones);</div><div>   </div><div>  double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k));</div><div>  arma::colvec stderrest = arma::sqrt(</div><div>       sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );</div><div>   </div><div>  return List::create(Named("coefficients") = coef,</div><div>                      Named("stderr")       = stderrest);</div><div>}', depends = c("RcppArmadillo"))</div><div><br></div><div><br></div><div>cppFunction('</div><div>List fastLmMod(NumericVector yr, NumericMatrix Xr) {</div><div>  int n = Xr.nrow(), k = Xr.ncol();</div><div>   </div><div>  arma::mat X = Rcpp::as<arma::mat>(Xr);</div><div>  arma::colvec y(yr.begin(), yr.size(), false);</div><div>   </div><div>  arma::colvec ones = arma::ones<arma::colvec>(X.n_rows);</div><div>  X.insert_cols(0, ones);</div><div>   </div><div>  arma::colvec coef = arma::solve(X, y);</div><div>  arma::colvec resid = y - X*coef;</div><div><br></div><div>  double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k));</div><div>  arma::colvec stderrest = arma::sqrt(</div><div>       sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );</div><div>   </div><div>  return List::create(Named("coefficients") = coef,</div><div>                      Named("stderr")       = stderrest);</div><div>}', depends = c("RcppArmadillo"))</div><div><br></div><div>fastLm(y, mat) # error is "error: Mat::init(): mismatch between size of auxiliary memory and requested size"</div><div>fastLmMod(y, mat) # coefficients are -0.15785722, -0.05668155, 0.15409366, -0.10079147</div><div>lm(y ~ mat) # confirm fastLmMod via lm function in R</div></div><div><br></div></body></html>