我通过 brms 包构建了序数回归模型。
bmodel<- brm(pop ~ RDB2000pop + Temperature2003 + Artificial.land2003 + Wasteland2003 + Volcanic.area + Agricultural.land2003 + RiverLake2003 + Seashore2003 + Protected.area +
(1+RDB2000pop+Temperature2003+Artificial.land2003+ Wasteland2003 + Volcanic.area + Agricultural.land2003 + RiverLake2003 + Seashore2003 + Protected.area |species_id),
data = dfpop_chenv,
family = cumulative(link = "logit", threshold = "flexible"),
prior = c(set_prior("normal(0,10)", class = "b")),
warmup = 1000,
iter = 10000,
chains = 4,
save_all_pars = TRUE,
save_model = "grass_w_stanscript",
file = "grass_w")
然后,我尝试绘制结果的条件效果(“ordinal = TRUE”已被弃用)。
rplot<- plot(conditional_effects(bmodel, categorical =TRUE)) #categorical
pp <- do.call("grid.arrange", c(rplot, ncol=4))
这些估计值是在特定类别中获得结果的概率,在给定条件下,所有类别的总和应为 1。来自terior_epred(),但不是来自conditional_effects()。
搜索类似的情况,我发现默认情况下 conditional_effects 返回样本的中位数而不是平均值。
所以,我又尝试了一次,参考以下页面(具有连续预测器的序数模型)。 https://cran.r-project.org/web/packages/tidybayes/vignettes/tidy-brms.html#ordinal-model-with-categorical-predictor
fit_plot = dfpop_chenv %>%
data_grid(Temperature2003 = seq_range(Temperature2003, n = 101)) %>%
add_epred_draws(bmodel, value = "P(pop | Temperature2003)", category = "pop")
ggplot(aes(x = Temperature2003, y = `P(pop | Temperature2003)`, color = pop)) +
stat_lineribbon(aes(fill = pop), alpha = 1/5) +
scale_color_brewer(palette = "Dark2") +
scale_fill_brewer(palette = "Dark2")
但是,它显示错误“未找到 RDB2000pop”。RDB2000pop 是解释变量之一。并且肯定会在“dfpop_chenv”中。
该问题似乎与“add_epred_draws()”有关。只是尝试简单地使用“add_epre_draws()”,它也显示相同的错误。
tibble(Temperature2003 = 2) %>%
add_epred_draws(bmodel) %>%
median_qi(.epred)
如何绘制每个类别的预测概率?