I have this data:
library(tidyverse)
library(marginaleffects)
data(efc, package = "ggeffects")
efc <- efc %>% na.omit()
efc <- datawizard::to_factor(efc, c("c161sex", "c172code"))
and I run this regression:
mod <- lm(barthtot ~ c172code +c12hour* c161sex , efc)
pred <- predictions(mod, newdata = datagrid(c172code = unique , c12hour=unique,
c161sex = unique))
then I generate this plot:
ggplot(pred, aes(c12hour, barthtot, colour = c161sex )) + facet_wrap(~c172code )+
geom_line(position = position_dodge(0.2)) +
ggtitle("Predicted values of barthtot")
I tried to reproduce the same plot using the get_datagrid()
and get_predicted()
functions:
library(easystats)
vizdata <- get_datagrid(efc, by = c("c12hour", "c161sex","c172code"))
vizdata$Predicted <- get_predicted(mod, vizdata)
ggplot(efc, aes(x = c12hour , y = barthtot, color =c161sex )) +
geom_line(data = vizdata, aes(y = Predicted), linewidth = 1) +facet_wrap(~c172code )
which produces:
Yet, this is different from the first one.
I have this data:
library(tidyverse)
library(marginaleffects)
data(efc, package = "ggeffects")
efc <- efc %>% na.omit()
efc <- datawizard::to_factor(efc, c("c161sex", "c172code"))
and I run this regression:
mod <- lm(barthtot ~ c172code +c12hour* c161sex , efc)
pred <- predictions(mod, newdata = datagrid(c172code = unique , c12hour=unique,
c161sex = unique))
then I generate this plot:
ggplot(pred, aes(c12hour, barthtot, colour = c161sex )) + facet_wrap(~c172code )+
geom_line(position = position_dodge(0.2)) +
ggtitle("Predicted values of barthtot")
I tried to reproduce the same plot using the get_datagrid()
and get_predicted()
functions:
library(easystats)
vizdata <- get_datagrid(efc, by = c("c12hour", "c161sex","c172code"))
vizdata$Predicted <- get_predicted(mod, vizdata)
ggplot(efc, aes(x = c12hour , y = barthtot, color =c161sex )) +
geom_line(data = vizdata, aes(y = Predicted), linewidth = 1) +facet_wrap(~c172code )
which produces:
Yet, this is different from the first one.
Share Improve this question edited Mar 11 at 7:57 mariann asked Mar 10 at 15:27 mariannmariann 331 silver badge5 bronze badges 1 |1 Answer
Reset to default 2In your first ggplot
call, you supply barthot
as the variable on the y-axis, but the predicted values from your regression models are labeled as estimate
in the pred
object resulting from predictions()
. Replacing barthot
with estimate
yields the plot you are expecting:
ggplot(pred, aes(x = c12hour, y = estimate, colour = c161sex )) +
facet_wrap(~c172code ) +
geom_line(linewidth = 1) +
ggtitle("Predicted values of barthtot")
Created on 2025-03-11 with reprex v2.1.1
get_datagrid()
fromeasystats
to the output ofdatagrid()
frommarginaleffects
? Remember thatdatagrid()
requires themodel
argument when it is called as a standalone function. – Vincent Commented Mar 10 at 15:45