stats-hb
stats-hb

Reputation: 988

Annotated correlation tables with stargazer

I want to report correlation tables in a latex report and I'm using 'stargazer' to transform my R objects into tex-code. The correlational data is currently stored in a data frame.

I would like to print rownames and possibly add an annotation under the table. I couldn't find a 'print rownames'-argument and the 'notes'-argument doesn't seem to work.

Any Ideas?

## create object
x           <- matrix(1:4, 2, byrow = TRUE)
dimnames(x) <- list(c("A", "B"), c("A", "B"))
x           <- data.frame(x)

## create Tex-Code
stargazer(x, summary = FALSE, title = "2x2 Matrix",
          notes = "This is a two by two Matrix")

Upvotes: 0

Views: 4869

Answers (3)

user1953965
user1953965

Reputation:

As of version 5.0, stargazer can directly output the content of matrices/vectors. The following code should provide an easy and intuitive resolution to your problem:

## create object
x           <- matrix(1:4, 2, byrow = TRUE)
dimnames(x) <- list(c("A", "B"), c("A", "B"))

## create Tex-Code
stargazer(x, title = "2x2 Matrix",
          notes = "This is a two by two Matrix")

Upvotes: 1

user1953965
user1953965

Reputation:

To get the 'rownames', try this hackish solution:

## create object
x           <- matrix(1:4, 2, byrow = TRUE)
x           <- data.frame(x)
x           <- cbind(c("A","B"),x)
colnames(x) <- c("","A", "B")

## create Tex-Code
stargazer(x, summary = FALSE, title = "2x2 Matrix",
          notes = "This is a two by two Matrix", type="text")

At the moment (v. 4.5.1), 'stargazer' is best suited to working with regression tables and data frames. Your question, however, suggests that users might be interested in better support for matrices. Expect this in future releases (next few months).

As for notes, these really only work for regression tables at the moment. However, they will be available for summary statistics and data frame tables in the next release. If you're willing to edit the source, you can get something very close (although not quite perfect) to the future implementation by replacing the following line(s):

.format.s.stat.parts <<- c("-!","stat names","-!","statistics1","-!")

by:

.format.s.stat.parts <<- c("-!","stat names","-!","statistics1","-!","notes")

Upvotes: 0

daroczig
daroczig

Reputation: 28632

This is rather a markdown solution that can be converted to LaTeX with e.g. Pandoc:

> require(pander)
> pander(x, caption = 'Annotation')

---------------
&nbsp;   A   B 
------- --- ---
 **A**   1   2 

 **B**   3   4 
---------------

Table: Annotation

Upvotes: 0

Related Questions