Memory issues using stan file created by brms

I am creating a multillevel time-series model with stan and specifically with the brms package in R. When I use the brm function from this package making a model with only a individual-specific intercept and just one explanatory variable it works fine. However, I want to really understand the stan code so now I try to use the generated script from the brm function and run this using the stan function from rstan. Now the problems start, since when I run the model, the memory used by R in my computer increases dramatically and after just a couple of seconds I get the error that R can’t allocate a vector of just 23 kb. I have 16GB of RAM, so that’s not that small. My dataset contains 14638 observations. Does anyone know why I can’t run the generated script from brm but running the brm function works fine? Thanks in advance

  • Operating System: Windows 10
  • brms Version: 2.5.0

How does your brms model look like?

Do you mean the stan code of the model?

More like the brm() call for a start and perhaps some more details (e.g., number of levels etc.) about your data.

My brm() call looks like this:

result_brms = brm(log(1 + Activations) ~ Share_total_market + (1 | ID), data = test_data, chains = 4, iter = 5000, warmup = 2000, save_model = "test_code_stan.txt", cores = getOption("mc.cores", 4L))

It is just one level, so it looks like a rather standard multilevel model to me.

I mean how many levels does ID have?

And do I understand correctly, that with brms it works but when using rstan directly, it doesn’t?

My ID variable has 187 levels.

Yes, indeed. Executing

result_brms = brm(log(1 + Activations) ~ Share_total_market + (1 | ID), data = test_data, chains = 4, iter = 5000, warmup = 2000, save_model = "test_code_stan.txt", cores = getOption("mc.cores", 4L))

works perfectly fine and converges nicely.

However, when I use the test_code_stan.txt file and run the model using the stan function I get memory issues.

That’s very strange to me. as brms uses rstan internally, it shouldn’t make a difference.

@bgoodri Have you seen this before?

No. Does brm pass save_warmup = FALSE?

I passed that to the stan function using the test_code_stan.txt code, but that doesn’t change my problem unfortunately

Not by default.

Could it be that I insert the wrong data in the stan function. Because I am not sure whether some variables are what I think they are. But could that cause the memory issue or is that per definition not possible?

Have you generated the data with make_standata?

No, I just make the data by putting the data from a dataframe into a list

Then you should use make_standata instead.

I am going to try this! Thanks!

edit: This solves the problem! Thank you so much!