2016-11-20 61 views
1

另一位用戶問How do I add confidence intervals to odds ratios in stargazer table?並闡述了他們解決問題的p值維數(我已經包含的代碼中的相關行這裏)使用斯塔蓋澤

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 

可能有人知道爲什麼會發生這種情況?預先感謝您的幫助!

更新:這裏是使用的.txt文件的link

我已經使用的代碼如下:

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) 
+0

很難說沒有看到您使用的代碼。你可以包括它和一個最小可重現的例子嗎? – paqmo

+0

我的猜測是你想'summary(ml.Tat)$ CoefTable [,4]'來提取p值。 'mlogit'包對象在'summary.mlogit'中存儲的內容與'summary.glm'不同,所以您鏈接的示例不是平行的。當出現維度錯誤時,請始終使用'str(object)'檢查該對象以查看其維度。 – paqmo

+0

@paqmo我已經添加了一個鏈接到我的數據文件和我使用的代碼。是的,我希望'summary(ml.Tat)$ CoefTable [,4]'提取我的p值並將它們包含在模型輸出中。 –

回答

1

mlogit包存儲在$CoefTable p值通過函數summary.mlogit,而不是在$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) 

你的表:

================================================ 
         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函數,所以總是很好探索目標,看看發生了什麼。

+0

謝謝你的幫助。我肯定面臨着一個陡峭的學習曲線。非常感謝! 而且,您建議的代碼給了我與上面包含的代碼相同的表格。 –