how to apply the margins() function within the map() function

I need to run a binomial logistic regression 20 times on a different subset of my dataset and calculate the marginal effect of each variable in the model for each run. What I first did was to create a dataframe consisting of all those 20 samples by using the function rep_sample_n(). Then I executed my logistic model on each sample by employing the function map(). I also obtained the model summary in the same way as can be seen in my code.

However, I also need to apply the margins() function within the map() function in order to obtain the marginal effects . This always throws the error
"Error in model[["call"]] : object of type 'closure' is not subsettable".
Is there any other way of obtaining the marginal effects for each model run?

Thanks a lot in advance for any suggestion.

library(dplyr)
library(infer)
library(purrr)
library(margins)

str(mydataframe)
'data.frame':	83 obs. of  8 variables:
 $ response       : Factor w/ 2 levels "0","1": 2 2 1 1 1 1 1 2 2 1 ...
 $ time           : int  2 4 1 3 2 4 2 3 2 4 ...
 $ distance       : num  0.2 0.7 1.8 0.7 2.7 1.5 1.9 0.8 66.3 8.9 ...
 $ densitychange  : int  0 0 0 0 0 0 0 0 -2799 -3925 ...
 $ persistTNo     : int  5 1 2 4 2 3 2 1 1 2 ...
 $ age            : int  4 7 1 3 2 4 35 3 2 5 ...
 $ fte1           : int  2 1 20 5 2 11 1 2 2 30 ...
 $ localpersistTNo: num  1 0 0 1 0 1 0 0 1 0 ...

deparse(mydataframe)
 [1] "structure(list(response = structure(c(2L, 2L, 1L, 1L, 1L, 1L, "          
 [2] "1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, "        
 [3] "1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, "        
 [4] "2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, "        
 [5] "2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, "        
 [6] "1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c(\"0\", " 
 [7] "\"1\"), class = \"factor\"), time = c(2L, 4L, 1L, 3L, 2L, 4L, 2L, "      
 [8] "3L, 2L, 4L, 3L, 4L, 2L, 3L, 3L, 4L, 1L, 2L, 5L, 1L, 2L, 3L, 1L, "        
 [9] "3L, 4L, 5L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 4L, 3L, 4L, 3L, 3L, "        
