clog14
clog14

Reputation: 1641

stargazer with R table output or for "special" summary statistics

I have a categorical variable, df$Chain, taking on levels 1,2,3 and 4. I have a variable, df$State, taking on levels 0, 1. I would like to produce a table that shows proportions by state. The standard deviation of the proportions and the t-statistic of a test of equality of the two proportions.

I can produce the proportions via

table.1 <- prop.table(table(dfLong$CHAIN,dfLong$STATE), margin = 2)

which gives

            0         1
  1 0.4430380 0.4108761
  2 0.1518987 0.2054381
  3 0.2151899 0.2477341
  4 0.1898734 0.1359517

and goes into the right direction.

But, when I give this object to stargazer via

stargazer(table.1, type = 'text')

it stacks the proportions to

  0 1  NA  
 -----------
 1 1 0 0.443
 2 2 0 0.152
 3 3 0 0.215
 4 4 0 0.190
 5 1 1 0.411
 6 2 1 0.205
 7 3 1 0.248
 8 4 1 0.136
 -----------

How can I get the proportions within a state next to each other and add standard errors and t-statistics?

Is stargazer the right tool for this at all?

Upvotes: 3

Views: 2426

Answers (1)

Weihuang Wong
Weihuang Wong

Reputation: 13108

Suppose your data and prop.table is

set.seed(123)
dfLong <- data.frame(CHAIN = sample(1:4, 100, replace = TRUE),
                     STATE = sample(0:1, 100, replace = TRUE))
table.1 <- prop.table(table(dfLong$CHAIN,dfLong$STATE), margin = 2)

The idea is to define class for table.1 as matrix and instruct stargazer to print out the matrix "as is":

class(table.1) <- "matrix"
stargazer(table.1, type = "text", summary = FALSE)
# =============
#     0     1  
# -------------
# 1 0.180 0.340
# 2 0.320 0.220
# 3 0.220 0.180
# 4 0.280 0.260
# -------------

Using this approach you can cbind other quantities of interest (standard errors, etc.) to the original matrix, and run the augmented matrix through stargazer.

Upvotes: 4

Related Questions