rggplot2tibblestat

extracting selected estimate and CI from a linear mixed model


I need help in extracting estimate and CIs only for yr_qun variable from my linear mixed model. when I run the following code I get

Error:

! Tibble columns must have compatible sizes. • Size 30: Existing data. • Size 32: Column ci_lower. ℹ Only values of size one are recycled.

any help much appreciated

library(lme4)       # linear mixed-effects models
library(lmerTest)   # test for linear mixed-effects models
library(gtsummary)
library(sjPlot)
library(ggplot2)

library(tidyverse)
library(tibble)

m1 <- lmer(distance ~ yr_qun+age+sex + (1 | id), data = trajectories)
summary(m1)

ci <- as.data.frame(confint(m1))
m1_estimate <- summary(m1)$coefficients[2:length(unique(trajectories$yr_qun)),1]
target <- setdiff(intersect(rownames(ci),rownames(summary(m1)$coefficients)),"(Intercept)")
m1_ci <- ci[target,]

summary(m1)

result <- tibble(name = names(m1_estimate), 
                 est = as.numeric(m1_estimate), 
                 ci_lower = as.numeric(m1_ci[,1]),
                 ci_upper = as.numeric(m1_ci[,2]))

here are my dummy data

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L), year = c(2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 
2010L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 
2010L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 
2009L, 2010L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 
2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 2004L, 2005L, 
2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 2004L, 
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 2003L, 
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2001L, 2002L, 
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L), distance = c(15, 
20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 21, 20, 21.5, 23, 
21, 21.5, 24, 25.5, 20.5, 24, 21, 20, 21.5, 23, 21, 21.5, 24, 
25.5, 20.5, 24, 21, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 
21, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 21, 20, 21.5, 
23, 21, 21.5, 21, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 
23, 21, 21.5, 24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 
24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 
24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 15, 20, 21.5, 
23, 21, 21.5, 24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 
24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 
24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 15, 20, 21.5, 
23, 21, 21.5, 24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 
24, 25.5, 20.5, 24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 
24, 15, 20, 21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24), age = c(8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 9L, 10L, 11L, 12L, 
13L, 14L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 28L, 40L, 41L, 42L, 43L, 44L, 45L, 
46L, 47L, 48L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 28L, 29L, 30L, 
31L, 32L, 33L, 34L, 35L, 36L, 37L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 
37L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 28L, 29L, 
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 28L, 29L, 30L, 31L, 32L, 
33L, 34L, 35L, 36L, 37L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L), 
    Quintile = structure(c(5L, 2L, 3L, 3L, 2L, 2L, 4L, 2L, 5L, 
    5L, 1L, 4L, 2L, 5L, 4L, 3L, 3L, 4L, 3L, 3L, 1L, 3L, 1L, 2L, 
    1L, 5L, 2L, 4L, 1L, 4L, 3L, 2L, 5L, 3L, 4L, 4L, 3L, 1L, 4L, 
    3L, 4L, 1L, 4L, 4L, 5L, 1L, 5L, 2L, 2L, 2L, 3L, 5L, 3L, 3L, 
    4L, 1L, 3L, 1L, 1L, 5L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 4L, 
    5L, 3L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 3L, 5L, 5L, 2L, 5L, 2L, 
    2L, 3L, 1L, 2L, 3L, 5L, 5L, 2L, 5L, 2L, 2L, 3L, 1L, 2L, 3L, 
    5L, 5L, 2L, 5L, 2L, 2L, 3L,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2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), levels = c("F", "M"), class = "factor"), yr_qun = structure(c(3L, 
    4L, 8L, 11L, 13L, 16L, 21L, 22L, 27L, 30L, 1L, 6L, 7L, 12L, 
    15L, 17L, 20L, 24L, 26L, 29L, 1L, 5L, 31L, 31L, 31L, 31L, 
    19L, 24L, 25L, 30L, 2L, 4L, 9L, 11L, 15L, 18L, 20L, 22L, 
    27L, 29L, 3L, 4L, 9L, 12L, 15L, 16L, 21L, 22L, 25L, 28L, 
    2L, 6L, 8L, 11L, 15L, 16L, 2L, 4L, 7L, 12L, 13L, 18L, 19L, 
    23L, 25L, 30L, 10L, 14L, 18L, 21L, 23L, 26L, 28L, 1L, 4L, 
    8L, 10L, 13L, 17L, 21L, 24L, 25L, 30L, 1L, 4L, 8L, 10L, 13L, 
    17L, 21L, 24L, 25L, 30L, 1L, 4L, 8L, 10L, 31L, 31L, 31L, 
    24L, 25L, 31L, 31L, 31L, 8L, 10L, 13L, 17L, 21L, 24L, 25L, 
    30L, 31L, 31L, 8L, 10L, 13L, 17L, 21L, 24L, 25L, 30L, 31L, 
    31L, 8L, 10L, 13L, 17L, 21L, 24L, 25L, 30L, 31L, 31L, 8L, 
    10L, 13L, 17L, 21L, 24L, 25L, 30L, 31L, 31L, 8L, 10L, 13L, 
    17L, 21L, 24L, 25L, 30L, 31L, 31L, 8L, 10L, 13L, 17L, 21L, 
    24L, 25L, 30L, 31L, 31L, 8L, 10L, 13L, 17L, 21L, 24L, 25L, 
    30L, 31L, 31L, 8L, 10L, 13L, 17L, 21L, 24L, 25L, 30L), levels = c("2001_low", 
    "2001_medium", "2001_high", "2002_low", "2002_medium", "2002_high", 
    "2003_low", "2003_medium", "2003_high", "2004_low", "2004_medium", 
    "2004_high", "2005_low", "2005_medium", "2005_high", "2006_low", 
    "2006_medium", "2006_high", "2007_low", "2007_medium", "2007_high", 
    "2008_low", "2008_medium", "2008_high", "2009_low", "2009_medium", 
    "2009_high", "2010_low", "2010_medium", "2010_high", ""), class = "factor")), class = "data.frame", row.names = c(NA, 
-183L))

