1
votes

Im trying to inculde the output of the mediate function from mediation package in my RMarkdown (PDF) document. Using summary it gives me the table with the results from the bootstrapping analysis when I knitr the RMarkdown to a PDF document, but:

  1. I don't like the look of the output and wuld like to have a more shiny table.
  2. I can't lable this table with a caption and it's not included in the autmatic nummeration of RMarkdown (and as a consequence I can't reference it in the text).

I tried to use kable or xtabs with the output of the 'mediate` function but it won't work since both functions don't accept the class ("summary.mediate" "mediate") of the output.

This is how the code chunk in my RMarkdown document loks like:

```{r med.y1.z6.z7.c, echo = F, comment = "", strip.white = T, fig.cap="test"}

regDFM <- na.omit(as.data.frame(cbind(Y1, X1, Z1, Z6, Z7))) 
regFIT1.C.medY <- lm(Y1 ~ X1+Z1+Z6+Z7+X1:Z1, data = regDFM)
regFIT1.C.medM <- lm(Z7 ~ X1+Z1+Z6+X1:Z1, data = regDFM)
fitMED <- mediation::mediate(regFIT1.C.medM, regFIT1.C.medY, 
                         boot = T, sims = 10, treat="Z6", mediator="Z7")
summary(fitMED)

```

Any help or ideas are very appreciated!

1
You could look at the source code for summary.mediate and create your own summary function which just outputs the relevant information as a data.frame. Have a look at github.com/cran/mediation/blob/master/R/mediate.R#L1949 - henrik_ibsen
Thanks! That's sounds like a good and interesting but time consuming idea. If I find time I'll try that. - Mobody

1 Answers

0
votes

With kable and the elements of the mediate function I finally created a nice RMarkdown > PDF output.

First I created a data.frame using the relevant elements of the mediate function (are the called 'elements'? what would yu call them?). Then just passing the data.frame to kable.

Thanks to @henrik_ibsen for the hint.

Here is my code:

regDFM <- na.omit(as.data.frame(cbind(Y1, X1, Z1, Z6, Z7))) 
regFIT1.C.medY <- lm(Y1 ~ X1+Z1+Z6+Z7+X1:Z1, data = regDFM)
regFIT1.C.medM <- lm(Z7 ~ X1+Z1+Z6+X1:Z1, data = regDFM)
fitMED <- mediation::mediate(regFIT1.C.medM, regFIT1.C.medY, 
                             boot = T, sims = 10, treat="Z6", mediator="Z7")

bt_effect <- c("Indirekter Effekt", "Direkter Effekt", "Gesamt Effekt", 
               "Anteil direkter Effekt")
bt_est <- c(fitMED$d1, fitMED$z1, fitMED$tau.coef, fitMED$n1)
#bt_p <- format.pval(c(fitMED$d1.p, fitMED$z1.p, fitMED$tau.p, fitMED$n1.p))
bt_p <- c(fitMED$d1.p, fitMED$z1.p, fitMED$tau.p, fitMED$n1.p)
bt_stars <- c(stars.pval(fitMED$d1.p), stars.pval(fitMED$z1.p),
              stars.pval(fitMED$tau.p), stars.pval(fitMED$n1.p))
bt_DF <- data.frame(row.names = bt_effect, format(bt_est, digits = 2), 
                    format(bt_p, nsmall = 3), bt_stars)
colnames(bt_DF) <- c("Koeffizienten", "p-Werte", "")

kable(bt_DF, booktabs = T, align = "c",
      caption = "Bootstraping-Analyse für Mediation") %>%
      footnote(general = c("Simulationen: 1000", "Signifikanzniveaus: ∗ p<0.05; 
                            ∗∗ p<0.01; ∗∗∗ p<0.001"),
               general_title = "Anmerkungen:")