rlme4r-marginaleffects

lme4 + lspline + marginaleffects: problems with CI?


I ran this code

library(lme4)
library(lspline)
library(marginaleffects)

m_weight <-  lmer(delta_weight ~ lspline(MINUTES, knots = c(5))*period +  ( 1 | ID) , data = dat )
summary(m_weight)


weight1 <- comparisons(
  m_weight,
  variables = "period",
  include_random = FALSE,
  newdata = datagrid(
    MINUTES = seq(from = 0, to = 30, by = 5)
  ))   


weight1

which gives this output:

enter image description here

Questions:

  1. conf.low and high, as well predicted_hi and lo, don't give results compatible with "comparsion" or "predicted". Is there any issue here?

  2. Also, which do you tend to report here? comparsion or predicted?

  3. Lastly, is it possible to change the reference level to "pre" in "weight1"?

dat <- structure(list(delta_weight = c(0, -14.5, -0.5, -12.5, -7.5, -17.5, 
                             -7.5, 0, -7.5, -17.5, -14.5, -12.5, -4.5, -12.5, 0, -6, -4, 2, 
                             -5, -1, -3, 0, -15.5, -30.5, -33.5, -31.5, -30.5, -32.5, 0, -9, 
                             -20, -15, -3, -8, -12, 0, -8, -11, -8, -6, -9, -5, 0, -8, -19, 
                             -20, -19, -11, -7, 0, -24, -7, -22, -24, -19, -19, 0, -5.5, -9.5, 
                             -9.5, -6.5, -4.5, -7.5, 0, -21.5, -26.5, -20.5, -26.5, -20.5, 
                             -29.5, 0, -9, -5, -4, -6, -1, -3, 0, -13, -9, -10, -4, -5, -12, 
                             0, -3.5, -17.5, -6.5, -13.5, -5.5, -18.5, 0, 0, 1, -1, -5, -3, 
                             -12, 0, -5.5, 0.5, -6.5, -10.5, -1.5, -4.5, 0, -13.5, -10.5, 
                             -13.5, -17.5, -9.5, -24.5, 0, -2, -5, -4, -9, -5, -9, 0, -16.5, 
                             -20.5, -13.5, -10.5, -14.5, -16.5, 0, 5, 6, 2, -1, 8, 5, 0, 0.5, 
                             -0.5, 0.5, 3.5, -3.5, 4.5, 0, -13, -22, -31, -33, -25, -24, 0, 
                             -15, -32, -34, -26, -32, -39, 0, -14.5, -5.5, -15.5, -0.5, -6.5, 
                             -10.5, 0, -17, -15, -20, -19, -9, -18, 0, 14.5, -2.5, -4.5, -4.5, 
                             -0.5, -8.5, 0, -24.5, -10.5, -4.5, -22.5, -9.5, -9.5, 0, -0.5, 
                             -3.5, -2.5, -0.5, 13.5, 1.5, 0, -4, -3, -3, -4, -9, -7, 0, -14.5, 
                             -13.5, -15.5, -13.5, -13.5, -14.5, 0, -10, -4, -13, -8, -9, -7, 
                             0, 8.5, -2.5, -13.5, -3.5, -4.5, 2.5, 0, -10, -15, -13, -12, 
                             -14, -7, 0, -13.5, -8.5, -43.5, -14.5, -15.5, -12.5, 0, -19, 
                             -9, -17, -20, -22, -12, 0, -4, -3, -3, -7, -4, -4, 0, -6, -15, 
                             -6, -10, -14, -12, 0, -9, -15, -19, -13, -15, -14, 0, -24.5, 
                             -22.5, -24.5, -19.5, -19.5, -24.5, 0, -4.5, -5.5, -5.5, -5.5, 
                             -6.5, -3.5, 0, -8, -10, -9, -11, -3, -6, 0, -15, -21, -26, -15, 
                             -15, -15, 0, 5, 1, -4, -4, 4, -1, 0, 1.5, 3.5, 5.5, 5.5, -8.5, 
                             10.5, 0, -5, -6, -13, -14, -24, -10, 0, 5, -1, -6, -7, -3, -5, 
                             0, -12, -22, -17, -15, -20, -23, 0, 0, -4, -6, -9, -9, -12, 0, 
                             -19, -18, -22, -14, -21, -11), ID = c(10, 10, 10, 10, 10, 10, 
                                                                   10, 10, 10, 10, 10, 10, 10, 10, 14, 14, 14, 14, 14, 14, 14, 14, 
                                                                   14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
                                                                   15, 15, 15, 15, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 
                                                                   17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
                                                                   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 20, 20, 20, 20, 
                                                                   20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 
                                                                   21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
                                                                   22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 
                                                                   23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 
                                                                   24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 28, 
                                                                   28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 3, 3, 3, 
                                                                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 31, 31, 31, 31, 31, 31, 31, 
                                                                   31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 
                                                                   32, 32, 32, 32, 32, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 
                                                                   34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 
                                                                   35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 4, 
                                                                   4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 
                                                                   5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 
                                                                   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 
                                                                   9, 9, 9, 9, 9, 9), MINUTES = c(0, 5, 10, 15, 20, 25, 30, 0, 5, 
                                                                                                  10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 
                                                                                                  25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 
                                                                                                  5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 
                                                                                                  20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 
                                                                                                  0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 
                                                                                                  15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 
                                                                                                  30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 
                                                                                                  10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 
                                                                                                  25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 
                                                                                                  5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 
                                                                                                  20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 
                                                                                                  0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 
                                                                                                  15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 
                                                                                                  30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 
                                                                                                  10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 
                                                                                                  25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 
                                                                                                  5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 
                                                                                                  20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 
                                                                                                  0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 15, 20, 25, 30, 0, 5, 10, 
                                                                                                  15, 20, 25, 30), period = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                                                                                                                        2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("pre", "post"
                                                                                                                                        ), class = c("ordered", "factor"))), row.names = c(15L, 16L, 
                                                                                                                                                                                           17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 57L, 
                                                                                                                                                                                           58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 
                                                                                                                                                                                           71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 
                                                                                                                                                                                           84L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 
                                                                                                                                                                                           103L, 104L, 105L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 
                                                                                                                                                                                           121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 
                                                                                                                                                                                           132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 
                                                                                                                                                                                           143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 
                                                                                                                                                                                           154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 
                                                                                                                                                                                           165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 
                                                                                                                                                                                           176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 
                                                                                                                                                                                           187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 
                                                                                                                                                                                           198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 
                                                                                                                                                                                           209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 
                                                                                                                                                                                           220L, 221L, 222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 230L, 
                                                                                                                                                                                           231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 
                                                                                                                                                                                           242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 
                                                                                                                                                                                           260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 
                                                                                                                                                                                           271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 281L, 
                                                                                                                                                                                           282L, 283L, 284L, 285L, 286L, 287L, 295L, 296L, 297L, 298L, 299L, 
                                                                                                                                                                                           300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 310L, 
                                                                                                                                                                                           311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 321L, 
                                                                                                                                                                                           322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L, 
                                                                                                                                                                                           333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L, 342L, 343L, 
                                                                                                                                                                                           344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L, 353L, 354L, 
                                                                                                                                                                                           355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L, 364L, 365L, 
                                                                                                                                                                                           366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L, 
                                                                                                                                                                                           377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 
                                                                                                                                                                                           388L, 389L, 390L, 391L, 392L, 400L, 401L, 402L, 403L, 404L, 405L, 
                                                                                                                                                                                           406L, 407L, 408L, 409L, 410L, 411L, 412L, 413L), class = "data.frame")

