MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / fastLm

Function fastLm

inst/examples/Attributes/Depends.cpp:9–27  ·  view source on GitHub ↗

[[Rcpp::export]]

Source from the content-addressed store, hash-verified

7
8// [[Rcpp::export]]
9List fastLm(NumericVector yr, NumericMatrix Xr) {
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

Callers

nothing calls this directly

Calls 6

NamedFunction · 0.85
sqrtFunction · 0.50
nrowMethod · 0.45
ncolMethod · 0.45
beginMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected