【发布时间】:2021-11-11 00:16:39
【问题描述】:
我有很多统计模型要运行,我正在尝试使用 pmap 循环遍历每个模型所需的变量名称。我想返回模型摘要以及有关每个模型的额外信息。我能够运行模型并返回模型摘要和额外信息,但不是将额外信息和模型摘要放在一起,而是输出首先返回所有额外信息,然后返回所有模型摘要。每次迭代是否可以将额外信息和模型摘要一起返回?下面我有一个简化的例子。
#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)
当我运行上面的代码时,我的输出如下所示:
>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
我希望结果看起来像这样:
>[[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
【问题讨论】:
-
您确实意识到在将数据打印/cat 到控制台时会返回结果。除非您返回数据,否则这在任何语言中几乎都是不可能的。更改您的函数以返回
list(data = mean_y, summary = summary(model))