Rcpp Version 1.0.14
Loading...
Searching...
No Matches
Depends.cpp
Go to the documentation of this file.
1
2// [[Rcpp::depends(RcppArmadillo)]]
3
4#include <RcppArmadillo.h>
5
6using 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
static Vector create()
Definition Vector.h:1121
Rcpp API.
Definition algo.h:28
Argument Named(const std::string &name)
Definition Named.h:40
T as(SEXP x)
Definition as.h:151