0

I am trying to build some tables, each for every elemnt (13 in total) of a list containing statistics from an lsmeans statistics, computed via this commands lines

md <- out_long %>%
  group_by(signals) %>%
  do(fit = lmerTest::lmer(value ~ COND + (1 |ID), data = .)) %>% 
  pull(fit) %>% 
  lapply(., function(m) lsmeans(m, pairwise ~ COND, adjust="tukey"))

Each element contains the following statistics (i.e. the first one):

$lsmeans
 COND    lsmean    SE   df lower.CL upper.CL
 NEG-CTR  2.471 0.772 38.9    0.909     4.03
 NEG-NOC  3.024 0.772 38.9    1.463     4.59
 NEU-NOC  0.711 0.772 38.9   -0.850     2.27

Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95 

$contrasts
 contrast              estimate    SE df t.ratio p.value
 (NEG-CTR) - (NEG-NOC)   -0.554 0.644 48  -0.860  0.6678
 (NEG-CTR) - (NEU-NOC)    1.760 0.644 48   2.735  0.0233
 (NEG-NOC) - (NEU-NOC)    2.314 0.644 48   3.595  0.0022

Degrees-of-freedom method: kenward-roger 
P value adjustment: tukey method for comparing a family of 3 estimates 

Desired outcome

In the case I need to build elegant tables for them, or specifically one single table for each elements, including both lsmeans statistics and constrast ones, how should I proceed?

Here the data I'm working on

> dput(head(out_long,50))
structure(list(ID = c("01", "01", "01", "04", "04", "04", "06", 
"06", "06", "07", "07", "07", "08", "08", "08", "09", "09", "09", 
"10", "10", "10", "11", "11", "11", "12", "12", "12", "13", "13", 
"13", "15", "15", "15", "16", "16", "16", "17", "17", "17", "18", 
"18", "18", "19", "19", "19", "21", "21", "21", "22", "22"), 
    GR = c("RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V"), COND = c("NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC"
    ), signals = c("P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", "P3FCz", 
    "P3FCz", "P3FCz", "P3FCz"), value = c(-11.6312151716924, 
    -11.1438413285935, -3.99591470944713, -0.314155675382471, 
    0.238885648959708, 5.03749946898385, -0.213621915029167, 
    -2.96032491743069, -1.97168681693488, -2.83109425298642, 
    1.09291198163802, -6.692991645215, 4.23849942428043, 2.9898889629932, 
    3.5510699900835, 9.57481668808606, 5.4167795618285, 1.7067607715475, 
    -6.13036076093477, -2.82955734597919, -2.50672211111696, 
    0.528517585832501, 8.16418133488309, 1.88777321897925, -7.73588468896919, 
    -9.83058052401056, -6.97442700196932, 1.27327945355082, 2.11962397764132, 
    0.524299677616254, -1.83310726842883, 0.658810483381172, 
    -0.261373488428192, 4.37524298634374, 0.625555654900511, 
    3.19617639836154, 0.0405517582137798, -3.29357103412113, 
    -0.381435057304614, -5.73445509910268, -6.1129152355645, 
    -2.45744234877604, 2.95352732001065, 0.527721249096473, 1.91803490989119, 
    -3.46703346467546, -2.40438419043702, -5.35374408162217, 
    -7.27028665849262, -7.1532211375959)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))
> 
12666727b9
  • 1,133
  • 1
  • 8
  • 22

1 Answers1

1

Does this give you the desired output?

broom::tidy(md[[1]]$lsmeans)
# A tibble: 3 x 6
COND    estimate std.error    df statistic p.value
<chr>      <dbl>     <dbl> <dbl>     <dbl>   <dbl>
1 NEG-CTR    -1.42      1.16  23.4    -1.22    0.234
2 NEG-NOC    -1.41      1.16  23.4    -1.21    0.239
3 NEU-NOC    -1.10      1.18  24.3    -0.935   0.359

broom::tidy(md[[1]]$contrasts)
# A tibble: 3 x 8
term  contrast              null.value estimate std.error    df statistic adj.p.value
<chr> <chr>                      <dbl>    <dbl>     <dbl> <dbl>     <dbl>       <dbl>
1 COND  (NEG-CTR) - (NEG-NOC)          0  -0.0166     0.869  31.0   -0.0191       1.00 
2 COND  (NEG-CTR) - (NEU-NOC)          0  -0.320      0.888  31.1   -0.360        0.931
3 COND  (NEG-NOC) - (NEU-NOC)          0  -0.303      0.888  31.1   -0.342        0.938

To produce flextables for different objects in one go, you can do the following:

library(tidyverse)
library(lubridate)
library(purrr)

md[[1]] %>%
  map(
    ~broom::tidy(.x) %>%
      flextable::flextable()
    )
Jakub.Novotny
  • 2,912
  • 2
  • 6
  • 21
  • No actually the table I'm looking for has to be created by using one of this package: gtsummay(), flextable, siPlot() and so on. – 12666727b9 Nov 04 '21 at 15:35
  • @mały_statystyczny So? The output is a data.frame which can be used as input for table packages. See the updated answer. – Jakub.Novotny Nov 04 '21 at 15:39
  • Yes, that was the attempt I did. By the way I'm interest to develpo a code for converting all the elemnts stored like you did just for the first one and convert them as a table. Just in one way – 12666727b9 Nov 04 '21 at 15:45
  • Probably I was not that clear. It would be better to have a iterating code that does this opeerations on all the lists' elements. – 12666727b9 Nov 04 '21 at 15:56
  • @mały_statystyczny I am afraid I do not follow. The code I provided does iterate on all elements and produces table for each object. In your example, there are lsmeans and contrasts objects and for both objects a plot is produced. – Jakub.Novotny Nov 04 '21 at 16:40
  • OK. I do not know wether you could try running the code I've used to create the list with contating the 13 statistics elements for lsmean() function. I've just shown the first one to make clearer as to the content of each of them. If you meanwhile could attempt some solution it would be better. By the way, since the tables to be modify properly, I think I'll open another post – 12666727b9 Nov 04 '21 at 16:40
  • 1
    @mały_statystyczny posting another question sounds like a good idea. – Jakub.Novotny Nov 04 '21 at 16:44
  • 1
    This is probably what you are looking for, but I am really not sure. It is hard to help you if the dput provided does not sufficiently represents your problem. md %>% map(~.x %>% map( ~broom::tidy(.x) %>% flextable::flextable() ) ) – Jakub.Novotny Nov 04 '21 at 16:54
  • Exactly men. Genius. That's it. Now I really another small issue. If I would like to assign to each lsmean() output the a title as follows: `lsmeans of (and proper signals name)` and for contrasts tables, a title like `Constrast for (proper signals names)`, what should I do? – 12666727b9 Nov 04 '21 at 17:02
  • @mały_statystyczny I am afraid I do not understand what you mean- – Jakub.Novotny Nov 04 '21 at 17:16
  • I've just opened a new post `https://stackoverflow.com/questions/69843422/tables-assignation-for-reiterated-flextable` – 12666727b9 Nov 04 '21 at 17:29