当我添加自定义置信区间与"ci。对于单变量模型,R返回:" if (ncol(ci.custom[[i]]) != 2){:参数长度为0时错误"。当我用几个独立的变量做同样的事情时,一切都很完美。
(1)逻辑模型: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)制作输出表:
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返回:" if (ncol(ci.custom[[i]]) != 2){:参数长度为0 "
========================================================================
与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")
我得到一个具有正确系数和间隔的正常表。我试过不同的变量,结果总是一样的:只适用于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")
有两个变量的模型
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
所以要解决这个问题,你可以手动设置confint.polr
的输出维度,当它在一个协变量为1的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:::confint.polr
, MASS:::confint.glm
, MASS:::confint.nls
)的所有MASS
方法上。