简体   繁体   English

如何从具有多个估计的数据框创建回归表?

[英]How to 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).所有相关信息都在数据集中(估计值、p 值、标准误差)。 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.我通常texreg使用texregstargazer类的包,虽然这个答案指出了一个可能的解决方案,但我不知道如何使它工作。

Any help is much appreciated!任何帮助深表感谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM