Memory issues using stan file created by brms


#1

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

#2

How does your brms model look like?


#3

Do you mean the stan code of the model?


#4

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


#5

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.


#6

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?


#7

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.


#8

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

@bgoodri Have you seen this before?


#9

No. Does brm pass save_warmup = FALSE?


#10

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


#11

Not by default.


#12

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?


#13

Have you generated the data with make_standata?


#14

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


#15

Then you should use make_standata instead.


#16

I am going to try this! Thanks!

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