library(marginaleffects)
library(brms)
dat <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/carData/TitanicSurvival.csv")
dat$survived <- ifelse(dat$survived == "yes", 1, 0)
dat$woman <- ifelse(dat$sex == "female", 1, 0) |> as.factor()
sum(is.na(dat$age)) # <-- 263 missing values
mod_miss <- brm(bf(survived ~ woman*mi(age) + passengerClass, family=bernoulli(link = "logit")) +
bf(age | mi() ~ passengerClass + woman) +
set_rescor(FALSE),
data = dat,
backend = "cmdstanr",
cores=4)
#--------------------------------------
## Example 1
plot_slopes(mod_miss,
variables = "woman",
condition = "age")
# Error in `[.data.frame`(model_data, , rn, drop = FALSE) :
# undefined columns selected
#--------------------------------------
## Example 2
slopes(mod_miss,
newdata = datagrid(
woman = 1,
passengerClass = "1st"))
# Error: The following variables can neither be found in 'data' nor in 'data2':
# 'age'
# In addition: Warning message:
# These variables were not found: survived, age, . Try specifying the `newdata` argument explicitly and make sure
# the missing variable is included.
#--------------------------------------
## Example 3
slopes(mod_miss,
variables = "age",
newdata = datagrid(woman = 0:1)) |>
posterior_draws()
# Error: There is no valid predictor variable. Please change the `variables` argument or supply a new data frame to the
# `newdata` argument.
# In addition: Warning message:
# These variables were not found: miage. Try specifying the `newdata` argument explicitly and make sure the
# missing variable is included.
Example based on the marginaleffects brms vignette (https://marginaleffects.com/vignettes/brms.html)
And the tracebacks.
sessionInfo()
Originally posted by @SethMusker in https://github.com/easystats/insight/issues/779#issuecomment-2227004987