Solution

  • If you look at what your combining, you should see the issue:

    > rownames(ci)
     [1] ".sig01"            ".sigma"            "(Intercept)"
     [4] "yr_qun2001_medium" "yr_qun2001_high"   "yr_qun2002_low"
     [7] "yr_qun2002_medium" "yr_qun2002_high"   "yr_qun2003_low"
    [10] "yr_qun2003_medium" "yr_qun2003_high"   "yr_qun2004_low"
    [13] "yr_qun2004_medium" "yr_qun2004_high"   "yr_qun2005_low"
    [16] "yr_qun2005_medium" "yr_qun2005_high"   "yr_qun2006_low"
    [19] "yr_qun2006_medium" "yr_qun2006_high"   "yr_qun2007_low"
    [22] "yr_qun2007_medium" "yr_qun2007_high"   "yr_qun2008_low"
    [25] "yr_qun2008_medium" "yr_qun2008_high"   "yr_qun2009_low"
    [28] "yr_qun2009_medium" "yr_qun2009_high"   "yr_qun2010_low"
    [31] "yr_qun2010_medium" "yr_qun2010_high"   "yr_qun"
    [34] "age"               "sexM"
    
    
    > names(m1_estimate)
     [1] "yr_qun2001_medium" "yr_qun2001_high"   "yr_qun2002_low"
     [4] "yr_qun2002_medium" "yr_qun2002_high"   "yr_qun2003_low"
     [7] "yr_qun2003_medium" "yr_qun2003_high"   "yr_qun2004_low"
    [10] "yr_qun2004_medium" "yr_qun2004_high"   "yr_qun2005_low"
    [13] "yr_qun2005_medium" "yr_qun2005_high"   "yr_qun2006_low"
    [16] "yr_qun2006_medium" "yr_qun2006_high"   "yr_qun2007_low"
    [19] "yr_qun2007_medium" "yr_qun2007_high"   "yr_qun2008_low"
    [22] "yr_qun2008_medium" "yr_qun2008_high"   "yr_qun2009_low"
    [25] "yr_qun2009_medium" "yr_qun2009_high"   "yr_qun2010_low"
    [28] "yr_qun2010_medium" "yr_qun2010_high"   "yr_qun"
    
    > target
     [1] "yr_qun2001_medium" "yr_qun2001_high"   "yr_qun2002_low"
     [4] "yr_qun2002_medium" "yr_qun2002_high"   "yr_qun2003_low"
     [7] "yr_qun2003_medium" "yr_qun2003_high"   "yr_qun2004_low"
    [10] "yr_qun2004_medium" "yr_qun2004_high"   "yr_qun2005_low"
    [13] "yr_qun2005_medium" "yr_qun2005_high"   "yr_qun2006_low"
    [16] "yr_qun2006_medium" "yr_qun2006_high"   "yr_qun2007_low"
    [19] "yr_qun2007_medium" "yr_qun2007_high"   "yr_qun2008_low"
    [22] "yr_qun2008_medium" "yr_qun2008_high"   "yr_qun2009_low"
    [25] "yr_qun2009_medium" "yr_qun2009_high"   "yr_qun2010_low"
    [28] "yr_qun2010_medium" "yr_qun2010_high"   "yr_qun"
    [31] "age"               "sexM"
    

    Notice, that target has elements age and sexM, which are not in m1.

    You can recreate target so that it only includes variables with substring qun:

    intersect(rownames(ci),rownames(summary(m1)$coefficients))[grepl( '*qun*', intersect(rownames(ci),rownames(summary(m1)$coefficients)))] -> target
    

    Now:

    m1_ci <- ci[target,]
    result <- tibble(name = names(m1_estimate), 
                     est = as.numeric(m1_estimate), 
                     ci_lower = as.numeric(m1_ci[,1]),
                     ci_upper = as.numeric(m1_ci[,2]))
    
    
    
    > result
    # A tibble: 30 × 4
       name                est ci_lower ci_upper
       <chr>             <dbl>    <dbl>    <dbl>
     1 yr_qun2001_medium 3.81     1.95      5.61
     2 yr_qun2001_high   0.946   -1.17      3.00
     3 yr_qun2002_low    2.83     1.40      4.23
     4 yr_qun2002_medium 2.02    -0.604     4.74
     5 yr_qun2002_high   2.72     0.658     4.75
     6 yr_qun2003_low    4.13     2.03      6.18
     7 yr_qun2003_medium 4.56     3.24      5.84
     8 yr_qun2003_high   4.24     2.18      6.28
     9 yr_qun2004_low    6.04     4.70      7.34
    10 yr_qun2004_medium 5.94     4.11      7.72
    # … with 20 more rows
    # ℹ Use `print(n = ...)` to see more rows