rgtsummarylmertest

How to generate table for linear mixed model with gtsummary


I would like to reproduce linear mixed models tables with gtsummary. This is the dataset I am working on

dput(head(dt, 300))
structure(list(A = c("51", "51", "51", "51", "51", "51", "51", 
"51", "51", "51", "51", "51", "51", "51", "51", "51", "51", "51", 
"51", "51", "51", "51", "51", "51", "51", "51", "51", "51", "51", 
"51", "51", "51", "51", "52", "52", "52", "52", "52", "52", "52", 
"52", "52", "52", "52", "52", "52", "52", "52", "52", "52", "52", 
"52", "52", "52", "52", "52", "52", "52", "52", "52", "52", "52", 
"52", "52", "52", "52", "52", "53", "53", "53", "53", "53", "53", 
"53", "53", "53", "53", "53", "53", "53", "53", "53", "53", "53", 
"53", "53", "53", "53", "53", "53", "53", "53", "53", "53", "53", 
"53", "53", "53", "53", "53", "53", "54", "54", "54", "54", "54", 
"54", "54", "54", "54", "54", "54", "54", "54", "54", "54", "54", 
"54", "54", "54", "54", "54", "54", "54", "54", "54", "54", "54", 
"54", "54", "54", "54", "54", "54", "54", "55", "55", "55", "55", 
"55", "55", "55", "55", "55", "55", "55", "55", "55", "55", "55", 
"55", "55", "55", "55", "55", "55", "55", "55", "55", "55", "55", 
"55", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", 
"56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", 
"56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "57", 
"57", "57", "57", "57", "57", "57", "57", "57", "57", "57", "57", 
"57", "57", "57", "57", "57", "57", "57", "57", "57", "57", "57", 
"57", "57", "57", "57", "57", "57", "57", "57", "57", "57", "58", 
"58", "58", "58", "58", "58", "58", "58", "58", "58", "58", "58", 
"58", "58", "58", "58", "58", "58", "58", "58", "58", "58", "58", 
"58", "58", "58", "58", "58", "58", "58", "58", "59", "59", "59", 
"59", "59", "59", "59", "59", "59", "59", "59", "59", "59", "59", 
"59", "59", "59", "59", "59", "59", "59", "59", "59", "59", "59", 
"59", "59", "59", "59", "60", "60", "60", "60", "60", "60", "60", 
"60", "60", "60", "60", "60", "60", "60"), B = c("1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), C = c(84.5, 
84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 
84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 
84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 84.5, 72, 
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 
72, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 
62, 62, 62, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 
88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 
88, 88, 88, 88, 88, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 67.5, 
67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 
67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 
67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 67.5, 96, 
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 76, 76, 
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 114, 114, 114, 114, 
114, 114, 114, 114, 114, 114, 114, 114, 114, 114), D = c("A", 
"B", "C", "D", "E", "F", "G", "H", "J", "K", "I", "L", "M", "N", 
"O", "P", "Q", "R", "S", "U", "W", "X", "Y", "Z", "A1", "B1", 
"C1", "D1", "E1", "F1", "G1", "H1", "J1", "A", "B", "C", "D", 
"E", "F", "G", "H", "J", "K", "I", "L", "M", "N", "O", "P", "Q", 
"R", "S", "U", "W", "X", "Y", "Z", "A1", "B1", "C1", "D1", "E1", 
"K1", "F1", "G1", "H1", "J1", "A", "B", "C", "D", "E", "F", "G", 
"H", "J", "K", "I", "L", "M", "N", "O", "P", "Q", "R", "S", "U", 
"W", "X", "Y", "Z", "A1", "B1", "C1", "D1", "E1", "K1", "F1", 
"G1", "H1", "J1", "A", "B", "C", "D", "E", "F", "G", "H", "J", 
"K", "I", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "W", "X", 
"Y", "Z", "A1", "B1", "C1", "D1", "E1", "K1", "F1", "G1", "H1", 
"J1", "A", "C", "F", "G", "H", "J", "K", "I", "L", "M", "N", 
"O", "P", "Q", "R", "U", "W", "X", "Y", "Z", "A1", "D1", "E1", 
"F1", "G1", "H1", "J1", "A", "B", "C", "D", "E", "F", "G", "H", 
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "U", "W", "X", 
"Y", "A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "J1", "A", 
"B", "C", "D", "E", "F", "G", "H", "J", "K", "I", "L", "M", "N", 
"O", "P", "Q", "R", "S", "U", "W", "X", "Y", "Z", "A1", "B1", 
"C1", "D1", "E1", "F1", "G1", "H1", "J1", "A", "B", "C", "E", 
"F", "G", "H", "J", "K", "I", "M", "N", "O", "P", "Q", "R", "S", 
"U", "W", "X", "Y", "Z", "A1", "B1", "C1", "D1", "E1", "F1", 
"G1", "H1", "J1", "A", "B", "C", "D", "E", "F", "G", "H", "J", 
"I", "M", "N", "O", "P", "Q", "R", "U", "W", "X", "Y", "Z", "A1", 
"C1", "D1", "E1", "F1", "G1", "H1", "J1", "A", "B", "C", "D", 
"E", "F", "H", "P", "Q", "R", "S", "U", "Z", "A1"), E = c(53.5486131719382, 
52.0658832701111, 51.1252703023116, 59.6739545998032, 85.865976725962, 
113.631908740832, 63.1049775640581, 54.4456826404504, 93.5395932868018, 
93.995437783387, 84.3354719738765, 63.4595744313194, 78.9841448658563, 
80.0955418627145, 57.3693357479924, 63.8069855423248, 54.5435047121041, 
54.4359096471545, 104.207884372387, 60.6889459499874, 76.1778733075911, 
69.0583401518195, 104.47253354457, 83.7416789866701, 50.2210260529369, 
80.6275644735832, 61.198042471331, 83.3120233171705, 55.2494834957416, 
73.3166134328764, 54.1755327745905, 101.902052850692, 51.7545000131658, 
62.1777969600058, 68.1653724448048, 55.1729911875874, 60.8386538371058, 
70.0968994187555, 78.942019990113, 59.775608067045, 67.0071207629859, 
84.7558493290474, 59.9827945641954, 66.5101252750979, 90.5019717484792, 
64.9092937722916, 58.3571820997381, 66.6329432096269, 67.1205696362189, 
50.6509772755951, 50.6250541888198, 51.3032509054271, 54.5634240777667, 
100.737462226385, 60.3790294243842, 68.3600912175845, 73.7351241074605, 
50.0872462865671, 50.795767069448, 70.8001111843104, 79.5524955072498, 
50.260631010864, 73.9586203858578, 55.9975862773843, 60.0923749090369, 
88.4356678154069, 61.3205867721683, 51.9208379138623, 77.7114105505713, 
51.1320936704931, 97.5576527975265, 109.408798916546, 97.5582215974771, 
62.3086020090593, 57.8304273171539, 86.4260724801027, 50.6136060462806, 
159.226033089812, 142.527435748128, 68.2509910677053, 50.7585563161078, 
72.5319013227746, 66.3728176444309, 51.3970723729174, 50.7052373649285, 
55.8340531142269, 77.722629180697, 77.1731558747748, 64.2394186691872, 
189.095920893828, 60.615727186107, 50.3645135141208, 55.9942816672048, 
128.159899427594, 52.0700161057344, 50.6764380248669, 66.5915700948859, 
52.293288405534, 50.8597241625586, 56.0309438799112, 65.6639233566106, 
53.2984048049127, 54.4032755860799, 54.5345061904833, 87.3398149095851, 
70.0394162153045, 62.5193493531448, 50.5776432558593, 61.3488842907225, 
54.3323955416404, 68.8961149578927, 72.2724562848431, 76.9210030237276, 
50.981156113135, 50.9657184258452, 58.142600273058, 62.5910834427581, 
52.1073411367228, 50.6432911385476, 103.558080309759, 81.233576570001, 
55.9546368219274, 58.9731261750758, 65.193324003293, 73.6731770381325, 
50.9849437764464, 68.252760924268, 77.5045817920251, 57.9762684219772, 
51.0383669946158, 80.0240979940811, 53.20028505691, 57.02354463002, 
55.7567157271443, 56.0506846957972, 80.4283499174123, 55.7842328037292, 
207.997342234812, 51.6921739529286, 57.3491380912525, 90.5056460230801, 
72.6707680779293, 72.8711020235701, 114.977472897774, 53.932054643335, 
53.6158517524058, 81.5473592217656, 79.9567024686436, 63.3500072523791, 
50.5485207695456, 118.822052950273, 101.271446713148, 61.2166167463886, 
64.0107863032943, 66.6487762165541, 58.8583604808201, 64.3599615688179, 
79.9317242811825, 127.161491565041, 76.3743045499247, 120.486969772759, 
67.4582106949668, 52.2450907798009, 119.762572667194, 50.4877114728201, 
82.7574060383183, 96.4140642808373, 108.389200406038, 54.1839718991094, 
58.5138428864389, 131.008826055881, 125.154978277626, 132.98212853187, 
98.929007653993, 68.8806138378277, 59.8788185288713, 56.976956121695, 
50.9102162899625, 51.2726148809701, 198.15146866854, 117.580941983352, 
201.78668948985, 103.854766901191, 159.690331183389, 54.9661582983925, 
194.1059243319, 67.2079451571218, 111.170233038542, 82.1453983419786, 
53.4915551572879, 54.3929611700716, 131.956661489323, 65.1562983240275, 
62.0289313492167, 162.4013400668, 53.879472459576, 159.287884346134, 
57.6989146237285, 52.1184249234992, 56.3289893502059, 53.235865231291, 
81.4312256692937, 79.1786714635215, 111.473904766285, 56.4795708235795, 
55.8204724082951, 57.8464980301213, 53.9811120574278, 59.8141814126068, 
55.1338340280094, 51.2196605317086, 84.1339094964576, 87.2396533788401, 
72.9237013420686, 118.76035795556, 70.9057425870373, 58.9326035381529, 
53.716759573972, 87.6656537129987, 52.7109830793293, 67.6138256264123, 
78.4068987953149, 61.8307880411051, 60.8013473936248, 59.4060994383861, 
98.085608459632, 64.5803707894519, 177.46836973785, 54.8477234996859, 
81.8170015518669, 69.2043125654496, 55.7846278423678, 57.1220876444381, 
55.8905809420105, 54.1240432686922, 153.948238453985, 57.921133450344, 
53.0666207122496, 62.0165981264953, 66.2163973372594, 50.3211074843001, 
50.4421905328129, 96.6862483702221, 165.023892857049, 55.3657894204705, 
61.8236534116221, 145.086461239493, 69.9904449190297, 50.2193262019902, 
112.998708003981, 77.4625319190458, 61.71826548918, 53.1582614429681, 
55.9027809250359, 64.7531484148432, 83.63819468709, 91.0574781582018, 
53.1836963432468, 67.5673750775146, 51.3066517681853, 123.181235932417, 
62.3288352087814, 78.6417479791861, 60.2225325746383, 52.7474995165124, 
166.645115814437, 102.414406703632, 76.1537411117896, 84.163010557389, 
53.3394167031625, 57.5540925685454, 54.8229784073972, 52.0681640127194, 
137.667634492671, 144.556542883844, 77.5384791431714, 58.8787603447788, 
61.0540530917981, 54.5532762632947, 62.1100731411146, 55.5283650723314, 
75.9959185450757, 52.3378671680705, 50.9931045174822, 52.2793494215867, 
56.2793169338571, 70.6715821731965, 97.1724466705225, 58.9404344307155, 
69.6992530973357, 70.9345181855089, 82.1063878264692, 162.422136003165, 
173.873478085856, 68.3646841369622, 56.923573242475, 103.313569709538, 
65.822202907022, 116.656650145337, 58.5151713891827)), row.names = c(NA, 
-300L), class = c("tbl_df", "tbl", "data.frame"))

and here as follows there is the code that I used that returns to me this code.

library(flextable)
library(gtsummary)
library(lmerTest)

dt %>%
  tbl_strata(
    strata = D, 
    ~ lmerTest::lmer(E ~ C*B + (1|A), data = .x) %>%
      tbl_regression(),
    .combine_with = "tbl_stack"
  ) %>%
  modify_column_unhide(c(std.error, statistic)) %>%
  as_flex_table()

However it returns the following error and I cannot realize either a solution or an an alternative to generate this table.

Error in `mutate()`:
ℹ In argument: `tbl = switch(...)`.
Caused by error in `map()`:
ℹ In index: 1.
Caused by error:
! number of levels of each grouping factor must be < number of observations (problems: A)

Is there anyone that can help?

Thanks


Solution

  • If you really want to fit these models even though the random effect is confounded with the residual variance term (that's what the error is trying to tell you) you can override the check using lmerControl() as shown below.

    Another alternative would be to use glmmTMB(..., dispformula = ~0) to force the residual variance to zero (although you would have to give up Satterthwaite/Kenward-Roger finite-size corrections).

    dt %>%
      dplyr::filter(D != "K1") %>%
      tbl_strata(
        strata = D, 
        ~ lmerTest::lmer(E ~ C*B + (1|A), data = .x,
                         control = lmerControl(check.nobs.vs.nlev="ignore",
                                               check.nobs.vs.nRE="ignore",
                                               )) %>%
          tbl_regression(),
        .combine_with = "tbl_stack"
      ) %>%
      modify_column_unhide(c(std.error, statistic)) %>%
      as_flex_table()
    

    I tried to get gtsummary to work with lme (which completely ignores the confounding/non-identifiability), but it was a little too much work ...