[10] "2L, 1L, 5L, 1L, 4L, 2L, 4L, 3L, 1L, 2L, 2L, 3L, 4L, 3L, 2L, 1L, "        
[11] "2L, 4L, 2L, 2L, 3L, 3L, 4L, 2L, 4L, 3L, 1L, 4L, 3L, 4L, 2L, 2L, "        
[12] "3L, 4L, 3L, 1L, 3L, 5L, 1L, 3L, 4L, 2L, 2L, 3L), distance = c(0.2, "     
[13] "0.7, 1.8, 0.7, 2.7, 1.5, 1.9, 0.8, 66.3, 8.9, 2.7, 4.4, 76.4, "          
[14] "1.3, 3.6, 1.9, 9.9, 1.2, 5.9, 21.4, 5.2, 9.9, 82.6, 8.3, 10.5, "         
[15] "11.2, 19.2, 18.2, 18.4, 10.9, 7, 7.8, 16.3, 25.1, 21.6, 90.3, "          
[16] "19.6, 30.4, 25.1, 23.6, 28.5, 40.7, 3.1, 34, 36, 48, 34.7, 63.2, "       
[17] "190, 102, 80.6, 140.5, 166, 55.6, 141.5, 126.5, 133, 61.5, 85.3, "       
[18] "58.5, 5.4, 8.4, 68.2, 7.7, 44.3, 9, 5.7, 0.3, 106, 71.8, 237, "          
[19] "193.5, 150, 49, 47.9, 28.6, 46.3, 1.1, 117.5, 184, 94.6, 161.5, "        
[20] "46.9), densitychange = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, -2799L, "       
[21] "-3925L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 147L, 0L, 0L, 1633L, "       
[22] "0L, -4660L, -368L, -30L, -100L, 1801L, -820L, 0L, 0L, 1124L, "           
[23] "-185L, -2579L, -1934L, 573L, 80L, 3669L, -1687L, -213L, -162L, "         
[24] "-2454L, 96L, 1258L, 3290L, -1626L, -3860L, 1681L, 256L, 471L, "          
[25] "-487L, -666L, -125L, 2011L, -4920L, -3679L, -526L, -1040L, -3241L, "     
[26] "0L, -1710L, -1498L, 1052L, 4144L, 618L, 0L, 0L, -1155L, -2481L, "        
[27] "-5283L, -2543L, -1114L, -1520L, 67L, -691L, -1578L, 0L, -528L, "         
[28] "200L, -3716L, -1032L, -240L), persistTNo = c(5L, 1L, 2L, 4L, "           
[29] "2L, 3L, 2L, 1L, 1L, 2L, 3L, 2L, 3L, 0L, 5L, 2L, 5L, 1L, 3L, 3L, "        
[30] "1L, 8L, 2L, 2L, 2L, 1L, 1L, 5L, 2L, 3L, 2L, 1L, 3L, 0L, 3L, 1L, "        
[31] "5L, 2L, 1L, 1L, 1L, 1L, 2L, 5L, 3L, 4L, 2L, 2L, 3L, 4L, 3L, 3L, "        
[32] "1L, 3L, 0L, 2L, 2L, 2L, 4L, 2L, 3L, 2L, 2L, 4L, 3L, 1L, 2L, 2L, "        
[33] "1L, 2L, 1L, 3L, 3L, 0L, 2L, 1L, 3L, 2L, 2L, 1L, 1L, 2L, 3L), "           
[34] "    age = c(4L, 7L, 1L, 3L, 2L, 4L, 35L, 3L, 2L, 5L, 11L, 7L, "          
[35] "    7L, 3L, 8L, 4L, 6L, 3L, 4L, 9L, 8L, 6L, 6L, 4L, 11L, 31L, "          
[36] "    6L, 7L, 3L, 3L, 2L, 5L, 5L, 6L, 11L, 3L, 14L, 19L, 3L, 3L, "         
[37] "    11L, 7L, 5L, 13L, 5L, 4L, 1L, 3L, 1L, 7L, 8L, 3L, 1L, 21L, "         
[38] "    2L, 7L, 2L, 8L, 5L, 6L, 3L, 3L, 12L, 8L, 2L, 10L, 15L, 34L, "        
[39] "    2L, 1L, 4L, 6L, 4L, 13L, 1L, 4L, 3L, 4L, 4L, 24L, 9L, 2L, "          
[40] "    7L), fte1 = c(2L, 1L, 20L, 5L, 2L, 11L, 1L, 2L, 2L, 30L, "           
[41] "    3L, 3L, 20L, 2L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 14L, 2L, 1L, "          
[42] "    4L, 3L, 13L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, "           
[43] "    1L, 1L, 3L, 4L, 1L, 2L, 1L, 8L, 4L, 1L, 1L, 1L, 1L, 2L, 1L, "        
[44] "    10L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 5L, "           
[45] "    15L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, "           
[46] "    1L, 1L), localpersistTNo = c(1, 0, 0, 1, 0, 1, 0, 0, 1, 0, "         
[47] "    0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, "           
[48] "    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, "           
[49] "    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 0, "           
[50] "    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), row.names = c(NA, "
[51] "-83L), class = \"data.frame\")"                                       

set.seed(1357)
samples20 <- mydataframe %>%
  rep_sample_n(size = 62, replace = FALSE, reps = 20)

robustness20 <- samples20 %>%
  split(samples20$replicate) %>%
  map(~ glm(response ~ age + fte1 + time +
              distance + densitychange + persistTNo + localpersistTNo, 
              family = "binomial", data = .)) %>%
  map(summary) %>%
  map(margins::margins(summary))
Error in model[["call"]] : object of type 'closure' is not subsettable

Can you modifiy your reprex to include your data ?

It will allow us to reproduce the error on our hand. Currently, mydataframe is unknow and I can rerun your code.

