Rcpp Version 1.0.9
Depends.cpp
Go to the documentation of this file.
1 
2 // [[Rcpp::depends(RcppArmadillo)]]
3 
4 #include <RcppArmadillo.h>
5 
6 using namespace Rcpp;
7 
8 // [[Rcpp::export]]
10 
11  int n = Xr.nrow(), k = Xr.ncol();
12 
13  arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
14  arma::colvec y(yr.begin(), yr.size(), false);
15 
16  arma::colvec coef = arma::solve(X, y); // fit model y ~ X
17  arma::colvec resid = y - X*coef; // residuals
18 
19  double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
20  // std.error of estimate
21  arma::colvec stderrest = arma::sqrt(
22  sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
23 
24  return List::create(Named("coefficients") = coef,
25  Named("stderr") = stderrest
26  );
27 }
28 
List fastLm(NumericVector yr, NumericMatrix Xr)
Definition: Depends.cpp:9
int ncol() const
Definition: Matrix.h:94
int nrow() const
Definition: Matrix.h:97
const_iterator begin() const
Definition: Matrix.h:112
R_xlen_t size() const
Definition: Vector.h:276
iterator begin()
Definition: Vector.h:334
static Vector create()
Definition: Vector.h:1122
void sqrt(InputIterator begin, InputIterator end, OutputIterator out)
Definition: algorithm.h:479
Rcpp API.
Definition: algo.h:28
Argument Named(const std::string &name)
Definition: Named.h:40