0

I have seen some publication quality correlation tables that contain in addition to other variables the mean and standard deviation within the table. I prepared my correlation matrix and used stargazer to output, but there is no way to to include the mean and standard deviation. I have searched extensively but this is not anywhere online. Note that stargazer produces the desired output of the mean/standard deviation table and correlation table, but fails to combine both into one table. So, I want enter image description here

Using the summary.stat function within stargazer does not solve this issue by the way. Any help how to do this within stargazer? Thanks!

JapJ
  • 13
  • 5
  • You're going to want to change your post to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to get the best answers quickly. However, in the PDF that stargazer includes with their library, they explain how to add things like SE to a correlation table. Have you looked at it? – Kat Aug 21 '21 at 20:17

2 Answers2

0

This is not a stargazer answer (sorry!), but I'm still leaving this here in case anyone finds it useful.

This result can be achieved rather easily with the datasummary family of functions from the modelsummary package. (Disclaimer: I am the maintainer.)

First, the datasummary function can be used to draw summary tables with arbitrary statistics, defined by the user. In this case, we can do:

library(modelsummary)

dat <- mtcars[, c("hp", "mpg", "drat")]

datasummary(All(dat) ~ Mean + SD, data = dat)

enter image description here

Then, the datasummary_correlation can produce a correlation table:

datasummary_correlation(dat)

enter image description here

Finally, we use the output argument to save the correlation table as a data.frame, and we feed this data.frame to the add_columns argument of datasummary to combine the two tables:

library(modelsummary)

corr <- datasummary_correlation(dat, output = "data.frame")[, 2:3]

datasummary(All(dat) ~ Mean + SD, data = dat, add_columns = corr)

enter image description here

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    This is a helpful response. Thanks @Vincent. Yes, model summary appears to be better suited for this manipulation. I was just wondering if this could be done in stargazer as well. – JapJ Aug 22 '21 at 01:02
0

Scientific publications use canonical formats for certain tables, for example for correlation tables, among others, in order to maintain a certain editorial and graphical style. The table you are looking into in an example of such a canonical format. See American Psychological Association required style here as an example.
I believe there is no such function to produce such an automated output, but it can be produced in the following way.

library(tidyverse)
library(corrr)
library(knitr)
library(kableExtra)


df  <- data.frame(Var1 = rnorm(100), Var2 = rnorm(100), Var3 = rnorm(100), Var4 = rnorm(100))


options(knitr.kable.NA = '')

df1 <- df %>% 
  correlate(diagonal = 1) %>%
  shave(upper = TRUE) 

df1$Mean <- unlist(lapply(df, mean))

df1$sd <- unlist(lapply(df, sd))

names(df1) <- c("Variable", 1, 2, 3, 4, "Mean", "s.d.")

df1 %>% select(Variable, Mean, `s.d.`, 1, 2, 3, 4) %>%
  kbl() %>% kable_classic(full_width = F)

enter image description here

jamoreiras
  • 315
  • 1
  • 14