strengejacke / ggeffects

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

ggpredict() does not recognise `` quoted response variable with a : symbol in mixed models #427

Open JuanWang814 opened 6 months ago

JuanWang814 commented 6 months ago

Here is a sample dataset:

data <- structure(list(ID = c(1482L, 1482L, 1482L, 1483L, 1483L, 1483L, 1516L, 1516L, 1516L, 1532L, 1532L, 1532L, 1545L, 1545L, 1545L), 
                       status = c("D", "D", "D", "D", "D", "D", "B", "B", "B", "B", "B", "B", "B", "B", "B"), 
                       x = c(0.107674842, 0.096711338, 0.104180187, 0.17978836, 0.144056182, 0.12804244, 0.240996261, 0.238791261, 0.274007305, 0.750598233, 0.757686569, 0.704884029, 0.468086496, 0.411304874, 0.348525367),
                       `x:y` = c(0.107674842, 0.096711338, 0.104180187, 0.17978836, 0.144056182, 0.12804244, 0.240996261, 0.238791261, 0.274007305, 0.750598233, 0.757686569, 0.704884029, 0.468086496, 0.411304874, 0.348525367)),
                  class = "data.frame", 
                  row.names = c(NA, -15L))

Note the variables x and x:y (quoted) had the same values in this dataset for illustration purpose.

Use ggpredict() for three models:

model_1 <- lme4::lmer(x ~ status + (1 | ID), data = data)
model_2 <- lme4::lmer(`x:y` ~ status + (1 | ID), data = data)
model_3 <- lm(`x:y` ~ status, data = data)

plot(ggeffects::ggpredict(model = model_1, terms = "status"))
plot(ggeffects::ggpredict(model = model_2, terms = "status"))
plot(ggeffects::ggpredict(model = model_3, terms = "status"))

Prediction of model_1 can be correctly plotted, but when we change the response variable in model_1 to the quoted response variable in model_2, ggpredict() cannot calculate confidence intervals anymore. However, this issue seems to be only associated with mixed models, as using ggpredict() with the simple linear model (model_3) had no issue.

Also, effects::effect() can take the quoted response variable in mixed models without an issue.

plot(effects::effect(mod = model_2, term = "status"))
strengejacke commented 4 months ago

Not sure if the code has changed, but all your three examples work fine for me:

data <- structure(
  list(
    ID = c(1482L, 1482L, 1482L, 1483L, 1483L, 1483L, 1516L, 1516L, 1516L, 1532L, 1532L, 1532L, 1545L, 1545L, 1545L),
    status = c("D", "D", "D", "D", "D", "D", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
    x = c(0.107674842, 0.096711338, 0.104180187, 0.17978836, 0.144056182, 0.12804244, 0.240996261, 0.238791261, 0.274007305, 0.750598233, 0.757686569, 0.704884029, 0.468086496, 0.411304874, 0.348525367),
    `x:y` = c(0.107674842, 0.096711338, 0.104180187, 0.17978836, 0.144056182, 0.12804244, 0.240996261, 0.238791261, 0.274007305, 0.750598233, 0.757686569, 0.704884029, 0.468086496, 0.411304874, 0.348525367)
  ),
  class = "data.frame",
  row.names = c(NA, -15L)
)

model_1 <- lme4::lmer(x ~ status + (1 | ID), data = data)
model_2 <- lme4::lmer(`x:y` ~ status + (1 | ID), data = data)
model_3 <- lm(`x:y` ~ status, data = data)

plot(ggeffects::ggpredict(model = model_1, terms = "status"))

plot(ggeffects::ggpredict(model = model_2, terms = "status"))

plot(ggeffects::ggpredict(model = model_3, terms = "status"))

Created on 2024-02-29 with reprex v2.1.0

Can you please update ggeffects (and possibly other packages, in particular insight) and try again?