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

问题描述

我有一个数据框,其中包含来自多个模型的估算值,每个模型都在单独的国家/地区样本上运行。

  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

数据available here以长格式构成。每个国家/地区的样本都包含相同的术语,就像使用以下数据将数据转换成宽格式一样:

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

我现在正在努力创建并排回归表。使用上面链接中提供的样本数据,这将意味着五个国家的五列,而各个术语根据显着性水平分别为行和星号。所有相关信息都在数据集中(估计值,p值,标准误差)。但是,我不知道如何显示它。我通常为此使用texregstargazer之类的软件包,尽管this answer指出了可能的解决方案,但我不知道如何使之工作。

非常感谢您的帮助!

解决方法

喜欢吗?

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)

输出:

===================================================================
             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