Stata_user
Stata_user

Reputation: 564

Calculate marginal effects after broom & present using modelsummary

I am trying to 1. calculate marginal effects after running a nested regression in broom and then convert the output into a format that is readable by modelsummary in order to knit into Word using R Markdown.

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)

I get all kinds of error msgs at every step.

Upvotes: 0

Views: 200

Answers (1)

Stata_user
Stata_user

Reputation: 564

I figured it out:

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)

Upvotes: 1

Related Questions