I have just added mydataframe. It is a small dataset, consisting of 83 observations of 8 variables. I hope this will help you to rerun my code.
Thank you very much.

margins::margins should be call on the model result, according to the doc. Here you try to call on summary. You'll also passing summary directly in margins in addition to the pipe. Not sure what it means.

Using tidyverse, and all in tibble approach, I would do something like this where every results is in a column, and you can access (or unnest) what you need for further analysis.

library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(infer)
library(purrr)
library(margins)
data

About the data, you need to add them in post so that it can be reused by other. deparse or dput is ok if you paste the result in a way it can be copy-paste. See the FAQ for other way to do this (like with datapasta :package:)

tab <- structure(list(
  response = structure(c(2L, 2L, 1L, 1L, 1L, 1L,           
                         1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L,         
                         1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,         
                         2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,         
                         2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,         
                         1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), 
                       .Label = c("0", "1"), 
                       class = "factor"), 
  time = c(2L, 4L, 1L, 3L, 2L, 4L, 2L,       
           3L, 2L, 4L, 3L, 4L, 2L, 3L, 3L, 4L, 1L, 2L, 5L, 1L, 2L, 3L, 1L,         
           3L, 4L, 5L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 4L, 3L, 4L, 3L, 3L,         
           2L, 1L, 5L, 1L, 4L, 2L, 4L, 3L, 1L, 2L, 2L, 3L, 4L, 3L, 2L, 1L,         
           2L, 4L, 2L, 2L, 3L, 3L, 4L, 2L, 4L, 3L, 1L, 4L, 3L, 4L, 2L, 2L,         
           3L, 4L, 3L, 1L, 3L, 5L, 1L, 3L, 4L, 2L, 2L, 3L), 
  distance = c(0.2,      
               0.7, 1.8, 0.7, 2.7, 1.5, 1.9, 0.8, 66.3, 8.9, 2.7, 4.4, 76.4,           
               1.3, 3.6, 1.9, 9.9, 1.2, 5.9, 21.4, 5.2, 9.9, 82.6, 8.3, 10.5,          
               11.2, 19.2, 18.2, 18.4, 10.9, 7, 7.8, 16.3, 25.1, 21.6, 90.3,           
               19.6, 30.4, 25.1, 23.6, 28.5, 40.7, 3.1, 34, 36, 48, 34.7, 63.2,       
               190, 102, 80.6, 140.5, 166, 55.6, 141.5, 126.5, 133, 61.5, 85.3,        
               58.5, 5.4, 8.4, 68.2, 7.7, 44.3, 9, 5.7, 0.3, 106, 71.8, 237,           
               193.5, 150, 49, 47.9, 28.6, 46.3, 1.1, 117.5, 184, 94.6, 161.5,         
               46.9), 
  densitychange = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, -2799L,        
                    -3925L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 147L, 0L, 0L, 1633L,        
                    0L, -4660L, -368L, -30L, -100L, 1801L, -820L, 0L, 0L, 1124L,            
                    -185L, -2579L, -1934L, 573L, 80L, 3669L, -1687L, -213L, -162L,          
                    -2454L, 96L, 1258L, 3290L, -1626L, -3860L, 1681L, 256L, 471L,           
                    -487L, -666L, -125L, 2011L, -4920L, -3679L, -526L, -1040L, -3241L,      
                    0L, -1710L, -1498L, 1052L, 4144L, 618L, 0L, 0L, -1155L, -2481L,         
                    -5283L, -2543L, -1114L, -1520L, 67L, -691L, -1578L, 0L, -528L,          
                    200L, -3716L, -1032L, -240L), 
  persistTNo = c(5L, 1L, 2L, 4L,            
                 2L, 3L, 2L, 1L, 1L, 2L, 3L, 2L, 3L, 0L, 5L, 2L, 5L, 1L, 3L, 3L,         
                 1L, 8L, 2L, 2L, 2L, 1L, 1L, 5L, 2L, 3L, 2L, 1L, 3L, 0L, 3L, 1L,         
                 5L, 2L, 1L, 1L, 1L, 1L, 2L, 5L, 3L, 4L, 2L, 2L, 3L, 4L, 3L, 3L,         
                 1L, 3L, 0L, 2L, 2L, 2L, 4L, 2L, 3L, 2L, 2L, 4L, 3L, 1L, 2L, 2L,         
                 1L, 2L, 1L, 3L, 3L, 0L, 2L, 1L, 3L, 2L, 2L, 1L, 1L, 2L, 3L),          
  age = c(4L, 7L, 1L, 3L, 2L, 4L, 35L, 3L, 2L, 5L, 11L, 7L,           
          7L, 3L, 8L, 4L, 6L, 3L, 4L, 9L, 8L, 6L, 6L, 4L, 11L, 31L,           
          6L, 7L, 3L, 3L, 2L, 5L, 5L, 6L, 11L, 3L, 14L, 19L, 3L, 3L,          
          11L, 7L, 5L, 13L, 5L, 4L, 1L, 3L, 1L, 7L, 8L, 3L, 1L, 21L,          
          2L, 7L, 2L, 8L, 5L, 6L, 3L, 3L, 12L, 8L, 2L, 10L, 15L, 34L,         
          2L, 1L, 4L, 6L, 4L, 13L, 1L, 4L, 3L, 4L, 4L, 24L, 9L, 2L,           
          7L), 
  fte1 = c(2L, 1L, 20L, 5L, 2L, 11L, 1L, 2L, 2L, 30L,            
           3L, 3L, 20L, 2L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 14L, 2L, 1L,           
           4L, 3L, 13L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L,            
           1L, 1L, 3L, 4L, 1L, 2L, 1L, 8L, 4L, 1L, 1L, 1L, 1L, 2L, 1L,         
           10L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 5L,            
           15L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,            
           1L, 1L), 
  localpersistTNo = c(1, 0, 0, 1, 0, 1, 0, 0, 1, 0,          
                      0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1,            
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,            
                      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 0,            
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), 
  row.names = c(NA, -83L), class = "data.frame")

set.seed(1357)
samples20 <- tab %>%
  rep_sample_n(size = 62, replace = FALSE, reps = 20)

robustness20 <- samples20 %>%
  group_nest() %>%
  mutate(glm = map(data, ~ glm(response ~ age + fte1 + time +
                    distance + densitychange + persistTNo + localpersistTNo, 
                  family = "binomial", data = .x)),
         # summary = map(glm, summary),
         margins = map2(glm, data, ~ margins::margins(.x, data = .y))
  )

robustness20
#> # A tibble: 20 x 4
#>    replicate data              glm    margins            
#>        <int> <list>            <list> <list>             
#>  1         1 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  2         2 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  3         3 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  4         4 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  5         5 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  6         6 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  7         7 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  8         8 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#>  9         9 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 10        10 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 11        11 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 12        12 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 13        13 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 14        14 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 15        15 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 16        16 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 17        17 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 18        18 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 19        19 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>
#> 20        20 <tibble [62 x 8]> <glm>  <df[,26] [62 x 26]>

robustness20 %>%
  tidyr::unnest(margins)
#> # A tibble: 1,240 x 27
#>    replicate response  time distance densitychange persistTNo   age  fte1
#>        <int> <fct>    <int>    <dbl>         <int>      <int> <int> <int>
#>  1         1 0            2     55.6          -125          3    21    10
#>  2         1 0            2    190            1681          3     1     1
#>  3         1 0            1      1.1             0          2     4     1
#>  4         1 1            1     47.9            67          2     1     1
#>  5         1 0            2     76.4             0          3     7    20
#>  6         1 1            1     25.1          -185          0     6     2
#>  7         1 0            2    102             256          4     7     1
#>  8         1 0            4     19.6           573          5    14     1
#>  9         1 0            1      1.8             0          2     1    20
#> 10         1 1            3     34.7         -1626          2     1     4
#> # ... with 1,230 more rows, and 19 more variables: localpersistTNo <dbl>,
#> #   fitted <dbl>, se.fitted <dbl>, dydx_age <dbl>, dydx_fte1 <dbl>,
#> #   dydx_time <dbl>, dydx_distance <dbl>, dydx_densitychange <dbl>,
#> #   dydx_persistTNo <dbl>, dydx_localpersistTNo <dbl>, Var_dydx_age <dbl>,
#> #   Var_dydx_fte1 <dbl>, Var_dydx_time <dbl>, Var_dydx_distance <dbl>,
#> #   Var_dydx_densitychange <dbl>, Var_dydx_persistTNo <dbl>,
#> #   Var_dydx_localpersistTNo <dbl>, `_weights` <dbl>, `_at_number` <int>

