0

When I'm adding custom confidence intervals with "ci.custom" in "stargazer" for one variable model, R returns: "Error in if (ncol(ci.custom[[i]]) != 2) { : argument is of length zero". When I do the same, but with several independent variables, everything works perfectly.

(1) ologit model: m <- polr(Risk_Taking_QNT ~ Wealth_log, data=F, Hess=T)

(2) confidence intervals: cim <- exp(confint(m))

I get:

2.5 % 97.5 %

1.006 1.223

(3) making output 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"

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

Same steps with 2-variable model:

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

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

I get:

2.5 % 97.5 %

Wealth_log 0.8768112 1.081713

Experience 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")

I get a normal table with correct coefficients and intervals. I've tried different variables and result is always the same: works only for 2+ variables.

Thank you all for any help!

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

Here is the reproducible example:

library(MASS)

library(stargazer)

Data

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)

Model with 1 variable where I get error

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")

Model with 2 variables which works fine

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")

  • Can you write a reproducible example? I just tried this on a dummy model `m <- polr(Sat ~ Infl, data = housing); cim <- exp(confint(m)); 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")`. Works fine for me. – Chrisss Sep 23 '16 at 16:58
  • Dear Chrisss, thank you for the response. I put a reproducible example above. You example indeed works for me, but the problem with my data remains. – Pavlo Illiashenko Sep 23 '16 at 18:33

1 Answers1

2

The problem is with MASS:::confint.polr, rather than stargazer. The error message is pretty descriptive here.

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

With a model with one covariate, the dimensions are not set in confint.polr but stargazer expects 2 columns (you can see how this makes sense since this is equivalent to the upper and lower bounds of a confidence interval).

This behaviour is absent in lm objects which uses the method confint.lm

m3 <- lm(mpg ~ 1, mtcars)
m4 <- lm(mpg ~ disp, mtcars) 
dim( confint(m3) )
#[1] 1 2
dim( confint(m4) )
[1] 2 2

So to fix this, you can manually set the dimensions on the output of confint.polr when it is run on a polr object with 1 covariate.

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

A bit of a pain, but it works.

Also, FYI, this behaviour happens on all MASS methods for confint (MASS:::confint.polr, MASS:::confint.glm, MASS:::confint.nls).

Chrisss
  • 3,211
  • 1
  • 16
  • 13