Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Fetch tidy names in grouped mixed-model #25

Closed
alapo opened this issue Oct 21, 2020 · 2 comments
Closed

Fetch tidy names in grouped mixed-model #25

alapo opened this issue Oct 21, 2020 · 2 comments

Comments

@alapo
Copy link

alapo commented Oct 21, 2020

This issue is related to Issue 21.

In the example below I want to loop through Species which I can do using grouped_tidy or grouped_glance
My goal is to obtain the p-values that I can obtain using parameters::model_parameters()

# Grouped analysis (does not provide p-value)
tmp <- broomExtra::grouped_glance(
  data = iris,
  grouping.vars = c(Species),
  formula = Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length*Petal.Width + (1|Sepal.Width),
  ..f = lme4::lmer,
  control = lme4::lmerControl(optimizer = "bobyqa")
)

# Ungrouped analyses (does not loop)
fm1 <- lmer(Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length*Petal.Width + (1|Sepal.Width), data = iris %>% filter(Species == unique(Species)[1]))
   parameters::model_parameters(fm1) %>%
   broomExtra::easystats_to_tidy_names()

I tried using the code supplied on the GitHubIO but was unable to fetch p-values. I tried 2 versions of tidy.args

# Version 1 of tidy_args
tmp <- broomExtra::grouped_tidy(
  data = iris,
  grouping.vars = c(Species),
  formula = Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length*Petal.Width + (1|Sepal.Width),
  ..f = lme4::lmer,
  control = lme4::lmerControl(optimizer = "bobyqa"),
  tidy.args = list(quick = TRUE)
)

# Version 2 of tidy_args
tmp <- broomExtra::grouped_tidy(
  data = iris,
  grouping.vars = c(Species),
  formula = Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length*Petal.Width + (1|Sepal.Width),
  ..f = lme4::lmer,
  control = lme4::lmerControl(optimizer = "bobyqa"),
  tidy.args = list(conf.int = TRUE, conf.level = 0.99)
  )

P.S: Was going to post on GitHub but there are not any broomExtra tags permitted at the moment so it seemed best to post here to get a response. Apologies if this is a mundane question

@alapo alapo closed this as completed Oct 21, 2020
@alapo alapo reopened this Oct 21, 2020
@IndrajeetPatil
Copy link
Owner

IndrajeetPatil commented Oct 21, 2020

Better to use dplyr::group_modify here

set.seed(123)
library(lme4)
library(dplyr)
options(tibble.width = Inf)

iris %>%
  group_by(Species) %>%
  group_modify(., ~ broomExtra::easystats_to_tidy_names(as.data.frame(parameters::model_parameters(
    lme4::lmer(Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length * Petal.Width + (1 | Sepal.Width),
      data = .x,
      control = lme4::lmerControl(optimizer = "bobyqa")
    )
  ))))
#> Registered S3 method overwritten by 'broom.mixed':
#>   method      from 
#>   tidy.gamlss broom
#> # A tibble: 12 x 9
#> # Groups:   Species [3]
#>    Species    term                     estimate std.error conf.low conf.high
#>    <fct>      <chr>                       <dbl>     <dbl>    <dbl>     <dbl>
#>  1 setosa     (Intercept)                 3.73      0.961   1.85       5.62 
#>  2 setosa     Petal.Length                0.900     0.635  -0.344      2.14 
#>  3 setosa     Petal.Width                 3.56      3.67   -3.64      10.8  
#>  4 setosa     Petal.Length:Petal.Width   -2.34      2.35   -6.94       2.27 
#>  5 versicolor (Intercept)                 1.08      2.89   -4.58       6.74 
#>  6 versicolor Petal.Length                1.23      0.677  -0.0930     2.56 
#>  7 versicolor Petal.Width                 0.814     2.39   -3.87       5.50 
#>  8 versicolor Petal.Length:Petal.Width   -0.259     0.537  -1.31       0.793
#>  9 virginica  (Intercept)                -4.04      4.16  -12.2        4.11 
#> 10 virginica  Petal.Length                1.92      0.753   0.444      3.40 
#> 11 virginica  Petal.Width                 2.53      2.07   -1.52       6.58 
#> 12 virginica  Petal.Length:Petal.Width   -0.459     0.371  -1.19       0.269
#>    statistic df.error  p.value
#>        <dbl>    <int>    <dbl>
#>  1     3.88        44 0.000104
#>  2     1.42        44 0.156   
#>  3     0.969       44 0.332   
#>  4    -0.994       44 0.320   
#>  5     0.374       44 0.708   
#>  6     1.82        44 0.0684  
#>  7     0.340       44 0.734   
#>  8    -0.482       44 0.630   
#>  9    -0.972       44 0.331   
#> 10     2.55        44 0.0108  
#> 11     1.22        44 0.221   
#> 12    -1.23        44 0.217

Created on 2020-10-21 by the reprex package (v0.3.0)

@alapo
Copy link
Author

alapo commented Oct 21, 2020

Such a boss! Thanks for the support.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants