Presenting tabulated linear regression result

Asked

Viewed 62 times

3

This is the matrix with the results of my regression models:

linear_models=structure(c(-0.9051, 2.0445, 0.0075, 0, -3.9959, 7.4458, 0, 0, 
-0.0666, 0.4933, 0.8627, 0.4268), .Dim = c(2L, 6L), .Dimnames = list(
    c("(Intercept)", "complete.ini$idiff_3m.grb[-c(502:504)]"
    ), c("Estimate", "Pr(>|t|)", "Estimate", "Pr(>|t|)", "Estimate", 
    "Pr(>|t|)")))

The command below is used to name every two columns of this matrix. I have 3 names, so I have 3 templates.

names(dimnames(linear_models)) <- c("         Variables",
                                    "     (Model 1)     (Model 2)      (Model 3)")

Now with the print.char.Matrix() function of the Hmisc package I do the following:

print.char.matrix(round(linear_models,4), col.txt.align = "left", col.names = TRUE,row.names = TRUE)

+--------------------------------------+--------+--------+--------+--------+--------+--------+
|                   Variables          |Estimate|Pr(>|t|)|Estimate|Pr(>|t|)|Estimate|Pr(>|t|)|
+--------------------------------------+--------+--------+--------+--------+--------+--------+
|(Intercept)                           |   -0.9051|   0.0075   |-3.9959|0|-0.0666|0.8627|
+--------------------------------------+--------+--------+--------+--------+--------+--------+
|complete.ini$idiff_3m.grb[-c(502:504)]|   2.0445 |   0        |7.4458 |0|0.4933 |0.4268|
+--------------------------------------+--------+--------+--------+--------+--------+--------+

As you can see, this is not well adjusted, and the model names have been deleted:

The result I wanted was this:

+------------------------------------------+----First Model--+---Second Model--+---Third Model---+
    |                   Variables          |Estimate|Pr(>|t|)|Estimate|Pr(>|t|)|Estimate|Pr(>|t|)|
    +--------------------------------------+--------+--------+--------+--------+--------+--------+
    |(Intercept)                           |-0.9051 | 0.0075 |-3.9959 |    0   |-0.0666 |0.8627  |
    +--------------------------------------+--------+--------+--------+--------+--------+--------+
    |complete.ini$idiff_3m.grb[-c(502:504)]| 2.0445 |   0    |7.4458  |    0   |0.4933  |0.4268  |
    +--------------------------------------+--------+--------+--------+--------+--------+--------+

Guys, how do I make this adjustment? Is there a function that does this for me?

Some help?

1 answer

2


You will need to work a little to your matrix, especially to put "Variables" because it does not even belong to the row.names nor the col.names, which are the arguments used by the most common functions. After that, just choose your function which has more familiarity (knitr::kable, pander::pandoc.table, etc.):

linear_models <- as.data.frame(linear_models)
linear_models$Variables <- row.names(linear_models)
linear_models <- linear_models[, c(ncol(linear_models), 1:(ncol(linear_models)-1))]
names(linear_models) <- c("Variables", rep(c("Estimate", "Pr(>|t|)"), 3))
kable(linear_models, row.names = F) %>% 
  add_header_above(c("", "First Model" = 2, "Second Model" = 2, "Third Model" = 2))

inserir a descrição da imagem aqui

Works both for PDF and HTML. If you are trying to generate a table in PDF try also stargazer: https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf. Instead of going in with the linear_models, you would enter the 3 models themselves, and the table is on account of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.