Home > Net >  Creation of table from lists elements containing lsmeans() statistics
Creation of table from lists elements containing lsmeans() statistics

Time:11-05

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"))
> 

CodePudding user response:

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()
    )
  • Related