How to improve R output

Asked

Viewed 192 times

3

Is there a package or way to make more visual the output of regressions in R? Something that leaves information more organized, similar to Stata and E-views.

1 answer

6


1st Form: Manual

I usually get the output of the regression model I want, turn it into a data frame, and for that I count on the help of the package broom.

Then I use the table formatting functions such as kable kableExtra::kableExtra.

Example "in the hand"

m1 <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris)
sm1 <- summary(m1)
sm1$coefficients # Eu descobri que é nesse local que fica "guardado" os valores dos coeficientes depois de fazer str(summary(m1))
                   Estimate Std. Error   t value     Pr(>|t|)
(Intercept)       2.2513932  0.3697543  6.088890 9.568102e-09
Sepal.Width       0.8035609  0.1063390  7.556598 4.187340e-12
Speciesversicolor 1.4587431  0.1121079 13.011954 3.478232e-26
Speciesvirginica  1.9468166  0.1000150 19.465255 2.094475e-42

Example with the broom

broom::tidy(m1)
# A tibble: 4 x 5
  term              estimate std.error statistic  p.value
  <chr>                <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)          2.25      0.370      6.09 9.57e- 9
2 Sepal.Width          0.804     0.106      7.56 4.19e-12
3 Speciesversicolor    1.46      0.112     13.0  3.48e-26
4 Speciesvirginica     1.95      0.100     19.5  2.09e-42

Broom + Rename columns

a <- broom::tidy(m1)
names(a) <- c("Covariáveis", "Estimativa", "Erro Padrão", "Estatística t", "Valor-p")
a
# A tibble: 4 x 5
  Covariáveis       Estimativa `Erro Padrão` `Estatística t` `Valor-p`
  <chr>                  <dbl>         <dbl>           <dbl>     <dbl>
1 (Intercept)            2.25          0.370            6.09  9.57e- 9
2 Sepal.Width            0.804         0.106            7.56  4.19e-12
3 Speciesversicolor      1.46          0.112           13.0   3.48e-26
4 Speciesvirginica       1.95          0.100           19.5   2.09e-42

Then just use the function kable and kable_extra (where necessary) to report: inserir a descrição da imagem aqui

And use the extra options of kable and kableExtra the will to customize.

2nd Form: Automatic

When I say "automatic", I mean: A function will collect key model information and printout in a formatted table.

Via stargazer

The package stargazer is very good and the most "famous":

library(stargazer)
stargazer(m1, type = "html")

inserir a descrição da imagem aqui

Via sjPlot

library(sjPlot)
tab_model(m1)

inserir a descrição da imagem aqui

Via jtools

library(jtools)
export_summs(m1)

inserir a descrição da imagem aqui

Summary

  • In the packages that bring the result ready, I assumed that you are
    generating a report in the format html, but with some amendments,
    it is also possible to generate for most of these functions a report
    pdf.
  • You can still make some variations depending on the
    argument of each of them, but you are limited to the scope of it and
    the types of regression model they accept, however, is well
    faster.
  • The advantage of using the form manual is that it is limitless, but demand more time.
  • It would be possible to put a practical example of how to do this?

  • 1

    Excellent. This is an answer that should be very well voted on by the OS participants.

  • 1

    Thank you @Marcus Nunes. Really, when I posted, I thought the examples were missing so the answer is very complete. I hope it helps so!!

  • It will certainly help AP and other users who have similar questions.

  • Thank you William, are very good ideas really helped me a lot!!

Browser other questions tagged

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