0

I'm going to create a series of table

library(sjPlot)    
tables_models <- lapply(models_list_2, tab_model, show.ci = FALSE, 
           show.se = TRUE, 
           show.p = TRUE, 
           show.stat = TRUE, 
           show.df = TRUE, 
           show.ngroups = TRUE,
           digits = 4,
           digits.p = 3, 
           digits.re = 3,
           **title = title**,
           string.pred = "Predictors",
           string.est = "Estimate",
           string.se = "std. Error",
           string.p = "Pr(>|t|)",
           string.df = "df",
           string.stat = "t-value",
           string.intercept = "(Intercept)", 
           df.method = 'kr', 
           p.val = 'kr', 
           p.style = 'numeric_stars') %>% 
      setNames(sort(unique(out_long$signals)))

Where the title input should assign a title like 'Description of....' and then proper related signals' names which it represents:

['P3(400-450).FCz', 'P3(400-450).Cz', 'P3(400-450).Pz', 'LPPearly(500-700).FCz', 'LPPearly(500-700).Cz', 'LPPearly(500-700).Pz', 'LPP1(500-1000).FCz', 'LPP1(500-1000).Cz', 'LPP1(500-1000).Pz', 'LPP2(1000-1500).FCz', 'LPP2(1000-1500).Cz', 'LPP2(1000-1500).Pz', 'LPP2(1000-1500).POz']

For title I just had a try creating the following object,

    title <- c('P3(400-450).FCz', 'P3(400-450).Cz', 'P3(400-450).Pz',
      'LPPearly(500-700).FCz', 'LPPearly(500-700).Cz',
      'LPPearly(500-700).Pz', 'LPP1(500-1000).FCz', 
      'LPP1(500-1000).Cz', 'LPP1(500-1000).Pz', 
      'LPP2(1000-1500).FCz', 'LPP2(1000-1500).Cz',
     'LPP2(1000-1500).Pz', 'LPP2(1000-1500).POz')

That just run as it is, the first code names each table with the first names indicated into the brackets, or 'P3(400-450).FCz'

How should I set out the title input for assign each name reported into the title object for a different table?

Thanks for paying attention

Here the dataset I'm working

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 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("P3FCz", 
    "P3Cz", "P3Pz", "LPPearlyFCz", "LPPearlyCz", "LPPearlyPz", 
    "LPP1FCz", "LPP1Cz", "LPP1Pz", "LPP2FCz", "LPP2Cz", "LPP2Pz", 
    "LPP2POz"), class = "factor"), 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"))

and the commands lines which I fitted the model through (because these are tables showing model output)

library(lme4) 
library(dplyr)

    models_list_2 <- out_long %>%
      group_by(signals) %>%
      do(fit = lmerTest::lmer(value ~ COND + (1|ID), data = .)) %>% 
      pull(fit)
12666727b9
  • 1,133
  • 1
  • 8
  • 22
  • There seem to be muptiple issues, but most importantly I don't understand what you are trying to do (*"How should I set out the title input for assign the title I would like to assign? "* - where does `title input` (<- what is this??) come into play?). Also, I highly doubt that you can use the generic `save` function to create a word file (I'd suggest looking into RMarkdown for that). Finally, it's better to ask only one specific question per post and not multiple questions at the same time... – dario Nov 03 '21 at 09:10
  • @dario Please check through the forst commands line I've reported. Amongst the other inputs (show.se, show.df, and so on), there is also title. Since this command creates a series of tables, that input is supposed to assign to each table the following wording: Description of (name of the signals, indicated as bold character) for each of them. For example, the first table, the should 'Description of P3 (400-450)FCZ. The next one, 'Description of P3(400-450)Cz and so on. Don't know of it is clearer. Let me know – 12666727b9 Nov 03 '21 at 09:23
  • Furthermore, I've tried also using RMarkdown for reporting tables but actually I'm jot able too. Since the file input is included in this tab-model() finction I reported both the doubts here in this post. – 12666727b9 Nov 03 '21 at 09:26
  • I think it's mostly a language issue, but I'm still not 100% sure if I understand what you want to say... However, it *looks* to me like if you'd want to use `mapply` instead of `apply` with the titles as second argument... – dario Nov 03 '21 at 09:29
  • Ok. What exaclty is it not so clear? I'll try explaing better – 12666727b9 Nov 03 '21 at 09:31
  • Did `mapply` not work? If not, what was the problem? – dario Nov 03 '21 at 09:33
  • Ok. Would you try to show please through an example what do you mean? – 12666727b9 Nov 03 '21 at 09:33
  • I tried my best to give you an example that's easy to understand. Since the code you provided is not reproducible I had to use dummy code. But I'd think the solution is easy enough that you should be able to adapt it to your usecase. It's always better to provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and an example of the desired output (in code form, not tables and pictures). It makes it much easier for others to find and test an answer to your question. That way you can help others to help you! – dario Nov 03 '21 at 09:49
  • It is a nice example. But actually to me it is quite difficult to fit to my case. Actually also this pate0() function could be useful. Indeed, if I enter into the title input of tab_model function it applies automatically the wording 'Descritpion of' to each table. The problem I'm metting is that, inspite of the vectopr title I created, containing the signals' names, they results overriden: I mean that they report always the same title 'Description of P3(400-450).FCz. – 12666727b9 Nov 03 '21 at 10:02
  • They should have always a different name: the first table (title: Desc of P3(400-450).FCz, the second (title ì: Desc of P3(400-450).Cz) and so onù – 12666727b9 Nov 03 '21 at 10:03
  • *"They should have always a different name: t"* If you check the otuput of my example, it does exactly that! – dario Nov 03 '21 at 10:04
  • You might want to wrap the `tab_model` function in a function to pass it the `title` argument. But again, provide a MRE and it would be much easier to help you... – dario Nov 03 '21 at 10:06
  • Still, I can't reproduce your issues... for example: Where does `tab_model` fucntion come from... And I don't have the `lmerTest` package installed, so I wouldn't be able to run the code anyways. But this should not be an issue in the first place since your question has nothing to do with `lmerTest`. But the question should include a MRE to reproduce the problem and test an answer for you. Again: The M in MRE stands for minimal... I really tried to help you out but you make it kind of difficult.. – dario Nov 03 '21 at 10:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238837/discussion-between-dario-and-maly-statystyczny). – dario Nov 03 '21 at 14:03

1 Answers1

1

Using mapply in case where we would want apply but on multiple lists/vectors (i.e. one list has models and another the titles we want to use:)

From ?mapply:

mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

models <- 1:4
titles <- paste0("Desc of ", models)

dummy_fun  <- function(x, y, other1, other2) return(paste0(x, y, other1, other2))

We pass the two (ore more) equal length vectors as unnamed arguments, FUN takes the function we want to apply and all other (constant) arguments we want to pass to FUN go packet into a list into MoreArgs

mapply(models, titles, FUN = dummy_fun, MoreArgs = list(other1 = "a", other2 = "b"))

       
dario
  • 6,415
  • 2
  • 12
  • 26