rdplyrpurrrbroommodelr

Access the estimate inside a model object in R


I am running a simulation and would like to know if there was a way to access the "x" estimate inside of my model using broom, dplyr, modelr, or purrr.

This gives me exactly what I want, but I don't want to use [[1]] in the last chunk of code.

library(tidyverse)
library(purrr)
library(broom)
mod <- function(df) {
  lm(y ~ x, data = df)
}
sim <- tibble(
model = "model1",
mu = 5,             #this is unknown in practice                         
beta = 2.7,         #this is unknown in practice
sigma = 0.15,       #this is unknown in practice
mu_e = 0,
sigma_e = 1
)
sim_dat <- sim %>% 
crossing(replication = 1:10000) %>%
mutate(e = rnorm(mu_e, mu_e),
       x = sample(c(0,1),size=n(),replace = TRUE,prob=c(0.5, 0.5)),
       y = mu+x*beta+e) %>% 
  group_by(model) %>% 
  nest() %>% 
  mutate(model_fit = map(data, mod)) 

broom::tidy(sim_dat$model_fit[[1]]) %>% 
  filter(term=="x") %>% 
  select(estimate)

Solution

  • you could use purrr::map_df():

    map_df(sim_dat$model_fit, broom::tidy) %>% 
        filter(term=="x") %>% 
        select(estimate)
    

    you could also drop it in mutate(model_fit = ...) like this:

    sim_dat <- sim %>% 
        crossing(replication = 1:10000) %>%
        mutate(e = rnorm(mu_e, mu_e),
               x = sample(c(0,1),size=n(),replace = TRUE,prob=c(0.5, 0.5)),
               y = mu+x*beta+e) %>% 
        group_by(model) %>% 
        nest() %>% 
        mutate(model_fit = map(data, mod),
    
               # you can pipe inside of mutate()
    
               x_coef = map_dbl(model_fit, ~broom::tidy(.) %>%
                   filter(term =="x") %>% 
                   select(estimate) %>%
                   unlist() ) ) 
    

    Depending on what class of object you want to return for x_coef, you could monkey around with the map_suffix() and possibly drop the unlist() I just thought dbl made sense.