0

I've got the coefficients related to some linear mixed models and stored into a list in the following way:

models_list_1 <- data_long %>%
  group_by(signals) %>%
  do(fit = lmerTest::lmer(value ~ COND*SES + (1 |ID), data = .)) %>% 
  pull(fit) %>% 
  lapply(., function(x) summary(x)$coefficients) %>% 
  setNames(unique(data_long$signals))

Since I'm interested to reproduce results iteratively into some table, I'm trying running the follwing code

models_list_1 %>%  
  map(.x ~broom::tidy() %>%
    flextable::flextable()
)

But getting back this error

Error: Can't convert a two-sided formula to a function

Does anyone know how to correct syntaxis?

12666727b9
  • 1,133
  • 1
  • 8
  • 22
  • The anonymous function you use in map is not defined correctly. Check the `map` documentation for correct definition. Hint: The left side must be empty. – AEF Nov 09 '21 at 12:41
  • So I'm quite new with R. Cannot understand how to fix this – 12666727b9 Nov 09 '21 at 12:43
  • I don't want to be rude, but I really suggest you use this as an opportunity to learn a bit more about it. The documentation of `map` is really good and there are many examples on how to use the formula syntax in map. – AEF Nov 09 '21 at 12:47

1 Answers1

2

You need to start the formula with ~ and not with .x using map:

models_list_1 %>%  
  map(
    ~ .x %>% broom::tidy() %>% pull(x) %>% as_tibble() %>% flextable::flextable()
  )
danlooo
  • 10,067
  • 2
  • 8
  • 22
  • It turns back the following error `Error in x$data[i, j] <- value : number of items to replace is not a multiple of replacement length In addition: Warning message` – 12666727b9 Nov 09 '21 at 12:50
  • I've just tapped into the example here reported https://stackoverflow.com/questions/69841042/creation-of-table-from-lists-elements-containing-lsmeans-statistics. I know that the code should me just rehandle but it escapes me how – 12666727b9 Nov 09 '21 at 12:52
  • `broom::tidy` creates a nested one column data frame which needs to be unpacked with pull() to be passed to `flextable` – danlooo Nov 09 '21 at 13:06
  • Thank you so much, but there is still an issue that is left. I saw that when the `as_tibble` is run that keeps out the columns with predictors elements `(I mean interceprt, COND, SES and so on)`. Is there a way to keep it? – 12666727b9 Nov 10 '21 at 09:33
  • There is also the function `coefficients` to do e.g. `iris %>% lmer(Petal.Width ~ Petal.Length + (1|Species), data = .) %>% summary() %>% coefficients()` – danlooo Nov 10 '21 at 09:38
  • Ok, but modifying the code as you've written `data_long %>% group_by(signals) %>% lmerTest::lmer(value ~ COND*SES + (1 |ID), data = .) %>% summary() %>% coefficients() ` it just turn me back the statistics of one signals, not for all the 13 I have into the dataset. Or probably I didn't get your clue – 12666727b9 Nov 10 '21 at 09:49
  • I've tried setting the code like this `data_long %>%group_by(signals) %>%do(fit = lmerTest::lmer(value ~ COND*SES + (1 |ID), data = .)) %>% pull(fit) %>% lapply(., function(x) summary(x) %>% coefficients()) ` and I got back the same error – 12666727b9 Nov 10 '21 at 09:54
  • Do `lapply(function(x) summary(x) %>% coefficients())` instead of `lapply(., function(x) summary(x) %>% coefficients()) ` – danlooo Nov 10 '21 at 10:09
  • It is the same. It push away the columns of `intercept COND and so on` when it prints te tables – 12666727b9 Nov 10 '21 at 10:23