使用RCPP更快提取和LM输出的摘要
当包含在极大的循环中时,我正在尝试加快从R的摘要LM对象提取。以下是我作为RCPP加快速度的尝试。
toy-data
synthetic_LMsummary<-lapply(1:100000L,function(UU){summary(lm(rnorm(500)~replicate(2,{rnorm(500)})))})
r版本
tidy.train <- function(s,SelectedRow=3) {
out<-data.frame(
estimate=s$coefficients[, "Estimate"][SelectedRow],
std.error=s$coefficients[, "Std. Error"][SelectedRow],
statistic=s$coefficients[, "t value"][SelectedRow],
p.value=s$coefficients[, "Pr(>|t|)"][SelectedRow],
rsquared=s$r.squared
)
row.names(out) <- NULL
out
}
synthetic_LMsummary %>% purrr::map_dfr(~tidy.train(.,SelectedRow=3))
尝试RCPP版本
Rcpp::sourceCpp(code='
#include <Rcpp.h>
// [[Rcpp::export]]
using namespace std;
using namespace Rcpp;
Rcpp::NumericMatrix RcppTidy(Rcpp::List Summary_List,int mat_cols, int select) {
int mat_rows = Summary_List.length();
Rcpp::NumericMatrix Output_Mat(mat_rows,mat_cols);
for (int i = 0; i < Summary_List.length(); ++i) {
Rcpp::List SubSet=Summary_List[i];
Rcpp::NumericMatrix CoefDF=SubSet["coefficients"];
Rcpp::NumericVector Coef=CoefDF.row(select);
Rcpp::NumericVector rsquared=SubSet["adj.r.squared"];
Output_Mat(i,_) = cbind(Coef,rsquared);
}
return Output_Mat;
}
')
Tidy_synLM<-RcppTidy(synthetic_LMsummary,5L,3L)
I'm trying to speed up extraction from R's summary lm object when contained in an extremely large loop. The following is my attempt as a Rcpp to speed it up.
Toy-Data
synthetic_LMsummary<-lapply(1:100000L,function(UU){summary(lm(rnorm(500)~replicate(2,{rnorm(500)})))})
R Version
tidy.train <- function(s,SelectedRow=3) {
out<-data.frame(
estimate=s$coefficients[, "Estimate"][SelectedRow],
std.error=s$coefficients[, "Std. Error"][SelectedRow],
statistic=s$coefficients[, "t value"][SelectedRow],
p.value=s$coefficients[, "Pr(>|t|)"][SelectedRow],
rsquared=s$r.squared
)
row.names(out) <- NULL
out
}
synthetic_LMsummary %>% purrr::map_dfr(~tidy.train(.,SelectedRow=3))
Attempted Rcpp Version
Rcpp::sourceCpp(code='
#include <Rcpp.h>
// [[Rcpp::export]]
using namespace std;
using namespace Rcpp;
Rcpp::NumericMatrix RcppTidy(Rcpp::List Summary_List,int mat_cols, int select) {
int mat_rows = Summary_List.length();
Rcpp::NumericMatrix Output_Mat(mat_rows,mat_cols);
for (int i = 0; i < Summary_List.length(); ++i) {
Rcpp::List SubSet=Summary_List[i];
Rcpp::NumericMatrix CoefDF=SubSet["coefficients"];
Rcpp::NumericVector Coef=CoefDF.row(select);
Rcpp::NumericVector rsquared=SubSet["adj.r.squared"];
Output_Mat(i,_) = cbind(Coef,rsquared);
}
return Output_Mat;
}
')
Tidy_synLM<-RcppTidy(synthetic_LMsummary,5L,3L)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论