虫子? Stargazer无法正确显示带有“忽略”选项的固定效果/因子标签?

时间:2018-10-27 21:13:14

标签: r stargazer

当我在stargazer中生成表格并省略固定效果时,然后指定omit.labels选项,则stargazer(版本5.2.2)对每一列均显示“否”。这是一个示例:

library(stargazer)

# generate example data
set.seed(1)
list_of_states <- rep(c(1, 2, 3, 4, 5), 5)
df <- data.frame("state" = list_of_states, "y" = runif(25),
    "x1" = runif(25), "x2" = runif(25))

# OLS without fixed effects
ols.1 <- glm(y ~ x1, data = df)
ols.2 <- glm(y ~ x1 + x2, data = df)

# OLS with fixed effects
fe.1 <- glm(y ~ x1 + factor(state), data = df)
fe.2 <- glm(y ~ x1 + x2 + factor(state), data = df)

stargazer(ols.1, ols.2, fe.1, fe.2,
  type = "text",
  align = TRUE,
  omit = c("state"),
  omit.labels = c("State FE"))

此输出

==================================================
                        Dependent variable:       
                  --------------------------------
                                 y                
                    (1)      (2)     (3)     (4)  
--------------------------------------------------
x1                 0.088    0.098   0.151   0.157 
                  (0.241)  (0.264) (0.263) (0.283)

x2                          0.028           0.022 
                           (0.270)         (0.287)

Constant          0.485*** 0.467*  0.466**  0.452 
                  (0.142)  (0.227) (0.215) (0.280)

--------------------------------------------------
State FE             No      No      No      No   
--------------------------------------------------
Observations         25      25      25      25   
Log Likelihood     -5.321  -5.315  -3.528  -3.524 
Akaike Inf. Crit.  14.642  16.630  19.056  21.048 
==================================================
Note:                  *p<0.1; **p<0.05; ***p<0.01

“状态FE”行中的标签在最后两列中应显示“是”。这是错误吗?

1 个答案:

答案 0 :(得分:1)

简短的答案是它是一个错误。看来,该代码的目的是创建一个看起来像

的矩阵
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
my @bitcounts = (
   0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 
   3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 
   3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 
   2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 
   3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 
   5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 
   2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 
   4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
   3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 
   4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 
   5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 
   5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
);

for (my $i=0; $i <= 10; $i++) {
system("dd if=/dev/hwrng of=temprnd.bin bs=25 count=1 status=none");
my $filename = 'temprnd.bin';
open(my $fh, '<', $filename) or die "$!";
binmode $fh;
my $count = 0;
my $byte  = 0;
while ( read $fh, $byte, 1 ) {
   $count += $bitcounts[ord($byte)];
}
my $dt = DateTime->now;
print join ',', $dt->ymd, $dt->hms,"$count\n";
system("rm temprnd.bin");
sleep 1;
}
__END__

,然后检查cbind(names(coef(ols.1)), names(coef(fe.1))) # [,1] [,2] # [1,] "(Intercept)" "(Intercept)" # [2,] "x1" "x1" # [3,] "(Intercept)" "factor(state)2" # [4,] "x1" "factor(state)3" # [5,] "(Intercept)" "factor(state)4" # [6,] "x1" "factor(state)5" 正则表达式的每一列。但是,实际发生的情况是

omit

,导致找不到cbind(cbind(NULL, names(coef(ols.1))), names(coef(fe.1))) # [,1] [,2] # [1,] "(Intercept)" "(Intercept)" # [2,] "x1" "x1" 字词。发生这种情况是因为,从omit

  

当参数由矩阵和向量的混合组成时,结果的列(行)数由矩阵参数的列(行)数确定。任何向量都会对其值进行回收或子集化以达到此长度。