最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - ggplot based on predictions(), get_datagrid(), and get_predicted() functions - Stack Overflow

programmeradmin2浏览0评论

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
  • Maybe try to compare the output of get_datagrid() from easystats to the output of datagrid() from marginaleffects? Remember that datagrid() requires the model argument when it is called as a standalone function. – Vincent Commented Mar 10 at 15:45
Add a comment  | 

1 Answer 1

Reset to default 2

In 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论