Erdne Htábrob
Erdne Htábrob

Reputation: 879

How do I create regression table from dataframe with multiple estimates?

I have a data frame with estimates from multiple models, each ran on a separate country sample.

  cntry        term      estimate    std.error   statistic    p.value
1    DE (Intercept) -2.775951e+00 1.140836e+00 -2.43325992 0.01496355
2    DE        agea  7.329692e-02 3.933452e-02  1.86342470 0.06240254
3    DE       agesq -9.109431e-04 3.989225e-04 -2.28350889 0.02240041
4    DE   children1 -3.243184e-01 2.325079e-01 -1.39487027 0.16305496
5    DE    class817 -1.487238e+01 5.319496e+02 -0.02795825 0.97769545
6    DE     class82 -7.983340e-01 5.897445e-01 -1.35369461 0.17583383

The data, available here, is structured in the long format. Each country sample has the same terms included, as can be seen when the data is transformed into a wide format with:

pivot_wider(names_from = cntry, 
              values_from = c("estimate", "std.error", "statistic", "p.value"))

I am now struggling to create a side by side regression table. With the sample data provided on the link above, this would mean five columns for the five countries, with the individual terms as rows and stars based on the significance levels. All the relevant information is in the dataset (estimates, p values, standard errors). However, I don't know how to display it. I typically use packages like texreg or stargazer for this, and while this answer points to a possible solution, I could not figure out how to make it work.

Upvotes: 1

Views: 476

Answers (1)

Philip Leifeld
Philip Leifeld

Reputation: 2323

Like this?

library("texreg")
dta <- read.csv("models.csv")
countries <- unique(as.character(dta$cntry))
tr <- lapply(countries, function(x) {
  d <- dta[dta$cntry == x, ]
  t <- createTexreg(coef.names = as.character(d$term),
                    coef = d$estimate,
                    se = d$std.error,
                    pvalues = d$p.value,
                    model.name = x)
  return(t)
})
screenreg(tr)

Output:

===================================================================
             DE           ES        FR        PL           SE      
-------------------------------------------------------------------
(Intercept)    -2.78 *    -2.73 **  -3.26 *      -3.91     -2.74 * 
               (1.14)     (0.96)    (1.28)       (2.03)    (1.12)  
agea            0.07       0.06      0.08 *       0.08     -0.04   
               (0.04)     (0.03)    (0.04)       (0.07)    (0.03)  
agesq          -0.00 *    -0.00 *   -0.00 **     -0.00      0.00   
               (0.00)     (0.00)    (0.00)       (0.00)    (0.00)  
children1      -0.32      -0.44 *   -0.06        -0.96 **  -0.46   
               (0.23)     (0.18)    (0.23)       (0.35)    (0.24)  
class817      -14.87       0.32      0.46         0.11      0.03   
             (531.95)     (0.61)    (0.87)       (1.08)    (1.24)  
class82        -0.80      -0.35     -0.13        -0.18      0.99   
               (0.59)     (0.59)    (0.73)       (0.78)    (0.74)  
class83        -0.30       0.59      0.70        -2.63 **   0.88   
               (0.49)     (0.58)    (0.67)       (1.02)    (0.70)  
class84        -0.34       0.11      0.34        -0.96      0.43   
               (0.55)     (0.57)    (0.70)       (0.88)    (0.76)  
class85        -0.49       0.36      0.80        -0.18      1.04   
               (0.48)     (0.56)    (0.66)       (0.72)    (0.67)  
class86        -0.41       1.01      0.42        -0.66      0.83   
               (0.54)     (0.58)    (0.69)       (0.89)    (0.73)  
class87         0.09       0.49      1.27        -0.79      1.14   
               (0.47)     (0.58)    (0.65)       (0.82)    (0.66)  
class88        -0.56       0.14      0.28        -0.46      1.33   
               (0.55)     (0.56)    (0.72)       (0.84)    (0.68)  
domicil2       -0.69 *     0.01      0.00         1.16     -0.46   
               (0.30)     (0.34)    (0.34)       (0.64)    (0.27)  
domicil3       -0.71 **   -0.11     -0.09        -1.06 **  -0.13   
               (0.25)     (0.23)    (0.29)       (0.39)    (0.25)  
domicil4       -1.18 ***  -0.24     -0.01        -0.67     -1.43 **
               (0.30)     (0.23)    (0.29)       (0.37)    (0.44)  
domicil5       -0.93      -0.71      0.27       -14.99     -0.75   
               (0.66)     (0.59)    (0.41)    (1329.91)    (0.39)  
eduyrs          0.04       0.03 *    0.01         0.16 **   0.08 **
               (0.03)     (0.01)    (0.03)       (0.05)    (0.03)  
gndr2          -0.12       0.32     -0.46 *      -0.13      0.24   
               (0.22)     (0.18)    (0.20)       (0.31)    (0.20)  
hincfel2        0.17      -0.06     -0.23        -1.10 **  -0.03   
               (0.21)     (0.19)    (0.21)       (0.36)    (0.22)  
hincfel3       -0.29       0.04     -0.17        -1.07 *   -0.09   
               (0.52)     (0.26)    (0.32)       (0.53)    (0.46)  
hincfel4        0.55      -0.39      0.81       -14.97      0.36   
               (0.84)     (0.58)    (0.66)    (1185.50)    (0.83)  
unemployed1     0.43 *     0.31      0.39         0.00      0.12   
               (0.22)     (0.19)    (0.27)       (0.37)    (0.22)  
===================================================================
*** p < 0.001; ** p < 0.01; * p < 0.05

Upvotes: 4

Related Questions