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