strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
607 stars 91 forks source link

tab_model col.order not recognizing 'df' & request for inclusion of SD in random effects #624

Open ari-khoudary opened 4 years ago

ari-khoudary commented 4 years ago

I've been really enjoying your tab_model function. However, I've run into two issues while converting the following model to a table. I'm running R version 3.6.3., lme4 1.1-2.3, and sjPlot 2.8.3 (installed from GitHub). Here is the model formula and my tab_model command:

modelMoral <- lmer(moral_decision ~ category + (1 | subID) + (1|vigfile), data = data_old, control=lmerControl(optCtrl = list(maxeval=5000)))

tab_model(modelMoral, file = 'figures/paper/table_modelMoral.html',
digits = 3, show.ci = 0.95, show.stat = TRUE, show.p = TRUE, show.ngroups = TRUE, show.df = TRUE, p.val = 'satterthwaite', strings = c(stat = 't value', ci = 'CI (95%)', pred = 'Factors'), dv.labels = 'moral judgment', col.order = c('est','ci','df','stat','p'), pred.labels = c('Intercept (Social Norms)', 'Authority', 'Loyalty', 'Purity', 'Care-Emo', 'Care-Phys', 'Fairness', 'Liberty'), title = 'Table 1: Moral judgment model summary')

First, the output does not include df when I run this call. If I comment out the col.order, then df shows up no problem. However, I find it counterintuitive to display df after p values, which is why I would like to be able to reorder my columns as specified.

Second, per the recommendations of Meteyard and Davies (2020), I would like to report the SD for my random effects variances. But there does not seem to be a parameter I can include to achieve this. Might you consider adding that in your next update?

Please let me know if you have any advice on how to proceed with my first issue, and thanks again for the awesome command.

strengejacke commented 4 years ago

col.order works for me:

library(lme4)
#> Loading required package: Matrix
library(sjPlot)

data(mtcars)
model <- lmer(mpg ~ wt + (1 | gear), data = mtcars)

tab_model(model, p.val = "satterthwaite", show.df = T)
  mpg
Predictors Estimates CI p df
(Intercept) 36.19 31.90 – 40.48 \<0.001 13.85
wt \-5.05 \-6.30 – -3.79 \<0.001 21.92
Random Effects
s2 8.46
t00 gear 1.60
ICC 0.16
N gear 3
Observations 32
Marginal R2 / Conditional R2 0.708 / 0.754

tab_model(model, p.val = "satterthwaite", show.df = T, col.order = c("est", "ci", "df.error", "p"))
  mpg
Predictors Estimates CI df p
(Intercept) 36.19 31.90 – 40.48 13.85 \<0.001
wt \-5.05 \-6.30 – -3.79 21.92 \<0.001
Random Effects
s2 8.46
t00 gear 1.60
ICC 0.16
N gear 3
Observations 32
Marginal R2 / Conditional R2 0.708 / 0.754

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

According to the SD: It's just the square root of the variance, so I thought there's not much benefit in this information, that's why I did not include it in the output yet. Maybe I can add an option to include it as well.

library(lme4)
#> Loading required package: Matrix
library(parameters)

data(mtcars)
model <- lmer(mpg ~ wt + (1 | gear), data = mtcars)

random_parameters(model)
#> # Random Effects
#> 
#> Within-Group Variance      8.46 (2.91)
#> Between-Group Variance
#>   Random Intercept (gear)   1.6 (1.26)
#> N (groups per factor)
#>   gear                        3
#> Observations                 32

sqrt(8.46)
#> [1] 2.908608
sqrt(1.6)
#> [1] 1.264911

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

ari-khoudary commented 4 years ago

Thanks for the quick response, and sorry for my late reply. After swapping 'df' for 'df.error', my code worked perfectly. And I agree that its a bit redundant, but it might be nice to be able to quickly include it just in case. Thank you again!!