Skip to content

Commit 6c66e5d

Browse files
committed
fic bug with converting from cmdstanr to rstan output format. this is due to a stan dev breaking change. see https://discourse.mc-stan.org/t/rstan-read-stan-csv-throwing-error-with-cmdstan-models-versions-2-35/35665?utm_source=chatgpt.com
1 parent 90511d1 commit 6c66e5d

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed
-635 KB
Binary file not shown.

man/bmcm_stan.Rd

Lines changed: 23 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/cgd_analysis.Rmd

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ cgd_center <- cgd_first |>
9292
sex_id = as.factor(as.numeric(sex)))
9393
```
9494

95-
Next, append the background hazard rate by age, sex and country. These rare are available from the [WHO website](https://www.who.int/data/gho/data/themes/topics/topic-details/GHO/healthy-life-expectancy-(hale)) @wholifetables. We will read from our own local package `{bgfscure}` for simplicity.
96-
Next, we need to harmonise the country names and their associated cities.
97-
Then we join the data with the background mortality data according to age, sex and country.
95+
Next, append the background hazard rate by age, sex and country. These rare are available from the [WHO website](https://www.who.int/data/gho/data/themes/topics/topic-details/GHO/healthy-life-expectancy-(hale)) @wholifetables. We will read from our own local package `{bgfscure}` for simplicity. Next, we need to harmonise the country names and their associated cities. Then we join the data with the background mortality data according to age, sex and country.
9896
```{r bg-hazard}
9997
# WHO background mortality data
10098
load(here::here("../bgfscure/data/bg.mortality.RData")) # bg.mortality
@@ -139,6 +137,7 @@ T \sim \text{Exp}(\lambda)\\
139137
$$
140138

141139
```{r}
140+
##TODO: crashes R session
142141
out <-
143142
bmcm_stan(
144143
input_data = input_data,
@@ -203,8 +202,29 @@ out <-
203202
# so that plotting functions work later
204203
# convert cmdstanr object to rstan stanfit format
205204
206-
##TODO: error. for some reason save_warmup is NA?...
207-
# out$output <- rstan::read_stan_csv(out$output$output_files())
205+
stanfit <- brms::read_csv_as_stanfit(out$output$output_files())
206+
```
207+
208+
```{r eval=FALSE, echo=FALSE}
209+
# using brms::read_csv_as_stanfit is not recommended because of breaking changes
210+
# recommended:
211+
# Stay in CmdStanR, replace the stanfit helpers with posterior equivalents
212+
# This is the path the Stan developers now recommend.
213+
#
214+
# library(posterior)
215+
# draws <- fit_cmd$draws() # draws_array by default
216+
# epred <- posterior::weighted_mean(draws[,"mu",], weights = …) # example
217+
# ytilde <- posterior::subset_draws(draws, "y_tilde")
218+
#
219+
# If you need the “list of arrays” interface that rstan::extract() used to give, a three-liner does the job :
220+
#
221+
# extract_samples <- function(fit) {
222+
# vars <- fit$metadata()$stan_variables
223+
# vec <- posterior::as_draws_rvars(fit$draws())
224+
# lapply(vars, \(v) posterior::draws_of(vec[[v]], with_chains = FALSE)) |>
225+
# setNames(vars)
226+
# }
227+
# post <- extract_samples(fit_cmd)
208228
```
209229

210230
Secondly, we can pass in a _precompiled_ `cmdstanr` model. This saves an `.exe` file in the `stan` folder.

0 commit comments

Comments
 (0)