I am trying to extract information using broom::glance()
from survey
models. While I can extract coefficients and other model results from each object using broom:tidy()
, I sometimes receive an error that the object I am passing does not exist when I try to obtain information via broom::glance()
. I am trying to understand how to address the errors in using glance()
.
Specifically, it looks like any time I pass a list of survey
objects to lapply()
or purrr::map()
and call broom::glance()
within those functions - especially when piping the lists - I attain an error akin to ""Error in .svycheck(design) : object '.' not found".
In the code below, I reproduce the error repeatedly and try to pinpoint the source of the issue. It is difficult to tell if this is a consequence of purrr
, survey
, or broom
having unexpected behavior. What I am expecting is glance
to return an error that the model type I passed to it is not supported or the model information it usually provides.
# Packages
library(dplyr)
library(purrr)
library(survey)
library(broom)
# Create example list of data.frames
df_list <- data.frame(z = rnorm(20),
y = rnorm(20)) %>%
list() %>%
rep(2)
# Turn data.frames into design objects
objs_survey <- df_list %>%
purrr::map(~ survey::svydesign(
data = .x, ids = ~ 1, weights = ~ 1
))
# Fit models
mods_survey <- objs_survey %>%
purrr::map(~ survey::svyglm(formula = y ~ z, design = .))
# Works with tidy
mods_survey %>% purrr::map( ~ broom::tidy(.))
# But doesn't work with glance
# "Error in .svycheck(design) : object '.' not found"
mods_survey %>% purrr::map( ~ broom::glance(.))
# Since it looks like the issue is
# the design argument is looking for
# .x and this may be a purrr issue
# let's avoid purrr
# First try manually on a single data.frame
# 1 df: Assigning design without %>% - WORKS
survey::svyglm(formula = y ~ z,
design = objs_survey[[1]]) %>%
broom::glance()
# 1d df: Assigning design with %>% - same error
objs_survey[[1]] %>% survey::svyglm(formula = y ~ z,
design = .) %>%
broom::glance()
# Also issues with lapply - same error
lapply(objs_survey, function(.) {
survey::svyglm(formula = y ~ z,
design = .) %>%
broom::glance(.)
})