Home > database >  Emmeans function - no variable in reference grid
Emmeans function - no variable in reference grid

Time:10-14

I'm trying to run the emmeans function on a lmer data set but it's not working. Here's my data:

structure(list(Date = structure(c(16578, 16578, 16578, 16578, 
16578, 16578), class = "Date"), Time = c(7, 7, 7, 9, 11, 11), 
    Turtle = c("R3L12", "R3L12", "R3L12", "R3L12", "R3L12", "R3L12"
    ), Tex = c(11.891, 12.008, 12.055, 13.219, 18.727, 18.992
    ), m.Tb = c(12.477, 12.54, 12.54, 12.978, 16.362, 16.612), 
    m.HR = c(7.56457, 6.66759, 17.51107, 9.72277, 19.44553, 13.07674
    ), season = c("beginning", "beginning", "beginning", "beginning", 
    "beginning", "beginning"), year = c(2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L), Mass = c(360L, 360L, 360L, 360L, 360L, 
    360L)), row.names = c(NA, 6L), class = "data.frame") 

code for the model: model1 <- lmer(m.HR ~ season (1|Time) (1|Date) (1|Turtle), turtledata)

emmeans code:

model1.emmeans <- emmeans(model1, "Turtle")

These are the errors I get:

To enable adjustments, add the argument 'pbkrtest.limit = 20608' (or larger)
[or, globally, 'set emm_options(pbkrtest.limit = 20608)' or larger];
but be warned that this may result in large computation time and memory use.
Note: D.f. calculations have been disabled because the number of observations exceeds 3000.
To enable adjustments, add the argument 'lmerTest.limit = 20608' (or larger)
[or, globally, 'set emm_options(lmerTest.limit = 20608)' or larger];
but be warned that this may result in large computation time and memory use.
Error in emmeans(model1, "Turtle") : 
  No variable named Turtle in the reference grid

I'm not sure why it says there is no Turtle since it's a character variable in my data set.

Basically, I just want the emmeans to run but I'm also afraid it won't because the full data set is 20,000 rows long.

CodePudding user response:

Function emmeans tests for fixed effects (something that is manipulated), not random effects (something that just happens due to the design). The following example shows this and also a way to create a minimal reproducible example:

library(emmeans)
library(lme4)

# some artificial data
set.seed(143)
foo <- data.frame(
  m.HR   <- rnorm(100, mean=c(rep(c(5, 6), 25), rep(c(8, 9), 25))),
  season <- rep(c("a", "b"), each=50),
  Turtle <- rep(c("T1", "T2"), 50)
)

# simplified model with one fixed and one raqndom effect
model1 <- lmer(m.HR ~ season    (1|Turtle), foo)

(model1.emmeans <- emmeans(model1, "Turtle"))
# --> error as Turtle is a random effect

(model1.emmeans <- emmeans(model1, "season"))
# --> works as season is a fixed effect

#season emmean    SE   df lower.CL upper.CL
#a        5.73 0.535 1.07  -0.0567     11.5
#b        8.61 0.535 1.07   2.8254     14.4
#
#Degrees-of-freedom method: kenward-roger 
#Confidence level used: 0.95 

More discussion about random vs. fixed may be found in Cross Validated.

CodePudding user response:

You can't necessarily get emmeans to do what you want directly, but some sort of sensible calculation is possible.

The simplest thing would be to get an average prediction for each turtle with the values averaged across seasons:

ref_grid <- with(turtledata, 
   expand.grid(season=unique(season), turtle=unique(Turtle)))
pp <- predict(model1, newdata = ref_grid)
aggregate(pp, by=ref_grid$turtle, FUN=mean)

Confidence intervals are harder ...

  • Related