You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems find_data() is confused when the data argument of the regression function was used with an anonymous function in a map call?
Interestingly, depending on whether one uses ~. or \(x) gives different error messages:
margins(purrr::map(list(mtcars), ~lm(mpg ~ cyl * hp + wt, data = ., x=TRUE))[[1]]) gives Error in eval(model[["call"]][["data"]], env) : object '.' not found
margins(purrr::map(list(mtcars), \(x) lm(mpg ~ cyl * hp + wt, data = x, x=TRUE))[[1]]) gives Error in rep(NA_real_, nrow(data)) : invalid 'times' argument
Interestingly, setting x=TRUE and model=TRUE will not help?
Clearly, setting the data explicitly will help (i.e. bypass find_data) but maybe you have alternative suggestions?
Thanks!
Please specify whether your issue is about:
a possible bug
library(margins)
library(tidyverse)
packageVersion("margins")
#> [1] '0.3.28'## simples examplereg<- map(list(mtcars), ~lm(mpg~cyl*hp+wt, data=., x=TRUE))[[1]]
reg2<- map(list(mtcars), \(x) lm(mpg~cyl*hp+wt, data=x, x=TRUE))[[1]]
margins(reg)
#> Error in eval(model[["call"]][["data"]], env): object '.' not found
margins(reg2)
#> Error in eval(model[["call"]][["data"]], env): object 'x' not found## my applicationmtcars_nst<-mtcars %>%
mutate(group= rep(c("A", "B"), times=c(16,16))) %>%
nest(.by=group) %>%
mutate(reg= map(data, ~lm(mpg~cyl*hp+wt, data=.)))
margins(mtcars_nst$reg[[1]])
#> Error in eval(model[["call"]][["data"]], env): object '.' not found
find_data(mtcars_nst$reg[[1]])
#> Error in eval(model[["call"]][["data"]], env): object '.' not found
margins(mtcars_nst$reg[[1]], data=mtcars_nst$data[[1]])
#> Average marginal effects#> lm(formula = mpg ~ cyl * hp + wt, data = .)#> cyl hp wt#> 0.8514 -0.06168 -2.895
sessionInfo()
#> R version 4.4.1 (2024-06-14)#> Platform: x86_64-pc-linux-gnu#> Running under: Ubuntu 20.04.6 LTS#> #> Matrix products: default#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 #> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3; LAPACK version 3.9.0#> #> locale:#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C #> [3] LC_TIME=fr_FR.UTF-8 LC_COLLATE=en_US.UTF-8 #> [5] LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=en_US.UTF-8 #> [7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C #> [9] LC_ADDRESS=C LC_TELEPHONE=C #> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C #> #> time zone: Europe/Zurich#> tzcode source: system (glibc)#> #> attached base packages:#> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages:#> [1] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 #> [5] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 #> [9] ggplot2_3.5.1 tidyverse_2.0.0 margins_0.3.28 #> #> loaded via a namespace (and not attached):#> [1] gtable_0.3.5 compiler_4.4.1 reprex_2.1.1 tidyselect_1.2.1 #> [5] scales_1.3.0 yaml_2.3.10 fastmap_1.2.0 R6_2.5.1 #> [9] generics_0.1.3 knitr_1.48 MASS_7.3-61 munsell_0.5.1 #> [13] tzdb_0.4.0 pillar_1.9.0 prediction_0.3.18 rlang_1.1.4 #> [17] utf8_1.2.4 stringi_1.8.4 xfun_0.48 fs_1.6.4 #> [21] timechange_0.3.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3 #> [25] digest_0.6.37 grid_4.4.1 rstudioapi_0.16.0 hms_1.1.3 #> [29] lifecycle_1.0.4 vctrs_0.6.5 evaluate_1.0.0 glue_1.8.0 #> [33] data.table_1.16.0 fansi_1.0.6 colorspace_2.1-1 rmarkdown_2.28 #> [37] tools_4.4.1 pkgconfig_2.0.3 htmltools_0.5.8.1
It seems
find_data()
is confused when thedata
argument of the regression function was used with an anonymous function in amap
call?Interestingly, depending on whether one uses
~.
or\(x)
gives different error messages:margins(purrr::map(list(mtcars), ~lm(mpg ~ cyl * hp + wt, data = ., x=TRUE))[[1]])
gives Error in eval(model[["call"]][["data"]], env) : object '.' not foundmargins(purrr::map(list(mtcars), \(x) lm(mpg ~ cyl * hp + wt, data = x, x=TRUE))[[1]])
gives Error in rep(NA_real_, nrow(data)) : invalid 'times' argumentInterestingly, setting
x=TRUE
andmodel=TRUE
will not help?Clearly, setting the
data
explicitly will help (i.e. bypassfind_data
) but maybe you have alternative suggestions?Thanks!
Please specify whether your issue is about:
Created on 2024-10-08 with reprex v2.1.1
The text was updated successfully, but these errors were encountered: