0

我正在尝试 1. 在运行嵌套回归后计算边际效应broom,然后将输出转换为 modelsummary 可读的格式,以便使用 R Markdown 编织成 Word。

library(tidyverse)
library(broom)
library(mfx)
library(modelsummary)


year <- rep(2014:2015, length.out=10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
female <- sample(c(0,1), replace=TRUE, size=10000)
smoker <- sample(c(0,1), replace=TRUE, size=10000)
dta <- tibble(year = year, group = group, female = female, smoker = smoker)

mods <- dta %>%
  nest(data = c(-year)) %>%
  mutate(model = map(data, ~ glm(smoker ~ female*group, data = .,
                                 family = binomial(link = "probit"))),  # this is the model itself
         mfx=probitmfx(model, data=data))%>%                             # trying to convert to marginal effects 
  tidy()                                                                 # convert to readable format

modelsummary(mods)

我在每一步都会收到各种错误消息。

4

1 回答 1

1

我想到了:

mods <- dta %>%
  nest(data = c(-year)) %>%
  mutate(model = map(data, ~ glm(smoker ~ female*group, data = .,
                                 family = binomial(link = "probit"))),
         mfx=map(data, ~ probitmfx(smoker ~ female*group, data = .,
                             )))
mfx <-mods[[4]]

modelsummary(mfx)
于 2021-12-09T12:23:12.867 回答