rgtsummary

tbl_regression not showing p-values when compiler is running on R Markdown


I am trying to run tbl_regression() on lme4 models. In R Markdown, the p-values can be seen in the output there. However, when I try to compile to a pdf or html, the p-values disappear, while the confidence intervals stay. I have no clue what is going wrong and would appreciate any help with this. Additionally, any help with keeping tbl_regression output from moving around in pdf compilers would be much appreciated.

fit <- lmer(va_int ~ (MMP9 == "TT") + age + Sex + BMI + smoking + (1 | id), 
            data = eye_data)
cat("Sample Size (n): ", nobs(fit), "\n")
tbl_regression(fit)

Is the code being ran. For note, the issues still occurred without results="asis" being present. In R Markdown, the p-value column shows up. But once the document is compiled, the p-value column completely disappears from tbl_regression output.

I have also attempted a regression on the mtcars dataset and had no issues printing p-values there. For whatever reason, it's the dataset I'm working with that will not print p-values.

The compiled output looks like this: enter image description here

Whereas in R Markdown it looks like this:

enter image description here


Solution

  • tl;dr: include library(lmerTest) in your Rmarkdown file and see if that solves the problem.

    A guess: are you somehow loading lmerTest in your interactive environment but only lme4 in your Rmarkdown file?

    It would be easy to not notice that you had previously loaded lmerTest in an interactive session; Rmarkdown files are typically rendered in a new, clean environment, so loading lme4 instead of lmerTest would give you a different answer.

    (This issue would only affect mixed-model output, not lm output as in your mtcars example ...)

    (The example below is with basic summary(), not tbl_regression(), but the issue should be general.)

    If in a clean R session I run:

    library(lme4)
    fm1 <- lmer(Reaction ~ 1 + Days + (1|Subject), sleepstudy)
    printCoefmat(coef(summary(fm1)))
    

    I get:

                 Estimate Std. Error t value
    (Intercept) 251.40510    9.74672  25.794
    Days         10.46729    0.80422  13.015
    

    whereas with lmerTest:

    library(lmerTest)
    fm1 <- lmer(Reaction ~ 1 + Days + (1|Subject), sleepstudy)
    printCoefmat(coef(summary(fm1)))
    
                 Estimate Std. Error        df t value  Pr(>|t|)    
    (Intercept) 251.40510    9.74672  22.81020  25.794 < 2.2e-16 ***
    Days         10.46729    0.80422 161.00000  13.015 < 2.2e-16 ***