简体   繁体   English

如何在stargazer表中添加系数,SE,置信区间和优势比?

[英]How do I add coefficients, SE, confidence intervals, and odds ratios in stargazer table?

A previous user asked How do I add confidence intervals to odds ratios in stargazer table? 之前的用户询问如何在stargazer表中为优势比添加置信区间? and outlined a clear solution to the problem. 并概述了该问题的明确解决方案。

Currently, I am typing out my tables by hand and that is very time consuming. 目前,我正在手工输入我的表格,这非常耗时。 example of my typed out table . 我打字表的例子 Here is a link to the .txt file used. 这是使用的.txt文件的链接

My model has size as a dependent variable (categorical) and sex (categorical), age (continuous), and year (continuous) as independent variables. 我的模型将大小作为因变量(分类)和性别(分类),年龄(连续)和年份(连续)作为自变量。 I am using mlogit to model the relationship between variables. 我使用mlogit来模拟变量之间的关系。

The code I used for the model is as follows: 我用于模型的代码如下:

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) 

The stargazer code I have tried is shown below for a small part of my data: 我尝试过的观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)

Odds ratio and confidence interval output: 优势比和置信区间输出: 赔率和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)`

Coefficient and SE output: 系数和SE输出: coeff和SE输出

I can combine odds ratios with confidence intervals or standard errors or coefficients with confidence intervals and standard errors, but when I write all three together the ci=TRUE function seems to overwrite the SE default. 我可以将优势比与置信区间或标准误差或系数与置信区间和标准误差相结合,但是当我将所有三个一起写入时, ci=TRUE函数似乎会覆盖SE默认值。

For my dissertation, I need tables to show the coefficients, standard errors, confidence intervals, and odds ratios (and p-values in some format). 对于我的论文,我需要表格来显示系数,标准误差,置信区间和比值比(以及某种格式的p值)。 Is there a way for stargazer to include all four things? 观星者有没有办法包括所有四件事? Perhaps in two different columns? 也许在两个不同的栏目? I am able to export the tables to excel, however without all 4 things in the same stargazer table I am stuck manually putting the two above tables together. 我可以将表导出到excel,但是如果没有同一个观星表中的所有4个东西,我会被手动将上面的两个表放在一起。 This is not a big deal for 1 table, but I am working with 36 models that all need tables (for my dissertation). 对于1个表来说这不是什么大问题,但我正在使用36个需要表格的模型(对于我的论文)。

How can I use stargazer to show all four things? 我如何使用观星者展示所有四件事? (odds ratio, confidence intervals, coefficients, and standard errors) (优势比,置信区间,系数和标准误差)

Stargazer accepts multiple models and appends each to a new row. Stargazer接受多个模型并将每个模型附加到新行。 So, you can make a second model and replace the standard coefficients with odds ratios and pass this to the stargazer call. 因此,您可以制作第二个模型并用比值比替换标准系数,并将其传递给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)

The argument ci=c(F,T) suppresses the confidence interval in the first column (so SEs are shown instead) and shows it in the second column. 参数ci=c(F,T)抑制第一列中的置信区间(因此显示SE)并在第二列中显示它。 The column.labels argument lets you name the columns. 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

Trying to extract those values from stargazer will be painful. 试图从观星者中提取这些值将是痛苦的。 The returned value from stargazer calls are just character lines. 来自stargazer调用的返回值只是字符行。 Instead you should look at the structure of the model. 相反,你应该看一下模型的结构。 It resemble the structure of glm results: 它类似于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"      

And the results of summary.mlogit resemble the results of summary.glm : 而结果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"         

So you should be using the [['CoefTable']] values which are most likely in the form of a matrix ... since they should be similar to the value of summary(mod)$coefficients. 所以你应该使用最有可能采用矩阵形式的[['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

The way should now be clear to complete your homework assignment. 现在应该清楚地完成你的家庭作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在stargazer表中为优势比添加置信区间? - How do I add confidence intervals to odds ratios in stargazer table? 如何结合优势比和置信区间 - How can to combine odds ratios and the confidence intervals Metafor 森林 plot 显示优势比的置信区间不正确 - Metafor forest plot shows incorrect confidence intervals for odds ratios R Stargazer报告系数,置信区间和精确的p值 - R Stargazer report coefficients, confidence intervals and exact p-values 在具有多个自变量的单变量逻辑回归后,将系数、置信区间和优势比存储在一个数据框中 - Store coefficients, confidence intervalls and odds ratios in one dataframe after univariate logistic regression with multiple independent variables 如何在ggplot中为glm模型添加置信区间? - How do I add confidence intervals to glm model in ggplot? 在 stargazer() LaTeX 输出中用赔率代替 logits - Odds ratios instead of logits in stargazer() LaTeX output 观星者-用户提供的系数和SE - stargazer - user supplied coefficients and SE 使用R中的metafor包从比值比和置信区间进行荟萃分析 - Meta-analysis from odds ratios and confidence intervals, using metafor package in r 我可以使用 Stargazer 在单个列中添加标准误差 * 和 * 置信区间吗? - Can I add standard errors *and* confidence intervals in a single column using Stargazer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM