2
votes

I am fitting a model to each group in a dataset. I am nesting the data by the grouping variable and then using map to fit a model to each group. Then I store the tidied model information as columns in a nested tibble.

I'd like to save each of these columns as its own file, this example saves them as sheets in a excel workbook.

Is there a way to not to unnest each column individually as a new tibble? Can all columns be unnested at once to a new list of tibbles? One that can be used in other functions (like writing an excel file)?

library(tidyverse)
library(broom)
data(mtcars)
df <- mtcars

nest.df <- df %>% nest(-carb) 

results <- nest.df %>% 
  mutate(fit = map(data, ~ lm(mpg ~ wt, data=.x)),
         tidied = map(fit, tidy),
         glanced = map(fit, glance),
         augmented = map(fit, augment))


glanced.df <- results %>% 
  unnest(glanced, .drop=T)

tidied.df <- results %>% 
  unnest(tidied, .drop=T)

augmented.df <- results %>% 
  unnest(augmented, .drop=T)

myList <- list(glanced.df, tidied.df, augmented.df)
names(myList) <- c("glance", "tidy", "augment")

openxlsx::write.xlsx(myList, file = "myResults.xlsx")
1

1 Answers

1
votes

Edit

read your question again, I missed the last part. But now it should be pretty much what you are searching for.


Like this?

library(tidyverse)
library(broom)
data(mtcars)
df <- mtcars

nest.df <- df %>% nest(-carb) 

results <- nest.df %>% 
  mutate(fit = map(data, ~ lm(mpg ~ wt, data=.x)),
         tidied = map(fit, tidy),
         glanced = map(fit, glance),
         augmented = map(fit, augment))
results %>% select(glanced, tidied, augmented ) %>% 
map(~bind_rows(.))
#> $glanced
#> # A tibble: 6 x 11
#>   r.squared adj.r.squared  sigma statistic  p.value    df logLik    AIC
#>       <dbl>         <dbl>  <dbl>     <dbl>    <dbl> <int>  <dbl>  <dbl>
#> 1   0.696           0.658   2.29  18.3      2.70e-3     2 -21.4    48.7
#> 2   0.654           0.585   3.87   9.44     2.77e-2     2 -18.2    42.4
#> 3   0.802           0.777   2.59  32.3      4.62e-4     2 -22.6    51.1
#> 4   0.00295        -0.994   1.49   0.00296  9.65e-1     2  -3.80   13.6
#> 5   0               0     NaN     NA       NA           1 Inf    -Inf  
#> 6   0               0     NaN     NA       NA           1 Inf    -Inf  
#> # … with 3 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>
#> 
#> $tidied
#> # A tibble: 10 x 5
#>    term        estimate std.error statistic      p.value
#>    <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#>  1 (Intercept)   27.9       2.91     9.56     0.0000118 
#>  2 wt            -3.10      0.724   -4.28     0.00270   
#>  3 (Intercept)   44.8       6.49     6.90     0.000982  
#>  4 wt            -7.81      2.54    -3.07     0.0277    
#>  5 (Intercept)   39.4       3.09    12.7      0.00000137
#>  6 wt            -5.92      1.04    -5.68     0.000462  
#>  7 (Intercept)   17.5      22.1      0.791    0.574     
#>  8 wt            -0.312     5.73    -0.0544   0.965     
#>  9 (Intercept)   19.7     NaN      NaN      NaN         
#> 10 (Intercept)   15       NaN      NaN      NaN         
#> 
#> $augmented
#> # A tibble: 32 x 9
#>      mpg    wt .fitted .se.fit .resid  .hat .sigma .cooksd .std.resid
#>    <dbl> <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>   <dbl>      <dbl>
#>  1  21    2.62    19.7   1.17   1.25  0.263   2.38 0.0730       0.639
#>  2  21    2.88    19.0   1.04   2.04  0.205   2.29 0.129        1.00 
#>  3  14.3  3.57    16.8   0.761 -2.50  0.111   2.23 0.0838      -1.16 
#>  4  19.2  3.44    17.2   0.796  1.99  0.121   2.31 0.0594       0.929
#>  5  17.8  3.44    17.2   0.796  0.594 0.121   2.43 0.00527      0.277
#>  6  10.4  5.25    11.6   1.22  -1.20  0.283   2.39 0.0760      -0.620
#>  7  10.4  5.42    11.1   1.32  -0.663 0.333   2.43 0.0315      -0.355
#>  8  14.7  5.34    11.3   1.27   3.39  0.310   1.90 0.714        1.78 
#>  9  13.3  3.84    16.0   0.725 -2.67  0.100   2.20 0.0842      -1.23 
#> 10  15.8  3.17    18.0   0.895 -2.24  0.153   2.27 0.102       -1.06 
#> # … with 22 more rows