使用 stargazer 的 p 值的维数

Number of dimensions for p-value using stargazer

另一位用户提问 How do I add confidence intervals to odds ratios in stargazer table? 并概述了他们对该问题的解决方案(我在此处包含了相关代码行)

OR.vector <- exp(mylogit$coef)
CI.vector <- exp(confint(mylogit))
p.values <- summary(mylogit)$coefficients[, 4]

# Table with ORs and CIs`
stargazer(mylogit, coef = list(OR.vector), ci = T, 
          ci.custom = list(CI.vector), p = list(p.values), 
          single.row = T, type = "text")

当我尝试运行我自己的模型的相同代码时,我收到以下错误

Error in summary(ml.TatC)$coefficients[, 4] : 
  incorrect number of dimensions

可能有人知道为什么会这样吗?预先感谢您的帮助!

更新:这里是 link 使用的 .txt 文件。

我用过的代码如下:

tattoo <- read.table("https://ndownloader.figshare.com/files/6920972", 
                          header=TRUE, na.strings=c("unk", "NA"))    

library(mlogit)

Tat<-mlogit.data(tattoo, varying=NULL, shape="wide", choice="size", id.var="date")

ml.Tat<-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")

library(stargazer)

OR.vector<-exp(ml.Tat$coef)
CI.vector<-exp(confint(ml.Tat))
p.values<-summary(ml.Tat)$coefficients[,4] #incorrect # of dimensions, how am I supposed to determine dimensions?

stargazer(ml.Tat, coef=list(OR.vector), ci=TRUE, ci.custom=list(CI.vector), single.row=T, type="text", star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4) 

mlogit 包通过函数 summary.mlogit$CoefTable 中存储 p 值,而不是在 $coefficients 中,与 summary.glm 一样。你可以看到这个:

> str(summary(ml.Tat)$coefficients)
atomic [1:8] -4.45e+02 -1.88e+02 2.51e-02 8.04e-03 1.38 ...

summary(ml.Tat)$coefficients 是原子向量,因此只有一维。这就是您收到错误的原因。

使用summary(ml.Tat)$CoefTable[,4]提取你想要的p值:

> summary(ml.Tat)$CoefTable[,4]
  large:(intercept) medium:(intercept)   large:age         medium:age          large:sexM        medium:sexM 
  0.000000e+00       0.000000e+00       8.536121e-10       1.731441e-03       0.000000e+00       0.000000e+00 
  large:yy          medium:yy 
  0.000000e+00       0.000000e+00 

因此您的代码应为:

library(stargazer)

OR.vector<-exp(ml.Tat$coef)
CI.vector<-exp(confint(ml.Tat))
p.values<-summary(ml.Tat)$CoefTable[,4]

stargazer(ml.Tat, coef=list(OR.vector), ci=TRUE, ci.custom=list(CI.vector),
          p = p.values, single.row=T, type="text",
          star.cutoffs=c(0.05,0.01,0.001), 
          out="table1.txt", digits=4)

你的table:

================================================
                        Dependent variable:     
                   -----------------------------
                               size             
------------------------------------------------
large:(intercept)   0.0000*** (0.0000, 0.0000)  
medium:(intercept)    0.0000 (0.0000, 0.0000)   
large:age             1.0254 (1.0172, 1.0336)   
medium:age            1.0081 (1.0030, 1.0132)   
large:sexM            3.9821 (3.5355, 4.4851)   
medium:sexM           2.0886 (1.9576, 2.2284)   
large:yy              1.2455 (1.2189, 1.2726)   
medium:yy             1.0976 (1.0849, 1.1105)   
------------------------------------------------
Observations                  18,162            
R2                            0.0410            
Log Likelihood             -15,882.7000         
LR Test               1,357.1140*** (df = 8)    
================================================
Note:              *p<0.05; **p<0.01; ***p<0.001

很高兴知道(如果您是 R 的新手)包以不同方式部署 summary 函数,因此探索对象以查看发生了什么总是好的。