Can't run brms in RStudio

I’m trying to run a simply brms analysis but get the response
Error in .requirePackage(package) :
unable to find required package ‘rethinking’

This is odd because brms has nothing to do with ‘rethinking’. I deleted the latter, brms, and rstan, started a new session in R, reinstalled the packages (not rethinking) and still can’t get the code to run.

I have the latest versions of R, RStudio, etc. Running on a macboook pro with M3 Pro chip.

I have used brms previously on this machine with no problem and haven’t used rethinking in a long time.

This is the code:
library(brms)
fit2.1 ←
brm(data = d,
family = gaussian,
weight ~ 1 + height,
prior = c(prior(normal(0, 100), class = Intercept),
prior(normal(0, 100), class = b),
prior(cauchy(0, 10), class = sigma)),
chains = 4, cores = 4, iter = 2000, warmup = 1000,
seed = 2)

The code isn’t the problem because the guy who wrote it took my version and it worked perfectly for him.

Any ideas? Very frustrating.
Thanks.

I can’t reproduce your problem either, suggesting that it’s something about your R workspace.

Suspect 1: Zombie objects from auto-reloaded .RData

My first suspicion is that you’re inadvertently loading an object created with rethinking that shares a name with one of your unquoted arguments in the call to brm(). When you restarted your R session, did you restore .RData from your previous session? I think RStudio still defaults to auto-saving all objects in your workspace to .RData, then reloads them when the new session starts. These objects can persist in the workspace even if the code that generated them is deleted and, even more alarmingly, if the package that define the object’s class has been deleted. The easiest diagnostic sign that this is happening will be the presence of objects listed in your Environment pane before you’ve sent any code to the console in your new session. If you do have objects that are reloading into a new session, you’re going to keep running into issues.
The fix: Current best practice for reproducibility is to turn off RStudio’s auto save+reload . When you start a new clean R session that isn’t contaminated by past sessions, your code should run as expected.

Suspect 2: Manually loaded R objects

Another possibility comes from R objects that you’ve deliberately loaded into your workspace. I’ve received warnings when loading some R-specific file types (.RData and .rds) about packages that have no relation to the objects in the saved file. I think some of these capture some details about the environment when the object was saved, but don’t know much about the details off-hand. If you have load() or readRDS() calls in your script, I’d suggest changing your workflow to store the intermediate data products as more generic file types (e.g., .csv).

2 Likes

great ideas. will try immediately.
many thanks.

Suspect 1 solution worked - thanks again, very grateful.

1 Like