5 NumericVector convolveCpp(NumericVector a, NumericVector b) {
7 int na = a.size(), nb = b.size();
9 NumericVector xab(nab);
11 for (int i = 0; i < na; i++)
12 for (int j = 0; j < nb; j++)
13 xab[i + j] += a[i] * b[j];
19 convolveCpp(c(1,2,3), matrix(3,3))
22 cppFunction(depends='RcppArmadillo', code='
23 List fastLm(NumericVector yr, NumericMatrix Xr) {
25 int n = Xr.nrow(), k = Xr.ncol();
27 arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids copy
28 arma::colvec y(yr.begin(), yr.size(), false);
30 arma::colvec coef = arma::solve(X, y); // fit model y ~ X
31 arma::colvec resid = y - X*coef; // residuals
33 double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
34 // std.error of estimate
35 arma::colvec stderrest = arma::sqrt(
36 sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
38 return List::create(Named("coefficients") = coef,
39 Named("stderr") = stderrest