Hi,
I’m currently using conditional_effects(fit, “condition”) that shows this graph for a ordinal regression model:
I want to show the exact numbers of mean and sd in this graph. How should I do that?
Thank you!
Hi,
I’m currently using conditional_effects(fit, “condition”) that shows this graph for a ordinal regression model:
Thank you!
You can modify the plot generated by conditional_effects
or create your own plot. I show both methods below with a reproducible example.
library(tidyverse)
theme_set(theme_bw())
library(brms)
m = brm(Petal.Width ~ Species, data=iris, cores=4,
file="tmp", backend="cmdstanr")
ce = conditional_effects(m)
conditional_effects
actually returns a list containing a data frame for each predictor variable in the model . These data frames are used to generate the conditional effects plots. You can get the exact values from these data frames.
# Look at the structure of the conditinal_effects() object
str(ce)
List of 1 $ Species:'data.frame': 3 obs. of 8 variables: ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 2 3 ..$ Petal.Width: num [1:3] 1.2 1.2 1.2 ..$ cond__ : Factor w/ 1 level "1": 1 1 1 ..$ effect1__ : Factor w/ 3 levels "setosa","versicolor",..: 1 2 3 ..$ estimate__ : num [1:3] 0.247 1.326 2.026 ..$ se__ : num [1:3] 0.0297 0.0277 0.0285 ..$ lower__ : num [1:3] 0.189 1.268 1.967 ..$ upper__ : num [1:3] 0.304 1.383 2.085 ..- attr(*, "effects")= chr "Species" ..- attr(*, "response")= chr "Petal.Width" ..- attr(*, "surface")= logi FALSE ..- attr(*, "categorical")= logi FALSE ..- attr(*, "ordinal")= logi FALSE ..- attr(*, "points")='data.frame': 150 obs. of 4 variables: .. ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... .. ..$ resp__ : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... .. ..$ cond__ : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ... .. ..$ effect1__: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "class")= chr "brms_conditional_effects"
# Look at the data frame conditional_effects created for Species
ce$Species
Species Petal.Width cond__ effect1__ estimate__ se__ lower__ upper__ 1 setosa 1.199333 1 setosa 0.2473169 0.02974676 0.1886225 0.3040592 2 versicolor 1.199333 1 versicolor 1.3256387 0.02766471 1.2675946 1.3831360 3 virginica 1.199333 1 virginica 2.0259349 0.02848083 1.9668068 2.0846869
# Modify conditional effects plot
# Use cat_args to remove point markers
plot(ce, ask=FALSE, cat_args=list(colour=NA))[[1]] +
geom_label(aes(label=round(estimate__, 2)), size=3) +
geom_text(aes(label=sprintf("SE: %0.3f", se__)), nudge_x=0.35,
hjust=1, colour="red", size=3)
# Create your own plot
ce$Species %>%
ggplot(aes(Species, estimate__)) +
geom_errorbar(aes(ymin=lower__, ymax=upper__), width=0.3) +
geom_label(aes(label=round(estimate__, 2)), size=3) +
geom_text(aes(label=sprintf("SE: %0.3f", se__)), nudge_x=0.35,
hjust=1, colour="red", size=3) +
labs(y="Petal Width")