Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poly pass through #175

Open
leachandrew opened this issue Jul 27, 2021 · 0 comments
Open

poly pass through #175

leachandrew opened this issue Jul 27, 2021 · 0 comments

Comments

@leachandrew
Copy link

Hello,

I see that you've flagged this issue in a related thread (#79) but reposting here to highlight another situation in which stats::poly() leads to an error.

Using a regression workflow with broom and the tidyverse:

test_data <- data.frame(region = sample(letters[1:3], 1000, replace = TRUE),
                        x = sample(0:1, 1000, replace = TRUE), 
                        y = sample(1:100, 1000, replace = TRUE), 
                        z = sample(0:1, 1000, replace = TRUE)) %>% 
  arrange(region)

# nested by region works fine with a basic regression
by_region <- 
  test_data %>% nest(data=-region)%>%
  mutate(mod_rat= map(data, ~ lm(x ~ y + z,data=.x)))%>%
  mutate(marginals      = map2(mod_rat, data, ~margins_summary(.x, data = .y)))

#introduce the poly term it throws an error
by_region <- 
  test_data %>% nest(data=-region)%>%
  mutate(mod_rat= map(data, ~ lm(x ~ z + poly(y,2),data=.x)))%>%
  mutate(marginals = map2(mod_rat, data, ~margins_summary(.x, data = .y)))

# add the solution of stats::poly(... and the error is resolved and we can run either a targeted marginal effects or summary.
by_region <- 
  test_data %>% nest(data=-region)%>%
  mutate(mod_rat= map(data, ~ lm(x ~ z + stats::poly(y,2),data=.x)))%>%
  mutate(marginals = map2(mod_rat, data, ~margins_summary(.x, data = .y)),
  marginal_key = map2(mod_rat, data, ~margins_summary(.x, data = .y,variables=c("z"))))
## session info for your system

#sessionInfo()
#loaded via a namespace (and not attached):
 #[1] Rcpp_1.0.6        cellranger_1.1.0  pillar_1.6.1      compiler_4.1.0    dbplyr_2.1.1      tools_4.1.0       jsonlite_1.7.2   
 #[8] lubridate_1.7.10  lifecycle_1.0.0   gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.11      reprex_2.0.0      cli_2.5.0        
#[15] rstudioapi_0.13   DBI_1.1.1         haven_2.4.1       xml2_1.3.2        withr_2.4.2       httr_1.4.2        fs_1.5.0         
#[22] generics_0.1.0    vctrs_0.3.8       hms_1.1.0         grid_4.1.0        tidyselect_1.1.1  glue_1.4.2        data.table_1.14.0
#[29] R6_2.5.0          fansi_0.5.0       readxl_1.3.1      prediction_0.3.14 modelr_0.1.8      magrittr_2.0.1    backports_1.2.1  
#[36] scales_1.1.1      ellipsis_0.3.2    MASS_7.3-54       rvest_1.0.0       assertthat_0.2.1  colorspace_2.0-1  utf8_1.2.1       
#[43] stringi_1.6.1     munsell_0.5.0     crayon_1.4.1     
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant