在 stargazer 中为一个变量模型自定义置信区间 ci.custom

Custom confidence intervals with ci.custom in stargazer for one variable model

当我在 "stargazer" 中为一个变量模型添加带有 "ci.custom" 的自定义置信区间时,R returns:"Error in if (ncol(ci.custom[[i]]) != 2) { : argument is of length zero"。当我做同样的事情,但有几个自变量时,一切都很完美。

(1) logit模型: m <- polr(Risk_Taking_QNT ~ Wealth_log, data=F, Hess=T)

(2) 置信区间: cim <- exp(confint(m))

我得到:

2.5% 97.5%

1.006 1.223

(3) 输出 table:

stargazer(m, ci.custom = list(cim), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")

R returns: "Error in if (ncol(ci.custom[[i]]) != 2) { : argument is of length zero"

============================================= ===========================

与 2 变量模型相同的步骤:

(1) m <- polr(Risk_Taking_QNT ~ Wealth_log + Experience, data=F, Hess=T)

(2) cim <- exp(confint(m))

我得到:

2.5% 97.5%

Wealth_log 0.8768112 1.081713

经验值 1.2705479 1.530633

(3) stargazer(m, ci.custom = list(cim), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")

我得到一个具有正确系数和间隔的正常 table。我尝试了不同的变量,结果总是一样的:仅适用于 2+ 个变量。

感谢大家的帮助!

============================================= ================================================ ================================================ =====

这是可重现的例子:

library(MASS)

library(stargazer)

数据

Y <- as.factor(c(3, 4, 4, 2, 1, 4, 3, 4, 3, 1))

X1 <- c(8.8, 6.2, 7.3, 7.3, 7.2, 6.4, 7.1, 5.5, 5.7, 7.2)

X2 <- c(7, 8, 9, 8, 8, 10, 9, 9, 7, 6)

带有 1 个变量的模型出现错误

m1 <- polr(Y ~ X1, Hess=T)

cim1 <- exp(confint(m1))

stargazer(m1, ci.custom = list(cim1), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")

具有 2 个变量的模型工作正常

m2 <- polr(Y ~ X1 + X2, Hess=T)

cim2 <- exp(confint(m2))

stargazer(m2, ci.custom = list(cim2), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")

问题出在 MASS:::confint.polr,而不是 stargazer。此处的错误消息非常具有描述性。

library(MASS);
library(stargazer);

Y <- as.factor(c(3, 4, 4, 2, 1, 4, 3, 4, 3, 1))
X1 <- c(8.8, 6.2, 7.3, 7.3, 7.2, 6.4, 7.1, 5.5, 5.7, 7.2)
X2 <- c(7, 8, 9, 8, 8, 10, 9, 9, 7, 6)

m1 <- polr(Y ~ X1, Hess=T)
m2 <- polr(Y ~ X1 + X2, Hess=T)

dim( confint(m1) )
# NULL
dim( confint(m2) )
#[1] 2 2

对于具有一个协变量的模型,维度未在 confint.polr 中设置,但 stargazer 需要 2 列(您可以看到这是有道理的,因为这相当于置信区间的上限和下限).

使用 confint.lm

方法的 lm 个对象中不存在此行为
m3 <- lm(mpg ~ 1, mtcars)
m4 <- lm(mpg ~ disp, mtcars) 
dim( confint(m3) )
#[1] 1 2
dim( confint(m4) )
[1] 2 2

因此,要解决此问题,您可以在具有 1 个协变量的 polr 对象上为 运行 时手动设置 confint.polr 输出的维度。

m1 <- polr(Y ~ X1, Hess = TRUE)
cim1 <- exp(confint(m1))
dim(cim1) <- c(1, 2)
stargazer(m1, ci.custom = list(cim1), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")

========================================
                 Dependent variable:    
             ---------------------------
                          Y             
----------------------------------------
X1                      0.473           
                    (0.104;1.537)       

----------------------------------------
Observations             10             
========================================
Note:        *p<0.1; **p<0.05; ***p<0.01

有点痛苦,但它有效。

此外,仅供参考,此行为发生在 confint 的所有 MASS 方法上(MASS:::confint.polrMASS:::confint.glmMASS:::confint.nls)。