请求帮助理解tidybayes::add_predicted_draws和brms::Poster_predicted之间的明显差异
我正在使用重新思考软件包中的Howell1数据集请求帮助理解tidybayes::add_predicted_draws和brms::Poster_predicted之间的明显差异,r,linux,statistics,bayesian,stan,R,Linux,Statistics,Bayesian,Stan,我正在使用重新思考软件包中的Howell1数据集 require(cmdstanr) require(brms) require(tidybayes) data("Howell1") d <- Howell1 d2 <- d[d$age > 18,] d2$hs <- (d2$height - mean(d2$height))/ sd(d2$height) d2$ws <- (d2$weight - mean(d2$weight))/ sd(
require(cmdstanr)
require(brms)
require(tidybayes)
data("Howell1")
d <- Howell1
d2 <- d[d$age > 18,]
d2$hs <- (d2$height - mean(d2$height))/ sd(d2$height)
d2$ws <- (d2$weight - mean(d2$weight))/ sd(d2$weight)
PS:我也在stan Discussion上发布了这个问题,但还没有收到回复。另外,请让我知道这是否更好地张贴在stats.stackexchange。因为这更多的是一个基于工具的问题,而不是一个概念性的问题,所以我将其发布在这里。结果表明,这种差异是由于brms不尊重种子设定::后预测 在与github中brms包的开发人员讨论后,他发现问题如下: 如果设置了选项(mc.cores=),则 默认情况下将并行计算,除非更改核心 论点在windows上,并行执行是通过 parallel::parLapply,我不知道该函数如何尊重种子, 如果有的话。当以串行方式(1核)执行代码时,结果 是可复制的 一旦我将mc.cores设置为1,我就再也看不到后预测和添加预测之间的差异 因此,我认为这个问题已经解决 相关github链接包括:
priors <- c(prior(normal(0,2), class = "Intercept"),
prior(normal(0,2), class = 'b'),
prior(cauchy(0,2), class = "sigma"))
m4.4 <- brm(formula = hs ~ 1 + ws + male, data = d2, family = gaussian,
backend = "cmdstanr", prior = priors
iter = 2000, warmup = 1000, chains = 4, cores = 4)
i <- 4 # looking at the results for a particular row of the input dataset
y <- posterior_epred(m4.4)
x <- d2 %>% add_fitted_draws(model = m4.4, value = "epred")
x %>% as_tibble() %>% filter(.row ==i) %>% dplyr::select(epred) %>% cbind(fitdr = y[,i]) %>% mutate(diff = fitdr - epred)
yp <- posterior_predict(m4.4)
xp <- d2 %>% add_predicted_draws(model = m4.4, prediction = "pred")
xp %>% as_tibble() %>% filter(.row ==i) %>% dplyr::select(pred) %>% cbind(preddr = yp[,i]) %>% mutate(diff = preddr - pred)
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
other attached packages:
[1] stringr_1.4.0 readr_1.4.0 tibble_3.0.4 tidyverse_1.3.0 MASS_7.3-53 bayesplot_1.8.0 cmdstanr_0.1.3 rethinking_2.13
[9] loo_2.4.1 gganimate_1.0.7 RColorBrewer_1.1-2 ggrepel_0.9.0 brms_2.14.4 Rcpp_1.0.5 rstan_2.21.2 StanHeaders_2.21.0-7
[17] cowplot_1.1.1 ggplot2_3.3.3 tidybayes_2.3.1 ggdist_2.4.0 modelr_0.1.8 tidyr_1.1.2 forcats_0.5.0 purrr_0.3.4
[25] dplyr_1.0.2 magrittr_2.0.1