Created on 2019-05-26 by the reprex package (v0.3.0.9000)

The map2 call is required here, because margins needs to know what is the data used with the model and can't derived it from the call directly.

Hope it helps

1 Like

The output of your code is not what I need, I'm afraid. The code for the full model of my study is below. I would like to repeat this process 20 times by using a different subset of mydataframe each time and obtain all these coefficients for each model to report in my study. I could not somehow manage to change your code to serve my purpose. I do hope that it is now clear what kind of output I am interested in. I will be very happy if you can help me in which way I should change my R code.
Thanks a lot once more.

set.seed(2715)
samples20 <- mydataframe %>%
  rep_sample_n(size = 62, replace = FALSE, reps = 20)

sample1 <- samples20 %>% filter(replicate == 1)

model1 <- glm(response ~ age + fte1 + time +
                distance + densitychange + persistTNo +
                localpersistTNo,
              data = sample1, 
              family = "binomial")

summary(model1)

Call:
glm(formula = response ~ age + fte1 + time + distance + densitychange + 
    persistTNo + localpersistTNo, family = "binomial", data = sample1)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.0025  -0.8994   0.3347   0.9002   1.9735  

Coefficients:
                  Estimate Std. Error z value Pr(>|z|)  
(Intercept)      2.9940383  1.2030374   2.489   0.0128 *
age             -0.1062477  0.0514544  -2.065   0.0389 *
fte1            -0.2538565  0.1444767  -1.757   0.0789 .
time             0.1686005  0.2824171   0.597   0.5505  
distance        -0.0035439  0.0060348  -0.587   0.5570  
densitychange    0.0002388  0.0002315   1.031   0.3024  
persistTNo      -0.6581163  0.2796150  -2.354   0.0186 *
localpersistTNo -0.9006372  0.6183884  -1.456   0.1453  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 85.886  on 61  degrees of freedom
Residual deviance: 65.350  on 54  degrees of freedom
AIC: 81.35

Number of Fisher Scoring iterations: 6

library(pscl)
pR2(model1)
        llh     llhNull          G2    McFadden        r2ML        r2CU 
-32.6750083 -42.9428615  20.5357065   0.2391050   0.2819536   0.3760687 

summary(margins(model1)) # marginal effects

          factor     AME     SE       z      p   lower   upper
             age -0.0189 0.0079 -2.3860 0.0170 -0.0344 -0.0034
   densitychange  0.0000 0.0000  1.0627 0.2879 -0.0000  0.0001
        distance -0.0006 0.0011 -0.5935 0.5529 -0.0027  0.0015
            fte1 -0.0451 0.0236 -1.9133 0.0557 -0.0913  0.0011
 localpersistTNo -0.1601 0.1029 -1.5549 0.1200 -0.3618  0.0417
      persistTNo -0.1170 0.0410 -2.8512 0.0044 -0.1974 -0.0366
            time  0.0300 0.0497  0.6034 0.5463 -0.0674  0.1273

From what I read , you have several output you need to compute from the glm model result.
You can add column to the tibble like the margin column using map to iterate on the glm column and apply your function. (summary or pR2). You need to check the result type though. You may need to use list

Otherwise for such task, Rmarkdown and parametrized report can be very useful.
You can create a rmarkdown for your analysis with one data.frame, then use parameters to change the input and rerun the all analysis on this new input. At the end you would have 1 html output for each subset, containing all the analysis results.

Some ressources

https://openscience.nuance-r.com/developer_parameterized_reports.html

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.