admin管理员组文章数量:1404588
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
本文标签:
版权声明:本文标题:r - ggplot based on predictions(), get_datagrid(), and get_predicted() functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744838222a2627761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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