# Cmdstanr fails to read its own csv files for large number of parameters

**URL:** <https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450>\
**Category:** CmdStan\
**Tags:** cmdstanr\
**Created:** [May 12, 2021, 8:13pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450 "2021-05-12T20:13:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihrke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ihrke/32/7561_2.png) [@ihrke](https://discourse.mc-stan.org/u/ihrke)\
**Post date:** [May 12, 2021, 8:13pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/1 "2021-05-12T20:13:38Z")

</div>

I am fitting a hierarchical Hidden-Markov model with `cmdstanr`. I am using data from 20 subjects, each with around a 1000 trials (distributed across 20 block). The forward algorithm is implemented directly, hence I have to store about 20 x 20 x 100 variables for the forward-variables (and an equal number for the backward- and forward-backward smoothed ones as I wish to estimate by-trial probabilities).

Fitting the model works just fine and with lower number of subjects/blocks/trials, there is no issue whatsoever. However, when using the full dataset, `cmdstanr` cannot read back its own output files. In fact, it gets stuck in some obscure computation when trying to access any of the fitted model variables (such as using `fit$draws()` and even if trying to use `fit$save_object()`. This is the case, even if I use `fit$optimize()` instead of `fit$sample()`, even though the resulting output file from the fit (attached) is has only a single line (but a lot of variables, obviously) and is only about 9 MB in size.

This is the cmdstanr-file from an `optimize()`-run: [https://dropfiles.org/asHh3fmL](https://dropfiles.org/asHh3fmL)

Is this a known issue? Can I do anything to circumvent the issue? Currently, I am switching back to `rstan`.

- Operating System: Linux (Debian 6.3.0-18)
- CmdStan Version: 2.26.1
- Compiler/Toolkit: GCC 6.3.0 20170516

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [May 12, 2021, 8:20pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/2 "2021-05-12T20:20:46Z")

</div>

Can you check on the raw size of the csv’s?

---

<div class="post-metadata">

**Author:** ![ihrke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ihrke/32/7561_2.png) [@ihrke](https://discourse.mc-stan.org/u/ihrke)\
**Post date:** [May 12, 2021, 8:25pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/3 "2021-05-12T20:25:44Z")

</div>

It’s just 9 MB for the optimize-run. Here is the file [https://dropfiles.org/asHh3fmL](https://dropfiles.org/asHh3fmL).

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 12, 2021, 10:21pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/4 "2021-05-12T22:21:45Z")

</div>

@ihrke I was able to reproduce this using the file you shared (thanks for that). The problem is happening when CmdStanR calls `posterior::subset_draws()` towards the end of `cmdstanr::read_cmdstan_csv`.

I made a branch that has a temporary fix for this when reading in the csv after optimization (I think ultimately we need to fix `posterior::subset_draws()`):

```
remotes::install_github("stan-dev/cmdstanr@temp-fix-optimize-csv")

```

This should get it to work with optimization (at least it allows me to use `read_cmdstan_csv()` successfully with the file you provided). Unfortunately I’m not sure where the problem is happening when you’re using sampling but if you share that csv I can probably track it down.

Edit: @ihrke I updated the branch to avoid using `subset_draws` also for sampling so perhaps it will solve that for you too but I’m not 100% sure.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [May 12, 2021, 10:22pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/5 "2021-05-12T22:22:09Z")

</div>

Yeah, this is a known problem. A workaround for now is to use `cmdstanr::read_cmdstan_csv()` to read the csv files directly instead of using `$draws()`.

Edit: @jonah notes that I’m wrong about this. Just for posterity, it is _also_ currently the case that `read_cmdstan_csv` works for `sampling` fits with large numbers of parameters, but `$draws()` does not.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 12, 2021, 10:23pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/6 "2021-05-12T22:23:04Z")

</div>

In this case it also seems to happen with `read_cmdstan_csv()` unfortunately.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 12, 2021, 10:34pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/7 "2021-05-12T22:34:01Z")

</div>

> [@jsocolar](#):
>
> Just for posterity, it is _also_ currently the case that `read_cmdstan_csv` works for `sampling` fits with large numbers of parameters, but `$draws()` does not.

Yeah, thanks for the reminder, I had forgotten about that. @rok_cesnovar We need to get back to that and figure something out.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 12, 2021, 10:44pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/8 "2021-05-12T22:44:12Z")

</div>

Actually I’m now pretty sure that the problem with `draws()` is also related to this issue with `posterior::subset_draws()` that I just opened:

> <https://github.com/stan-dev/posterior/issues/129>
>
> If we have a lot of variables, e.g. 
> 
> \`\`\`r
> x \<- matrix(rnorm(10 \* 300000), nr…ow = 10, ncol = 300000)
> x \<- as\_draws\_matrix(x)
> \`\`\`
> 
> then regular R subsetting is super fast: 
> 
> \`\`\`r
> \# super fast
> x1 \<- x\[, colnames(x)\]
> \`\`\`
> 
> but using \`subset\_draws()\` is so slow I gave up and killed it before it finished:
> 
> \`\`\`r
> \# broken
> x2 \<- subset\_draws(x, variable = colnames(x))
> \`\`\`

---

<div class="post-metadata">

**Author:** ![ihrke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ihrke/32/7561_2.png) [@ihrke](https://discourse.mc-stan.org/u/ihrke)\
**Post date:** [May 13, 2021, 10:17am UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/9 "2021-05-13T10:17:27Z")

</div>

That’s amazing, thank you! I will try it asap!

---

<div class="post-metadata">

**Author:** ![ihrke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ihrke/32/7561_2.png) [@ihrke](https://discourse.mc-stan.org/u/ihrke)\
**Post date:** [May 13, 2021, 11:49am UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/10 "2021-05-13T11:49:03Z")

</div>

I tried using the branch with your fix and reading the csv now seems to work fine. I use the following convenience function to read the whole fit into memory (before storing it as an .RData file) and it is lightning fast (as opposed to taking ages before your fix).

```
cmdstanr.resolve <- function(fit){
  temp_rds_file <- tempfile(fileext = ".RDS")
  fit$save_object(file = temp_rds_file)
  fit <- readRDS(temp_rds_file)  
  return(fit)
}

```

However, I cannot use the `fit$summary()` or `fit$draws()` function for this object as I used to. The error I get is

```
> mod_opt_probed.r$draws("gmu")
Error in `[.default`(private$draws_, , variables, drop = FALSE) : 
  subscript out of bounds
> mod_opt_probed.r$summary()
Error: Can't subset columns that don't exist.
x Columns `variable` and `mean` don't exist.
Run `rlang::last_error()` to see where the error occurred.
```

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 13, 2021, 3:18pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/11 "2021-05-13T15:18:38Z")

</div>

Oops I may have broken `draws()` on that branch when I fixed the CSV reading. But I think this PR that we just merged in the posterior package

> <https://github.com/stan-dev/posterior/pull/130>
>
> Addresses #129.
> 
> Makes \`check\_existing\_variables()\`, and therefore \`subset\_dra…ws()\` much more efficient. For example, with default arguments, this suggested version can process 300k variables in a fraction of a second while the current version takes almost 10 minutes. However, both versions still have comparable runtimes when \`regex = TRUE\`.
> 
> \`\`\`
> x \<- as\_draws\_matrix(matrix(rnorm(10 \* 300000), nrow = 10, ncol = 300000))
> microbenchmark::microbenchmark(
> posterior:::check\_existing\_variables(variables = colnames(x), x = x),
> posterior::subset\_draws(x, variable = colnames(x)),
> times = 10,
> unit = "s"
> )
> 
> Unit: seconds
> min lq mean median uq max neval
> 0.3240343 0.3354325 0.3669232 0.3541255 0.3870651 0.4765244 10
> 0.4236527 0.4271074 0.4862439 0.4566513 0.5303358 0.6543742 10
> \`\`\`

will hopefully fix the problem. Can you try reinstalling both posterior and cmdstanr from master?

```
remotes::install_github("stan-dev/posterior")
remotes::install_github("stan-dev/cmdstanr")

```

and let let me know if that fixes the problem? Sorry for the hassle, but thanks for helping us fix this!

---

<div class="post-metadata">

**Author:** ![ihrke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ihrke/32/7561_2.png) [@ihrke](https://discourse.mc-stan.org/u/ihrke)\
**Post date:** [May 13, 2021, 5:58pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/12 "2021-05-13T17:58:44Z")

</div>

Thanks, that seems to have fixed the problem! Thanks so much for your efforts and the incredibly fast fix!

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 13, 2021, 6:04pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/13 "2021-05-13T18:04:11Z")

</div>

That’s great, thanks for trying that out. Glad it’s working now!

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 13, 2021, 9:59pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/14 "2021-05-13T21:59:39Z")

</div>

> [@jsocolar](#):
>
> Just for posterity, it is _also_ currently the case that `read_cmdstan_csv` works for `sampling` fits with large numbers of parameters, but `$draws()` does not.

@jsocolar I’m hopeful that with `posterior::subset_draws()` now fixed this will drastically improve the speed of `$draws()` with many parameters. I haven’t done any rigorous testing yet though.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [May 13, 2021, 10:01pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/15 "2021-05-13T22:01:32Z")

</div>

@jonah, can I test by updating posterior without rebuilding the R6 object, or do I need to rebuild after updating?

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 13, 2021, 10:28pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/16 "2021-05-13T22:28:14Z")

</div>

My hunch is that you’ll need to rebuild the R6 object. Unfortunately if we update a method it doesn’t update the methods associated with existing R6 objects.

However, there may be an alternative: do you by any chance still have the CSV files or were those just written to temp files? If you still have the CSV files associated with the old R6 object then you can recreate the R6 object without having to rerun the model using `as_cmdstan_fit(paths_to_csv_files)`. Then the resulting fit object would use the latest `draws` method.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [May 13, 2021, 10:29pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/17 "2021-05-13T22:29:38Z")

</div>

Yeah, that’s what I meant by rebuild :)  
I’ll go ahead and give it a crack.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 13, 2021, 10:32pm UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/18 "2021-05-13T22:32:04Z")

</div>

Cool, thanks for trying. You might also try using `format = "draws_list"` when running `as_cmdstan_fit`. According to @rok_cesnovar that’s the most efficient format to use if there are a ton of parameters. (That will just affect how the draws are stored internally. If you then use `draws()` it will use the regular default of “draws\_array” unless you specify a different format.)

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [May 14, 2021, 1:53am UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/19 "2021-05-14T01:53:47Z")

</div>

My 3yo arrived home so I just got around to this.

`$draws()` is now blazing fast on a fit where it was previously unusable (250K parameters, now takes about 30 seconds, previously I killed it after 90 minutes).

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 14, 2021, 3:52am UTC](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450/20 "2021-05-14T03:52:03Z")

</div>

Awesome, that’s great news! Thanks for testing it out for us.

[Next page](https://discourse.mc-stan.org/t/cmdstanr-fails-to-read-its-own-csv-files-for-large-number-of-parameters/22450.md?page=2)
