首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制R中pmap输出的顺序

控制R中pmap输出的顺序
EN

Stack Overflow用户
提问于 2021-09-16 00:19:04
回答 1查看 37关注 0票数 1

我有很多统计模型要运行,并且我正在尝试使用pmap来遍历每个模型所需的变量名。我想返回模型摘要以及每个模型的额外信息。我可以运行模型并返回模型摘要和额外信息,但输出不是将额外信息和模型摘要保存在一起,而是首先返回所有额外信息,然后返回所有模型摘要。是否可以让每次迭代一起返回额外的信息和模型摘要?下面我有一个简化的例子。

代码语言:javascript
复制
    #Import library
    library(purrr)
    
    #Set up vars
    y1 <- c(runif(20, 0, 1))
    y2 <- c(runif(20, 0, 1))
    x1 <- c(rnorm(20, 0, 1))
    x2 <- c(rnorm(20, 0, 1))
    
    #Collect vars in lists
    ys <- list(y1, y2)
    xs <- list(x1, x2)
    
    #Write function with a model and "extra information"
    regressor <- function(y, x){
      #extra information
      mean_y <- mean(y) 
      cat("data:", mean_y, "\n\n")
      
      #model
      model <- lm(y ~ x)
      summary(model)
    }
    
    #Use pmap to run model over the vars
    pmap(list(ys, xs), regressor)

当我运行上面的代码时,我的输出如下所示:

代码语言:javascript
复制
>data: 0.5281057 
>
>data: 0.5522678 
>
>[[1]]
>
>Call:
>lm(formula = y ~ x)
>
>Residuals:
>     Min       1Q   Median       3Q      Max 
>-0.57284 -0.24802  0.03689  0.26913  0.47428 
>
>Coefficients:
>            Estimate Std. Error t value Pr(>|t|)    
>(Intercept)  0.54894    0.07203   7.621 4.86e-07 ***
>x           -0.12909    0.07718  -1.673    0.112    
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>Residual standard error: 0.3173 on 18 degrees of freedom
>Multiple R-squared:  0.1345,   Adjusted R-squared:  0.08643 
>F-statistic: 2.797 on 1 and 18 DF,  p-value: 0.1117
>
>
>[[2]]
>
>Call:
>lm(formula = y ~ x)
>
>Residuals:
>     Min       1Q   Median       3Q      Max 
>-0.49107 -0.18591 -0.05057  0.27710  0.50679 
>
>Coefficients:
>            Estimate Std. Error t value Pr(>|t|)    
>(Intercept)  0.54197    0.06764   8.013  2.4e-07 ***
>x           -0.05526    0.06441  -0.858    0.402    
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>Residual standard error: 0.2977 on 18 degrees of freedom
>Multiple R-squared:  0.03928,  Adjusted R-squared:  -0.01409 
>F-statistic: 0.736 on 1 and 18 DF,  p-value: 0.4022

我希望结果看起来像这样:

代码语言:javascript
复制
>[[1]]
>
>data: 0.5281057 
>
>Call:
>lm(formula = y ~ x)
>
>Residuals:
>     Min       1Q   Median       3Q      Max 
>-0.57284 -0.24802  0.03689  0.26913  0.47428 
>
>Coefficients:
>            Estimate Std. Error t value Pr(>|t|)    
>(Intercept)  0.54894    0.07203   7.621 4.86e-07 ***
>x           -0.12909    0.07718  -1.673    0.112    
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>Residual standard error: 0.3173 on 18 degrees of freedom
>Multiple R-squared:  0.1345,   Adjusted R-squared:  0.08643 
>F-statistic: 2.797 on 1 and 18 DF,  p-value: 0.1117
>
>
>[[2]]
>
>data: 0.5522678 
>
>Call:
>lm(formula = y ~ x)
>
>Residuals:
>     Min       1Q   Median       3Q      Max 
>-0.49107 -0.18591 -0.05057  0.27710  0.50679 
>
>Coefficients:
>            Estimate Std. Error t value Pr(>|t|)    
>(Intercept)  0.54197    0.06764   8.013  2.4e-07 ***
>x           -0.05526    0.06441  -0.858    0.402    
>---
>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>Residual standard error: 0.2977 on 18 degrees of freedom
>Multiple R-squared:  0.03928,  Adjusted R-squared:  -0.01409 
>F-statistic: 0.736 on 1 and 18 DF,  p-value: 0.4022
EN

回答 1

Stack Overflow用户

发布于 2021-09-16 00:30:19

让输出包含在一个块中

代码语言:javascript
复制
regressor <- function(y, x){
  #extra information
  mean_y <- mean(y) 
  #cat("data:", mean_y, "\n\n")
  
  #model
  model <- lm(y ~ x)
  smmod <- summary(model)
  smmod$information <- paste("data:", mean_y)
  list(summary = smmod, information = smmod$information)
}

[[1]]
[[1]]$summary

Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.36805 -0.22448  0.00118  0.20575  0.45271 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.538509   0.059874   8.994 4.45e-08 ***
x           0.004642   0.058710   0.079    0.938    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2676 on 18 degrees of freedom
Multiple R-squared:  0.0003473, Adjusted R-squared:  -0.05519 
F-statistic: 0.006253 on 1 and 18 DF,  p-value: 0.9378


[[1]]$information
[1] "data: 0.538674127601553"


[[2]]
[[2]]$summary

Call:
lm(formula = y ~ x)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.50855 -0.25834  0.02311  0.28248  0.40919 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.583976   0.071464   8.172 1.81e-07 ***
x           0.003352   0.072773   0.046    0.964    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3177 on 18 degrees of freedom
Multiple R-squared:  0.0001178, Adjusted R-squared:  -0.05543 
F-statistic: 0.002121 on 1 and 18 DF,  p-value: 0.9638


[[2]]$information
[1] "data: 0.583617433917243"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69201057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档