strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
544 stars 35 forks source link

`mutate()` fails on ggeffects #279

Closed mattansb closed 1 year ago

mattansb commented 1 year ago

(This use to work)

library(ggeffects)
library(dplyr, warn.conflicts = FALSE)

m <- lm(mpg ~ factor(cyl), mtcars)

gge <- ggpredict(m, "cyl")

class(gge)
#> [1] "ggeffects"  "data.frame"

as.data.frame(gge)
#>   x predicted std.error conf.low conf.high group
#> 1 4  26.66364 0.9718008 24.75894  28.56833     1
#> 2 6  19.74286 1.2182168 17.35520  22.13052     1
#> 3 8  15.10000 0.8614094 13.41167  16.78833     1

mutate(gge) # very minimal example
#> Error in colnames(x)[1] <- focal_term: replacement has length zero

Created on 2023-02-07 with reprex v2.0.2

See here for why this isn't actually related to dplyr, which is why I'm posting here.

strengejacke commented 1 year ago

What does mutate() on a data frame do, or what would you like to achieve?

mattansb commented 1 year ago

I would like to be able to manipulate the ggeffects data frame.

It seems like these break the printing method somehow.

m <- lm(mpg ~ factor(cyl), mtcars)

gge <- ggeffects::ggpredict(m, "cyl")

gge1 <- dplyr::mutate(gge)
print(gge1)
#> Error in colnames(x)[1] <- focal_term: replacement has length zero

gge2 <- gge[c(1:2, 4:6)]
print(gge2)
#> Error in colnames(x)[1] <- focal_term: replacement has length zero

Created on 2023-02-08 with reprex v2.0.2

strengejacke commented 1 year ago

Do you know more about [.class generics?

mattansb commented 1 year ago

I do not :(

strengejacke commented 1 year ago
library(ggeffects)

m <- lm(mpg ~ gear * factor(cyl), mtcars)
gge <- ggeffects::ggpredict(m, c("gear", "cyl"))

gge
#> # Predicted values of mpg
#> 
#> # cyl = 4
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    3 |     23.57 | [18.82, 28.33]
#>    4 |     26.41 | [24.35, 28.46]
#>    5 |     29.24 | [25.12, 33.35]
#> 
#> # cyl = 6
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    3 |     19.76 | [15.51, 24.01]
#>    4 |     19.74 | [17.14, 22.34]
#>    5 |     19.72 | [14.52, 24.92]
#> 
#> # cyl = 8
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    3 |     15.05 | [13.11, 16.99]
#>    4 |     15.22 | [12.66, 17.79]
#>    5 |     15.40 | [10.65, 20.15]
gge[c(1:2, 4:6)]
#> # Predicted values of mpg
#> 
#> # cyl = 4
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    3 |     23.57 | [18.82, 28.33]
#>    4 |     26.41 | [24.35, 28.46]
#> 
#> # cyl = 6
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    3 |     19.76 | [15.51, 24.01]
#>    4 |     19.74 | [17.14, 22.34]
#> 
#> # cyl = 8
#> 
#> gear | Predicted |         95% CI
#> ---------------------------------
#>    4 |     15.22 | [12.66, 17.79]

m <- lm(mpg ~ factor(cyl), mtcars)
gge <- ggeffects::ggpredict(m, "cyl")

gge
#> # Predicted values of mpg
#> 
#> cyl | Predicted |         95% CI
#> --------------------------------
#>   4 |     26.66 | [24.68, 28.65]
#>   6 |     19.74 | [17.25, 22.23]
#>   8 |     15.10 | [13.34, 16.86]
gge[c(1:2)]
#> # Predicted values of mpg
#> 
#> cyl | Predicted |         95% CI
#> --------------------------------
#>   4 |     26.66 | [24.68, 28.65]
#>   6 |     19.74 | [17.25, 22.23]

Created on 2023-04-26 with reprex v2.0.2

strengejacke commented 1 year ago

Can you check if this works with mutate() now?

mattansb commented 1 year ago

Great! Thanks!