Search code examples
rkablegtsummaryflextablenlme

How to convert nlme summary object into table


I have the following dataset

df3 = 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", "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", "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", "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"), B = 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, 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, 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, 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), 
    C = c("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", "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", "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", "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"), D = c(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, 
    73, 73, 73, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
    71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
    71, 71, 71, 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, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
    71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
    71, 71, 71, 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, 
    73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 
    74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 
    74, 74, 74), E = c(136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 136.061064886813, 
    136.061064886813, 136.061064886813, 136.061064886813, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 91.138451650252, 91.138451650252, 91.138451650252, 
    91.138451650252, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 144.422871899338, 
    144.422871899338, 144.422871899338, 144.422871899338, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 105.603501603159, 105.603501603159, 105.603501603159, 
    105.603501603159, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 128.444375466774, 
    128.444375466774, 128.444375466774, 128.444375466774, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815, 57.1855792612815, 57.1855792612815, 57.1855792612815, 
    57.1855792612815)), row.names = c(NA, -180L), class = c("tbl_df", 
"tbl", "data.frame"))

on which I have run the following code to get the mixed model

obj = (nlme::lme(E ~ B + C + D, random = ~1|A, df3) %>% 
      summary())$tTable 

flextable(obj)

however I am not able to get the table in which showing the results. Has anyone any idea to get the table with any package that you you (flextable, gtsummary, kableExtra and so on)? Of course I would like to get all the columns (including variable names)

                   Value  Std.Error   DF     t-value      p-value
(Intercept) -12.68019966 3.10808245 1827  -4.0797501 4.702378e-05
B            -0.24611480 0.02288007   59 -10.7567325 1.557930e-15
C1            0.36800234 3.57315266   59   0.1029909 9.183194e-01
D             0.08818415 0.02749166   59   3.2076696 2.163542e-03

Thanks


Solution

  • With flextable, you can use as_flextable() to convert models into tables.

    library(flextable)
    
    obj <- nlme::lme(E ~ B + C + D, random = ~1|A, df3)
    as_flextable(obj)
    

    enter image description here

    With the dev version, there is support for options(show.signif.stars = FALSE).

    enter image description here

    If you don't want to keep the footer part, just drop it with delete_part()

    as_flextable(obj) |> delete_part(part = "footer")
    

    enter image description here