I have a lot of statistical models to run and I am trying to use pmap to loop over the variable names needed for each model. I want to return the model summary along with extra information about each model. I'm able to run the models and return the model summary and extra information, but instead of keeping the extra information and the model summary together, the output returns all of the extra information first and then it returns all of the model summaries. Is it possible for each iteration to return the extra information and model summary together? I have a simplified example below.
#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)
When I run the code above my output looks like this:
>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
I want the results to look something like this:
>[[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