Solution

  • The CRAN version of marginaleffects does not support lspline. Thankfully, recent improvements in the insight package (an upstream dependency) should now allow you should to do this.

    First, install the development version of both packages (will not be necessary next month, after CRAN release):

    library(remotes)
    install_github("easystats/insight")
    install_github("vincentarelbundock/marginaleffects")
    

    RESTART R COMPLETELY to make sure the change takes effect.

    Then, you can use the variables argument: the order in which you enter values determines the reference level.

    library(lme4)
    #> Loading required package: Matrix
    library(lspline)
    library(marginaleffects)
    
    m_weight <- lmer(
        delta_weight ~ lspline(MINUTES, knots = c(5)) * period + (1 | ID),
        data = dat)
    
    weight1 <- comparisons(
        m_weight,
        variables = list("period" = c("post", "pre")),
        include_random = FALSE,
        newdata = datagrid(MINUTES = seq(from = 0, to = 30, by = 5)))
    
    weight1
    #>   rowid     type   term   contrast      estimate std.error     statistic
    #> 1     1 response period pre - post  1.550531e-15 1.9043454  8.142070e-16
    #> 2     2 response period pre - post -5.480159e+00 1.3782640 -3.976131e+00
    #> 3     3 response period pre - post -5.632540e+00 1.0347416 -5.443426e+00
    #> 4     4 response period pre - post -5.784921e+00 0.8100799 -7.141173e+00
    #> 5     5 response period pre - post -5.937302e+00 0.8100799 -7.329279e+00
    #> 6     6 response period pre - post -6.089683e+00 1.0347416 -5.885221e+00
    #> 7     7 response period pre - post -6.242063e+00 1.3782640 -4.528932e+00
    #>        p.value  conf.low conf.high     predicted  predicted_hi  predicted_lo
    #> 1 1.000000e+00 -3.732448  3.732448 -1.904410e-14 -1.749357e-14 -1.904410e-14
    #> 2 7.004543e-05 -8.181507 -2.778811 -7.045635e+00 -1.252579e+01 -7.045635e+00
    #> 3 5.226536e-08 -7.660596 -3.604483 -7.263492e+00 -1.289603e+01 -7.263492e+00
    #> 4 9.253795e-13 -7.372648 -4.197193 -7.481349e+00 -1.326627e+01 -7.481349e+00
    #> 5 2.313945e-13 -7.525029 -4.349574 -7.699206e+00 -1.363651e+01 -7.699206e+00
    #> 6 3.975236e-09 -8.117739 -4.061626 -7.917063e+00 -1.400675e+01 -7.917063e+00
    #> 7 5.928265e-06 -8.943411 -3.540716 -8.134921e+00 -1.437698e+01 -8.134921e+00
    #>   delta_weight period ID MINUTES
    #> 1    -9.017857   post 10       0
    #> 2    -9.017857   post 10       5
    #> 3    -9.017857   post 10      10
    #> 4    -9.017857   post 10      15
    #> 5    -9.017857   post 10      20
    #> 6    -9.017857   post 10      25
    #> 7    -9.017857   post 10      30
    

    The confidence intervals seem fine:

    all(weight1$estimate > weight1$conf.low)
    #> [1] TRUE
    all(weight1$estimate < weight1$conf.high)
    #> [1] TRUE
    

    Whether you want to report predicted or comparison depends on your scientific/analytic interest; it is not a question for StackOverflow. Please read the vignettes on the website to learn the difference about those two quantities: https://vincentarelbundock.github.io/marginaleffects/