1

I have been having trouble with running emmeans function (from the emmeans package) whenever I try to follow up a two way between groups ANOVA with estimated marginal means.

A simple example:

library(emmeans)
library(tidyverse)

df <- tibble(fct1 = factor(rep(1:3, 10)),
             fct2 = factor(rep(2:1, 15)),
             DV = rnorm(30, 100, 15))


model1 <- lm(DV ~ fct1 * fct2, df)

emmeans(model1, "fct1", by = "fct2")

Returns:

Error in assign(".Last.ref_grid", object, inherits = TRUE) : 
  cannot change value of locked binding for '.Last.ref_grid'

No matter what data I run it on, always the same error shows up.

Thank you for any help!

Caban
  • 25
  • 4

1 Answers1

1

This should stop it:

emm_options(save.ref_grid = FALSE)

This will keep it from saving the most recently created reference grid (or trying to, in your case). However, it may be worth trying to understand why this is happening. If you do:

.Last.ref_grid

you should see what it is that was last saved. That might be a clue. And try to delete it.

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • Using .Last.ref_grid revealed that it was an emmgrid object composed of two variables that I have created as part of one dataframe in one of my packages. No idea what is the mechanism behind it. Anyway, thank you very much for your help, I appreciate! – Caban Apr 06 '21 at 08:39
  • Is it in the namespace of your package? Say as part of the data? If so, you want to get it out of there for sure. It would explain your problem if emmeans is trying to write a new version of the reference grid into your package namespace... – Russ Lenth Apr 06 '21 at 13:21
  • @Caban I was able to reproduce this same error by adding a variable named `.Last.ref_grid` in my package namespace. So I strongly suspect this is what happened to you. Do `ls(asNamespace("my-package-name"), all = TRUE)` and see if it includes `.Last.ref_grid`. If there, figure out where it came from; most likely in some saved datasets. It's vital to get it out of there, as this will create the same problem for anybody that loads your package! – Russ Lenth Apr 06 '21 at 15:42
  • Strange. I inspected objects in my package using the code you provided and it didn't reveal `.Last.ref_grid` . There were only `".__NAMESPACE__." ".__S3MethodsTable__." ".packageName" `. And actually there are no dataframes in the package which causes the problem in the first place. The variables that are loaded are from another package that I have created. Loading this second package doesn't create the problem. – Caban Apr 07 '21 at 08:22
  • I will try to build the package from scratch, see if that helps. Thank you Russ for all your input! – Caban Apr 07 '21 at 08:26
  • Look in the namespace for that other package too. My suspicion is it is probably in an .rda or .RData file. Ref grids are NOT data frames, so it wouldn't be found there. – Russ Lenth Apr 07 '21 at 12:44
  • 1
    You were right, it was in the .RData file. I had to load the .Rdata of the package directly and then, from there, inspect objects with ls(). It is removed now. Thank you! – Caban Apr 08 '21 at 17:52