使用RCPP更快提取和LM输出的摘要

发布于 2025-02-02 03:48:55 字数 1491 浏览 4 评论 0原文

当包含在极大的循环中时,我正在尝试加快从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文