[英]How do I add coefficients, SE, confidence intervals, and odds ratios in stargazer table?
之前的用戶詢問如何在stargazer表中為優勢比添加置信區間? 並概述了該問題的明確解決方案。
目前,我正在手工輸入我的表格,這非常耗時。 我打字表的例子 。 這是使用的.txt文件的鏈接 。
我的模型將大小作為因變量(分類)和性別(分類),年齡(連續)和年份(連續)作為自變量。 我使用mlogit
來模擬變量之間的關系。
我用於模型的代碼如下:
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)$CoefTable[,4]
#table with odds ratios and confidence intervals
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)
#table with coefficients and standard errors
stargazer(ml.Tat, type="text", single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4)
我嘗試過的觀stargazer
代碼如下所示:我的數據的一小部分:
library(stargazer)
OR.vector<-exp(ml.Tat$coef)
CI.vector<-exp(confint(ml.Tat))
p.values<-summary(ml.Tat)$CoefTable[,4] #incorrect # of dimensions, unsure how 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) #gives odds ratio (2.5%CI, 97.5%CI)
stargazer(ml.Tat, type="text", single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001), out="table1.txt", digits=4) #gives coeff (SE)`
我可以將優勢比與置信區間或標准誤差或系數與置信區間和標准誤差相結合,但是當我將所有三個一起寫入時, ci=TRUE
函數似乎會覆蓋SE默認值。
對於我的論文,我需要表格來顯示系數,標准誤差,置信區間和比值比(以及某種格式的p值)。 觀星者有沒有辦法包括所有四件事? 也許在兩個不同的欄目? 我可以將表導出到excel,但是如果沒有同一個觀星表中的所有4個東西,我會被手動將上面的兩個表放在一起。 對於1個表來說這不是什么大問題,但我正在使用36個需要表格的模型(對於我的論文)。
我如何使用觀星者展示所有四件事? (優勢比,置信區間,系數和標准誤差)
Stargazer接受多個模型並將每個模型附加到新行。 因此,您可以制作第二個模型並用比值比替換標准系數,並將其傳遞給stargazer
調用。
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")
ml.TatOR<-mlogit(size~1|age+sex+yy, Tat, reflevel="small", id.var="date")
ml.TatOR$coefficients <- exp(ml.TatOR$coefficients) #replace coefficents with odds ratios
library(stargazer)
stargazer(ml.Tat, ml.TatOR, ci=c(F,T),column.labels=c("coefficients","odds ratio"),
type="text",single.row=TRUE, star.cutoffs=c(0.05,0.01,0.001),
out="table1.txt", digits=4)
參數ci=c(F,T)
抑制第一列中的置信區間(因此顯示SE)並在第二列中顯示它。 column.labels
參數允許您命名列。
====================================================================
Dependent variable:
-------------------------------------------------
size
coefficients odds ratio
(1) (2)
--------------------------------------------------------------------
large:(intercept) -444.6032*** (22.1015) 0.0000 (-43.3181, 43.3181)
medium:(intercept) -187.9871*** (11.9584) 0.0000 (-23.4381, 23.4381)
large:age 0.0251*** (0.0041) 1.0254*** (1.0174, 1.0334)
medium:age 0.0080** (0.0026) 1.0081*** (1.0030, 1.0131)
large:sexM 1.3818*** (0.0607) 3.9821*** (3.8632, 4.1011)
medium:sexM 0.7365*** (0.0330) 2.0886*** (2.0239, 2.1534)
large:yy 0.2195*** (0.0110) 1.2455*** (1.2239, 1.2670)
medium:yy 0.0931*** (0.0059) 1.0976*** (1.0859, 1.1093)
--------------------------------------------------------------------
Observations 18,162 18,162
R2 0.0410 0.0410
Log Likelihood -15,882.7000 -15,882.7000
LR Test (df = 8) 1,357.1140*** 1,357.1140***
====================================================================
Note: *p<0.05; **p<0.01; ***p<0.001
試圖從觀星者中提取這些值將是痛苦的。 來自stargazer調用的返回值只是字符行。 相反,你應該看一下模型的結構。 它類似於glm
結果的結構:
> names(ml.Tat)
[1] "coefficients" "logLik" "gradient" "hessian"
[5] "est.stat" "fitted.values" "probabilities" "residuals"
[9] "omega" "rpar" "nests" "model"
[13] "freq" "formula" "call"
而結果summary.mlogit
類似的結果summary.glm
:
> names(summary(ml.Tat))
[1] "coefficients" "logLik" "gradient" "hessian"
[5] "est.stat" "fitted.values" "probabilities" "residuals"
[9] "omega" "rpar" "nests" "model"
[13] "freq" "formula" "call" "CoefTable"
[17] "lratio" "mfR2"
所以你應該使用最有可能采用矩陣形式的[['CoefTable']]
值...因為它們應該類似於summary(mod)$ coefficient的值。
> summary(ml.Tat)$CoefTable
Estimate Std. Error t-value Pr(>|t|)
large:(intercept) -444.39366673 2.209599e+01 -20.1119625 0.000000e+00
medium:(intercept) -187.91353927 1.195601e+01 -15.7170716 0.000000e+00
unk:(intercept) 117.92620950 2.597647e+02 0.4539731 6.498482e-01
large:age 0.02508481 4.088134e-03 6.1360059 8.462202e-10
medium:age 0.00804593 2.567671e-03 3.1335519 1.727044e-03
unk:age 0.01841371 4.888656e-02 0.3766620 7.064248e-01
large:sexM 1.38163894 6.068763e-02 22.7663996 0.000000e+00
medium:sexM 0.73646230 3.304341e-02 22.2877210 0.000000e+00
unk:sexM 1.27203654 7.208632e-01 1.7646018 7.763071e-02
large:yy 0.21941592 1.098606e-02 19.9722079 0.000000e+00
medium:yy 0.09308689 5.947246e-03 15.6521007 0.000000e+00
unk:yy -0.06266765 1.292543e-01 -0.4848399 6.277899e-01
現在應該清楚地完成你的家庭作業。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.