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

Weird behavior with bind_rows(), mutate(), and ggeffects package #7019

Open
sschooler opened this issue May 6, 2024 · 0 comments
Open

Weird behavior with bind_rows(), mutate(), and ggeffects package #7019

sschooler opened this issue May 6, 2024 · 0 comments

Comments

@sschooler
Copy link

sschooler commented May 6, 2024

This is kind of a weird niche problem. The mutate function is interacting strangely with the ggeffects package when using bind_rows to bind multiple ggeffects objects together then using mutate() to add a column (for both functions ggeffect and ggemmeans). It drops rows for seemingly no reason. There is an easy work around, which is simply to wrap bind_rows in data.frame(), but I figured I'd alert devs to the bug. Anyway, here's a reprex using mtcars.

require(dplyr)
require(ggeffects)
data(mtcars)
# create simple linear models
model.fourcyl <- lm(data = mtcars[mtcars$cyl==4,], disp ~ hp)
model.sixcyl <- lm(data = mtcars[mtcars$cyl==6,], disp ~ hp)
model.eightcyl <- lm(data = mtcars[mtcars$cyl==8,], disp ~ hp)
# doesn't work- drops rows beyond row 7
test.df <- bind_rows("FourCyl" = ggemmeans(model.fourcyl, terms = "hp"),
                  "SixCyl" = ggemmeans(model.sixcyl, terms = "hp"),
                  "EightCyl" = ggemmeans(model.eightcyl, terms = "hp"),
                  .id = "group") %>% mutate(pred2 = predicted/10)
# shows the complete data frame
test2.df <- data.frame(bind_rows("FourCyl" = ggemmeans(model.fourcyl, terms = "hp"),
                              "SixCyl" = ggemmeans(model.sixcyl, terms = "hp"),
                              "EightCyl" = ggemmeans(model.eightcyl, terms = "hp"),
                              .id = "group")) %>% mutate(pred2 = predicted/10)

Here's sessionInfo

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C LC_TIME=English_United States.utf8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] ggeffects_1.3.4 dplyr_1.1.4

loaded via a namespace (and not attached):
[1] Matrix_1.6-1.1 compiler_4.3.2 tidyselect_1.2.0 Rcpp_1.0.12 survey_4.2-1 splines_4.3.2
[7] boot_1.3-28.1 lattice_0.21-9 coda_0.19-4.1 R6_2.5.1 generics_0.1.3 MASS_7.3-60
[13] tibble_3.2.1 nloptr_2.0.3 nnet_7.3-19 insight_0.19.10 DBI_1.2.1 minqa_1.2.6
[19] pillar_1.9.0 rlang_1.1.3 utf8_1.2.4 datawizard_0.10.0 pkgload_1.3.4 estimability_1.4.1
[25] cli_3.6.2 withr_3.0.0 magrittr_2.0.3 emmeans_1.9.0 grid_4.3.2 mvtnorm_1.2-4
[31] xtable_1.8-4 lme4_1.1-35.1 lifecycle_1.0.4 nlme_3.1-163 vctrs_0.6.5 glue_1.7.0
[37] mitools_2.4 survival_3.5-7 carData_3.0-5 fansi_1.0.6 colorspace_2.1-0 tools_4.3.2
[43] pkgconfig_2.0.3 effects_4.2-2

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