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
6
using namespace
Rcpp
;
7
8
// [[Rcpp::export]]
9
List
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
fastLm
List fastLm(NumericVector yr, NumericMatrix Xr)
Definition
Depends.cpp:9
Rcpp::Matrix
Definition
Matrix.h:28
Rcpp::Vector< VECSXP >
Rcpp::Vector< VECSXP >::create
static Vector create()
Definition
Vector.h:1121
Rcpp
Rcpp API.
Definition
algo.h:28
Rcpp::Named
Argument Named(const std::string &name)
Definition
Named.h:40
Rcpp::as
T as(SEXP x)
Definition
as.h:151
inst
examples
Attributes
Depends.cpp
Generated on Sun Jan 12 2025 11:21:42 for Rcpp Version 1.0.14 by
1.9.8